teletex.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2009 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* teletex.c - Teletex Terminal Identifier syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. static int teletex_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  18. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  19. static int teletex_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  20. char *final, Slapi_Value **bvals );
  21. static int teletex_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  22. Slapi_Value ***ivals, int ftype );
  23. static int teletex_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  24. Slapi_Value ***ivals, int ftype );
  25. static int teletex_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  26. char *final, Slapi_Value ***ivals );
  27. static int teletex_compare(struct berval *v1, struct berval *v2);
  28. static int teletex_validate(struct berval *val);
  29. static int ttx_param_validate(const char *start, const char *end);
  30. static void teletex_normalize(
  31. Slapi_PBlock *pb,
  32. char *s,
  33. int trim_spaces,
  34. char **alt
  35. );
  36. /* the first name is the official one from RFC 4517 */
  37. static char *names[] = { "Teletex Terminal Identifier", "teletextermid", TELETEXTERMID_SYNTAX_OID, 0 };
  38. static Slapi_PluginDesc pdesc = { "teletextermid-syntax", VENDOR, DS_PACKAGE_VERSION,
  39. "Teletex Terminal Identifier attribute syntax plugin" };
  40. int
  41. teletex_init( Slapi_PBlock *pb )
  42. {
  43. int rc, flags;
  44. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> teletex_init\n", 0, 0, 0 );
  45. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  46. (void *) SLAPI_PLUGIN_VERSION_01 );
  47. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  48. (void *)&pdesc );
  49. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  50. (void *) teletex_filter_ava );
  51. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  52. (void *) teletex_filter_sub );
  53. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  54. (void *) teletex_values2keys );
  55. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  56. (void *) teletex_assertion2keys_ava );
  57. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  58. (void *) teletex_assertion2keys_sub );
  59. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  60. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  61. (void *) &flags );
  62. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  63. (void *) names );
  64. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  65. (void *) TELETEXTERMID_SYNTAX_OID );
  66. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  67. (void *) teletex_compare );
  68. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  69. (void *) teletex_validate );
  70. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  71. (void *) teletex_normalize );
  72. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= teletex_init %d\n", rc, 0, 0 );
  73. return( rc );
  74. }
  75. static int
  76. teletex_filter_ava(
  77. Slapi_PBlock *pb,
  78. struct berval *bvfilter,
  79. Slapi_Value **bvals,
  80. int ftype,
  81. Slapi_Value **retVal
  82. )
  83. {
  84. int filter_normalized = 0;
  85. int syntax = SYNTAX_CIS;
  86. if (pb) {
  87. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  88. &filter_normalized );
  89. if (filter_normalized) {
  90. syntax |= SYNTAX_NORM_FILT;
  91. }
  92. }
  93. return( string_filter_ava( bvfilter, bvals, syntax,
  94. ftype, retVal ) );
  95. }
  96. static int
  97. teletex_filter_sub(
  98. Slapi_PBlock *pb,
  99. char *initial,
  100. char **any,
  101. char *final,
  102. Slapi_Value **bvals
  103. )
  104. {
  105. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CIS ) );
  106. }
  107. static int
  108. teletex_values2keys(
  109. Slapi_PBlock *pb,
  110. Slapi_Value **vals,
  111. Slapi_Value ***ivals,
  112. int ftype
  113. )
  114. {
  115. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS,
  116. ftype ) );
  117. }
  118. static int
  119. teletex_assertion2keys_ava(
  120. Slapi_PBlock *pb,
  121. Slapi_Value *val,
  122. Slapi_Value ***ivals,
  123. int ftype
  124. )
  125. {
  126. return(string_assertion2keys_ava( pb, val, ivals,
  127. SYNTAX_CIS, ftype ));
  128. }
  129. static int
  130. teletex_assertion2keys_sub(
  131. Slapi_PBlock *pb,
  132. char *initial,
  133. char **any,
  134. char *final,
  135. Slapi_Value ***ivals
  136. )
  137. {
  138. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  139. SYNTAX_CIS ) );
  140. }
  141. static int teletex_compare(
  142. struct berval *v1,
  143. struct berval *v2
  144. )
  145. {
  146. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  147. }
  148. static int
  149. teletex_validate(
  150. struct berval *val
  151. )
  152. {
  153. int rc = 0; /* assume the value is valid */
  154. const char *start = NULL;
  155. const char *end = NULL;
  156. const char *p = NULL;
  157. int got_ttx_term = 0;
  158. /* Per RFC4517:
  159. *
  160. * teletex-id = ttx-term *(DOLLAR ttx-param)
  161. * ttx-term = PrintableString
  162. * ttx-param = ttx-key COLON ttx-value
  163. * tty-key = "graphic" / "control" / "misc" / "page" / "private"
  164. * ttx-value = *ttx-value-octet
  165. *
  166. * ttx-value-octet = %x00-23
  167. * / (%x5C "24") ; escaped "$"
  168. * / %x25-5B
  169. * / (%x5C "5C") ; escaped "\"
  170. * / %x5D-FF
  171. */
  172. /* Don't allow a 0 length string */
  173. if ((val == NULL) || (val->bv_len == 0)) {
  174. rc = 1;
  175. goto exit;
  176. }
  177. start = &(val->bv_val[0]);
  178. end = &(val->bv_val[val->bv_len - 1]);
  179. /* Look for a DOLLAR separator. */
  180. for (p = start; p <= end; p++) {
  181. if (IS_DOLLAR(*p)) {
  182. /* Ensure we don't have an empty element. */
  183. if ((p == start) || (p == end)) {
  184. rc = 1;
  185. goto exit;
  186. }
  187. if (!got_ttx_term) {
  188. /* Validate the ttx-term. */
  189. while (start < p) {
  190. if (!IS_PRINTABLE(*start)) {
  191. rc = 1;
  192. goto exit;
  193. }
  194. start++;
  195. }
  196. got_ttx_term = 1;
  197. } else {
  198. /* Validate the ttx-param. */
  199. if ((rc = ttx_param_validate(start, p - 1)) != 0) {
  200. rc = 1;
  201. goto exit;
  202. }
  203. }
  204. /* Reset start to point at the
  205. * next ttx-param. We're
  206. * guaranteed to have at least
  207. * one more char after p. */
  208. start = p + 1;
  209. }
  210. }
  211. /* If we didn't find the ttx-term, validate
  212. * the whole value as the ttx-term. */
  213. if (!got_ttx_term) {
  214. for (p = start; p <= end; p++) {
  215. if (!IS_PRINTABLE(*p)) {
  216. rc = 1;
  217. goto exit;
  218. }
  219. }
  220. } else {
  221. /* Validate the final ttx-param. */
  222. rc = ttx_param_validate(start, end);
  223. }
  224. exit:
  225. return rc;
  226. }
  227. static int
  228. ttx_param_validate(
  229. const char *start,
  230. const char *end)
  231. {
  232. int rc = 0;
  233. const char *p = NULL;
  234. int found_colon = 0;
  235. for (p = start; p <= end; p++) {
  236. if (IS_COLON(*p)) {
  237. found_colon = 1;
  238. /* Validate the ttx-key before the COLON. */
  239. switch (p - start) {
  240. case 4:
  241. /* "misc" / "page" */
  242. if ((strncmp(start, "misc", 4) != 0) &&
  243. (strncmp(start, "page", 4) != 0)) {
  244. rc = 1;
  245. goto exit;
  246. }
  247. break;
  248. case 7:
  249. /* "graphic" / "control" / "private" */
  250. if ((strncmp(start, "graphic", 7) != 0) &&
  251. (strncmp(start, "control", 7) != 0) &&
  252. (strncmp(start, "private", 7) != 0)) {
  253. rc = 1;
  254. goto exit;
  255. }
  256. break;
  257. default:
  258. rc = 1;
  259. goto exit;
  260. }
  261. /* Validate the ttx-value after the COLON.
  262. * It is allowed to be 0 length. */
  263. if (p != end) {
  264. for (++p; p <= end; p++) {
  265. /* Ensure that '\' is only used
  266. * to escape a '$' or a '\'. */
  267. if (*p == '\\') {
  268. p++;
  269. /* Ensure that we're not at the end of the value */
  270. if ((p > end) || ((strncmp(p, "24", 2) != 0)
  271. && (strncasecmp(p, "5C", 2) != 0))) {
  272. rc = 1;
  273. goto exit;
  274. } else {
  275. /* advance the pointer to point to the end
  276. * of the hex code for the escaped character */
  277. p++;
  278. }
  279. } else if (*p == '$') {
  280. /* This should be escaped. Fail. */
  281. rc = 1;
  282. goto exit;
  283. }
  284. }
  285. }
  286. /* We're done. */
  287. break;
  288. }
  289. }
  290. /* If we didn't find a COLON, fail. */
  291. if (!found_colon) {
  292. rc = 1;
  293. }
  294. exit:
  295. return rc;
  296. }
  297. static void teletex_normalize(
  298. Slapi_PBlock *pb,
  299. char *s,
  300. int trim_spaces,
  301. char **alt
  302. )
  303. {
  304. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  305. return;
  306. }