rdn.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #include "slap.h"
  39. #define FLAG_RDNS 0
  40. Slapi_RDN *
  41. slapi_rdn_new()
  42. {
  43. Slapi_RDN *rdn= (Slapi_RDN *)slapi_ch_malloc(sizeof(struct slapi_rdn));
  44. slapi_rdn_init(rdn);
  45. return rdn;
  46. }
  47. Slapi_RDN *
  48. slapi_rdn_new_dn(const char *dn)
  49. {
  50. Slapi_RDN *rdn= slapi_rdn_new();
  51. slapi_rdn_init_dn(rdn,dn);
  52. return rdn;
  53. }
  54. Slapi_RDN *
  55. slapi_rdn_new_sdn(const Slapi_DN *sdn)
  56. {
  57. Slapi_RDN *rdn= slapi_rdn_new();
  58. slapi_rdn_init_sdn(rdn,sdn);
  59. return rdn;
  60. }
  61. Slapi_RDN *
  62. slapi_rdn_new_rdn(const Slapi_RDN *fromrdn)
  63. {
  64. Slapi_RDN *rdn= slapi_rdn_new();
  65. slapi_rdn_init_rdn(rdn,fromrdn);
  66. return rdn;
  67. }
  68. void
  69. slapi_rdn_init(Slapi_RDN *rdn)
  70. {
  71. rdn->flag= 0;
  72. rdn->rdn= NULL;
  73. rdn->rdns= NULL;
  74. rdn->butcheredupto= -1; /* Means we haven't started converting '=' to '\0' in rdns */
  75. }
  76. void
  77. slapi_rdn_init_dn(Slapi_RDN *rdn,const char *dn)
  78. {
  79. slapi_rdn_init(rdn);
  80. if(dn!=NULL)
  81. {
  82. char **dns= ldap_explode_dn(dn, 0);
  83. if(dns!=NULL)
  84. {
  85. rdn->rdn= slapi_ch_strdup(dns[0]);
  86. ldap_value_free(dns);
  87. }
  88. }
  89. }
  90. void
  91. slapi_rdn_init_sdn(Slapi_RDN *rdn,const Slapi_DN *sdn)
  92. {
  93. if(sdn!=NULL)
  94. {
  95. slapi_rdn_init_dn(rdn,slapi_sdn_get_dn(sdn));
  96. }
  97. else
  98. {
  99. slapi_rdn_init(rdn);
  100. }
  101. }
  102. void
  103. slapi_rdn_init_rdn(Slapi_RDN *rdn,const Slapi_RDN *fromrdn)
  104. {
  105. slapi_rdn_init(rdn);
  106. rdn->rdn= slapi_ch_strdup(fromrdn->rdn);
  107. }
  108. void
  109. slapi_rdn_set_dn(Slapi_RDN *rdn,const char *dn)
  110. {
  111. slapi_rdn_done(rdn);
  112. slapi_rdn_init_dn(rdn,dn);
  113. }
  114. void
  115. slapi_rdn_set_sdn(Slapi_RDN *rdn,const Slapi_DN *sdn)
  116. {
  117. slapi_rdn_done(rdn);
  118. slapi_rdn_init_sdn(rdn,sdn);
  119. }
  120. void
  121. slapi_rdn_set_rdn(Slapi_RDN *rdn,const Slapi_RDN *fromrdn)
  122. {
  123. slapi_rdn_done(rdn);
  124. slapi_rdn_init_rdn(rdn,fromrdn);
  125. }
  126. static char **
  127. slapi_rdn_get_rdns(Slapi_RDN *rdn)
  128. {
  129. char **rdns= NULL;
  130. /* Check if rdns is upto date */
  131. if(!slapi_isbitset_uchar(rdn->flag,FLAG_RDNS))
  132. {
  133. if(rdn->rdns!=NULL)
  134. {
  135. ldap_value_free(rdn->rdns);
  136. rdn->rdns= NULL;
  137. }
  138. if(rdn->rdn!=NULL)
  139. {
  140. rdn->rdns = ldap_explode_rdn( rdn->rdn, 0 );
  141. rdns= rdn->rdns;
  142. }
  143. slapi_setbit_uchar(rdn->flag,FLAG_RDNS);
  144. rdn->butcheredupto= -1;
  145. }
  146. return rdns;
  147. }
  148. void
  149. slapi_rdn_free(Slapi_RDN **rdn)
  150. {
  151. if(rdn!=NULL)
  152. {
  153. slapi_rdn_done(*rdn);
  154. slapi_ch_free((void**)rdn);
  155. }
  156. }
  157. void
  158. slapi_rdn_done(Slapi_RDN *rdn)
  159. {
  160. if(rdn!=NULL)
  161. {
  162. slapi_ch_free((void**)&(rdn->rdn));
  163. ldap_value_free(rdn->rdns);
  164. slapi_rdn_init(rdn);
  165. }
  166. }
  167. int
  168. slapi_rdn_get_first(Slapi_RDN *rdn, char **type, char **value)
  169. {
  170. return slapi_rdn_get_next(rdn, 0, type, value);
  171. }
  172. int
  173. slapi_rdn_get_next(Slapi_RDN *rdn, int index, char **type, char **value)
  174. {
  175. int returnindex;
  176. PR_ASSERT(index>=0);
  177. if(rdn->rdns==NULL)
  178. {
  179. rdn->rdns= slapi_rdn_get_rdns(rdn);
  180. }
  181. if (rdn->rdns == NULL || rdn->rdns[index]==NULL)
  182. {
  183. *type= NULL;
  184. *value= NULL;
  185. returnindex= -1;
  186. }
  187. else
  188. {
  189. if(rdn->butcheredupto>=index)
  190. {
  191. /* the '=' has already been converted to a '\0' */
  192. *type= rdn->rdns[index];
  193. *value= *type + strlen(*type) + 1;
  194. returnindex= ++index;
  195. }
  196. else
  197. {
  198. *type= strchr(rdn->rdns[index],'=');
  199. if(*type==NULL)
  200. {
  201. /* This just shouldn't happen... */
  202. *type= NULL;
  203. *value= NULL;
  204. returnindex= -1;
  205. }
  206. else
  207. {
  208. **type = '\0';
  209. *value= *type;
  210. (*value)++; /* Skip the '\0' */
  211. *type = rdn->rdns[index];
  212. rdn->butcheredupto= index;
  213. returnindex= ++index;
  214. }
  215. }
  216. }
  217. return returnindex;
  218. }
  219. int
  220. slapi_rdn_get_index(Slapi_RDN *rdn, const char *type, const char *value, size_t length)
  221. {
  222. int result;
  223. char *theValue;
  224. result= slapi_rdn_get_index_attr(rdn, type, &theValue);
  225. if(result!=-1)
  226. {
  227. if(theValue==NULL || (strncasecmp(value,theValue,length)!=0))
  228. {
  229. result= -1;
  230. }
  231. }
  232. return result;
  233. }
  234. int
  235. slapi_rdn_get_index_attr(Slapi_RDN *rdn, const char *type, char **value)
  236. {
  237. int result= -1;
  238. int index;
  239. char *theType;
  240. index= slapi_rdn_get_first(rdn, &theType, value);
  241. while(index!=-1)
  242. {
  243. if(theType!=NULL && value!=NULL &&
  244. (strcasecmp(type,theType)==0))
  245. {
  246. result= index;
  247. index= -1;
  248. }
  249. else
  250. {
  251. index= slapi_rdn_get_next(rdn, index, &theType, value);
  252. }
  253. }
  254. return result;
  255. }
  256. int
  257. slapi_rdn_contains_attr(Slapi_RDN *rdn, const char *type, char **value)
  258. {
  259. return (slapi_rdn_get_index_attr(rdn,type,value)!=-1);
  260. }
  261. int
  262. slapi_rdn_contains(Slapi_RDN *rdn, const char *type, const char *value, size_t length)
  263. {
  264. return (slapi_rdn_get_index(rdn,type,value,length)!=-1);
  265. }
  266. int
  267. slapi_rdn_add(Slapi_RDN *rdn, const char *type, const char *value)
  268. {
  269. PR_ASSERT(NULL != type);
  270. PR_ASSERT(NULL != value);
  271. if(rdn->rdn==NULL)
  272. {
  273. /* type=value '\0' */
  274. rdn->rdn= slapi_ch_malloc(strlen(type)+1+strlen(value)+1);
  275. strcpy( rdn->rdn, type );
  276. strcat( rdn->rdn, "=" );
  277. strcat( rdn->rdn, value );
  278. }
  279. else
  280. {
  281. /* type=value+rdn '\0' */
  282. char *newrdn= slapi_ch_malloc(strlen(type)+1+strlen(value)+1+strlen(rdn->rdn)+1);
  283. strcpy( newrdn, type );
  284. strcat( newrdn, "=" );
  285. strcat( newrdn, value );
  286. strcat( newrdn, "+" );
  287. strcat( newrdn, rdn->rdn );
  288. slapi_ch_free((void**)&rdn->rdn);
  289. rdn->rdn= newrdn;
  290. }
  291. slapi_unsetbit_uchar(rdn->flag,FLAG_RDNS);
  292. return 1;
  293. }
  294. int
  295. slapi_rdn_remove_index(Slapi_RDN *rdn, int atindex)
  296. {
  297. Slapi_RDN newrdn;
  298. int result= 0;
  299. int index;
  300. char *theType;
  301. char *theValue;
  302. slapi_rdn_init(&newrdn);
  303. index= slapi_rdn_get_first(rdn, &theType, &theValue);
  304. while(index!=-1)
  305. {
  306. if(index!=atindex)
  307. {
  308. slapi_rdn_add(&newrdn,theType,theValue);
  309. }
  310. else
  311. {
  312. result= 1;
  313. }
  314. index= slapi_rdn_get_next(rdn, index, &theType, &theValue);
  315. }
  316. if(result)
  317. {
  318. slapi_rdn_set_rdn(rdn,&newrdn);
  319. }
  320. slapi_rdn_done(&newrdn);
  321. return result;
  322. }
  323. int
  324. slapi_rdn_remove(Slapi_RDN *rdn, const char *type, const char *value, size_t length)
  325. {
  326. int result= 0;
  327. if(rdn->rdn!=NULL)
  328. {
  329. int atindex= slapi_rdn_get_index(rdn, type, value, length);
  330. if(atindex!=-1)
  331. {
  332. result= slapi_rdn_remove_index(rdn, atindex);
  333. }
  334. }
  335. return result;
  336. }
  337. int
  338. slapi_rdn_remove_attr(Slapi_RDN *rdn, const char *type)
  339. {
  340. int result= 0;
  341. if(rdn->rdn!=NULL)
  342. {
  343. char *value;
  344. int atindex= slapi_rdn_get_index_attr(rdn, type, &value);
  345. if(atindex!=-1)
  346. {
  347. result= slapi_rdn_remove_index(rdn, atindex);
  348. }
  349. }
  350. return result;
  351. }
  352. int
  353. slapi_rdn_isempty(const Slapi_RDN *rdn)
  354. {
  355. return (rdn->rdn==NULL || rdn->rdn[0]=='\0');
  356. }
  357. int
  358. slapi_rdn_get_num_components(Slapi_RDN *rdn)
  359. {
  360. int i= 0;
  361. char **rdns= slapi_rdn_get_rdns(rdn);
  362. if(rdns!=NULL)
  363. {
  364. for(i=0; rdns[i]!=NULL; i++);
  365. }
  366. return i;
  367. }
  368. int
  369. slapi_rdn_compare(Slapi_RDN *rdn1, Slapi_RDN *rdn2)
  370. {
  371. int r= 1;
  372. int n1= slapi_rdn_get_num_components(rdn1);
  373. int n2= slapi_rdn_get_num_components(rdn2);
  374. if (n1==n2)
  375. {
  376. char *type, *value;
  377. int i= slapi_rdn_get_first(rdn1, &type, &value);
  378. while(r==1 && i!=-1)
  379. {
  380. r= slapi_rdn_contains(rdn2, type, value, strlen(value));
  381. i= slapi_rdn_get_next(rdn1, i, &type, &value);
  382. }
  383. if(r==1) /* All rdn1's rdn components were in rdn2 */
  384. {
  385. r= 0; /* SAME */
  386. }
  387. else
  388. {
  389. r= -1; /* NOT SAME */
  390. }
  391. }
  392. else
  393. {
  394. r= -1; /* NOT SAME */
  395. }
  396. return r;
  397. }
  398. const char *
  399. slapi_rdn_get_rdn(const Slapi_RDN *rdn)
  400. {
  401. return rdn->rdn;
  402. }
  403. const char *
  404. slapi_rdn_get_nrdn(const Slapi_RDN *rdn)
  405. {
  406. /* JCM - Normalised RDN? */
  407. PR_ASSERT(0);
  408. return NULL;
  409. }