dn.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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. /* dn.c - routines for dealing with distinguished names */
  39. #include <stdio.h>
  40. #include <ctype.h>
  41. #include <string.h>
  42. #include <sys/types.h>
  43. #ifndef _WIN32
  44. #include <sys/time.h>
  45. #include <sys/socket.h>
  46. #endif
  47. #include "slap.h"
  48. #undef SDN_DEBUG
  49. static void add_rdn_av( char *avstart, char *avend, int *rdn_av_countp,
  50. struct berval **rdn_avsp, struct berval *avstack );
  51. static void reset_rdn_avs( struct berval **rdn_avsp, int *rdn_av_countp );
  52. static void sort_rdn_avs( struct berval *avs, int count );
  53. static int rdn_av_cmp( struct berval *av1, struct berval *av2 );
  54. static void rdn_av_swap( struct berval *av1, struct berval *av2 );
  55. int
  56. hexchar2int( char c )
  57. {
  58. if ( '0' <= c && c <= '9' ) {
  59. return( c - '0' );
  60. }
  61. if ( 'a' <= c && c <= 'f' ) {
  62. return( c - 'a' + 10 );
  63. }
  64. if ( 'A' <= c && c <= 'F' ) {
  65. return( c - 'A' + 10 );
  66. }
  67. return( -1 );
  68. }
  69. #define DNSEPARATOR(c) (c == ',' || c == ';')
  70. #define SEPARATOR(c) (c == ',' || c == ';' || c == '+')
  71. #define SPACE(c) (c == ' ' || c == '\n') /* XXX 518524 */
  72. #define NEEDSESCAPE(c) (c == '\\' || c == '"')
  73. #define B4TYPE 0
  74. #define INTYPE 1
  75. #define B4EQUAL 2
  76. #define B4VALUE 3
  77. #define INVALUE 4
  78. #define INQUOTEDVALUE 5
  79. #define B4SEPARATOR 6
  80. #define SLAPI_DNNORM_INITIAL_RDN_AVS 10
  81. #define SLAPI_DNNORM_SMALL_RDN_AV 512
  82. /*
  83. * substr_dn_normalize - map a DN to a canonical form.
  84. * The DN is read from *dn through *(end-1) and normalized in place.
  85. * The new end is returned; that is, the canonical form is in
  86. * *dn through *(the_return_value-1).
  87. */
  88. /* The goals of this function are:
  89. * 1. be compatible with previous implementations. Especially, enable
  90. * a server running this code to find database index keys that were
  91. * computed by Directory Server 3.0 with a prior version of this code.
  92. * 2. Normalize in place; that is, avoid allocating memory to contain
  93. * the canonical form.
  94. * 3. eliminate insignificant differences; that is, any two DNs are
  95. * not significantly different if and only if their canonical forms
  96. * are identical (ignoring upper/lower case).
  97. * 4. handle a DN in the syntax defined by RFC 2253.
  98. * 5. handle a DN in the syntax defined by RFC 1779.
  99. *
  100. * Goals 3 through 5 are not entirely achieved by this implementation,
  101. * because it can't be done without violating goal 1. Specifically,
  102. * DNs like cn="a,b" and cn=a\,b are not mapped to the same canonical form,
  103. * although they're not significantly different. Likewise for any pair
  104. * of DNs that differ only in their choice of quoting convention.
  105. * A previous version of this code changed all DNs to the most compact
  106. * quoting convention, but that violated goal 1, since Directory Server
  107. * 3.0 did not.
  108. *
  109. * Also, this implementation handles the \xx convention of RFC 2253 and
  110. * consequently violates RFC 1779, according to which this type of quoting
  111. * would be interpreted as a sequence of 2 numerals (not a single byte).
  112. *
  113. * Finally, if the DN contains any RDNs that are multivalued, we sort
  114. * the values in the RDN(s) to help meet goal 3. Ordering is based on a
  115. * case-insensitive comparison of the "attribute=value" pairs.
  116. *
  117. * This function does not support UTF-8 multi-byte encoding for attribute
  118. * values, in particular it does not support UTF-8 whitespace. First the
  119. * SPACE macro above is limited, but also its frequent use of '-1' indexing
  120. * into a char[] may hit the middle of a multi-byte UTF-8 whitespace character
  121. * encoding (518524).
  122. */
  123. char *
  124. substr_dn_normalize( char *dn, char *end )
  125. {
  126. /* \xx is changed to \c.
  127. * \c is changed to c, unless this would change its meaning.
  128. * All values that contain 2 or more separators are "enquoted";
  129. * all other values are not enquoted.
  130. */
  131. char *value = NULL;
  132. char *value_separator = NULL;
  133. char *d = NULL;
  134. char *s = NULL;
  135. char *typestart = NULL;
  136. int gotesc = 0;
  137. int state = B4TYPE;
  138. int rdn_av_count = 0;
  139. struct berval *rdn_avs = NULL;
  140. struct berval initial_rdn_av_stack[ SLAPI_DNNORM_INITIAL_RDN_AVS ];
  141. for ( d = s = dn; s != end; s++ ) {
  142. switch ( state ) {
  143. case B4TYPE:
  144. if ( ! SPACE( *s ) ) {
  145. state = INTYPE;
  146. typestart = d;
  147. *d++ = *s;
  148. }
  149. break;
  150. case INTYPE:
  151. if ( *s == '=' ) {
  152. state = B4VALUE;
  153. *d++ = *s;
  154. } else if ( SPACE( *s ) ) {
  155. state = B4EQUAL;
  156. } else {
  157. *d++ = *s;
  158. }
  159. break;
  160. case B4EQUAL:
  161. if ( *s == '=' ) {
  162. state = B4VALUE;
  163. *d++ = *s;
  164. } else if ( ! SPACE( *s ) ) {
  165. /* not a valid dn - but what can we do here? */
  166. *d++ = *s;
  167. }
  168. break;
  169. case B4VALUE:
  170. if ( *s == '"' || ! SPACE( *s ) ) {
  171. value_separator = NULL;
  172. value = d;
  173. state = ( *s == '"' ) ? INQUOTEDVALUE : INVALUE;
  174. *d++ = *s;
  175. }
  176. break;
  177. case INVALUE:
  178. if ( gotesc ) {
  179. if ( SEPARATOR( *s ) ) {
  180. if ( value_separator ) value_separator = dn;
  181. else value_separator = d;
  182. } else if ( ! NEEDSESCAPE( *s ) ) {
  183. --d; /* eliminate the \ */
  184. }
  185. } else if ( SEPARATOR( *s ) ) {
  186. while ( SPACE( *(d - 1) ) )
  187. d--;
  188. if ( value_separator == dn ) { /* 2 or more separators */
  189. /* convert to quoted value: */
  190. char *L = NULL; /* char after last seperator */
  191. char *R; /* value character iterator */
  192. int escape_skips = 0; /* number of escapes we have seen after the first */
  193. for ( R = value; (R = strchr( R, '\\' )) && (R < d); L = ++R ) {
  194. if ( SEPARATOR( R[1] )) {
  195. if ( L == NULL ) {
  196. /* executes once, at first escape, adds opening quote */
  197. const size_t len = R - value;
  198. /* make room for quote by covering escape */
  199. if ( len > 0 ) {
  200. memmove( value+1, value, len );
  201. }
  202. *value = '"'; /* opening quote */
  203. value = R + 1; /* move passed what has been parsed */
  204. } else {
  205. const size_t len = R - L;
  206. if ( len > 0 ) {
  207. /* remove the seperator */
  208. memmove( value, L, len );
  209. value += len; /* move passed what has been parsed */
  210. }
  211. --d;
  212. ++escape_skips;
  213. }
  214. }
  215. }
  216. memmove( value, L, d - L + escape_skips );
  217. *d++ = '"'; /* closing quote */
  218. }
  219. state = B4TYPE;
  220. /*
  221. * Track and sort attribute values within
  222. * multivalued RDNs.
  223. */
  224. if ( *s == '+' || rdn_av_count > 0 ) {
  225. add_rdn_av( typestart, d, &rdn_av_count,
  226. &rdn_avs, initial_rdn_av_stack );
  227. }
  228. if ( *s != '+' ) { /* at end of this RDN */
  229. if ( rdn_av_count > 1 ) {
  230. sort_rdn_avs( rdn_avs, rdn_av_count );
  231. }
  232. if ( rdn_av_count > 0 ) {
  233. reset_rdn_avs( &rdn_avs, &rdn_av_count );
  234. }
  235. }
  236. *d++ = (*s == '+') ? '+' : ',';
  237. break;
  238. }
  239. *d++ = *s;
  240. break;
  241. case INQUOTEDVALUE:
  242. if ( gotesc ) {
  243. if ( ! NEEDSESCAPE( *s ) ) {
  244. --d; /* eliminate the \ */
  245. }
  246. } else if ( *s == '"' ) {
  247. state = B4SEPARATOR;
  248. if ( value_separator == dn /* 2 or more separators */
  249. || SPACE( value[1] ) || SPACE( d[-1] ) ) {
  250. *d++ = *s;
  251. } else {
  252. /* convert to non-quoted value: */
  253. if ( value_separator == NULL ) { /* no separators */
  254. memmove ( value, value+1, (d-value)-1 );
  255. --d;
  256. } else { /* 1 separator */
  257. memmove ( value, value+1, (value_separator-value)-1 );
  258. *(value_separator - 1) = '\\';
  259. }
  260. }
  261. break;
  262. }
  263. if ( SEPARATOR( *s )) {
  264. if ( value_separator ) value_separator = dn;
  265. else value_separator = d;
  266. }
  267. *d++ = *s;
  268. break;
  269. case B4SEPARATOR:
  270. if ( SEPARATOR( *s ) ) {
  271. state = B4TYPE;
  272. /*
  273. * Track and sort attribute values within
  274. * multivalued RDNs.
  275. */
  276. if ( *s == '+' || rdn_av_count > 0 ) {
  277. add_rdn_av( typestart, d, &rdn_av_count,
  278. &rdn_avs, initial_rdn_av_stack );
  279. }
  280. if ( *s != '+' ) { /* at end of this RDN */
  281. if ( rdn_av_count > 1 ) {
  282. sort_rdn_avs( rdn_avs, rdn_av_count );
  283. }
  284. if ( rdn_av_count > 0 ) {
  285. reset_rdn_avs( &rdn_avs, &rdn_av_count );
  286. }
  287. }
  288. *d++ = (*s == '+') ? '+' : ',';
  289. }
  290. break;
  291. default:
  292. LDAPDebug( LDAP_DEBUG_ANY,
  293. "slapi_dn_normalize - unknown state %d\n", state, 0, 0 );
  294. break;
  295. }
  296. if ( *s != '\\' ) {
  297. gotesc = 0;
  298. } else {
  299. gotesc = 1;
  300. if ( s+2 < end ) {
  301. int n = hexchar2int( s[1] );
  302. if ( n >= 0 ) {
  303. int n2 = hexchar2int( s[2] );
  304. if ( n2 >= 0 ) {
  305. n = (n << 4) + n2;
  306. if (n == 0) { /* don't change \00 */
  307. *d++ = *++s;
  308. *d++ = *++s;
  309. gotesc = 0;
  310. } else { /* change \xx to a single char */
  311. ++s;
  312. *(unsigned char*)(s+1) = n;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. }
  319. /*
  320. * Track and sort attribute values within multivalued RDNs.
  321. */
  322. if ( rdn_av_count > 0 ) {
  323. add_rdn_av( typestart, d, &rdn_av_count,
  324. &rdn_avs, initial_rdn_av_stack );
  325. }
  326. if ( rdn_av_count > 1 ) {
  327. sort_rdn_avs( rdn_avs, rdn_av_count );
  328. }
  329. if ( rdn_av_count > 0 ) {
  330. reset_rdn_avs( &rdn_avs, &rdn_av_count );
  331. }
  332. /* Trim trailing spaces */
  333. while ( d != dn && *(d - 1) == ' ' ) d--; /* XXX 518524 */
  334. return( d );
  335. }
  336. /*
  337. * Append previous AV to the attribute value array if multivalued RDN.
  338. * We use a stack based array at first and if we overflow that, we
  339. * allocate a larger one from the heap, copy the stack based data in,
  340. * and continue to grow the heap based one as needed.
  341. */
  342. static void
  343. add_rdn_av( char *avstart, char *avend, int *rdn_av_countp,
  344. struct berval **rdn_avsp, struct berval *avstack )
  345. {
  346. if ( *rdn_av_countp == 0 ) {
  347. *rdn_avsp = avstack;
  348. } else if ( *rdn_av_countp == SLAPI_DNNORM_INITIAL_RDN_AVS ) {
  349. struct berval *tmpavs;
  350. tmpavs = (struct berval *)slapi_ch_calloc(
  351. SLAPI_DNNORM_INITIAL_RDN_AVS * 2, sizeof( struct berval ));
  352. memcpy( tmpavs, *rdn_avsp,
  353. SLAPI_DNNORM_INITIAL_RDN_AVS * sizeof( struct berval ));
  354. *rdn_avsp = tmpavs;
  355. } else if (( *rdn_av_countp % SLAPI_DNNORM_INITIAL_RDN_AVS ) == 0 ) {
  356. *rdn_avsp = (struct berval *)slapi_ch_realloc( (char *)*rdn_avsp,
  357. (*rdn_av_countp + SLAPI_DNNORM_INITIAL_RDN_AVS)*sizeof(struct berval) );
  358. }
  359. /*
  360. * Note: The bv_val's are just pointers into the dn itself. Also,
  361. * we DO NOT zero-terminate the bv_val's. The sorting code in
  362. * sort_rdn_avs() takes all of this into account.
  363. */
  364. (*rdn_avsp)[ *rdn_av_countp ].bv_val = avstart;
  365. (*rdn_avsp)[ *rdn_av_countp ].bv_len = avend - avstart;
  366. ++(*rdn_av_countp);
  367. }
  368. /*
  369. * Reset RDN attribute value array, freeing memory if any was allocated.
  370. */
  371. static void
  372. reset_rdn_avs( struct berval **rdn_avsp, int *rdn_av_countp )
  373. {
  374. if ( *rdn_av_countp > SLAPI_DNNORM_INITIAL_RDN_AVS ) {
  375. slapi_ch_free( (void **)rdn_avsp );
  376. }
  377. *rdn_avsp = NULL;
  378. *rdn_av_countp = 0;
  379. }
  380. /*
  381. * Perform an in-place, case-insensitive sort of RDN attribute=value pieces.
  382. * This function is always called with more than one element in "avs".
  383. *
  384. * Note that this is used by the DN normalization code, so if any changes
  385. * are made to the comparison function used for sorting customers will need
  386. * to rebuild their database/index files.
  387. *
  388. * Also note that the bv_val's in the "avas" array are not zero-terminated.
  389. */
  390. static void
  391. sort_rdn_avs( struct berval *avs, int count )
  392. {
  393. int i, j, swaps;
  394. /*
  395. * Since we expect there to be a small number of AVs, we use a
  396. * simple bubble sort. rdn_av_swap() only works correctly on
  397. * adjacent values anyway.
  398. */
  399. for ( i = 0; i < count - 1; ++i ) {
  400. swaps = 0;
  401. for ( j = 0; j < count - 1; ++j ) {
  402. if ( rdn_av_cmp( &avs[j], &avs[j+1] ) > 0 ) {
  403. rdn_av_swap( &avs[j], &avs[j+1] );
  404. ++swaps;
  405. }
  406. }
  407. if ( swaps == 0 ) {
  408. break; /* stop early if no swaps made during the last pass */
  409. }
  410. }
  411. }
  412. /*
  413. * strcasecmp()-like function for RDN attribute values.
  414. */
  415. static int
  416. rdn_av_cmp( struct berval *av1, struct berval *av2 )
  417. {
  418. int rc;
  419. rc = strncasecmp( av1->bv_val, av2->bv_val,
  420. ( av1->bv_len < av2->bv_len ) ? av1->bv_len : av2->bv_len );
  421. if ( rc == 0 ) {
  422. return( av1->bv_len - av2->bv_len ); /* longer is greater */
  423. } else {
  424. return( rc );
  425. }
  426. }
  427. /*
  428. * Swap two adjacent attribute=value pieces within an (R)DN.
  429. * Avoid allocating any heap memory for reasonably small AVs.
  430. */
  431. static void
  432. rdn_av_swap( struct berval *av1, struct berval *av2 )
  433. {
  434. char *buf1, *buf2;
  435. char stackbuf1[ SLAPI_DNNORM_SMALL_RDN_AV ];
  436. char stackbuf2[ SLAPI_DNNORM_SMALL_RDN_AV ];
  437. int len1, len2;
  438. /*
  439. * Copy the two avs into temporary buffers. We use stack-based buffers
  440. * if the avs are small and allocate buffers from the heap to hold
  441. * large values.
  442. */
  443. if (( len1 = av1->bv_len ) <= SLAPI_DNNORM_SMALL_RDN_AV ) {
  444. buf1 = stackbuf1;
  445. } else {
  446. buf1 = slapi_ch_malloc( len1 );
  447. }
  448. memcpy( buf1, av1->bv_val, len1 );
  449. if (( len2 = av2->bv_len ) <= SLAPI_DNNORM_SMALL_RDN_AV ) {
  450. buf2 = stackbuf2;
  451. } else {
  452. buf2 = slapi_ch_malloc( len2 );
  453. }
  454. memcpy( buf2, av2->bv_val, len2 );
  455. /*
  456. * Copy av2 over av1 and reset length of av1.
  457. */
  458. memcpy( av1->bv_val, buf2, av2->bv_len );
  459. av1->bv_len = len2;
  460. /*
  461. * Add separator character (+) and copy av1 into place.
  462. * Also reset av2 pointer and length.
  463. */
  464. av2->bv_val = av1->bv_val + len2;
  465. *(av2->bv_val)++ = '+';
  466. memcpy( av2->bv_val, buf1, len1 );
  467. av2->bv_len = len1;
  468. /*
  469. * Clean up.
  470. */
  471. if ( len1 > SLAPI_DNNORM_SMALL_RDN_AV ) {
  472. slapi_ch_free( (void **)&buf1 );
  473. }
  474. if ( len2 > SLAPI_DNNORM_SMALL_RDN_AV ) {
  475. slapi_ch_free( (void **)&buf2 );
  476. }
  477. }
  478. /*
  479. * slapi_dn_normalize - put dn into a canonical format. the dn is
  480. * normalized in place, as well as returned.
  481. */
  482. char *
  483. slapi_dn_normalize( char *dn )
  484. {
  485. /* LDAPDebug( LDAP_DEBUG_TRACE, "=> slapi_dn_normalize \"%s\"\n", dn, 0, 0 ); */
  486. *(substr_dn_normalize( dn, dn + strlen( dn ))) = '\0';
  487. /* LDAPDebug( LDAP_DEBUG_TRACE, "<= slapi_dn_normalize \"%s\"\n", dn, 0, 0 ); */
  488. return dn;
  489. }
  490. /* Note that this routine normalizes to the end and doesn't null terminate */
  491. char *
  492. slapi_dn_normalize_to_end( char *dn , char *end)
  493. {
  494. return ( substr_dn_normalize( dn, end ? end : dn + strlen( dn )) );
  495. }
  496. /*
  497. * dn could contain UTF-8 multi-byte characters,
  498. * which also need to be converted to the lower case.
  499. */
  500. char *
  501. slapi_dn_ignore_case( char *dn )
  502. {
  503. unsigned char *s, *d;
  504. int ssz, dsz;
  505. /* normalize case (including UTF-8 multi-byte chars) */
  506. for ( s = d = (unsigned char *)dn; *s; s += ssz, d += dsz ) {
  507. slapi_utf8ToLower( s, d, &ssz, &dsz );
  508. }
  509. *d = '\0'; /* utf8ToLower result may be shorter than the original */
  510. return( dn );
  511. }
  512. /*
  513. * slapi_dn_normalize_case - put dn into a canonical form suitable for storing
  514. * in a hash database. this involves normalizing the case as well as
  515. * the format. the dn is normalized in place as well as returned.
  516. */
  517. char *
  518. slapi_dn_normalize_case( char *dn )
  519. {
  520. /* normalize format */
  521. slapi_dn_normalize( dn );
  522. /* normalize case */
  523. return( slapi_dn_ignore_case( dn ));
  524. }
  525. /*
  526. * slapi_dn_beparent - return a copy of the dn of dn's parent,
  527. * NULL if the DN is a suffix of the backend.
  528. */
  529. char *
  530. slapi_dn_beparent(
  531. Slapi_PBlock *pb,
  532. const char *dn
  533. )
  534. {
  535. char *r= NULL;
  536. if ( dn != NULL && *dn != '\0')
  537. {
  538. if(!slapi_dn_isbesuffix( pb, dn ))
  539. {
  540. r= slapi_dn_parent( dn );
  541. }
  542. }
  543. return r;
  544. }
  545. char*
  546. slapi_dn_parent( const char *dn )
  547. {
  548. const char *s;
  549. int inquote;
  550. if ( dn == NULL || *dn == '\0' ) {
  551. return( NULL );
  552. }
  553. /*
  554. * An X.500-style distinguished name looks like this:
  555. * foo=bar,sha=baz,...
  556. */
  557. inquote = 0;
  558. for ( s = dn; *s; s++ ) {
  559. if ( *s == '\\' ) {
  560. if ( *(s + 1) )
  561. s++;
  562. continue;
  563. }
  564. if ( inquote ) {
  565. if ( *s == '"' )
  566. inquote = 0;
  567. } else {
  568. if ( *s == '"' )
  569. inquote = 1;
  570. else if ( DNSEPARATOR( *s ) )
  571. return( slapi_ch_strdup( s + 1 ) );
  572. }
  573. }
  574. return( NULL );
  575. }
  576. /*
  577. * slapi_dn_issuffix - tells whether suffix is a suffix of dn. both dn
  578. * and suffix must be normalized.
  579. */
  580. int
  581. slapi_dn_issuffix(const char *dn, const char *suffix)
  582. {
  583. int dnlen, suffixlen;
  584. if ( dn==NULL || suffix==NULL)
  585. {
  586. return( 0 );
  587. }
  588. suffixlen = strlen( suffix );
  589. dnlen = strlen( dn );
  590. if ( suffixlen > dnlen )
  591. {
  592. return( 0 );
  593. }
  594. if ( suffixlen == 0 )
  595. {
  596. return ( 1 );
  597. }
  598. return( (slapi_utf8casecmp( (unsigned char *)(dn + dnlen - suffixlen),
  599. (unsigned char *)suffix ) == 0)
  600. && ( (dnlen == suffixlen) || DNSEPARATOR(dn[dnlen-suffixlen-1])) );
  601. }
  602. int
  603. slapi_dn_isbesuffix( Slapi_PBlock *pb, const char *dn )
  604. {
  605. int r;
  606. Slapi_DN sdn;
  607. slapi_sdn_init_dn_byref(&sdn,dn);
  608. r= slapi_be_issuffix( pb->pb_backend, &sdn );
  609. slapi_sdn_done(&sdn);
  610. return r;
  611. }
  612. /*
  613. * slapi_dn_isparent - returns non-zero if parentdn is the parent of childdn,
  614. * 0 otherwise
  615. */
  616. int
  617. slapi_dn_isparent( const char *parentdn, const char *childdn )
  618. {
  619. char *realparentdn, *copyparentdn;
  620. int rc;
  621. /* child is root - has no parent */
  622. if ( childdn == NULL || *childdn == '\0' ) {
  623. return( 0 );
  624. }
  625. /* construct the actual parent dn and normalize it */
  626. if ( (realparentdn = slapi_dn_parent( childdn )) == NULL ) {
  627. return( parentdn == NULL || *parentdn == '\0' );
  628. }
  629. slapi_dn_normalize( realparentdn );
  630. /* normalize the purported parent dn */
  631. copyparentdn = slapi_ch_strdup( (char *)parentdn );
  632. slapi_dn_normalize( copyparentdn );
  633. /* compare them */
  634. rc = ! strcasecmp( realparentdn, copyparentdn );
  635. slapi_ch_free( (void**)&copyparentdn );
  636. slapi_ch_free( (void**)&realparentdn );
  637. return( rc );
  638. }
  639. /*
  640. * Function: slapi_dn_isroot
  641. *
  642. * Returns: 1 if "dn" is the root dn
  643. * 0 otherwise.
  644. * dn must be normalized
  645. *
  646. */
  647. int
  648. slapi_dn_isroot( const char *dn )
  649. {
  650. int rc;
  651. char *rootdn;
  652. if ( NULL == dn ) {
  653. return( 0 );
  654. }
  655. if ( NULL == (rootdn = config_get_rootdn())) {
  656. return( 0 );
  657. }
  658. /* note: global root dn is normalized when read from config. file */
  659. rc = (strcasecmp( rootdn, dn ) == 0);
  660. slapi_ch_free ( (void **) &rootdn );
  661. return( rc );
  662. }
  663. int
  664. slapi_is_rootdse( const char *dn )
  665. {
  666. if ( NULL != dn )
  667. {
  668. if ( *dn == '\0' )
  669. {
  670. return 1;
  671. }
  672. }
  673. return 0;
  674. }
  675. /*
  676. ** This function takes a quoted attribute value of the form "abc",
  677. ** and strips off the enclosing quotes. It also deals with quoted
  678. ** characters by removing the preceeding '\' character.
  679. **
  680. */
  681. static void
  682. strcpy_unescape_dnvalue( char *d, const char *s )
  683. {
  684. const char *end = s + strlen(s);
  685. for ( ; *s; s++ )
  686. {
  687. switch ( *s )
  688. {
  689. case '"':
  690. break;
  691. case '\\':
  692. {
  693. /*
  694. * The '\' could be escaping a single character, ie \"
  695. * or could be escaping a hex byte, ie \01
  696. */
  697. int singlecharacter= 1;
  698. if ( s+2 < end )
  699. {
  700. int n = hexchar2int( s[1] );
  701. if ( n >= 0 )
  702. {
  703. int n2 = hexchar2int( s[2] );
  704. if ( n2 >= 0 )
  705. {
  706. singlecharacter= 0;
  707. n = (n << 4) + n2;
  708. if (n == 0)
  709. {
  710. /* don't change \00 */
  711. *d++ = *++s;
  712. *d++ = *++s;
  713. }
  714. else
  715. {
  716. /* change \xx to a single char */
  717. ++s;
  718. *(unsigned char*)(s+1) = n;
  719. }
  720. }
  721. }
  722. }
  723. if(singlecharacter)
  724. {
  725. s++;
  726. *d++ = *s;
  727. }
  728. break;
  729. }
  730. default:
  731. *d++ = *s;
  732. break;
  733. }
  734. }
  735. *d = '\0';
  736. }
  737. int
  738. slapi_rdn2typeval(
  739. char *rdn,
  740. char **type,
  741. struct berval *bv
  742. )
  743. {
  744. char *s;
  745. if ( (s = strchr( rdn, '=' )) == NULL ) {
  746. return( -1 );
  747. }
  748. *s++ = '\0';
  749. *type = rdn;
  750. /* MAB 9 Oct 00 : explicit bug fix of 515715
  751. implicit bug fix of 394800 (can't reproduce anymore)
  752. When adding the rdn attribute in the entry, we need to remove
  753. all special escaped characters included in the value itself,
  754. i.e., strings like "\;" must be converted to ";" and so on... */
  755. strcpy_unescape_dnvalue(s,s);
  756. bv->bv_val = s;
  757. bv->bv_len = strlen( s );
  758. return( 0 );
  759. }
  760. /*
  761. * Add an RDN to a DN, getting back the new DN.
  762. */
  763. char *
  764. slapi_dn_plus_rdn(const char *dn, const char *rdn)
  765. {
  766. /* rdn + separator + dn + null */
  767. char *newdn = slapi_ch_smprintf("%s,%s", rdn, dn);
  768. return newdn;
  769. }
  770. /* ====== Slapi_DN functions ====== */
  771. #ifdef SDN_DEBUG
  772. #define SDN_DUMP(sdn,name) sdn_dump(sdn,name)
  773. static void sdn_dump( const Slapi_DN *sdn, const char *text);
  774. #else
  775. #define SDN_DUMP(sdn,name) ((void)0)
  776. #endif
  777. #ifndef SLAPI_DN_COUNTERS
  778. #undef DEBUG /* disable counters */
  779. #endif
  780. #include <prcountr.h>
  781. static int counters_created= 0;
  782. PR_DEFINE_COUNTER(slapi_sdn_counter_created);
  783. PR_DEFINE_COUNTER(slapi_sdn_counter_deleted);
  784. PR_DEFINE_COUNTER(slapi_sdn_counter_exist);
  785. PR_DEFINE_COUNTER(slapi_sdn_counter_dn_created);
  786. PR_DEFINE_COUNTER(slapi_sdn_counter_dn_deleted);
  787. PR_DEFINE_COUNTER(slapi_sdn_counter_dn_exist);
  788. PR_DEFINE_COUNTER(slapi_sdn_counter_ndn_created);
  789. PR_DEFINE_COUNTER(slapi_sdn_counter_ndn_deleted);
  790. PR_DEFINE_COUNTER(slapi_sdn_counter_ndn_exist);
  791. static void
  792. sdn_create_counters()
  793. {
  794. PR_CREATE_COUNTER(slapi_sdn_counter_created,"Slapi_DN","created","");
  795. PR_CREATE_COUNTER(slapi_sdn_counter_deleted,"Slapi_DN","deleted","");
  796. PR_CREATE_COUNTER(slapi_sdn_counter_exist,"Slapi_DN","exist","");
  797. PR_CREATE_COUNTER(slapi_sdn_counter_dn_created,"Slapi_DN","internal_dn_created","");
  798. PR_CREATE_COUNTER(slapi_sdn_counter_dn_deleted,"Slapi_DN","internal_dn_deleted","");
  799. PR_CREATE_COUNTER(slapi_sdn_counter_dn_exist,"Slapi_DN","internal_dn_exist","");
  800. PR_CREATE_COUNTER(slapi_sdn_counter_ndn_created,"Slapi_DN","internal_ndn_created","");
  801. PR_CREATE_COUNTER(slapi_sdn_counter_ndn_deleted,"Slapi_DN","internal_ndn_deleted","");
  802. PR_CREATE_COUNTER(slapi_sdn_counter_ndn_exist,"Slapi_DN","internal_ndn_exist","");
  803. counters_created= 1;
  804. }
  805. #define FLAG_ALLOCATED 0
  806. #define FLAG_DN 1
  807. #define FLAG_NDN 2
  808. Slapi_DN *
  809. slapi_sdn_new()
  810. {
  811. Slapi_DN *sdn= (Slapi_DN *)slapi_ch_malloc(sizeof(Slapi_DN));
  812. slapi_sdn_init(sdn);
  813. sdn->flag= slapi_setbit_uchar(sdn->flag,FLAG_ALLOCATED);
  814. SDN_DUMP( sdn, "slapi_sdn_new");
  815. PR_INCREMENT_COUNTER(slapi_sdn_counter_created);
  816. PR_INCREMENT_COUNTER(slapi_sdn_counter_exist);
  817. return sdn;
  818. }
  819. Slapi_DN *
  820. slapi_sdn_init(Slapi_DN *sdn)
  821. {
  822. sdn->flag= 0;
  823. sdn->dn= NULL;
  824. sdn->ndn= NULL;
  825. sdn->ndn_len=0;
  826. if(!counters_created)
  827. {
  828. sdn_create_counters();
  829. }
  830. return sdn;
  831. }
  832. Slapi_DN *
  833. slapi_sdn_init_dn_byref(Slapi_DN *sdn,const char *dn)
  834. {
  835. slapi_sdn_init(sdn);
  836. slapi_sdn_set_dn_byref(sdn,dn);
  837. return sdn;
  838. }
  839. Slapi_DN *
  840. slapi_sdn_init_dn_byval(Slapi_DN *sdn,const char *dn)
  841. {
  842. slapi_sdn_init(sdn);
  843. slapi_sdn_set_dn_byval(sdn,dn);
  844. return sdn;
  845. }
  846. Slapi_DN *
  847. slapi_sdn_init_dn_passin(Slapi_DN *sdn,const char *dn)
  848. {
  849. slapi_sdn_init(sdn);
  850. slapi_sdn_set_dn_passin(sdn,dn);
  851. return sdn;
  852. }
  853. /* use when dn is normalized previously */
  854. Slapi_DN *
  855. slapi_sdn_init_dn_ndn_byref(Slapi_DN *sdn,const char *dn) {
  856. slapi_sdn_init(sdn);
  857. slapi_sdn_set_dn_byref(sdn,dn);
  858. /* slapi_sdn_set_ndn_byref nulls out dn set in above statement */
  859. sdn->flag= slapi_unsetbit_uchar(sdn->flag,FLAG_NDN);
  860. sdn->ndn= dn;
  861. if(dn == NULL) {
  862. sdn->ndn_len=0;
  863. } else {
  864. sdn->ndn_len=strlen(dn);
  865. }
  866. return sdn;
  867. }
  868. Slapi_DN *
  869. slapi_sdn_init_ndn_byref(Slapi_DN *sdn,const char *dn)
  870. {
  871. slapi_sdn_init(sdn);
  872. slapi_sdn_set_ndn_byref(sdn,dn);
  873. return sdn;
  874. }
  875. Slapi_DN *
  876. slapi_sdn_init_ndn_byval(Slapi_DN *sdn,const char *dn)
  877. {
  878. slapi_sdn_init(sdn);
  879. slapi_sdn_set_ndn_byval(sdn,dn);
  880. return sdn;
  881. }
  882. Slapi_DN *
  883. slapi_sdn_new_dn_byval(const char *dn)
  884. {
  885. Slapi_DN *sdn= slapi_sdn_new();
  886. slapi_sdn_set_dn_byval(sdn,dn);
  887. SDN_DUMP( sdn, "slapi_sdn_new_dn_byval");
  888. return sdn;
  889. }
  890. Slapi_DN *
  891. slapi_sdn_new_ndn_byval(const char *ndn)
  892. {
  893. Slapi_DN *sdn= slapi_sdn_new();
  894. slapi_sdn_set_ndn_byval(sdn,ndn);
  895. SDN_DUMP( sdn, "slapi_sdn_new_ndn_byval");
  896. return sdn;
  897. }
  898. Slapi_DN *
  899. slapi_sdn_new_dn_byref(const char *dn)
  900. {
  901. Slapi_DN *sdn= slapi_sdn_new();
  902. slapi_sdn_set_dn_byref(sdn,dn);
  903. SDN_DUMP( sdn, "slapi_sdn_new_dn_byref");
  904. return sdn;
  905. }
  906. Slapi_DN *
  907. slapi_sdn_new_dn_passin(const char *dn)
  908. {
  909. Slapi_DN *sdn= slapi_sdn_new();
  910. slapi_sdn_set_dn_passin(sdn,dn);
  911. SDN_DUMP( sdn, "slapi_sdn_new_dn_passin");
  912. return sdn;
  913. }
  914. Slapi_DN *
  915. slapi_sdn_new_ndn_byref(const char *ndn)
  916. {
  917. Slapi_DN *sdn= slapi_sdn_new();
  918. slapi_sdn_set_ndn_byref(sdn,ndn);
  919. SDN_DUMP( sdn, "slapi_sdn_new_ndn_byref");
  920. return sdn;
  921. }
  922. Slapi_DN *
  923. slapi_sdn_set_dn_byval(Slapi_DN *sdn, const char *dn)
  924. {
  925. slapi_sdn_done(sdn);
  926. sdn->flag= slapi_setbit_uchar(sdn->flag,FLAG_DN);
  927. if(dn!=NULL)
  928. {
  929. sdn->dn= slapi_ch_strdup(dn);
  930. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_created);
  931. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_exist);
  932. }
  933. return sdn;
  934. }
  935. Slapi_DN *
  936. slapi_sdn_set_dn_byref(Slapi_DN *sdn, const char *dn)
  937. {
  938. slapi_sdn_done(sdn);
  939. sdn->flag= slapi_unsetbit_uchar(sdn->flag,FLAG_DN);
  940. sdn->dn= dn;
  941. return sdn;
  942. }
  943. Slapi_DN *
  944. slapi_sdn_set_dn_passin(Slapi_DN *sdn, const char *dn)
  945. {
  946. slapi_sdn_done(sdn);
  947. sdn->flag= slapi_setbit_uchar(sdn->flag,FLAG_DN);
  948. sdn->dn= dn;
  949. if(dn!=NULL)
  950. {
  951. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_created);
  952. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_exist);
  953. }
  954. return sdn;
  955. }
  956. Slapi_DN *
  957. slapi_sdn_set_ndn_byval(Slapi_DN *sdn, const char *ndn)
  958. {
  959. slapi_sdn_done(sdn);
  960. sdn->flag= slapi_setbit_uchar(sdn->flag,FLAG_NDN);
  961. if(ndn!=NULL)
  962. {
  963. sdn->ndn= slapi_ch_strdup(ndn);
  964. sdn->ndn_len=strlen(ndn);
  965. PR_INCREMENT_COUNTER(slapi_sdn_counter_ndn_created);
  966. PR_INCREMENT_COUNTER(slapi_sdn_counter_ndn_exist);
  967. }
  968. return sdn;
  969. }
  970. Slapi_DN *
  971. slapi_sdn_set_ndn_byref(Slapi_DN *sdn, const char *ndn)
  972. {
  973. slapi_sdn_done(sdn);
  974. sdn->flag= slapi_unsetbit_uchar(sdn->flag,FLAG_NDN);
  975. sdn->ndn= ndn;
  976. if(ndn == NULL) {
  977. sdn->ndn_len=0;
  978. } else {
  979. sdn->ndn_len=strlen(ndn);
  980. }
  981. return sdn;
  982. }
  983. /*
  984. * Set the RDN of the DN.
  985. */
  986. Slapi_DN *
  987. slapi_sdn_set_rdn(Slapi_DN *sdn, const Slapi_RDN *rdn)
  988. {
  989. const char *rawrdn= slapi_rdn_get_rdn(rdn);
  990. if(slapi_sdn_isempty(sdn))
  991. {
  992. slapi_sdn_set_dn_byval(sdn,rawrdn);
  993. }
  994. else
  995. {
  996. /* NewDN= NewRDN + OldParent */
  997. char *parentdn= slapi_dn_parent(sdn->dn);
  998. char *newdn= slapi_ch_malloc(strlen(rawrdn)+1+strlen(parentdn)+1);
  999. strcpy( newdn, rawrdn );
  1000. strcat( newdn, "," );
  1001. strcat( newdn, parentdn );
  1002. slapi_ch_free((void**)&parentdn);
  1003. slapi_sdn_set_dn_passin(sdn,newdn);
  1004. }
  1005. return sdn;
  1006. }
  1007. /*
  1008. * Add the RDN to the DN.
  1009. */
  1010. Slapi_DN *
  1011. slapi_sdn_add_rdn(Slapi_DN *sdn, const Slapi_RDN *rdn)
  1012. {
  1013. const char *rawrdn= slapi_rdn_get_rdn(rdn);
  1014. if(slapi_sdn_isempty(sdn))
  1015. {
  1016. slapi_sdn_set_dn_byval(sdn,rawrdn);
  1017. }
  1018. else
  1019. {
  1020. /* NewDN= NewRDN + DN */
  1021. const char *dn= slapi_sdn_get_dn(sdn);
  1022. char *newdn= slapi_ch_malloc(strlen(rawrdn)+1+strlen(dn)+1);
  1023. strcpy( newdn, rawrdn );
  1024. strcat( newdn, "," );
  1025. strcat( newdn, dn );
  1026. slapi_sdn_set_dn_passin(sdn,newdn);
  1027. }
  1028. return sdn;
  1029. }
  1030. /*
  1031. * Set the parent of the DN.
  1032. */
  1033. Slapi_DN *
  1034. slapi_sdn_set_parent(Slapi_DN *sdn, const Slapi_DN *parentdn)
  1035. {
  1036. if(slapi_sdn_isempty(sdn))
  1037. {
  1038. slapi_sdn_copy(parentdn, sdn);
  1039. }
  1040. else
  1041. {
  1042. /* NewDN= OldRDN + NewParent */
  1043. Slapi_RDN rdn;
  1044. const char *rawrdn;
  1045. slapi_rdn_init_dn(&rdn,sdn->dn);
  1046. rawrdn= slapi_rdn_get_rdn(&rdn);
  1047. if(slapi_sdn_isempty(parentdn))
  1048. {
  1049. slapi_sdn_set_dn_byval(sdn,rawrdn);
  1050. }
  1051. else
  1052. {
  1053. char *newdn;
  1054. newdn= slapi_ch_malloc(strlen(rawrdn)+1+strlen(parentdn->dn)+1);
  1055. strcpy( newdn, rawrdn );
  1056. strcat( newdn, "," );
  1057. strcat( newdn, parentdn->dn );
  1058. slapi_sdn_set_dn_passin(sdn,newdn);
  1059. }
  1060. slapi_rdn_done(&rdn);
  1061. }
  1062. return sdn;
  1063. }
  1064. void
  1065. slapi_sdn_done(Slapi_DN *sdn)
  1066. {
  1067. /* sdn_dump( sdn, "slapi_sdn_done"); */
  1068. if(sdn->dn!=NULL)
  1069. {
  1070. if(slapi_isbitset_uchar(sdn->flag,FLAG_DN))
  1071. {
  1072. slapi_ch_free((void**)&(sdn->dn));
  1073. sdn->flag= slapi_unsetbit_uchar(sdn->flag,FLAG_DN);
  1074. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_deleted);
  1075. PR_DECREMENT_COUNTER(slapi_sdn_counter_dn_exist);
  1076. }
  1077. else
  1078. {
  1079. sdn->dn= NULL;
  1080. }
  1081. }
  1082. if(sdn->ndn!=NULL)
  1083. {
  1084. if(slapi_isbitset_uchar(sdn->flag,FLAG_NDN))
  1085. {
  1086. slapi_ch_free((void**)&(sdn->ndn));
  1087. sdn->flag= slapi_unsetbit_uchar(sdn->flag,FLAG_NDN);
  1088. sdn->ndn_len=0;
  1089. PR_INCREMENT_COUNTER(slapi_sdn_counter_ndn_deleted);
  1090. PR_DECREMENT_COUNTER(slapi_sdn_counter_ndn_exist);
  1091. }
  1092. else
  1093. {
  1094. sdn->ndn= NULL;
  1095. sdn->ndn_len=0;
  1096. }
  1097. }
  1098. }
  1099. void
  1100. slapi_sdn_free(Slapi_DN **sdn)
  1101. {
  1102. if(sdn!=NULL && *sdn!=NULL)
  1103. {
  1104. SDN_DUMP( *sdn, "slapi_sdn_free");
  1105. slapi_sdn_done(*sdn);
  1106. if(slapi_isbitset_uchar((*sdn)->flag,FLAG_ALLOCATED))
  1107. {
  1108. slapi_ch_free((void**)sdn);
  1109. PR_INCREMENT_COUNTER(slapi_sdn_counter_deleted);
  1110. PR_DECREMENT_COUNTER(slapi_sdn_counter_exist);
  1111. }
  1112. }
  1113. }
  1114. const char *
  1115. slapi_sdn_get_dn(const Slapi_DN *sdn)
  1116. {
  1117. return (sdn->dn!=NULL ? sdn->dn : sdn->ndn);
  1118. }
  1119. const char *
  1120. slapi_sdn_get_ndn(const Slapi_DN *sdn)
  1121. {
  1122. if(sdn->ndn==NULL)
  1123. {
  1124. if(sdn->dn!=NULL)
  1125. {
  1126. char *p= slapi_ch_strdup(sdn->dn);
  1127. Slapi_DN *ncsdn= (Slapi_DN*)sdn; /* non-const Slapi_DN */
  1128. slapi_dn_normalize_case(p);
  1129. ncsdn->ndn= p;
  1130. ncsdn->ndn_len=strlen(p);
  1131. ncsdn->flag= slapi_setbit_uchar(sdn->flag,FLAG_NDN);
  1132. PR_INCREMENT_COUNTER(slapi_sdn_counter_ndn_created);
  1133. PR_INCREMENT_COUNTER(slapi_sdn_counter_ndn_exist);
  1134. }
  1135. }
  1136. return sdn->ndn;
  1137. }
  1138. void
  1139. slapi_sdn_get_parent(const Slapi_DN *sdn,Slapi_DN *sdn_parent)
  1140. {
  1141. const char *parentdn= slapi_dn_parent(slapi_sdn_get_dn(sdn));
  1142. slapi_sdn_set_dn_passin(sdn_parent,parentdn);
  1143. sdn_parent->flag= slapi_setbit_uchar(sdn_parent->flag,FLAG_DN);
  1144. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_created);
  1145. PR_INCREMENT_COUNTER(slapi_sdn_counter_dn_exist);
  1146. }
  1147. void
  1148. slapi_sdn_get_backend_parent(const Slapi_DN *sdn,Slapi_DN *sdn_parent,const Slapi_Backend *backend)
  1149. {
  1150. if(slapi_sdn_isempty(sdn) || slapi_be_issuffix( backend, sdn ))
  1151. {
  1152. slapi_sdn_done(sdn_parent);
  1153. }
  1154. else
  1155. {
  1156. slapi_sdn_get_parent(sdn,sdn_parent);
  1157. }
  1158. }
  1159. void
  1160. slapi_sdn_get_rdn(const Slapi_DN *sdn,Slapi_RDN *rdn)
  1161. {
  1162. slapi_rdn_set_dn(rdn,sdn->dn);
  1163. }
  1164. Slapi_DN *
  1165. slapi_sdn_dup(const Slapi_DN *sdn)
  1166. {
  1167. Slapi_DN *tmp;
  1168. SDN_DUMP( sdn, "slapi_sdn_dup");
  1169. tmp=slapi_sdn_new_dn_byval(slapi_sdn_get_dn(sdn));
  1170. /* can't use slapi_set_ndn_byval -- it nulls the dn */
  1171. tmp->flag= slapi_setbit_uchar(tmp->flag,FLAG_NDN);
  1172. if(sdn->ndn!=NULL)
  1173. {
  1174. tmp->ndn= slapi_ch_strdup(sdn->ndn);
  1175. tmp->ndn_len=sdn->ndn_len;
  1176. } else tmp->ndn=NULL;
  1177. return tmp;
  1178. }
  1179. void
  1180. slapi_sdn_copy(const Slapi_DN *from, Slapi_DN *to)
  1181. {
  1182. SDN_DUMP( from, "slapi_sdn_copy from");
  1183. SDN_DUMP( to, "slapi_sdn_copy to");
  1184. slapi_sdn_done(to);
  1185. slapi_sdn_set_dn_byval(to,slapi_sdn_get_dn(from));
  1186. }
  1187. int
  1188. slapi_sdn_compare( const Slapi_DN *sdn1, const Slapi_DN *sdn2 )
  1189. {
  1190. int rc;
  1191. const char *ndn1= slapi_sdn_get_ndn(sdn1);
  1192. const char *ndn2= slapi_sdn_get_ndn(sdn2);
  1193. if(ndn1==ndn2)
  1194. {
  1195. rc= 0;
  1196. }
  1197. else
  1198. {
  1199. if(ndn1==NULL)
  1200. {
  1201. rc= -1;
  1202. }
  1203. else
  1204. {
  1205. if(ndn2==NULL)
  1206. {
  1207. rc= 1;
  1208. }
  1209. else
  1210. {
  1211. rc= strcmp(ndn1,ndn2);
  1212. }
  1213. }
  1214. }
  1215. return rc;
  1216. }
  1217. int
  1218. slapi_sdn_isempty( const Slapi_DN *sdn)
  1219. {
  1220. const char *dn= slapi_sdn_get_dn(sdn);
  1221. return (dn==NULL || dn[0]=='\0');
  1222. }
  1223. int
  1224. slapi_sdn_issuffix(const Slapi_DN *sdn, const Slapi_DN *suffixsdn)
  1225. {
  1226. int rc;
  1227. const char *dn= slapi_sdn_get_ndn(sdn);
  1228. const char *suffixdn= slapi_sdn_get_ndn(suffixsdn);
  1229. if(dn!=NULL && suffixdn!=NULL)
  1230. {
  1231. int dnlen = slapi_sdn_get_ndn_len(sdn);
  1232. int suffixlen= slapi_sdn_get_ndn_len(suffixsdn);
  1233. if (dnlen<suffixlen)
  1234. {
  1235. rc= 0;
  1236. }
  1237. else
  1238. {
  1239. if ( suffixlen == 0 )
  1240. {
  1241. return ( 1 );
  1242. }
  1243. rc= ( (strcasecmp(suffixdn, dn+dnlen-suffixlen)==0)
  1244. && ( (dnlen == suffixlen)
  1245. || DNSEPARATOR(dn[dnlen-suffixlen-1])) );
  1246. }
  1247. }
  1248. else
  1249. {
  1250. rc= 0;
  1251. }
  1252. return rc;
  1253. }
  1254. /* normalizes sdn if it hasn't already been done */
  1255. int
  1256. slapi_sdn_get_ndn_len(const Slapi_DN *sdn)
  1257. {
  1258. int r= 0;
  1259. (void)slapi_sdn_get_ndn(sdn); /* does the normalization if needed */
  1260. if(sdn->ndn!=NULL)
  1261. {
  1262. r= sdn->ndn_len;
  1263. }
  1264. return r;
  1265. }
  1266. int
  1267. slapi_sdn_isparent( const Slapi_DN *parent, const Slapi_DN *child )
  1268. {
  1269. int rc= 0;
  1270. /* child is root - has no parent */
  1271. if ( !slapi_sdn_isempty(child) )
  1272. {
  1273. Slapi_DN childparent;
  1274. slapi_sdn_init(&childparent);
  1275. slapi_sdn_get_parent(child,&childparent);
  1276. rc= (slapi_sdn_compare(parent,&childparent)==0);
  1277. slapi_sdn_done(&childparent);
  1278. }
  1279. return( rc );
  1280. }
  1281. int
  1282. slapi_sdn_isgrandparent( const Slapi_DN *parent, const Slapi_DN *child )
  1283. {
  1284. int rc= 0;
  1285. /* child is root - has no parent */
  1286. if ( !slapi_sdn_isempty(child) )
  1287. {
  1288. Slapi_DN childparent;
  1289. slapi_sdn_init(&childparent);
  1290. slapi_sdn_get_parent(child,&childparent);
  1291. if ( !slapi_sdn_isempty(&childparent) )
  1292. {
  1293. Slapi_DN childchildparent;
  1294. slapi_sdn_init(&childchildparent);
  1295. slapi_sdn_get_parent(&childparent,&childchildparent);
  1296. rc= (slapi_sdn_compare(parent,&childchildparent)==0);
  1297. slapi_sdn_done(&childchildparent);
  1298. }
  1299. slapi_sdn_done(&childparent);
  1300. }
  1301. return( rc );
  1302. }
  1303. /*
  1304. * Return non-zero if "dn" matches the scoping criteria
  1305. * given by "base" and "scope".
  1306. */
  1307. int
  1308. slapi_sdn_scope_test( const Slapi_DN *dn, const Slapi_DN *base, int scope )
  1309. {
  1310. int rc = 0;
  1311. switch ( scope ) {
  1312. case LDAP_SCOPE_BASE:
  1313. rc = ( slapi_sdn_compare( dn, base ) == 0 );
  1314. break;
  1315. case LDAP_SCOPE_ONELEVEL:
  1316. rc = ( slapi_sdn_isparent( base, dn ) != 0 );
  1317. break;
  1318. case LDAP_SCOPE_SUBTREE:
  1319. rc = ( slapi_sdn_issuffix( dn, base ) != 0 );
  1320. break;
  1321. }
  1322. return rc;
  1323. }
  1324. /*
  1325. * build the new dn of an entry for moddn operations
  1326. */
  1327. char *
  1328. slapi_moddn_get_newdn(Slapi_DN *dn_olddn, char *newrdn, char *newsuperiordn)
  1329. {
  1330. char *newdn;
  1331. if( newsuperiordn!=NULL)
  1332. {
  1333. /* construct the new dn */
  1334. newdn= slapi_dn_plus_rdn(newsuperiordn, newrdn); /* JCM - Use Slapi_RDN */
  1335. }
  1336. else
  1337. {
  1338. /* construct the new dn */
  1339. char *pdn;
  1340. const char *dn= slapi_sdn_get_dn(dn_olddn);
  1341. pdn = slapi_dn_parent( dn );
  1342. if ( pdn != NULL )
  1343. {
  1344. newdn= slapi_dn_plus_rdn(pdn, newrdn); /* JCM - Use Slapi_RDN */
  1345. }
  1346. else
  1347. {
  1348. newdn= slapi_ch_strdup(newrdn);
  1349. }
  1350. slapi_ch_free( (void**)&pdn );
  1351. }
  1352. return newdn;
  1353. }
  1354. /* JCM slapi_sdn_get_first ? */
  1355. /* JCM slapi_sdn_get_next ? */
  1356. #ifdef SDN_DEBUG
  1357. static void
  1358. sdn_dump( const Slapi_DN *sdn, const char *text)
  1359. {
  1360. LDAPDebug( LDAP_DEBUG_ANY, "SDN %s ptr=%lx dn=%s\n", text, sdn, (sdn->dn==NULL?"NULL":sdn->dn));
  1361. }
  1362. #endif