cis.c 21 KB

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