dn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. /* dn.c - dn syntax routines */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "syntax.h"
  46. static int dn_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  47. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  48. static int dn_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  49. char *final, Slapi_Value **bvals );
  50. static int dn_values2keys( Slapi_PBlock *pb, Slapi_Value **vals,
  51. Slapi_Value ***ivals, int ftype );
  52. static int dn_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  53. Slapi_Value ***ivals, int ftype );
  54. static int dn_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  55. char *final, Slapi_Value ***ivals );
  56. static int dn_validate( struct berval *val );
  57. static int rdn_validate( const char *begin, const char *end, const char **last );
  58. /* the first name is the official one from RFC 2252 */
  59. static char *names[] = { "DN", DN_SYNTAX_OID, 0 };
  60. static Slapi_PluginDesc pdesc = { "dn-syntax", PLUGIN_MAGIC_VENDOR_STR,
  61. PRODUCTTEXT, "distinguished name attribute syntax plugin" };
  62. int
  63. dn_init( Slapi_PBlock *pb )
  64. {
  65. int rc;
  66. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> dn_init\n", 0, 0, 0 );
  67. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  68. (void *) SLAPI_PLUGIN_VERSION_01 );
  69. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  70. (void *)&pdesc );
  71. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  72. (void *) dn_filter_ava );
  73. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  74. (void *) dn_filter_sub );
  75. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  76. (void *) dn_values2keys );
  77. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  78. (void *) dn_assertion2keys_ava );
  79. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  80. (void *) dn_assertion2keys_sub );
  81. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  82. (void *) names );
  83. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  84. (void *) DN_SYNTAX_OID );
  85. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  86. (void *) dn_validate );
  87. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= dn_init %d\n", rc, 0, 0 );
  88. return( rc );
  89. }
  90. static int
  91. dn_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  92. Slapi_Value **bvals, int ftype, Slapi_Value **retVal )
  93. {
  94. return( string_filter_ava( bvfilter, bvals, SYNTAX_CIS | SYNTAX_DN,
  95. ftype, retVal ) );
  96. }
  97. static int
  98. dn_filter_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  99. Slapi_Value **bvals )
  100. {
  101. return( string_filter_sub( pb, initial, any, final, bvals,
  102. SYNTAX_CIS | SYNTAX_DN ) );
  103. }
  104. static int
  105. dn_values2keys( Slapi_PBlock *pb, Slapi_Value **vals, Slapi_Value ***ivals,
  106. int ftype )
  107. {
  108. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS | SYNTAX_DN,
  109. ftype ) );
  110. }
  111. static int
  112. dn_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  113. Slapi_Value ***ivals, int ftype )
  114. {
  115. return( string_assertion2keys_ava( pb, val, ivals,
  116. SYNTAX_CIS | SYNTAX_DN, ftype ) );
  117. }
  118. static int
  119. dn_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any, char *final,
  120. Slapi_Value ***ivals )
  121. {
  122. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  123. SYNTAX_CIS | SYNTAX_DN ) );
  124. }
  125. static int dn_validate( struct berval *val )
  126. {
  127. int rc = 0; /* Assume value is valid */
  128. char *val_copy = NULL;
  129. if (val != NULL) {
  130. /* Per RFC 4514:
  131. *
  132. * distinguishedName = [ relativeDistinguishedName
  133. * *( COMMA relativeDistinguishedName ) ]
  134. * relativeDistinguishedName = attributeTypeAndValue
  135. * *( PLUS attributeTypeAndValue )
  136. * attributeTypeAndValue = attribyteType EQUALS attributeValue
  137. * attributeType = descr / numericoid
  138. * attributeValue = string / hexstring
  139. */
  140. if (val->bv_len > 0) {
  141. int strict = 0;
  142. const char *p = val->bv_val;
  143. const char *end = &(val->bv_val[val->bv_len - 1]);
  144. const char *last = NULL;
  145. /* Check if we should be performing strict validation. */
  146. strict = config_get_dn_validate_strict();
  147. if (!strict) {
  148. /* Create a normalized copy of the value to use
  149. * for validation. The original value will be
  150. * stored in the backend unmodified. */
  151. val_copy = PL_strndup(val->bv_val, val->bv_len);
  152. p = val_copy;
  153. end = slapi_dn_normalize_to_end(val_copy, NULL) - 1;
  154. }
  155. /* Validate one RDN at a time in a loop. */
  156. while (p <= end) {
  157. if ((rc = rdn_validate(p, end, &last)) != 0) {
  158. goto exit;
  159. }
  160. p = last + 1;
  161. /* p should be pointing at a comma, or one past
  162. * the end of the entire dn value. If we have
  163. * not reached the end, ensure that the next
  164. * character is a comma and that there is at
  165. * least another character after the comma. */
  166. if ((p <= end) && ((p == end) || (*p != ','))) {
  167. rc = 1;
  168. goto exit;
  169. }
  170. /* Advance the pointer past the comma so it
  171. * points at the beginning of the next RDN
  172. * (if there is one). */
  173. p++;
  174. }
  175. }
  176. } else {
  177. rc = 1;
  178. goto exit;
  179. }
  180. exit:
  181. if (val_copy) {
  182. slapi_ch_free_string(&val_copy);
  183. }
  184. return rc;
  185. }
  186. /*
  187. * Helper function for validating a DN. This function will validate
  188. * a single RDN. If the RDN is valid, 0 will be returned, otherwise
  189. * non-zero will be returned. A pointer to the last character processed
  190. * will be set in the "last parameter. This will be the end of the RDN
  191. * in the valid case, and the illegal character in the invalid case.
  192. */
  193. static int rdn_validate( const char *begin, const char *end, const char **last )
  194. {
  195. int rc = 0; /* Assume RDN is valid */
  196. int numericform = 0;
  197. char *separator = NULL;
  198. const char *p = begin;
  199. /* Find the '=', then use the helpers for descr and numericoid */
  200. if ((separator = PL_strnchr(p, '=', end - begin + 1)) == NULL) {
  201. rc = 1;
  202. goto exit;
  203. }
  204. /* Process an attribute type. The 'descr'
  205. * form must start with a 'leadkeychar'. */
  206. if (IS_LEADKEYCHAR(*p)) {
  207. if ((rc = keystring_validate(p, separator - 1))) {
  208. goto exit;
  209. }
  210. /* See if the 'numericoid' form is being used */
  211. } else if (isdigit(*p)) {
  212. numericform = 1;
  213. if ((rc = numericoid_validate(p, separator - 1))) {
  214. goto exit;
  215. }
  216. } else {
  217. rc = 1;
  218. goto exit;
  219. }
  220. /* Advance the pointer past the '=' and make sure
  221. * we're not past the end of the string. */
  222. p = separator + 1;
  223. if (p > end) {
  224. rc = 1;
  225. goto exit;
  226. }
  227. /* The value must be a 'hexstring' if the 'numericoid'
  228. * form of 'attributeType' is used. Per RFC 4514:
  229. *
  230. * hexstring = SHARP 1*hexpair
  231. * hexpair = HEX HEX
  232. */
  233. if (numericform) {
  234. if ((p == end) || !IS_SHARP(*p)) {
  235. rc = 1;
  236. goto exit;
  237. }
  238. p++;
  239. /* The value must be a 'string' when the 'descr' form
  240. * of 'attributeType' is used. Per RFC 4514:
  241. *
  242. * string = [ ( leadchar / pair ) [ *( stringchar / pair )
  243. * ( trailchar / pair ) ] ]
  244. *
  245. * leadchar = LUTF1 / UTFMB
  246. * trailchar = TUTF1 / UTFMB
  247. * stringchar = SUTF1 / UTFMB
  248. *
  249. * pair = ESC (ESC / special / hexpair )
  250. * special = escaped / SPACE / SHARP / EQUALS
  251. * escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
  252. * hexpair = HEX HEX
  253. */
  254. } else {
  255. /* Check the leadchar to see if anything illegal
  256. * is there. We need to allow a 'pair' to get
  257. * through, so we'll assume that a '\' is the
  258. * start of a 'pair' for now. */
  259. if (IS_UTF1(*p) && !IS_ESC(*p) && !IS_LUTF1(*p)) {
  260. rc = 1;
  261. goto exit;
  262. }
  263. }
  264. /* Loop through string until we find the ',' separator, a '+'
  265. * char indicating a multi-value RDN, or we reach the end. */
  266. while ((p <= end) && (*p != ',') && (*p != '+')) {
  267. if (numericform) {
  268. /* Process a single 'hexpair' */
  269. if ((p == end) || !isxdigit(*p) || !isxdigit(*p + 1)) {
  270. rc = 1;
  271. goto exit;
  272. }
  273. p = p + 2;
  274. } else {
  275. /* Check for a valid 'stringchar'. We handle
  276. * multi-byte characters separately. */
  277. if (IS_UTF1(*p)) {
  278. /* If we're at the end, check if we have
  279. * a valid 'trailchar'. */
  280. if ((p == end) && !IS_TUTF1(*p)) {
  281. rc = 1;
  282. goto exit;
  283. /* Check for a 'pair'. */
  284. } else if (IS_ESC(*p)) {
  285. /* We're guaranteed to still have at
  286. * least one more character, so lets
  287. * take a look at it. */
  288. p++;
  289. if (!IS_ESC(*p) && !IS_SPECIAL(*p)) {
  290. /* The only thing valid now
  291. * is a 'hexpair'. */
  292. if ((p == end) || !isxdigit(*p) ||!isxdigit(*p + 1)) {
  293. rc = 1;
  294. goto exit;
  295. }
  296. p++;
  297. }
  298. p++;
  299. /* Only allow 'SUTF1' chars now. */
  300. } else if (!IS_SUTF1(*p)) {
  301. rc = 1;
  302. goto exit;
  303. }
  304. p++;
  305. } else {
  306. /* Validate a single 'UTFMB' (multi-byte) character. */
  307. if (utf8char_validate(p, end, &p ) != 0) {
  308. rc = 1;
  309. goto exit;
  310. }
  311. /* Advance the pointer past the multi-byte char. */
  312. p++;
  313. }
  314. }
  315. }
  316. /* We'll end up either at the comma, a '+', or one past end.
  317. * If we are processing a multi-valued RDN, we recurse to
  318. * process the next 'attributeTypeAndValue'. */
  319. if ((p <= end) && (*p == '+')) {
  320. /* Make sure that there is something after the '+'. */
  321. if (p == end) {
  322. rc = 1;
  323. goto exit;
  324. }
  325. p++;
  326. /* Recurse to process the next value. We need to reset p to
  327. * ensure that last is set correctly for the original caller. */
  328. rc = rdn_validate( p, end, last );
  329. p = *last + 1;
  330. }
  331. exit:
  332. *last = p - 1;
  333. return rc;
  334. }