aclutil.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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. #include "acl.h"
  42. /**************************************************************************
  43. * Defines and usefuls stuff
  44. ****************************************************************************/
  45. /*************************************************************************
  46. * Prototypes
  47. *************************************************************************/
  48. static void aclutil__typestr (int type , char str[]);
  49. static void aclutil__Ruletypestr (int type , char str[]);
  50. static char* __aclutil_extract_dn_component ( char **e_dns, int position,
  51. char *attrName );
  52. static int acl_find_comp_start(char * s, int pos );
  53. static PRIntn acl_ht_free_entry_and_value(PLHashEntry *he, PRIntn i,
  54. void *arg);
  55. static PLHashNumber acl_ht_hash( const void *key);
  56. #ifdef FOR_DEBUGGING
  57. static PRIntn acl_ht_display_entry(PLHashEntry *he, PRIntn i, void *arg);
  58. #endif
  59. /***************************************************************************/
  60. /* UTILITY FUNCTIONS */
  61. /***************************************************************************/
  62. int
  63. aclutil_str_appened(char **str1, const char *str2)
  64. {
  65. int new_len;
  66. if ( str1 == NULL || str2 == NULL )
  67. return(0);
  68. if ( *str1 == NULL ) {
  69. new_len = strlen(str2) + 1;
  70. *str1 = (char *)slapi_ch_malloc(new_len);
  71. *str1[0] = 0;
  72. } else {
  73. new_len = strlen(*str1) + strlen(str2) + 1;
  74. *str1 = (char *)slapi_ch_realloc(*str1, new_len);
  75. }
  76. if ( *str1 == NULL )
  77. return(-1);
  78. strcat(*str1, str2);
  79. return(0);
  80. }
  81. /***************************************************************************/
  82. /* Print routines */
  83. /***************************************************************************/
  84. /* print eroror message returned from the ACL Library */
  85. #define ACLUTIL_ACLLIB_MSGBUF_LEN 200
  86. void
  87. acl_print_acllib_err (NSErr_t *errp , char * str)
  88. {
  89. char msgbuf[ ACLUTIL_ACLLIB_MSGBUF_LEN ];
  90. if ((NULL == errp ) || !slapi_is_loglevel_set ( SLAPI_LOG_ACL ) )
  91. return;
  92. aclErrorFmt(errp, msgbuf, ACLUTIL_ACLLIB_MSGBUF_LEN, 1);
  93. msgbuf[ACLUTIL_ACLLIB_MSGBUF_LEN-1] = '\0';
  94. if (msgbuf)
  95. slapi_log_error(SLAPI_LOG_ACL, plugin_name,"ACL LIB ERR:(%s)(%s)\n",
  96. msgbuf, str ? str: "NULL");
  97. }
  98. void
  99. aclutil_print_aci (aci_t *aci_item, char *type)
  100. {
  101. char str[BUFSIZ];
  102. const char *dn;
  103. if ( ! slapi_is_loglevel_set ( SLAPI_LOG_ACL ) )
  104. return;
  105. if (!aci_item) {
  106. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  107. "acl__print_aci: Null item\n");
  108. return;
  109. }
  110. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  111. "***BEGIN ACL INFO[ Name:%s]***\n", aci_item->aclName);
  112. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  113. "ACL Index:%d ACL_ELEVEL:%d\n", aci_item->aci_index, aci_item->aci_elevel );
  114. aclutil__access_str (aci_item->aci_access, str);
  115. aclutil__typestr (aci_item->aci_type, &str[strlen(str)]);
  116. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  117. "ACI type:(%s)\n", str);
  118. aclutil__Ruletypestr (aci_item->aci_ruleType, str);
  119. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  120. "ACI RULE type:(%s)\n",str);
  121. dn = slapi_sdn_get_dn ( aci_item->aci_sdn );
  122. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  123. "Slapi_Entry DN:%s\n", escape_string_with_punctuation (dn, str));
  124. slapi_log_error (SLAPI_LOG_ACL, plugin_name,
  125. "***END ACL INFO*****************************\n");
  126. }
  127. void
  128. aclutil_print_err (int rv , const Slapi_DN *sdn, const struct berval* val,
  129. char **errbuf)
  130. {
  131. char ebuf [BUFSIZ];
  132. /*
  133. * The maximum size of line is ebuf_size + the log message
  134. * itself (less than 200 characters for all but potentially ACL_INVALID_TARGET)
  135. */
  136. char line [BUFSIZ + 200];
  137. char str [1024];
  138. const char *dn;
  139. char *lineptr = line;
  140. char *newline = NULL;
  141. if ( rv >= 0)
  142. return;
  143. if (val->bv_len > 0 && val->bv_val != NULL) {
  144. PR_snprintf (str, sizeof(str), "%.1023s", val->bv_val);
  145. } else {
  146. str[0] = '\0';
  147. }
  148. dn = slapi_sdn_get_dn ( sdn );
  149. if (dn && (rv == ACL_INVALID_TARGET) && ((strlen(dn) + strlen(str)) > BUFSIZ)) {
  150. /*
  151. * if (str_length + dn_length + 200 char message) > (BUFSIZ + 200) line
  152. * we have to make space for a bigger line...
  153. */
  154. newline = slapi_ch_malloc(strlen(dn) + strlen(str) + 200);
  155. lineptr = newline;
  156. }
  157. switch (rv) {
  158. case ACL_TARGET_FILTER_ERR:
  159. sprintf (line, "ACL Internal Error(%d): "
  160. "Error in generating the target filter for the ACL(%s)\n",
  161. rv, escape_string_with_punctuation (str, ebuf));
  162. break;
  163. case ACL_TARGETATTR_FILTER_ERR:
  164. sprintf (line, "ACL Internal Error(%d): "
  165. "Error in generating the targetattr filter for the ACL(%s)\n",
  166. rv, escape_string_with_punctuation (str, ebuf));
  167. break;
  168. case ACL_TARGETFILTER_ERR:
  169. sprintf (line, "ACL Internal Error(%d): "
  170. "Error in generating the targetfilter filter for the ACL(%s)\n",
  171. rv, escape_string_with_punctuation (str, ebuf));
  172. break;
  173. case ACL_SYNTAX_ERR:
  174. sprintf (line, "ACL Syntax Error(%d):%s\n",
  175. rv, escape_string_with_punctuation (str, ebuf));
  176. break;
  177. case ACL_ONEACL_TEXT_ERR:
  178. sprintf (line, "ACL Syntax Error in the Bind Rules(%d):%s\n",
  179. rv, escape_string_with_punctuation (str, ebuf));
  180. break;
  181. case ACL_ERR_CONCAT_HANDLES:
  182. sprintf (line, "ACL Internal Error(%d): "
  183. "Error in Concatenating List handles\n",
  184. rv);
  185. break;
  186. case ACL_INVALID_TARGET:
  187. sprintf (lineptr, "ACL Invalid Target Error(%d): "
  188. "Target is beyond the scope of the ACL(SCOPE:%s)",
  189. rv, dn ? escape_string_with_punctuation (dn, ebuf) : "NULL");
  190. sprintf (lineptr + strlen(lineptr), " %s\n", escape_string_with_punctuation (str, ebuf));
  191. break;
  192. case ACL_INVALID_AUTHMETHOD:
  193. sprintf (line, "ACL Multiple auth method Error(%d):"
  194. "Multiple Authentication Metod in the ACL(%s)\n",
  195. rv, escape_string_with_punctuation (str, ebuf));
  196. break;
  197. case ACL_INVALID_AUTHORIZATION:
  198. sprintf (line, "ACL Syntax Error(%d):"
  199. "Invalid Authorization statement in the ACL(%s)\n",
  200. rv, escape_string_with_punctuation (str, ebuf));
  201. break;
  202. case ACL_INCORRECT_ACI_VERSION:
  203. sprintf (line, "ACL Syntax Error(%d):"
  204. "Incorrect version Number in the ACL(%s)\n",
  205. rv, escape_string_with_punctuation (str, ebuf));
  206. break;
  207. default:
  208. sprintf (line, "ACL Internal Error(%d):"
  209. "ACL generic error (%s)\n",
  210. rv, escape_string_with_punctuation (str, ebuf));
  211. break;
  212. }
  213. if (errbuf) {
  214. /* If a buffer is provided, then copy the error */
  215. aclutil_str_appened(errbuf, lineptr );
  216. }
  217. slapi_log_error( SLAPI_LOG_FATAL, plugin_name, "%s", lineptr);
  218. if (newline) slapi_ch_free((void **) &newline);
  219. }
  220. /***************************************************************************
  221. * Convert access to str
  222. ***************************************************************************/
  223. char*
  224. aclutil__access_str (int type , char str[])
  225. {
  226. char *p;
  227. str[0] = '\0';
  228. p = str;
  229. if (type & SLAPI_ACL_COMPARE) {
  230. strcpy (p, "compare ");
  231. p = strchr (p, '\0');
  232. }
  233. if (type & SLAPI_ACL_SEARCH) {
  234. strcpy (p, "search ");
  235. p = strchr (p, '\0');
  236. }
  237. if (type & SLAPI_ACL_READ) {
  238. strcpy (p, "read ");
  239. p = strchr (p, '\0');
  240. }
  241. if (type & SLAPI_ACL_WRITE) {
  242. strcpy (p, "write ");
  243. p = strchr (p, '\0');
  244. }
  245. if (type & SLAPI_ACL_DELETE) {
  246. strcpy (p, "delete ");
  247. p = strchr (p, '\0');
  248. }
  249. if (type & SLAPI_ACL_ADD) {
  250. strcpy (p, "add ");
  251. p = strchr (p, '\0');
  252. }
  253. if (type & SLAPI_ACL_SELF) {
  254. strcpy (p, "self ");
  255. p = strchr (p, '\0');
  256. }
  257. if (type & SLAPI_ACL_PROXY) {
  258. strcpy (p, "proxy ");
  259. }
  260. return str;
  261. }
  262. /***************************************************************************
  263. * Convert type to str
  264. ***************************************************************************/
  265. static void
  266. aclutil__typestr (int type , char str[])
  267. {
  268. char *p;
  269. /* Start copying in at whatever location is passed in */
  270. p = str;
  271. if (type & ACI_TARGET_DN) {
  272. strcpy (p, "target_DN ");
  273. p = strchr (p, '\0');
  274. }
  275. if (type & ACI_TARGET_ATTR) {
  276. strcpy (p, "target_attr ");
  277. p = strchr (p, '\0');
  278. }
  279. if (type & ACI_TARGET_PATTERN) {
  280. strcpy (p, "target_patt ");
  281. p = strchr (p, '\0');
  282. }
  283. if ((type & ACI_TARGET_ATTR_ADD_FILTERS) | (type & ACI_TARGET_ATTR_DEL_FILTERS)) {
  284. strcpy (p, "targetattrfilters ");
  285. p = strchr (p, '\0');
  286. }
  287. if (type & ACI_TARGET_FILTER) {
  288. strcpy (p, "target_filter ");
  289. p = strchr (p, '\0');
  290. }
  291. if (type & ACI_ACLTXT) {
  292. strcpy (p, "acltxt ");
  293. p = strchr (p, '\0');
  294. }
  295. if (type & ACI_TARGET_NOT) {
  296. strcpy (p, "target_not ");
  297. p = strchr (p, '\0');
  298. }
  299. if (type & ACI_TARGET_ATTR_NOT) {
  300. strcpy (p, "target_attr_not ");
  301. p = strchr (p, '\0');
  302. }
  303. if (type & ACI_TARGET_FILTER_NOT) {
  304. strcpy (p, "target_filter_not ");
  305. p = strchr (p, '\0');
  306. }
  307. if (type & ACI_HAS_ALLOW_RULE) {
  308. strcpy (p, "allow_rule ");
  309. p = strchr (p, '\0');
  310. }
  311. if (type & ACI_HAS_DENY_RULE) {
  312. strcpy (p, "deny_rule ");
  313. p = strchr (p, '\0');
  314. }
  315. }
  316. static void
  317. aclutil__Ruletypestr (int type , char str[])
  318. {
  319. char *p;
  320. str[0] = '\0';
  321. p = str;
  322. if ( type & ACI_USERDN_RULE) {
  323. strcpy (p, "userdn ");
  324. p = strchr (p, '\0');
  325. }
  326. if ( type & ACI_USERDNATTR_RULE) {
  327. strcpy (p, "userdnattr ");
  328. p = strchr (p, '\0');
  329. }
  330. if ( type & ACI_USERATTR_RULE) {
  331. strcpy (p, "userattr ");
  332. p = strchr (p, '\0');
  333. }
  334. if ( type & ACI_GROUPDN_RULE) {
  335. strcpy (p, "groupdn ");
  336. p = strchr (p, '\0');
  337. }
  338. if ( type & ACI_GROUPDNATTR_RULE) {
  339. strcpy (p, "groupdnattr ");
  340. p = strchr (p, '\0');
  341. }
  342. if ( type & ACI_ROLEDN_RULE) {
  343. strcpy (p, "roledn ");
  344. p = strchr (p, '\0');
  345. }
  346. if ( type & ACI_IP_RULE) {
  347. strcpy (p, "ip ");
  348. p = strchr (p, '\0');
  349. }
  350. if ( type & ACI_DNS_RULE) {
  351. strcpy (p, "dns ");
  352. p = strchr (p, '\0');
  353. }
  354. if ( type & ACI_TIMEOFDAY_RULE) {
  355. strcpy (p, "timeofday ");
  356. p = strchr (p, '\0');
  357. }
  358. if ( type & ACI_DAYOFWEEK_RULE) {
  359. strcpy (p, "dayofweek ");
  360. p = strchr (p, '\0');
  361. }
  362. if ( type & ACI_AUTHMETHOD_RULE) {
  363. strcpy (p, "authmethod ");
  364. p = strchr (p, '\0');
  365. }
  366. if ( type & ACI_PARAM_DNRULE) {
  367. strcpy (p, "paramdn ");
  368. p = strchr (p, '\0');
  369. }
  370. if ( type & ACI_PARAM_ATTRRULE) {
  371. strcpy (p, "paramAttr ");
  372. p = strchr (p, '\0');
  373. }
  374. if ( type & ACI_SSF_RULE) {
  375. strcpy (p, "ssf ");
  376. p = strchr (p, '\0');
  377. }
  378. }
  379. /*
  380. ** acl_gen_err_msg
  381. ** This function is called by backend to generate the error message
  382. ** if access is denied.
  383. */
  384. void
  385. acl_gen_err_msg(int access, char *edn, char *attr, char **errbuf)
  386. {
  387. char *line = NULL;
  388. if (access & SLAPI_ACL_WRITE) {
  389. line = PR_smprintf(
  390. "Insufficient 'write' privilege to the '%s' attribute of entry '%s'.\n",
  391. attr ? attr: "NULL", edn);
  392. } else if ( access & SLAPI_ACL_ADD ) {
  393. line = PR_smprintf(
  394. "Insufficient 'add' privilege to add the entry '%s'.\n",edn);
  395. } else if ( access & SLAPI_ACL_DELETE ) {
  396. line = PR_smprintf(
  397. "Insufficient 'delete' privilege to delete the entry '%s'.\n",edn);
  398. }
  399. aclutil_str_appened(errbuf, line );
  400. if (line) {
  401. PR_smprintf_free(line);
  402. line = NULL;
  403. }
  404. }
  405. short
  406. aclutil_gen_signature ( short c_signature )
  407. {
  408. short o_signature = 0;
  409. short randval = (short)slapi_rand();
  410. o_signature = c_signature ^ (randval % 32768);
  411. if (!o_signature) {
  412. randval = (short)slapi_rand();
  413. o_signature = c_signature ^ (randval % 32768);
  414. }
  415. return o_signature;
  416. }
  417. void
  418. aclutil_print_resource( struct acl_pblock *aclpb, char *right , char *attr, char *clientdn )
  419. {
  420. char str[BUFSIZ];
  421. const char *dn;
  422. if ( aclpb == NULL) return;
  423. if ( ! slapi_is_loglevel_set ( SLAPI_LOG_ACL ) )
  424. return;
  425. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " ************ RESOURCE INFO STARTS *********\n");
  426. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " Client DN: %s\n",
  427. clientdn ? escape_string_with_punctuation (clientdn, str) : "NULL");
  428. aclutil__access_str (aclpb->aclpb_access, str);
  429. aclutil__typestr (aclpb->aclpb_res_type, &str[strlen(str)]);
  430. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " resource type:%d(%s)\n",
  431. aclpb->aclpb_res_type, str);
  432. dn = slapi_sdn_get_dn ( aclpb->aclpb_curr_entry_sdn );
  433. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " Slapi_Entry DN: %s\n",
  434. dn ? escape_string_with_punctuation ( dn , str) : "NULL");
  435. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " ATTR: %s\n", attr ? attr : "NULL");
  436. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " rights:%s\n", right ? right: "NULL");
  437. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " ************ RESOURCE INFO ENDS *********\n");
  438. }
  439. /*
  440. * The input string contains a rule like
  441. * "cn=helpdesk, ou=$attr.deptName, o=$dn.o, o=ISP"
  442. *
  443. * Where $attr -- means look into the attribute list for values
  444. * $dn -- means look into the entry's dn
  445. *
  446. * We extract the values from the entry and returned a string
  447. * with the values added.
  448. * For "$attr" rule - if we find multiple values then it is
  449. * the pattern is not expanded.
  450. * For "$dn" rule, if we find multiple of them, we use the relative
  451. * position.
  452. * NOTE: The caller is responsible in freeing the memory.
  453. */
  454. char *
  455. aclutil_expand_paramString ( char *str, Slapi_Entry *e )
  456. {
  457. char **e_dns;
  458. char **a_dns;
  459. char *attrName;
  460. char *s, *p;
  461. char *attrVal;
  462. int i, len;
  463. int ncomponents, type;
  464. int rc = -1;
  465. char *buf = NULL;
  466. e_dns = ldap_explode_dn ( slapi_entry_get_ndn ( e ), 0 );
  467. a_dns = ldap_explode_dn ( str, 0 );
  468. i = 0;
  469. ncomponents = 0;
  470. while ( a_dns[ncomponents] )
  471. ncomponents++;
  472. for (i=0; i < ncomponents; i++ ) {
  473. /* Look for"$" char */
  474. if ( (s = strchr ( a_dns[i], '$') ) != NULL) {
  475. p = s;
  476. s++;
  477. if ( strncasecmp (s, "dn", 2) == 0 )
  478. type = 1;
  479. else if ( strncasecmp (s, "attr", 4) == 0 )
  480. type = 2;
  481. else {
  482. /* error */
  483. goto cleanup;
  484. }
  485. *p = '\0';
  486. aclutil_str_appened ( &buf,a_dns[i]);
  487. if ( type == 1 ) {
  488. /* xyz = $dn.o */
  489. s +=3;
  490. attrName = s;
  491. attrVal = __aclutil_extract_dn_component (e_dns,
  492. ncomponents-i, attrName);
  493. if ( NULL == attrVal ) /*error*/
  494. goto cleanup;
  495. } else {
  496. Slapi_Attr *attr;
  497. const struct berval *attrValue;
  498. int kk;
  499. Slapi_Value *sval, *t_sval;
  500. /* The pattern is x=$attr.o" */
  501. s +=5;
  502. attrName = s;
  503. slapi_entry_attr_find ( e, attrName, &attr );
  504. if ( NULL == attr )
  505. goto cleanup;
  506. kk= slapi_attr_first_value ( attr, &sval );
  507. if ( kk != -1 ) {
  508. t_sval = sval;
  509. kk= slapi_attr_next_value( attr, kk, &sval );
  510. if ( kk != -1 ) /* can't handle multiple --error */
  511. goto cleanup;
  512. }
  513. attrValue = slapi_value_get_berval ( t_sval );
  514. attrVal = attrValue->bv_val;
  515. }
  516. } else {
  517. attrVal = a_dns[i];
  518. }
  519. aclutil_str_appened ( &buf, attrVal);
  520. aclutil_str_appened ( &buf, ",");
  521. }
  522. rc = 0; /* everything is okay*/
  523. /* remove the last comma */
  524. if (buf) {
  525. len = strlen ( buf);
  526. buf[len-1] = '\0';
  527. }
  528. cleanup:
  529. slapi_ldap_value_free ( a_dns );
  530. slapi_ldap_value_free ( e_dns );
  531. if ( 0 != rc ) /* error */ {
  532. slapi_ch_free ( (void **) &buf );
  533. buf = NULL;
  534. }
  535. return buf;
  536. }
  537. static char *
  538. __aclutil_extract_dn_component ( char **e_dns, int position, char *attrName )
  539. {
  540. int i, matched, len;
  541. char *s;
  542. int matchedPosition;
  543. len = strlen ( attrName );
  544. /* First check if there thare are multiple of these */
  545. i = matched = 0;
  546. while ( e_dns[i] ) {
  547. if (0 == strncasecmp (e_dns[i], attrName, len) ) {
  548. matched++;
  549. matchedPosition = i;
  550. }
  551. i++;
  552. }
  553. if (!matched )
  554. return NULL;
  555. if ( matched > 1 ) {
  556. matchedPosition = i - position;
  557. }
  558. if ( NULL == e_dns[matchedPosition])
  559. return NULL;
  560. s = strstr ( e_dns[matchedPosition], "=");
  561. if ( NULL == s)
  562. return NULL;
  563. else
  564. return s+1;
  565. }
  566. /*
  567. * Does the first component of ndn match the first component of match_this ?
  568. */
  569. int
  570. acl_dn_component_match( const char *ndn, char *match_this, int component_number) {
  571. return(1);
  572. }
  573. /*
  574. * Here, ndn is a resource dn and match_this is a dn, containing a macro, ($dn).
  575. *
  576. * eg. ndn is cn=fred,ou=groups,ou=people,ou=icnc,o=ISP and
  577. * match_this is "ou=Groups,($dn),o=ISP" or
  578. * "cn=*,ou=Groups,($dn),o=ISP".
  579. *
  580. * They match if:
  581. * match_this is a suffix of ndn
  582. *
  583. * It returns NULL, if they do not match.
  584. * Otherwise it returns a copy of the substring of ndn that matches the ($dn).
  585. *
  586. * eg. in the above example, "ou=people,ou=icnc"
  587. */
  588. char *
  589. acl_match_macro_in_target( const char *ndn, char * match_this,
  590. char *macro_ptr) {
  591. char *macro_prefix = NULL;
  592. int macro_prefix_len = 0;
  593. char *macro_suffix = NULL;
  594. char *tmp_ptr = NULL;
  595. char *matched_val = NULL;
  596. char *ret_val = NULL;
  597. int ndn_len = 0;
  598. int macro_suffix_len = 0;
  599. int ndn_prefix_len = 0;
  600. int ndn_prefix_end = 0;
  601. int matched_val_len = 0;
  602. /*
  603. * First, grab the macro_suffix--the bit after the ($dn)
  604. *
  605. */
  606. if (strlen(macro_ptr) == strlen(ACL_TARGET_MACRO_DN_KEY)) {
  607. macro_suffix = NULL; /* just ($dn) */
  608. } else {
  609. if ( macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY)] == ',') {
  610. macro_suffix = &macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY) + 1];
  611. } else {
  612. macro_suffix = &macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY)];
  613. }
  614. }
  615. /*
  616. * First ensure that the suffix of match_this is
  617. * a suffix of ndn.
  618. */
  619. ndn_len = strlen(ndn);
  620. if ( macro_suffix != NULL) {
  621. macro_suffix_len = strlen(macro_suffix);
  622. if( macro_suffix_len >= ndn_len ) {
  623. /*
  624. * eg ndn: o=icnc,o=sun.com
  625. * match_this: ($dn),o=icnc,o=sun.com
  626. */
  627. return(NULL); /* ($dn) must match something. */
  628. } else {
  629. /*
  630. * eg ndn: ou=People,o=icnc,o=sun.com
  631. * match_this: ($dn),o=icnc,o=sun.com
  632. *
  633. * we can do a direct strncmp() because we know that
  634. * there can be no "*" after the ($dn)...by definition.
  635. */
  636. if (strncasecmp( macro_suffix, &ndn[ndn_len-macro_suffix_len],
  637. macro_suffix_len) != 0) {
  638. return(NULL); /* suffix must match */
  639. }
  640. }
  641. }
  642. /* Here, macro_suffix is a suffix of ndn.
  643. *
  644. *
  645. * Now, look at macro_prefix, if it is NULL, then ($dn) matches
  646. * ndn[0..ndn_len-macro_suffix_len].
  647. * (eg, ndn: cn=fred,ou=People,o=sun.com
  648. * match_this: ($dn),o=sun.com.
  649. *
  650. */
  651. macro_prefix = slapi_ch_strdup(match_this);
  652. /* we know it's got a $(dn) */
  653. tmp_ptr = strstr(macro_prefix, ACL_TARGET_MACRO_DN_KEY);
  654. *tmp_ptr = '\0';
  655. /* There may be a NULL prefix eg. match_this: ($dn),o=sun.com */
  656. macro_prefix_len = strlen(macro_prefix);
  657. if (macro_prefix_len == 0) {
  658. slapi_ch_free((void **) &macro_prefix);
  659. macro_prefix = NULL;
  660. }
  661. if (macro_prefix == NULL ) {
  662. /*
  663. * ($dn) matches ndn[0..ndn_len-macro_suffix_len]
  664. */
  665. int matched_val_len = 0;
  666. matched_val_len = ndn_len-macro_suffix_len;
  667. matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
  668. strncpy(matched_val, ndn, ndn_len-macro_suffix_len);
  669. /*
  670. * Null terminate matched_val, removing trailing "," if there is
  671. * one.
  672. */
  673. if (matched_val_len > 1) {
  674. if (matched_val[matched_val_len-1] == ',' ) {
  675. matched_val[matched_val_len-1] = '\0';
  676. } else {
  677. matched_val[matched_val_len] = '\0';
  678. }
  679. }
  680. ret_val = matched_val;
  681. } else {
  682. /*
  683. * If it is not NULL, then if macro_prefix contains a * then
  684. * it needs to be an exact prefix of ndn (modulo the * component
  685. * which matches anything) becuase that's the semantics
  686. * of target patterns containing *'s, except that we just
  687. * make it match one component.
  688. * If it is such a prefix then ($dn) matches that portion of ndn
  689. * from the end of the prefix, &ndn[ndn_prefix_end] to
  690. * ndn_suffix_start.
  691. * If ndn_prefix_len > ndn_len-macro_suffix_len then return(NULL),
  692. * otherwise $(dn) matches ndn[ndn_prefix_len..ndn_len-macro_suffix_len].
  693. *
  694. *
  695. * eg. ndn: cn=fred,ou=P,o=sun.com
  696. * match_this: cn=*,($dn),o=sun.com
  697. */
  698. if ( strstr(macro_prefix, "=*") != NULL ) {
  699. int exact_match = 0;
  700. ndn_prefix_len = acl_match_prefix( macro_prefix, ndn, &exact_match);
  701. if ( ndn_prefix_len != -1 ) {
  702. /*
  703. * ndn[0..ndn_prefix_len] is the prefix in ndn.
  704. * ndn[ndn_prefix_len..ndn_len-macro_suffix_len] is the
  705. * matched string.
  706. */
  707. if (ndn_prefix_len >= ndn_len-macro_suffix_len) {
  708. /*
  709. * eg ndn: cn=fred,ou=People,o=icnc,o=sun.com
  710. * cn=*,ou=People,o=icnc,($dn),o=icnc,o=sun.com
  711. */
  712. ret_val = NULL; /* matched string is empty */
  713. } else {
  714. /*
  715. * eg ndn: cn=fred,ou=People,o=icnc,o=sun.com
  716. * cn=*,ou=People,($dn),o=sun.com
  717. */
  718. matched_val_len = ndn_len-macro_suffix_len-ndn_prefix_len;
  719. matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
  720. strncpy(matched_val, &ndn[ndn_prefix_len], matched_val_len);
  721. if (matched_val_len > 1) {
  722. if (matched_val[matched_val_len-1] == ',' ) {
  723. matched_val[matched_val_len-1] = '\0';
  724. } else {
  725. matched_val[matched_val_len] = '\0';
  726. }
  727. }
  728. matched_val[matched_val_len] = '\0';
  729. ret_val = matched_val;
  730. }
  731. } else {
  732. /* Was not a prefix so not a match */
  733. ret_val = NULL;
  734. }
  735. } else {
  736. /*
  737. *
  738. * If macro_prefix is not NULL and it does not
  739. * contain a =* then
  740. * we need to ensure that macro_prefix is a substring
  741. * ndn.
  742. * If it is and the position of the character after it's end in
  743. * ndn is
  744. * ndn_prefix_end then ($dn) matches
  745. * ndn[ndn_prefix_end..ndn_len-macro_suffix_len].
  746. *
  747. *
  748. * One important principal is that ($dn) matches a maximal
  749. * chunk--this way it will serve to make the link
  750. * between resources and users at each level of the structure.
  751. *
  752. * eg. ndn: ou=Groups,ou=Groups,ou=Groups,c=fr
  753. * macro_prefix: ou=Groups,($dn),c=fr
  754. *
  755. * then ($dn) matches ou=Groups,ou=Groups.
  756. *
  757. *
  758. *
  759. * If it is not a substring, then there is no match.
  760. * If it is a substring and
  761. * ndn[ndn_prefix_end..ndn_len-macro_suffix_len] is empty then
  762. * it's also not a match as we demand that ($dn) match a non-empty
  763. * string.
  764. *
  765. *
  766. *
  767. * (eg. ndn: cn=fred,o=icnc,ou=People,o=sun.com
  768. * match_this: o=icnc,($dn),o=sun.com.)
  769. *
  770. *
  771. * (eg. ndn: cn=fred,o=menlo park,ou=People,o=icnc,o=sun.com
  772. * match_this: o=menlo park,ou=People,($dn),o=sun.com
  773. *
  774. */
  775. ndn_prefix_end = acl_strstr((char *)ndn, macro_prefix);
  776. if ( ndn_prefix_end == -1) {
  777. ret_val = NULL;
  778. } else {
  779. /* Is a substring */
  780. ndn_prefix_end += macro_prefix_len;
  781. /*
  782. * make sure the matching part is non-empty:
  783. *
  784. * ndn[ndn_prefix_end..mndn_len-macro_suffix_len].
  785. */
  786. if ( ndn_prefix_end >= ndn_len-macro_suffix_len) {
  787. ret_val = NULL;
  788. } else {
  789. /*
  790. * ($dn) matches the non-empty string segment
  791. * ndn[ndn_prefix_end..mndn_len-macro_suffix_len]
  792. * the -1 is because macro_suffix_eln does not include
  793. * the coma before the suffix.
  794. */
  795. matched_val_len = ndn_len-macro_suffix_len-
  796. ndn_prefix_end - 1;
  797. matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
  798. strncpy(matched_val, &ndn[ndn_prefix_end],
  799. matched_val_len);
  800. matched_val[matched_val_len] = '\0';
  801. ret_val = matched_val;
  802. }
  803. }
  804. }/* contains an =* */
  805. slapi_ch_free((void **) &macro_prefix);
  806. }/* macro_prefix != NULL */
  807. return(ret_val);
  808. }
  809. /*
  810. * Checks to see if macro_prefix is an exact prefix of ndn.
  811. * macro_prefix may contain a * component.
  812. *
  813. * The length of the matched prefix in ndn is returned.
  814. * If it was not a match, a negative int is returned.
  815. * Also, if the string matched exactly,
  816. * exact_match is set to 1, other wise it was a proper prefix.
  817. *
  818. */
  819. int
  820. acl_match_prefix( char *macro_prefix, const char *ndn, int *exact_match) {
  821. int ret_code = -1;
  822. int macro_prefix_len = 0;
  823. int ndn_len = 0;
  824. int i = 0;
  825. int j = 0;
  826. int done = 0;
  827. int t = 0;
  828. char * tmp_str = NULL;
  829. int k,l = 0;
  830. *exact_match = 0; /* default to not an exact match */
  831. /* The NULL prefix matches everthing*/
  832. if (macro_prefix == NULL) {
  833. if ( ndn == NULL ) {
  834. *exact_match = 1;
  835. }
  836. return(0);
  837. } else {
  838. /* macro_prefix is not null, so if ndn is NULL, it's not a match. */
  839. if ( ndn == NULL) {
  840. return(-1);
  841. }
  842. }
  843. /*
  844. * Here, neither macro_prefix nor ndn are NULL.
  845. *
  846. * eg. macro_prefix: cn=*,ou=people,o=sun.com
  847. * ndn : cn=fred,ou=people,o=sun.com
  848. */
  849. /*
  850. * Here, there is a component with a * (eg. cn=* ) so
  851. * we need to step through the macro_prefix, and where there is
  852. * such a * match on that component,
  853. * when we run out of * componenets, jsut do a straight match.
  854. *
  855. * Out of interest, the following extended regular expression
  856. * will match just one ou rdn value from a string:
  857. * "^uid=admin,ou=\([^,]*\\\,\)*[^,]*,o=sun.com$"
  858. *
  859. *
  860. * eg. cn=fred,ou=People,o=sun.com
  861. *
  862. *
  863. * s points to the = of the component.
  864. */
  865. macro_prefix_len = strlen(macro_prefix);
  866. ndn_len = strlen(ndn);
  867. i = 0;
  868. j = 0;
  869. done = 0;
  870. while ( !done ) {
  871. /* Here ndn[0..i] has matched macro_prefix[0..j] && j<= i
  872. * i<=ndn_len j<=macro_prefix_len */
  873. if ( (t = acl_strstr(&macro_prefix[j], "=*")) < 0 ) {
  874. /*
  875. * No more *'s, do a straight match on
  876. * macro_prefix[j..macro_prefix_len] and
  877. * ndn[i..macro_prefix_len]
  878. */
  879. if( macro_prefix_len-j > ndn_len-i) {
  880. /* Not a prefix, nor a match */
  881. *exact_match = 0;
  882. ret_code = -1;
  883. done = 1;
  884. } else {
  885. /*
  886. * ndn_len-i >= macro_prefix_len - j
  887. * if macro_prefix_len-j is 0, then
  888. * it's a null prefix, so it matches.
  889. * If in addition ndn_len-i is 0 then it's
  890. * an exact match.
  891. * Otherwise, do the cmp.
  892. */
  893. if ( macro_prefix_len-j == 0) {
  894. done = 1;
  895. ret_code = i;
  896. if ( ndn_len-i == 0) {
  897. *exact_match = 1;
  898. }
  899. }else {
  900. if (strncasecmp(&macro_prefix[j], &ndn[i],
  901. macro_prefix_len-j) == 0) {
  902. *exact_match = (macro_prefix_len-j == ndn_len-i);
  903. ret_code = i + macro_prefix_len -j;
  904. done = 1;
  905. } else {
  906. /* not a prefix not a match */
  907. *exact_match = 0;
  908. ret_code = -1;
  909. done = 1;
  910. }
  911. }
  912. }
  913. }else {
  914. /*
  915. * Is another * component, so:
  916. * 1. match that component in macro_prefix (at pos k say)
  917. * with the corresponding compoent (at pos l say ) in ndn
  918. *
  919. * 2. match the intervening string ndn[i..l] and
  920. * macro_prefix[j..k].
  921. */
  922. /* First, find the start of the component in macro_prefix. */
  923. t++; /* move to the--this way we will look for "ou=" in ndn */
  924. k = acl_find_comp_start(macro_prefix, t);
  925. /* Now identify that component in ndn--if it's not there no match */
  926. tmp_str = slapi_ch_malloc(t-k+1);
  927. strncpy(tmp_str, &macro_prefix[k], t-k);
  928. tmp_str[t-k] = '\0';
  929. l = acl_strstr((char*)&ndn[i], tmp_str);
  930. if (l == -1) {
  931. *exact_match = 0;
  932. ret_code = -1;
  933. done = 1;
  934. } else {
  935. /*
  936. * Found the comp in ndn, so the comp matches.
  937. * Now test the intervening string segments:
  938. * ndn[i..l] and macro_prefix[j..k]
  939. */
  940. if ( k-j != l-i ) {
  941. *exact_match = 0;
  942. ret_code = -1;
  943. done = 1;
  944. } else{
  945. if (strncasecmp(&macro_prefix[j], &ndn[i], k-j) != 0) {
  946. *exact_match = 0;
  947. ret_code = -1;
  948. done = 1;
  949. } else {
  950. /* Matched, so bump i and j and keep going.*/
  951. i += acl_find_comp_end((char*)&ndn[l]);
  952. j += acl_find_comp_end((char*)&macro_prefix[k]);
  953. }
  954. }
  955. }
  956. slapi_ch_free((void **)&tmp_str);
  957. }
  958. }/* while */
  959. return(ret_code);
  960. }
  961. /*
  962. * returns the index in s of where the component at position
  963. * s[pos] starts.
  964. * This is the index of the character after the first unescaped comma
  965. * moving backwards in s from pos.
  966. * If this is not found then return 0, ie. the start of the string.
  967. * If the index returned is > strlen(s) then could not find it.
  968. * only such case is if you pass ",", in which case there is no component start.
  969. */
  970. static int
  971. acl_find_comp_start(char * s, int pos ) {
  972. int i =0;
  973. int comp_start = 0;
  974. i = pos;
  975. while( i > 0 && (s[i] != ',' ||
  976. s[i-1] == '\\')) {
  977. i--;
  978. }
  979. /*
  980. * i == 0 || (s[i] == ',' && s[i-1] != '\\')
  981. */
  982. if (i==0) {
  983. /* Got all the way with no unescaped comma */
  984. if (s[i] == ',') {
  985. comp_start = i+1;
  986. } else {
  987. comp_start = i;
  988. }
  989. } else { /* Found an unescaped comma */
  990. comp_start = i + 1;
  991. }
  992. return( comp_start);
  993. }
  994. /*
  995. * returns the index in s of the first character after the
  996. * first unescaped comma.
  997. * If ther is no such character, returns strlen(s);
  998. */
  999. int
  1000. acl_find_comp_end( char * s) {
  1001. int i = 0;
  1002. int s_len = 0;
  1003. s_len = strlen(s);
  1004. if ( s_len == 0 || s_len == 1) {
  1005. return(s_len);
  1006. }
  1007. /* inv: i+1<s_len && (s[i] == '\\' || s[i+1] != ',')*/
  1008. i = 0;
  1009. while( i+1 < s_len && (s[i] == '\\' ||
  1010. s[i+1] != ',')) {
  1011. i++;
  1012. }
  1013. if ( i + 1 == s_len) {
  1014. return(s_len);
  1015. } else {
  1016. return(i+2);
  1017. }
  1018. }
  1019. /*
  1020. * return the index in s where substr occurs, if none
  1021. * returns -1.
  1022. */
  1023. int
  1024. acl_strstr(char * s, char *substr) {
  1025. char *t = NULL;
  1026. char *tmp_str = NULL;
  1027. tmp_str = slapi_ch_strdup(s);
  1028. if ( (t = strstr(tmp_str, substr)) == NULL ) {
  1029. slapi_ch_free((void **)&tmp_str);
  1030. return(-1);
  1031. } else {
  1032. int l = 0;
  1033. *t = '\0';
  1034. l = strlen(tmp_str);
  1035. slapi_ch_free((void **)&tmp_str);
  1036. return(l);
  1037. }
  1038. }
  1039. /*
  1040. * replace all occurences of substr in s with replace_str.
  1041. *
  1042. * returns a malloced version of the patched string.
  1043. */
  1044. char *
  1045. acl_replace_str(char * s, char *substr, char* replace_with_str) {
  1046. char *str = NULL;
  1047. char *working_s, *suffix, *prefix, *patched;
  1048. int replace_with_len, substr_len, prefix_len, suffix_len;
  1049. if ( (str = strstr(s, substr)) == NULL) {
  1050. return(slapi_ch_strdup(s));
  1051. } else {
  1052. replace_with_len = strlen(replace_with_str);
  1053. substr_len = strlen(substr);
  1054. working_s = slapi_ch_strdup(s);
  1055. prefix = working_s;
  1056. str = strstr(prefix, substr);
  1057. while (str != NULL) {
  1058. /*
  1059. * working_s is a copy of the string to be patched
  1060. * str points to a substr to be patched
  1061. * prefix points to working_s
  1062. */
  1063. *str = '\0';
  1064. suffix = &str[substr_len];
  1065. prefix_len = strlen(prefix);
  1066. suffix_len = strlen(suffix);
  1067. patched = (char *)slapi_ch_malloc(prefix_len +
  1068. replace_with_len +
  1069. suffix_len +1 );
  1070. strcpy(patched, prefix);
  1071. strcat(patched, replace_with_str);
  1072. strcat(patched, suffix);
  1073. slapi_ch_free((void **)&working_s);
  1074. working_s = patched;
  1075. prefix = working_s;
  1076. str = strstr(prefix, substr);
  1077. }
  1078. return(working_s);
  1079. }
  1080. }
  1081. /*
  1082. * Start at index and return a malloced string that is the
  1083. * next component in dn (eg. "ou=People"),
  1084. * or NULL if couldn't find the next one.
  1085. */
  1086. char *
  1087. get_next_component(char *dn, int *index) {
  1088. int dn_len = strlen(dn);
  1089. int start_next = -1;
  1090. int i = 0;
  1091. char *ret_comp;
  1092. if (*index>= dn_len) {
  1093. return(NULL);
  1094. }
  1095. start_next = acl_find_comp_end( &dn[*index]);
  1096. if ( start_next >= dn_len ) {
  1097. *index = start_next;
  1098. return(NULL); /* no next comp */
  1099. }
  1100. /*
  1101. *Here, start_next should be the start of the next
  1102. * component--so far have not run off the end.
  1103. */
  1104. i = acl_find_comp_end( &dn[start_next]);
  1105. /*
  1106. * Here, the matched string is all from start_next to i.
  1107. */
  1108. ret_comp = (char *)slapi_ch_malloc(i - start_next +1);
  1109. memcpy( ret_comp, &dn[start_next], i-start_next);
  1110. ret_comp[i-start_next] = '\0';
  1111. return(ret_comp);
  1112. }
  1113. char *
  1114. get_this_component(char *dn, int *index) {
  1115. int dn_len = strlen(dn);
  1116. int i = 0;
  1117. char *ret_comp;
  1118. if (*index>= dn_len) {
  1119. return(NULL);
  1120. }
  1121. if (dn_len == *index + 1) {
  1122. /* Just return a copy of the string. */
  1123. return(slapi_ch_strdup(dn));
  1124. }else {
  1125. /* *index + 1 < dn_len */
  1126. i = *index+1;
  1127. while( (dn[i] != '\0') && dn[i] != ',' && dn[i-1] != '\\') {
  1128. i += 1;
  1129. }
  1130. /*
  1131. * Here, the matched string is all from *index to i.
  1132. */
  1133. ret_comp = (char *)slapi_ch_malloc(i - *index +1);
  1134. memcpy( ret_comp, &dn[*index], i - *index);
  1135. ret_comp[i-*index] = '\0';
  1136. if (i < dn_len) {
  1137. /* Found a comma before the end */
  1138. *index = i + 1; /* skip it */
  1139. }
  1140. return(ret_comp);
  1141. }
  1142. }
  1143. /* acl hash table funcs */
  1144. /*
  1145. * Add the key adn value to the ht.
  1146. * If it already exists then remove the old one and free
  1147. * the value.
  1148. */
  1149. void acl_ht_add_and_freeOld(acl_ht_t * acl_ht,
  1150. PLHashNumber key,
  1151. char *value){
  1152. char *old_value = NULL;
  1153. uintptr_t pkey = (uintptr_t)key;
  1154. if ( (old_value = (char *)acl_ht_lookup( acl_ht, key)) != NULL ) {
  1155. acl_ht_remove( acl_ht, key);
  1156. slapi_ch_free((void **)&old_value);
  1157. }
  1158. PL_HashTableAdd( acl_ht, (const void *)pkey, value);
  1159. }
  1160. /*
  1161. * Return a new acl_ht_t *
  1162. */
  1163. acl_ht_t *acl_ht_new(void) {
  1164. return(PL_NewHashTable(30, acl_ht_hash, /* key hasher */
  1165. PL_CompareValues, /* keyCompare */
  1166. PL_CompareStrings, 0, 0)); /* value compare */
  1167. }
  1168. static PLHashNumber acl_ht_hash( const void *key) {
  1169. return( (PLHashNumber)((uintptr_t)key) );
  1170. }
  1171. /* Free all the values in the ht */
  1172. void acl_ht_free_all_entries_and_values( acl_ht_t *acl_ht) {
  1173. PL_HashTableEnumerateEntries( acl_ht, acl_ht_free_entry_and_value,
  1174. NULL);
  1175. }
  1176. static PRIntn
  1177. acl_ht_free_entry_and_value(PLHashEntry *he, PRIntn i, void *arg)
  1178. {
  1179. slapi_ch_free((void **)&he->value); /* free value */
  1180. /* Free this entry anfd go on to next one */
  1181. return ( HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
  1182. }
  1183. /* Free all the values in the ht */
  1184. void acl_ht_display_ht( acl_ht_t *acl_ht) {
  1185. #ifdef FOR_DEBUGGING
  1186. PL_HashTableEnumerateEntries( acl_ht, acl_ht_display_entry, NULL);
  1187. #endif
  1188. }
  1189. #ifdef FOR_DEBUGGING
  1190. static PRIntn
  1191. acl_ht_display_entry(PLHashEntry *he, PRIntn i, void *arg)
  1192. {
  1193. PLHashNumber aci_index = (PLHashNumber)he->key;
  1194. char *matched_val = (char *)he->value;
  1195. LDAPDebug(LDAP_DEBUG_ACL,"macro ht entry: key='%d' matched_val='%s'"
  1196. "keyhash='%d'\n",
  1197. aci_index, (matched_val ? matched_val: "NULL"),
  1198. (PLHashNumber)he->keyHash);
  1199. return HT_ENUMERATE_NEXT;
  1200. }
  1201. #endif
  1202. /* remove this entry from the ht--doesn't free the value.*/
  1203. void acl_ht_remove( acl_ht_t *acl_ht, PLHashNumber key) {
  1204. PL_HashTableRemove( acl_ht, (const void *)((uintptr_t)key) );
  1205. }
  1206. /* Retrieve a pointer to the value of the entry with key */
  1207. void *acl_ht_lookup( acl_ht_t *acl_ht,
  1208. PLHashNumber key) {
  1209. return( PL_HashTableLookup( acl_ht, (const void *)((uintptr_t)key)) );
  1210. }
  1211. /***************************************************************************/
  1212. /* E N D */
  1213. /***************************************************************************/