cis.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* cis.c - caseignorestring syntax routines */
  42. /*
  43. * This file actually implements numerous syntax plugins:
  44. *
  45. * Boolean
  46. * CountryString
  47. * DirectoryString
  48. * GeneralizedTime
  49. * OID
  50. * PostalAddress
  51. * PrintableString
  52. */
  53. #include <stdio.h>
  54. #include <string.h>
  55. #include <sys/types.h>
  56. #include "syntax.h"
  57. static int cis_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  58. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  59. static int cis_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  60. char *final, Slapi_Value **bvals );
  61. static int cis_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  62. Slapi_Value ***ivals, int ftype );
  63. static int cis_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  64. Slapi_Value ***ivals, int ftype );
  65. static int cis_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  66. char *final, Slapi_Value ***ivals );
  67. static int cis_compare(struct berval *v1, struct berval *v2);
  68. static int dirstring_validate(struct berval *val);
  69. static int boolean_validate(struct berval *val);
  70. static int time_validate(struct berval *val);
  71. static int country_validate(struct berval *val);
  72. static int postal_validate(struct berval *val);
  73. static int oid_validate(struct berval *val);
  74. static int printable_validate(struct berval *val);
  75. /*
  76. * Attribute syntaxes. We treat all of these the same for now, even though
  77. * the specifications (e.g., RFC 2252) impose various constraints on the
  78. * the format for each of these.
  79. *
  80. * Note: the first name is the official one from RFC 2252.
  81. */
  82. static char *dirstring_names[] = { "DirectoryString", "cis",
  83. "caseignorestring", DIRSTRING_SYNTAX_OID, 0 };
  84. static char *boolean_names[] = { "Boolean", BOOLEAN_SYNTAX_OID, 0 };
  85. static char *time_names[] = { "GeneralizedTime", "time",
  86. GENERALIZEDTIME_SYNTAX_OID, 0 };
  87. #define GENERALIZEDTIMEMATCH_OID "2.5.13.27"
  88. #define GENERALIZEDTIMEORDERINGMATCH_OID "2.5.13.28"
  89. static Slapi_MatchingRuleEntry
  90. generalizedTimeMatch = { GENERALIZEDTIMEMATCH_OID, NULL /* no alias? */,
  91. "generalizedTimeMatch", "The rule evaluates to TRUE if and only if the attribute value represents the same universal coordinated time as the assertion value.",
  92. GENERALIZEDTIME_SYNTAX_OID, 0 /* not obsolete */ };
  93. static Slapi_MatchingRuleEntry
  94. generalizedTimeOrderingMatch = { GENERALIZEDTIMEORDERINGMATCH_OID, NULL /* no alias? */,
  95. "generalizedTimeOrderingMatch", "The rule evaluates to TRUE if and only if the attribute value represents a universal coordinated time that is earlier than the universal coordinated time represented by the assertion value.",
  96. GENERALIZEDTIME_SYNTAX_OID, 0 /* not obsolete */ };
  97. static char *country_names[] = { "Country String",
  98. COUNTRYSTRING_SYNTAX_OID, 0};
  99. static char *postal_names[] = { "Postal Address",
  100. POSTALADDRESS_SYNTAX_OID, 0};
  101. static char *oid_names[] = { "OID",
  102. OID_SYNTAX_OID, 0};
  103. static char *printable_names[] = { "Printable String",
  104. PRINTABLESTRING_SYNTAX_OID, 0};
  105. /*
  106. TBD (XXX)
  107. "1.3.6.1.4.1.1466.115.121.1.16 \"DIT Content Rule Description
  108. \" "
  109. "1.3.6.1.4.1.1466.115.121.1.17 \"DIT Structure Rule Descripti
  110. on\" "
  111. "1.3.6.1.4.1.1466.115.121.1.20 \"DSE Type\" "
  112. "1.3.6.1.4.1.1466.115.121.1.30 \"Matching Rule Description\"
  113. "
  114. "1.3.6.1.4.1.1466.115.121.1.31 \"Matching Rule Use Descriptio
  115. n\" "
  116. "1.3.6.1.4.1.1466.115.121.1.35 \"Name Form Description\" "
  117. "1.3.6.1.4.1.1466.115.121.1.44 \"Printable String\" "
  118. "1.3.6.1.4.1.1466.115.121.1.45 \"Subtree Specification\" "
  119. "1.3.6.1.4.1.1466.115.121.1.54 \"LDAP Syntax Description\" "
  120. "1.3.6.1.4.1.1466.115.121.1.55 \"Modify Rights\" "
  121. "1.3.6.1.4.1.1466.115.121.1.56 \"LDAP Schema Description\" "
  122. "1.3.6.1.4.1.1466.115.121.1.25 \"Guide\" "
  123. "1.3.6.1.4.1.1466.115.121.1.52 \"Telex Number\" "
  124. "1.3.6.1.4.1.1466.115.121.1.51 \"Teletex Terminal Identifier\
  125. " "
  126. "1.3.6.1.4.1.1466.115.121.1.14 \"Delivery Method\" "
  127. "1.3.6.1.4.1.1466.115.121.1.43 \"Presentation Address\" "
  128. "1.3.6.1.4.1.1466.115.121.1.21 \"Enhanced Guide\" "
  129. "1.3.6.1.4.1.1466.115.121.1.34 \"Name and Optional UID\" "
  130. "1.2.840.113556.1.4.905 \"CaseIgnoreString\" "
  131. "1.3.6.1.1.1.0.0 \"nisNetgroupTripleSyntax\" "
  132. "1.3.6.1.1.1.0.1 \"bootParameterSyntax\" ");
  133. */
  134. static Slapi_PluginDesc dirstring_pdesc = { "directorystring-syntax",
  135. VENDOR, PACKAGE_VERSION,
  136. "DirectoryString attribute syntax plugin" };
  137. static Slapi_PluginDesc boolean_pdesc = { "boolean-syntax",
  138. VENDOR, PACKAGE_VERSION,
  139. "Boolean attribute syntax plugin" };
  140. static Slapi_PluginDesc time_pdesc = { "time-syntax",
  141. VENDOR, PACKAGE_VERSION,
  142. "GeneralizedTime attribute syntax plugin" };
  143. static Slapi_PluginDesc country_pdesc = { "countrystring-syntax",
  144. VENDOR, PACKAGE_VERSION,
  145. "Country String attribute syntax plugin" };
  146. static Slapi_PluginDesc postal_pdesc = { "postaladdress-syntax",
  147. VENDOR, PACKAGE_VERSION,
  148. "Postal Address attribute syntax plugin" };
  149. static Slapi_PluginDesc oid_pdesc = { "oid-syntax",
  150. VENDOR, PACKAGE_VERSION,
  151. "OID attribute syntax plugin" };
  152. static Slapi_PluginDesc printable_pdesc = { "printablestring-syntax",
  153. VENDOR, PACKAGE_VERSION,
  154. "Printable String attribtue syntax plugin" };
  155. /*
  156. * register_cis_like_plugin(): register all items for a cis-like plugin.
  157. */
  158. static int
  159. register_cis_like_plugin( Slapi_PBlock *pb, Slapi_PluginDesc *pdescp,
  160. char **names, char *oid, void *validate_fn )
  161. {
  162. int rc, flags;
  163. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  164. (void *) SLAPI_PLUGIN_VERSION_01 );
  165. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  166. (void *) pdescp );
  167. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  168. (void *) cis_filter_ava );
  169. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  170. (void *) cis_filter_sub );
  171. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  172. (void *) cis_values2keys );
  173. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  174. (void *) cis_assertion2keys_ava );
  175. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  176. (void *) cis_assertion2keys_sub );
  177. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  178. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  179. (void *) &flags );
  180. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  181. (void *) names );
  182. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  183. (void *) oid );
  184. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  185. (void *) cis_compare );
  186. if (validate_fn != NULL) {
  187. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  188. (void *)validate_fn );
  189. }
  190. return( rc );
  191. }
  192. int
  193. cis_init( Slapi_PBlock *pb )
  194. {
  195. int rc;
  196. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> cis_init\n", 0, 0, 0 );
  197. rc = register_cis_like_plugin( pb, &dirstring_pdesc, dirstring_names,
  198. DIRSTRING_SYNTAX_OID, dirstring_validate );
  199. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= cis_init %d\n", rc, 0, 0 );
  200. return( rc );
  201. }
  202. int
  203. boolean_init( Slapi_PBlock *pb )
  204. {
  205. int rc;
  206. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> boolean_init\n", 0, 0, 0 );
  207. rc = register_cis_like_plugin( pb, &boolean_pdesc, boolean_names,
  208. BOOLEAN_SYNTAX_OID, boolean_validate );
  209. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= boolean_init %d\n", rc, 0, 0 );
  210. return( rc );
  211. }
  212. int
  213. time_init( Slapi_PBlock *pb )
  214. {
  215. int rc;
  216. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> time_init\n", 0, 0, 0 );
  217. rc = register_cis_like_plugin( pb, &time_pdesc, time_names,
  218. GENERALIZEDTIME_SYNTAX_OID, time_validate );
  219. /* also register this plugin for matching rules */
  220. rc |= slapi_matchingrule_register(&generalizedTimeMatch);
  221. rc |= slapi_matchingrule_register(&generalizedTimeOrderingMatch);
  222. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= time_init %d\n", rc, 0, 0 );
  223. return( rc );
  224. }
  225. int
  226. country_init( Slapi_PBlock *pb )
  227. {
  228. int rc;
  229. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> country_init\n", 0, 0, 0 );
  230. rc = register_cis_like_plugin( pb, &country_pdesc, country_names,
  231. COUNTRYSTRING_SYNTAX_OID, country_validate );
  232. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= country_init %d\n", rc, 0, 0 );
  233. return( rc );
  234. }
  235. int
  236. postal_init( Slapi_PBlock *pb )
  237. {
  238. int rc;
  239. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> postal_init\n", 0, 0, 0 );
  240. rc = register_cis_like_plugin( pb, &postal_pdesc, postal_names,
  241. POSTALADDRESS_SYNTAX_OID, postal_validate );
  242. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= postal_init %d\n", rc, 0, 0 );
  243. return( rc );
  244. }
  245. int
  246. oid_init( Slapi_PBlock *pb )
  247. {
  248. int rc;
  249. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> oid_init\n", 0, 0, 0 );
  250. rc = register_cis_like_plugin( pb, &oid_pdesc, oid_names, OID_SYNTAX_OID, oid_validate );
  251. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= oid_init %d\n", rc, 0, 0 );
  252. return( rc );
  253. }
  254. int
  255. printable_init( Slapi_PBlock *pb )
  256. {
  257. int rc;
  258. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> printable_init\n", 0, 0, 0 );
  259. rc = register_cis_like_plugin( pb, &printable_pdesc, printable_names,
  260. PRINTABLESTRING_SYNTAX_OID, printable_validate );
  261. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= printable_init %d\n", rc, 0, 0 );
  262. return( rc );
  263. }
  264. static int
  265. cis_filter_ava(
  266. Slapi_PBlock *pb,
  267. struct berval *bvfilter,
  268. Slapi_Value **bvals,
  269. int ftype,
  270. Slapi_Value **retVal
  271. )
  272. {
  273. return( string_filter_ava( bvfilter, bvals, SYNTAX_CIS, ftype,
  274. retVal ) );
  275. }
  276. static int
  277. cis_filter_sub(
  278. Slapi_PBlock *pb,
  279. char *initial,
  280. char **any,
  281. char *final,
  282. Slapi_Value **bvals
  283. )
  284. {
  285. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CIS ) );
  286. }
  287. static int
  288. cis_values2keys(
  289. Slapi_PBlock *pb,
  290. Slapi_Value **vals,
  291. Slapi_Value ***ivals,
  292. int ftype
  293. )
  294. {
  295. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS, ftype ) );
  296. }
  297. static int
  298. cis_assertion2keys_ava(
  299. Slapi_PBlock *pb,
  300. Slapi_Value *val,
  301. Slapi_Value ***ivals,
  302. int ftype
  303. )
  304. {
  305. return(string_assertion2keys_ava( pb, val, ivals, SYNTAX_CIS, ftype ));
  306. }
  307. static int
  308. cis_assertion2keys_sub(
  309. Slapi_PBlock *pb,
  310. char *initial,
  311. char **any,
  312. char *final,
  313. Slapi_Value ***ivals
  314. )
  315. {
  316. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  317. SYNTAX_CIS ) );
  318. }
  319. static int cis_compare(
  320. struct berval *v1,
  321. struct berval *v2
  322. )
  323. {
  324. return value_cmp(v1,v2,SYNTAX_CIS,3 /* Normalise both values */);
  325. }
  326. static int dirstring_validate(
  327. struct berval *val
  328. )
  329. {
  330. int rc = 0; /* assume the value is valid */
  331. char *p = NULL;
  332. char *end = NULL;
  333. /* Per RFC4517:
  334. *
  335. * DirectoryString = 1*UTF8
  336. */
  337. if ((val != NULL) && (val->bv_len > 0)) {
  338. p = val->bv_val;
  339. end = &(val->bv_val[val->bv_len - 1]);
  340. rc = utf8string_validate(p, end, NULL);
  341. } else {
  342. rc = 1;
  343. goto exit;
  344. }
  345. exit:
  346. return( rc );
  347. }
  348. static int boolean_validate(
  349. struct berval *val
  350. )
  351. {
  352. int rc = 0; /* assume the value is valid */
  353. /* Per RFC4517:
  354. *
  355. * Boolean = "TRUE" / "FALSE"
  356. */
  357. if (val != NULL) {
  358. if (val->bv_len == 4) {
  359. if (strncmp(val->bv_val, "TRUE", 4) != 0) {
  360. rc = 1;
  361. goto exit;
  362. }
  363. } else if (val->bv_len == 5) {
  364. if (strncmp(val->bv_val, "FALSE", 5) != 0) {
  365. rc = 1;
  366. goto exit;
  367. }
  368. } else {
  369. rc = 1;
  370. goto exit;
  371. }
  372. } else {
  373. rc = 1;
  374. }
  375. exit:
  376. return(rc);
  377. }
  378. static int time_validate(
  379. struct berval *val
  380. )
  381. {
  382. int rc = 0; /* assume the value is valid */
  383. int i = 0;
  384. const char *p = NULL;
  385. char *end = NULL;
  386. /* Per RFC4517:
  387. *
  388. * GeneralizedTime = century year month day hour
  389. * [ minute [ second / leap-second ] ]
  390. * [ fraction ]
  391. * g-time-zone
  392. *
  393. * century = 2(%x30-39) ; "00" to "99"
  394. * year = 2(%x30-39) ; "00" to "99"
  395. * month = ( %x30 %x31-39 ) ; "01" (January) to "09"
  396. * / ( %x31 %x30-32 ) ; "10 to "12"
  397. * day = ( %x30 %x31-39 ) ; "01" to "09"
  398. * / ( %x31-x32 %x30-39 ) ; "10" to "29"
  399. * / ( %x33 %x30-31 ) ; "30" to "31"
  400. * hour = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23"
  401. * minute = %x30-35 %x30-39 ; "00" to "59"
  402. *
  403. * second = ( %x30-35 - %x30-39 ) ; "00" to "59"
  404. * leap-second = ( %x36 %x30 ) ; "60"
  405. *
  406. * fraction = ( DOT / COMMA ) 1*(%x30-39)
  407. * g-time-zone = %x5A ; "Z"
  408. * / g-differential
  409. * g-differential = ( MINUS / PLUS ) hour [ minute ]
  410. */
  411. if (val != NULL) {
  412. /* A valid GeneralizedTime should be at least 11 characters. There
  413. * is no upper bound due to the variable length of "fraction". */
  414. if (val->bv_len < 11) {
  415. rc = 1;
  416. goto exit;
  417. }
  418. /* We're guaranteed that the value is at least 11 characters, so we
  419. * don't need to bother checking if we're at the end of the value
  420. * until we start processing the "minute" part of the value. */
  421. p = val->bv_val;
  422. end = &(val->bv_val[val->bv_len - 1]);
  423. /* Process "century year". First 4 characters can be any valid digit. */
  424. for (i=0; i<4; i++) {
  425. if (!isdigit(*p)) {
  426. rc = 1;
  427. goto exit;
  428. }
  429. p++;
  430. }
  431. /* Process "month". Next character can be "0" or "1". */
  432. if (*p == '0') {
  433. p++;
  434. /* any LDIGIT is valid now */
  435. if (!IS_LDIGIT(*p)) {
  436. rc = 1;
  437. goto exit;
  438. }
  439. p++;
  440. } else if (*p == '1') {
  441. p++;
  442. /* only "0"-"2" are valid now */
  443. if ((*p < '0') || (*p > '2')) {
  444. rc = 1;
  445. goto exit;
  446. }
  447. p++;
  448. } else {
  449. rc = 1;
  450. goto exit;
  451. }
  452. /* Process "day". Next character can be "0"-"3". */
  453. if (*p == '0') {
  454. p++;
  455. /* any LDIGIT is valid now */
  456. if (!IS_LDIGIT(*p)) {
  457. rc = 1;
  458. goto exit;
  459. }
  460. p++;
  461. } else if ((*p == '1') || (*p == '2')) {
  462. p++;
  463. /* any digit is valid now */
  464. if (!isdigit(*p)) {
  465. rc = 1;
  466. goto exit;
  467. }
  468. p++;
  469. } else if (*p == '3') {
  470. p++;
  471. /* only "0"-"1" are valid now */
  472. if ((*p != '0') && (*p != '1')) {
  473. rc = 1;
  474. goto exit;
  475. }
  476. p++;
  477. } else {
  478. rc = 1;
  479. goto exit;
  480. }
  481. /* Process "hour". Next character can be "0"-"2". */
  482. if ((*p == '0') || (*p == '1')) {
  483. p++;
  484. /* any digit is valid now */
  485. if (!isdigit(*p)) {
  486. rc = 1;
  487. goto exit;
  488. }
  489. p++;
  490. } else if (*p == '2') {
  491. p++;
  492. /* only "0"-"3" are valid now */
  493. if ((*p < '0') || (*p > '3')) {
  494. rc = 1;
  495. goto exit;
  496. }
  497. p++;
  498. } else {
  499. rc = 1;
  500. goto exit;
  501. }
  502. /* Time for the optional stuff. We know we have at least one character here, but
  503. * we need to start checking for the end of the string afterwards.
  504. *
  505. * See if a "minute" was specified. */
  506. if ((*p >= '0') && (*p <= '5')) {
  507. p++;
  508. /* any digit is valid for the second char of a minute */
  509. if ((p > end) || (!isdigit(*p))) {
  510. rc = 1;
  511. goto exit;
  512. }
  513. p++;
  514. /* At this point, there has to at least be a "g-time-zone" left.
  515. * Make sure we're not at the end of the string. */
  516. if (p > end) {
  517. rc = 1;
  518. goto exit;
  519. }
  520. /* See if a "second" or "leap-second" was specified. */
  521. if ((*p >= '0') && (*p <= '5')) {
  522. p++;
  523. /* any digit is valid now */
  524. if ((p > end) || (!isdigit(*p))) {
  525. rc = 1;
  526. goto exit;
  527. }
  528. p++;
  529. } else if (*p == '6') {
  530. p++;
  531. /* only a '0' is valid now */
  532. if ((p > end) || (*p != '0')) {
  533. rc = 1;
  534. goto exit;
  535. }
  536. p++;
  537. }
  538. /* At this point, there has to at least be a "g-time-zone" left.
  539. * Make sure we're not at the end of the string. */
  540. if (p > end) {
  541. rc = 1;
  542. goto exit;
  543. }
  544. }
  545. /* See if a fraction was specified. */
  546. if ((*p == '.') || (*p == ',')) {
  547. p++;
  548. /* An arbitrary length string of digit chars is allowed here.
  549. * Ensure we have at least one digit character. */
  550. if ((p >= end) || (!isdigit(*p))) {
  551. rc = 1;
  552. goto exit;
  553. }
  554. /* Just loop through the rest of the fraction until we encounter a non-digit */
  555. p++;
  556. while ((p < end) && (isdigit(*p))) {
  557. p++;
  558. }
  559. }
  560. /* Process "g-time-zone". We either end with 'Z', or have a differential. */
  561. if (p == end) {
  562. if (*p != 'Z') {
  563. rc = 1;
  564. goto exit;
  565. }
  566. } else if (p < end) {
  567. if ((*p != '-') && (*p != '+')) {
  568. rc = 1;
  569. goto exit;
  570. } else {
  571. /* A "g-differential" was specified. An "hour" must be present now. */
  572. p++;
  573. if ((*p == '0') || (*p == '1')) {
  574. p++;
  575. /* any digit is valid now */
  576. if ((p > end) || !isdigit(*p)) {
  577. rc = 1;
  578. goto exit;
  579. }
  580. p++;
  581. } else if (*p == '2') {
  582. p++;
  583. /* only "0"-"3" are valid now */
  584. if ((p > end) || (*p < '0') || (*p > '3')) {
  585. rc = 1;
  586. goto exit;
  587. }
  588. p++;
  589. } else {
  590. rc = 1;
  591. goto exit;
  592. }
  593. /* See if an optional minute is present ("00"-"59"). */
  594. if (p <= end) {
  595. /* "0"-"5" are valid now */
  596. if ((*p < '0') || (*p > '5')) {
  597. rc = 1;
  598. goto exit;
  599. }
  600. p++;
  601. /* We should be at the last character of the string
  602. * now, which must be a valid digit. */
  603. if ((p != end) || !isdigit(*p)) {
  604. rc = 1;
  605. goto exit;
  606. }
  607. }
  608. }
  609. } else {
  610. /* Premature end of string */
  611. rc = 1;
  612. goto exit;
  613. }
  614. } else {
  615. rc = 1;
  616. goto exit;
  617. }
  618. exit:
  619. return( rc );
  620. }
  621. static int country_validate(
  622. struct berval *val
  623. )
  624. {
  625. int rc = 0; /* assume the value is valid */
  626. /* Per RFC4517:
  627. *
  628. * CountryString = 2(PrintableCharacter)
  629. */
  630. if (val != NULL) {
  631. if ((val->bv_len != 2) || !IS_PRINTABLE(val->bv_val[0]) || !IS_PRINTABLE(val->bv_val[1])) {
  632. rc = 1;
  633. goto exit;
  634. }
  635. } else {
  636. rc = 1;
  637. }
  638. exit:
  639. return(rc);
  640. }
  641. static int postal_validate(
  642. struct berval *val
  643. )
  644. {
  645. int rc = 0; /* assume the value is valid */
  646. const char *p = NULL;
  647. const char *start = NULL;
  648. char *end = NULL;
  649. /* Per RFC4517:
  650. * PostalAddress = line *( DOLLAR line )
  651. * line = 1*line-char
  652. * line-char = %x00-23
  653. * / (%x5C "24") ; escaped "$"
  654. * / %x25-5B
  655. * / (%x5C "5C") ; escaped "\"
  656. * / %x5D-7F
  657. * / UTFMB
  658. */
  659. if (val != NULL) {
  660. start = val->bv_val;
  661. end = &(val->bv_val[val->bv_len - 1]);
  662. for (p = start; p <= end; p++) {
  663. /* look for a '\' and make sure it's only used to escape a '$' or a '\' */
  664. if (*p == '\\') {
  665. p++;
  666. /* ensure that we're not at the end of the value */
  667. if ((p > end) || ((strncmp(p, "24", 2) != 0) && (strncasecmp(p, "5C", 2) != 0))) {
  668. rc = 1;
  669. goto exit;
  670. } else {
  671. /* advance the pointer to point to the end
  672. * of the hex code for the escaped character */
  673. p++;
  674. }
  675. } else if (*p == '$') {
  676. /* This signifies the end of a line. We need
  677. * to ensure that the line is not empty. */
  678. if (p == start) {
  679. rc = 1;
  680. goto exit;
  681. }
  682. /* make sure the value doesn't end with a '$' */
  683. if (p == end) {
  684. rc = 1;
  685. goto exit;
  686. }
  687. /* Make sure the line (start to p) is valid UTF-8. */
  688. if ((rc = utf8string_validate(start, p, NULL)) != 0) {
  689. goto exit;
  690. }
  691. /* make the start pointer point to the
  692. * beginning of the next line */
  693. start = p + 1;
  694. }
  695. }
  696. } else {
  697. rc = 1;
  698. }
  699. exit:
  700. return(rc);
  701. }
  702. static int oid_validate(
  703. struct berval *val
  704. )
  705. {
  706. int rc = 0; /* assume the value is valid */
  707. const char *p = NULL;
  708. const char *end = NULL;
  709. /* Per RFC4512:
  710. *
  711. * oid = descr / numericoid
  712. * descr = keystring
  713. */
  714. if ((val != NULL) && (val->bv_len > 0)) {
  715. p = val->bv_val;
  716. end = &(val->bv_val[val->bv_len - 1]);
  717. /* check if the value matches the descr form */
  718. if (IS_LEADKEYCHAR(*p)) {
  719. rc = keystring_validate(p, end);
  720. /* check if the value matches the numericoid form */
  721. } else if (isdigit(*p)) {
  722. rc = numericoid_validate(p, end);
  723. } else {
  724. rc = 1;
  725. goto exit;
  726. }
  727. } else {
  728. rc = 1;
  729. }
  730. exit:
  731. return( rc );
  732. }
  733. static int printable_validate(
  734. struct berval *val
  735. )
  736. {
  737. int rc = 0; /* assume the value is valid */
  738. int i = 0;
  739. /* Per RFC4517:
  740. *
  741. * PrintableString = 1*PrintableCharacter
  742. */
  743. if ((val != NULL) && (val->bv_len > 0)) {
  744. /* Make sure all chars are a PrintableCharacter */
  745. for (i=0; i < val->bv_len; i++) {
  746. if (!IS_PRINTABLE(val->bv_val[i])) {
  747. rc = 1;
  748. goto exit;
  749. }
  750. }
  751. } else {
  752. rc = 1;
  753. }
  754. exit:
  755. return( rc );
  756. }