aclutil.c 37 KB

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