teletex.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2009 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* teletex.c - Teletex Terminal Identifier syntax routines */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "syntax.h"
  46. static int teletex_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  47. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  48. static int teletex_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  49. char *final, Slapi_Value **bvals );
  50. static int teletex_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  51. Slapi_Value ***ivals, int ftype );
  52. static int teletex_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  53. Slapi_Value ***ivals, int ftype );
  54. static int teletex_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  55. char *final, Slapi_Value ***ivals );
  56. static int teletex_compare(struct berval *v1, struct berval *v2);
  57. static int teletex_validate(struct berval *val);
  58. static int ttx_param_validate(const char *start, const char *end);
  59. static void teletex_normalize(
  60. Slapi_PBlock *pb,
  61. char *s,
  62. int trim_spaces,
  63. char **alt
  64. );
  65. /* the first name is the official one from RFC 4517 */
  66. static char *names[] = { "Teletex Terminal Identifier", "teletextermid", TELETEXTERMID_SYNTAX_OID, 0 };
  67. static Slapi_PluginDesc pdesc = { "teletextermid-syntax", VENDOR, DS_PACKAGE_VERSION,
  68. "Teletex Terminal Identifier attribute syntax plugin" };
  69. int
  70. teletex_init( Slapi_PBlock *pb )
  71. {
  72. int rc, flags;
  73. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> teletex_init\n", 0, 0, 0 );
  74. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  75. (void *) SLAPI_PLUGIN_VERSION_01 );
  76. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  77. (void *)&pdesc );
  78. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  79. (void *) teletex_filter_ava );
  80. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  81. (void *) teletex_filter_sub );
  82. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  83. (void *) teletex_values2keys );
  84. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  85. (void *) teletex_assertion2keys_ava );
  86. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  87. (void *) teletex_assertion2keys_sub );
  88. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  89. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  90. (void *) &flags );
  91. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  92. (void *) names );
  93. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  94. (void *) TELETEXTERMID_SYNTAX_OID );
  95. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  96. (void *) teletex_compare );
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  98. (void *) teletex_validate );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  100. (void *) teletex_normalize );
  101. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= teletex_init %d\n", rc, 0, 0 );
  102. return( rc );
  103. }
  104. static int
  105. teletex_filter_ava(
  106. Slapi_PBlock *pb,
  107. struct berval *bvfilter,
  108. Slapi_Value **bvals,
  109. int ftype,
  110. Slapi_Value **retVal
  111. )
  112. {
  113. int filter_normalized = 0;
  114. int syntax = SYNTAX_CIS;
  115. if (pb) {
  116. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED,
  117. &filter_normalized );
  118. if (filter_normalized) {
  119. syntax |= SYNTAX_NORM_FILT;
  120. }
  121. }
  122. return( string_filter_ava( bvfilter, bvals, syntax,
  123. ftype, retVal ) );
  124. }
  125. static int
  126. teletex_filter_sub(
  127. Slapi_PBlock *pb,
  128. char *initial,
  129. char **any,
  130. char *final,
  131. Slapi_Value **bvals
  132. )
  133. {
  134. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CIS ) );
  135. }
  136. static int
  137. teletex_values2keys(
  138. Slapi_PBlock *pb,
  139. Slapi_Value **vals,
  140. Slapi_Value ***ivals,
  141. int ftype
  142. )
  143. {
  144. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS,
  145. ftype ) );
  146. }
  147. static int
  148. teletex_assertion2keys_ava(
  149. Slapi_PBlock *pb,
  150. Slapi_Value *val,
  151. Slapi_Value ***ivals,
  152. int ftype
  153. )
  154. {
  155. return(string_assertion2keys_ava( pb, val, ivals,
  156. SYNTAX_CIS, ftype ));
  157. }
  158. static int
  159. teletex_assertion2keys_sub(
  160. Slapi_PBlock *pb,
  161. char *initial,
  162. char **any,
  163. char *final,
  164. Slapi_Value ***ivals
  165. )
  166. {
  167. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  168. SYNTAX_CIS ) );
  169. }
  170. static int teletex_compare(
  171. struct berval *v1,
  172. struct berval *v2
  173. )
  174. {
  175. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  176. }
  177. static int
  178. teletex_validate(
  179. struct berval *val
  180. )
  181. {
  182. int rc = 0; /* assume the value is valid */
  183. const char *start = NULL;
  184. const char *end = NULL;
  185. const char *p = NULL;
  186. int got_ttx_term = 0;
  187. /* Per RFC4517:
  188. *
  189. * teletex-id = ttx-term *(DOLLAR ttx-param)
  190. * ttx-term = PrintableString
  191. * ttx-param = ttx-key COLON ttx-value
  192. * tty-key = "graphic" / "control" / "misc" / "page" / "private"
  193. * ttx-value = *ttx-value-octet
  194. *
  195. * ttx-value-octet = %x00-23
  196. * / (%x5C "24") ; escaped "$"
  197. * / %x25-5B
  198. * / (%x5C "5C") ; escaped "\"
  199. * / %x5D-FF
  200. */
  201. /* Don't allow a 0 length string */
  202. if ((val == NULL) || (val->bv_len == 0)) {
  203. rc = 1;
  204. goto exit;
  205. }
  206. start = &(val->bv_val[0]);
  207. end = &(val->bv_val[val->bv_len - 1]);
  208. /* Look for a DOLLAR separator. */
  209. for (p = start; p <= end; p++) {
  210. if (IS_DOLLAR(*p)) {
  211. /* Ensure we don't have an empty element. */
  212. if ((p == start) || (p == end)) {
  213. rc = 1;
  214. goto exit;
  215. }
  216. if (!got_ttx_term) {
  217. /* Validate the ttx-term. */
  218. while (start < p) {
  219. if (!IS_PRINTABLE(*start)) {
  220. rc = 1;
  221. goto exit;
  222. }
  223. start++;
  224. }
  225. got_ttx_term = 1;
  226. } else {
  227. /* Validate the ttx-param. */
  228. if ((rc = ttx_param_validate(start, p - 1)) != 0) {
  229. rc = 1;
  230. goto exit;
  231. }
  232. }
  233. /* Reset start to point at the
  234. * next ttx-param. We're
  235. * guaranteed to have at least
  236. * one more char after p. */
  237. start = p + 1;
  238. }
  239. }
  240. /* If we didn't find the ttx-term, validate
  241. * the whole value as the ttx-term. */
  242. if (!got_ttx_term) {
  243. for (p = start; p <= end; p++) {
  244. if (!IS_PRINTABLE(*p)) {
  245. rc = 1;
  246. goto exit;
  247. }
  248. }
  249. } else {
  250. /* Validate the final ttx-param. */
  251. rc = ttx_param_validate(start, end);
  252. }
  253. exit:
  254. return rc;
  255. }
  256. static int
  257. ttx_param_validate(
  258. const char *start,
  259. const char *end)
  260. {
  261. int rc = 0;
  262. const char *p = NULL;
  263. int found_colon = 0;
  264. for (p = start; p <= end; p++) {
  265. if (IS_COLON(*p)) {
  266. found_colon = 1;
  267. /* Validate the ttx-key before the COLON. */
  268. switch (p - start) {
  269. case 4:
  270. /* "misc" / "page" */
  271. if ((strncmp(start, "misc", 4) != 0) &&
  272. (strncmp(start, "page", 4) != 0)) {
  273. rc = 1;
  274. goto exit;
  275. }
  276. break;
  277. case 7:
  278. /* "graphic" / "control" / "private" */
  279. if ((strncmp(start, "graphic", 7) != 0) &&
  280. (strncmp(start, "control", 7) != 0) &&
  281. (strncmp(start, "private", 7) != 0)) {
  282. rc = 1;
  283. goto exit;
  284. }
  285. break;
  286. default:
  287. rc = 1;
  288. goto exit;
  289. }
  290. /* Validate the ttx-value after the COLON.
  291. * It is allowed to be 0 length. */
  292. if (p != end) {
  293. for (++p; p <= end; p++) {
  294. /* Ensure that '\' is only used
  295. * to escape a '$' or a '\'. */
  296. if (*p == '\\') {
  297. p++;
  298. /* Ensure that we're not at the end of the value */
  299. if ((p > end) || ((strncmp(p, "24", 2) != 0)
  300. && (strncasecmp(p, "5C", 2) != 0))) {
  301. rc = 1;
  302. goto exit;
  303. } else {
  304. /* advance the pointer to point to the end
  305. * of the hex code for the escaped character */
  306. p++;
  307. }
  308. } else if (*p == '$') {
  309. /* This should be escaped. Fail. */
  310. rc = 1;
  311. goto exit;
  312. }
  313. }
  314. }
  315. /* We're done. */
  316. break;
  317. }
  318. }
  319. /* If we didn't find a COLON, fail. */
  320. if (!found_colon) {
  321. rc = 1;
  322. }
  323. exit:
  324. return rc;
  325. }
  326. static void teletex_normalize(
  327. Slapi_PBlock *pb,
  328. char *s,
  329. int trim_spaces,
  330. char **alt
  331. )
  332. {
  333. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  334. return;
  335. }