aclutil.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. }
  351. }
  352. static void
  353. aclutil__Ruletypestr (int type , char str[])
  354. {
  355. char *p;
  356. str[0] = '\0';
  357. p = str;
  358. if ( type & ACI_USERDN_RULE) {
  359. strcpy (p, "userdn ");
  360. p = strchr (p, '\0');
  361. }
  362. if ( type & ACI_USERDNATTR_RULE) {
  363. strcpy (p, "userdnattr ");
  364. p = strchr (p, '\0');
  365. }
  366. if ( type & ACI_USERATTR_RULE) {
  367. strcpy (p, "userattr ");
  368. p = strchr (p, '\0');
  369. }
  370. if ( type & ACI_GROUPDN_RULE) {
  371. strcpy (p, "groupdn ");
  372. p = strchr (p, '\0');
  373. }
  374. if ( type & ACI_GROUPDNATTR_RULE) {
  375. strcpy (p, "groupdnattr ");
  376. p = strchr (p, '\0');
  377. }
  378. if ( type & ACI_ROLEDN_RULE) {
  379. strcpy (p, "roledn ");
  380. p = strchr (p, '\0');
  381. }
  382. if ( type & ACI_IP_RULE) {
  383. strcpy (p, "ip ");
  384. p = strchr (p, '\0');
  385. }
  386. if ( type & ACI_DNS_RULE) {
  387. strcpy (p, "dns ");
  388. p = strchr (p, '\0');
  389. }
  390. if ( type & ACI_TIMEOFDAY_RULE) {
  391. strcpy (p, "timeofday ");
  392. p = strchr (p, '\0');
  393. }
  394. if ( type & ACI_DAYOFWEEK_RULE) {
  395. strcpy (p, "dayofweek ");
  396. p = strchr (p, '\0');
  397. }
  398. if ( type & ACI_AUTHMETHOD_RULE) {
  399. strcpy (p, "authmethod ");
  400. p = strchr (p, '\0');
  401. }
  402. if ( type & ACI_PARAM_DNRULE) {
  403. strcpy (p, "paramdn ");
  404. p = strchr (p, '\0');
  405. }
  406. if ( type & ACI_PARAM_ATTRRULE) {
  407. strcpy (p, "paramAttr ");
  408. p = strchr (p, '\0');
  409. }
  410. if ( type & ACI_SSF_RULE) {
  411. strcpy (p, "ssf ");
  412. }
  413. }
  414. /*
  415. ** acl_gen_err_msg
  416. ** This function is called by backend to generate the error message
  417. ** if access is denied.
  418. */
  419. void
  420. acl_gen_err_msg(int access, char *edn, char *attr, char **errbuf)
  421. {
  422. char *line = NULL;
  423. if (access & SLAPI_ACL_WRITE) {
  424. line = PR_smprintf(
  425. "Insufficient 'write' privilege to the '%s' attribute of entry '%s'.\n",
  426. attr ? attr: "NULL", edn);
  427. } else if ( access & SLAPI_ACL_ADD ) {
  428. line = PR_smprintf(
  429. "Insufficient 'add' privilege to add the entry '%s'.\n",edn);
  430. } else if ( access & SLAPI_ACL_DELETE ) {
  431. line = PR_smprintf(
  432. "Insufficient 'delete' privilege to delete the entry '%s'.\n",edn);
  433. }
  434. aclutil_str_append(errbuf, line );
  435. if (line) {
  436. PR_smprintf_free(line);
  437. line = NULL;
  438. }
  439. }
  440. short
  441. aclutil_gen_signature ( short c_signature )
  442. {
  443. short o_signature = 0;
  444. short randval = (short)slapi_rand();
  445. o_signature = c_signature ^ (randval % 32768);
  446. if (!o_signature) {
  447. randval = (short)slapi_rand();
  448. o_signature = c_signature ^ (randval % 32768);
  449. }
  450. return o_signature;
  451. }
  452. void
  453. aclutil_print_resource( struct acl_pblock *aclpb, char *right , char *attr, char *clientdn )
  454. {
  455. char str[BUFSIZ];
  456. const char *dn;
  457. if ( aclpb == NULL) return;
  458. if ( ! slapi_is_loglevel_set ( SLAPI_LOG_ACL ) )
  459. return;
  460. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " ************ RESOURCE INFO STARTS *********\n");
  461. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " Client DN: %s\n",
  462. clientdn ? escape_string_with_punctuation (clientdn, str) : "NULL");
  463. aclutil__access_str (aclpb->aclpb_access, str);
  464. aclutil__typestr (aclpb->aclpb_res_type, &str[strlen(str)]);
  465. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " resource type:%d(%s)\n",
  466. aclpb->aclpb_res_type, str);
  467. dn = slapi_sdn_get_dn ( aclpb->aclpb_curr_entry_sdn );
  468. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " Slapi_Entry DN: %s\n",
  469. dn ? escape_string_with_punctuation ( dn , str) : "NULL");
  470. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " ATTR: %s\n", attr ? attr : "NULL");
  471. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " rights:%s\n", right ? right: "NULL");
  472. slapi_log_error (SLAPI_LOG_ACL, plugin_name, " ************ RESOURCE INFO ENDS *********\n");
  473. }
  474. /*
  475. * The input string contains a rule like
  476. * "cn=helpdesk, ou=$attr.deptName, o=$dn.o, o=ISP"
  477. *
  478. * Where $attr -- means look into the attribute list for values
  479. * $dn -- means look into the entry's dn
  480. *
  481. * We extract the values from the entry and returned a string
  482. * with the values added.
  483. * For "$attr" rule - if we find multiple values then it is
  484. * the pattern is not expanded.
  485. * For "$dn" rule, if we find multiple of them, we use the relative
  486. * position.
  487. * NOTE: The caller is responsible in freeing the memory.
  488. */
  489. char *
  490. aclutil_expand_paramString ( char *str, Slapi_Entry *e )
  491. {
  492. char **e_dns;
  493. char **a_dns;
  494. char *attrName;
  495. char *s, *p;
  496. char *attrVal;
  497. int i, len;
  498. int ncomponents, type;
  499. int rc = -1;
  500. char *buf = NULL;
  501. e_dns = ldap_explode_dn ( slapi_entry_get_ndn ( e ), 0 );
  502. a_dns = ldap_explode_dn ( str, 0 );
  503. i = 0;
  504. ncomponents = 0;
  505. while ( a_dns[ncomponents] )
  506. ncomponents++;
  507. for (i=0; i < ncomponents; i++ ) {
  508. /* Look for"$" char */
  509. if ( (s = strchr ( a_dns[i], '$') ) != NULL) {
  510. p = s;
  511. s++;
  512. if ( strncasecmp (s, "dn", 2) == 0 )
  513. type = 1;
  514. else if ( strncasecmp (s, "attr", 4) == 0 )
  515. type = 2;
  516. else {
  517. /* error */
  518. goto cleanup;
  519. }
  520. *p = '\0';
  521. aclutil_str_append ( &buf,a_dns[i]);
  522. if ( type == 1 ) {
  523. /* xyz = $dn.o */
  524. s +=3;
  525. attrName = s;
  526. attrVal = __aclutil_extract_dn_component (e_dns,
  527. ncomponents-i, attrName);
  528. if ( NULL == attrVal ) /*error*/
  529. goto cleanup;
  530. } else {
  531. Slapi_Attr *attr;
  532. const struct berval *attrValue;
  533. int kk;
  534. Slapi_Value *sval, *t_sval;
  535. /* The pattern is x=$attr.o" */
  536. s +=5;
  537. attrName = s;
  538. slapi_entry_attr_find ( e, attrName, &attr );
  539. if ( NULL == attr )
  540. goto cleanup;
  541. kk= slapi_attr_first_value ( attr, &sval );
  542. if ( kk != -1 ) {
  543. t_sval = sval;
  544. kk= slapi_attr_next_value( attr, kk, &sval );
  545. if ( kk != -1 ) /* can't handle multiple --error */
  546. goto cleanup;
  547. attrValue = slapi_value_get_berval ( t_sval );
  548. attrVal = attrValue->bv_val;
  549. }
  550. }
  551. } else {
  552. attrVal = a_dns[i];
  553. }
  554. aclutil_str_append ( &buf, attrVal);
  555. aclutil_str_append ( &buf, ",");
  556. }
  557. rc = 0; /* everything is okay*/
  558. /* remove the last comma */
  559. if (buf) {
  560. len = strlen ( buf);
  561. buf[len-1] = '\0';
  562. }
  563. cleanup:
  564. slapi_ldap_value_free ( a_dns );
  565. slapi_ldap_value_free ( e_dns );
  566. if ( 0 != rc ) /* error */ {
  567. slapi_ch_free ( (void **) &buf );
  568. buf = NULL;
  569. }
  570. return buf;
  571. }
  572. static char *
  573. __aclutil_extract_dn_component ( char **e_dns, int position, char *attrName )
  574. {
  575. int i, matched, len;
  576. char *s;
  577. int matchedPosition = 0;
  578. len = strlen ( attrName );
  579. /* First check if there thare are multiple of these */
  580. i = matched = 0;
  581. while ( e_dns[i] ) {
  582. if (0 == strncasecmp (e_dns[i], attrName, len) ) {
  583. matched++;
  584. matchedPosition = i;
  585. }
  586. i++;
  587. }
  588. if (!matched )
  589. return NULL;
  590. if ( matched > 1 ) {
  591. matchedPosition = i - position;
  592. }
  593. if ( NULL == e_dns[matchedPosition])
  594. return NULL;
  595. s = strstr ( e_dns[matchedPosition], "=");
  596. if ( NULL == s)
  597. return NULL;
  598. else
  599. return s+1;
  600. }
  601. /*
  602. * Does the first component of ndn match the first component of match_this ?
  603. */
  604. int
  605. acl_dn_component_match( const char *ndn, char *match_this, int component_number) {
  606. return(1);
  607. }
  608. /*
  609. * Here, ndn is a resource dn and match_this is a dn, containing a macro, ($dn).
  610. *
  611. * eg. ndn is cn=fred,ou=groups,ou=people,ou=icnc,o=ISP and
  612. * match_this is "ou=Groups,($dn),o=ISP" or
  613. * "cn=*,ou=Groups,($dn),o=ISP".
  614. *
  615. * They match if:
  616. * match_this is a suffix of ndn
  617. *
  618. * It returns NULL, if they do not match.
  619. * Otherwise it returns a copy of the substring of ndn that matches the ($dn).
  620. *
  621. * eg. in the above example, "ou=people,ou=icnc"
  622. */
  623. char *
  624. acl_match_macro_in_target( const char *ndn, char * match_this,
  625. char *macro_ptr) {
  626. char *macro_prefix = NULL;
  627. int macro_prefix_len = 0;
  628. char *macro_suffix = NULL;
  629. char *tmp_ptr = NULL;
  630. char *matched_val = NULL;
  631. char *ret_val = NULL;
  632. int ndn_len = 0;
  633. int macro_suffix_len = 0;
  634. int ndn_prefix_len = 0;
  635. int ndn_prefix_end = 0;
  636. int matched_val_len = 0;
  637. /*
  638. * First, grab the macro_suffix--the bit after the ($dn)
  639. *
  640. */
  641. if (strlen(macro_ptr) == strlen(ACL_TARGET_MACRO_DN_KEY)) {
  642. macro_suffix = NULL; /* just ($dn) */
  643. } else {
  644. if ( macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY)] == ',') {
  645. macro_suffix = &macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY) + 1];
  646. } else {
  647. macro_suffix = &macro_ptr[strlen(ACL_TARGET_MACRO_DN_KEY)];
  648. }
  649. }
  650. /*
  651. * First ensure that the suffix of match_this is
  652. * a suffix of ndn.
  653. */
  654. ndn_len = strlen(ndn);
  655. if ( macro_suffix != NULL) {
  656. macro_suffix_len = strlen(macro_suffix);
  657. if( macro_suffix_len >= ndn_len ) {
  658. /*
  659. * eg ndn: o=icnc,o=sun.com
  660. * match_this: ($dn),o=icnc,o=sun.com
  661. */
  662. return(NULL); /* ($dn) must match something. */
  663. } else {
  664. /*
  665. * eg ndn: ou=People,o=icnc,o=sun.com
  666. * match_this: ($dn),o=icnc,o=sun.com
  667. *
  668. * we can do a direct strncmp() because we know that
  669. * there can be no "*" after the ($dn)...by definition.
  670. */
  671. if (strncasecmp( macro_suffix, &ndn[ndn_len-macro_suffix_len],
  672. macro_suffix_len) != 0) {
  673. return(NULL); /* suffix must match */
  674. }
  675. }
  676. }
  677. /* Here, macro_suffix is a suffix of ndn.
  678. *
  679. *
  680. * Now, look at macro_prefix, if it is NULL, then ($dn) matches
  681. * ndn[0..ndn_len-macro_suffix_len].
  682. * (eg, ndn: cn=fred,ou=People,o=sun.com
  683. * match_this: ($dn),o=sun.com.
  684. *
  685. */
  686. macro_prefix = slapi_ch_strdup(match_this);
  687. /* we know it's got a $(dn) */
  688. tmp_ptr = strstr(macro_prefix, ACL_TARGET_MACRO_DN_KEY);
  689. *tmp_ptr = '\0';
  690. /* There may be a NULL prefix eg. match_this: ($dn),o=sun.com */
  691. macro_prefix_len = strlen(macro_prefix);
  692. if (macro_prefix_len == 0) {
  693. slapi_ch_free((void **) &macro_prefix);
  694. macro_prefix = NULL;
  695. }
  696. if (macro_prefix == NULL ) {
  697. /*
  698. * ($dn) matches ndn[0..ndn_len-macro_suffix_len]
  699. */
  700. int matched_val_len = 0;
  701. matched_val_len = ndn_len-macro_suffix_len;
  702. matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
  703. strncpy(matched_val, ndn, ndn_len-macro_suffix_len);
  704. /*
  705. * Null terminate matched_val, removing trailing "," if there is
  706. * one.
  707. */
  708. if (matched_val_len > 1) {
  709. if (matched_val[matched_val_len-1] == ',' ) {
  710. matched_val[matched_val_len-1] = '\0';
  711. } else {
  712. matched_val[matched_val_len] = '\0';
  713. }
  714. }
  715. ret_val = matched_val;
  716. } else {
  717. /*
  718. * If it is not NULL, then if macro_prefix contains a * then
  719. * it needs to be an exact prefix of ndn (modulo the * component
  720. * which matches anything) becuase that's the semantics
  721. * of target patterns containing *'s, except that we just
  722. * make it match one component.
  723. * If it is such a prefix then ($dn) matches that portion of ndn
  724. * from the end of the prefix, &ndn[ndn_prefix_end] to
  725. * ndn_suffix_start.
  726. * If ndn_prefix_len > ndn_len-macro_suffix_len then return(NULL),
  727. * otherwise $(dn) matches ndn[ndn_prefix_len..ndn_len-macro_suffix_len].
  728. *
  729. *
  730. * eg. ndn: cn=fred,ou=P,o=sun.com
  731. * match_this: cn=*,($dn),o=sun.com
  732. */
  733. if ( strstr(macro_prefix, "=*") != NULL ) {
  734. int exact_match = 0;
  735. ndn_prefix_len = acl_match_prefix( macro_prefix, ndn, &exact_match);
  736. if ( ndn_prefix_len != -1 ) {
  737. /*
  738. * ndn[0..ndn_prefix_len] is the prefix in ndn.
  739. * ndn[ndn_prefix_len..ndn_len-macro_suffix_len] is the
  740. * matched string.
  741. */
  742. if (ndn_prefix_len >= ndn_len-macro_suffix_len) {
  743. /*
  744. * eg ndn: cn=fred,ou=People,o=icnc,o=sun.com
  745. * cn=*,ou=People,o=icnc,($dn),o=icnc,o=sun.com
  746. */
  747. ret_val = NULL; /* matched string is empty */
  748. } else {
  749. /*
  750. * eg ndn: cn=fred,ou=People,o=icnc,o=sun.com
  751. * cn=*,ou=People,($dn),o=sun.com
  752. */
  753. matched_val_len = ndn_len-macro_suffix_len-ndn_prefix_len;
  754. matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
  755. strncpy(matched_val, &ndn[ndn_prefix_len], matched_val_len);
  756. if (matched_val_len > 1) {
  757. if (matched_val[matched_val_len-1] == ',' ) {
  758. matched_val[matched_val_len-1] = '\0';
  759. } else {
  760. matched_val[matched_val_len] = '\0';
  761. }
  762. }
  763. matched_val[matched_val_len] = '\0';
  764. ret_val = matched_val;
  765. }
  766. } else {
  767. /* Was not a prefix so not a match */
  768. ret_val = NULL;
  769. }
  770. } else {
  771. /*
  772. *
  773. * If macro_prefix is not NULL and it does not
  774. * contain a =* then
  775. * we need to ensure that macro_prefix is a substring
  776. * ndn.
  777. * If it is and the position of the character after it's end in
  778. * ndn is
  779. * ndn_prefix_end then ($dn) matches
  780. * ndn[ndn_prefix_end..ndn_len-macro_suffix_len].
  781. *
  782. *
  783. * One important principal is that ($dn) matches a maximal
  784. * chunk--this way it will serve to make the link
  785. * between resources and users at each level of the structure.
  786. *
  787. * eg. ndn: ou=Groups,ou=Groups,ou=Groups,c=fr
  788. * macro_prefix: ou=Groups,($dn),c=fr
  789. *
  790. * then ($dn) matches ou=Groups,ou=Groups.
  791. *
  792. *
  793. *
  794. * If it is not a substring, then there is no match.
  795. * If it is a substring and
  796. * ndn[ndn_prefix_end..ndn_len-macro_suffix_len] is empty then
  797. * it's also not a match as we demand that ($dn) match a non-empty
  798. * string.
  799. *
  800. *
  801. *
  802. * (eg. ndn: cn=fred,o=icnc,ou=People,o=sun.com
  803. * match_this: o=icnc,($dn),o=sun.com.)
  804. *
  805. *
  806. * (eg. ndn: cn=fred,o=menlo park,ou=People,o=icnc,o=sun.com
  807. * match_this: o=menlo park,ou=People,($dn),o=sun.com
  808. *
  809. */
  810. ndn_prefix_end = acl_strstr((char *)ndn, macro_prefix);
  811. if ( ndn_prefix_end == -1) {
  812. ret_val = NULL;
  813. } else {
  814. /* Is a substring */
  815. ndn_prefix_end += macro_prefix_len;
  816. /*
  817. * make sure the matching part is non-empty:
  818. *
  819. * ndn[ndn_prefix_end..mndn_len-macro_suffix_len].
  820. */
  821. if ( ndn_prefix_end >= ndn_len-macro_suffix_len) {
  822. ret_val = NULL;
  823. } else {
  824. /*
  825. * ($dn) matches the non-empty string segment
  826. * ndn[ndn_prefix_end..mndn_len-macro_suffix_len]
  827. * the -1 is because macro_suffix_eln does not include
  828. * the coma before the suffix.
  829. */
  830. matched_val_len = ndn_len-macro_suffix_len-
  831. ndn_prefix_end - 1;
  832. matched_val = (char *)slapi_ch_malloc(matched_val_len + 1);
  833. strncpy(matched_val, &ndn[ndn_prefix_end],
  834. matched_val_len);
  835. matched_val[matched_val_len] = '\0';
  836. ret_val = matched_val;
  837. }
  838. }
  839. }/* contains an =* */
  840. slapi_ch_free((void **) &macro_prefix);
  841. }/* macro_prefix != NULL */
  842. return(ret_val);
  843. }
  844. /*
  845. * Checks to see if macro_prefix is an exact prefix of ndn.
  846. * macro_prefix may contain a * component.
  847. *
  848. * The length of the matched prefix in ndn is returned.
  849. * If it was not a match, a negative int is returned.
  850. * Also, if the string matched exactly,
  851. * exact_match is set to 1, other wise it was a proper prefix.
  852. *
  853. */
  854. int
  855. acl_match_prefix( char *macro_prefix, const char *ndn, int *exact_match) {
  856. int ret_code = -1;
  857. int macro_prefix_len = 0;
  858. int ndn_len = 0;
  859. int i = 0;
  860. int j = 0;
  861. int done = 0;
  862. int t = 0;
  863. char * tmp_str = NULL;
  864. int k,l = 0;
  865. *exact_match = 0; /* default to not an exact match */
  866. /* The NULL prefix matches everthing*/
  867. if (macro_prefix == NULL) {
  868. if ( ndn == NULL ) {
  869. *exact_match = 1;
  870. }
  871. return(0);
  872. } else {
  873. /* macro_prefix is not null, so if ndn is NULL, it's not a match. */
  874. if ( ndn == NULL) {
  875. return(-1);
  876. }
  877. }
  878. /*
  879. * Here, neither macro_prefix nor ndn are NULL.
  880. *
  881. * eg. macro_prefix: cn=*,ou=people,o=sun.com
  882. * ndn : cn=fred,ou=people,o=sun.com
  883. */
  884. /*
  885. * Here, there is a component with a * (eg. cn=* ) so
  886. * we need to step through the macro_prefix, and where there is
  887. * such a * match on that component,
  888. * when we run out of * componenets, jsut do a straight match.
  889. *
  890. * Out of interest, the following extended regular expression
  891. * will match just one ou rdn value from a string:
  892. * "^uid=admin,ou=\([^,]*\\\,\)*[^,]*,o=sun.com$"
  893. *
  894. *
  895. * eg. cn=fred,ou=People,o=sun.com
  896. *
  897. *
  898. * s points to the = of the component.
  899. */
  900. macro_prefix_len = strlen(macro_prefix);
  901. ndn_len = strlen(ndn);
  902. i = 0;
  903. j = 0;
  904. done = 0;
  905. while ( !done ) {
  906. /* Here ndn[0..i] has matched macro_prefix[0..j] && j<= i
  907. * i<=ndn_len j<=macro_prefix_len */
  908. if ( (t = acl_strstr(&macro_prefix[j], "=*")) < 0 ) {
  909. /*
  910. * No more *'s, do a straight match on
  911. * macro_prefix[j..macro_prefix_len] and
  912. * ndn[i..macro_prefix_len]
  913. */
  914. if( macro_prefix_len-j > ndn_len-i) {
  915. /* Not a prefix, nor a match */
  916. *exact_match = 0;
  917. ret_code = -1;
  918. done = 1;
  919. } else {
  920. /*
  921. * ndn_len-i >= macro_prefix_len - j
  922. * if macro_prefix_len-j is 0, then
  923. * it's a null prefix, so it matches.
  924. * If in addition ndn_len-i is 0 then it's
  925. * an exact match.
  926. * Otherwise, do the cmp.
  927. */
  928. if ( macro_prefix_len-j == 0) {
  929. done = 1;
  930. ret_code = i;
  931. if ( ndn_len-i == 0) {
  932. *exact_match = 1;
  933. }
  934. }else {
  935. if (strncasecmp(&macro_prefix[j], &ndn[i],
  936. macro_prefix_len-j) == 0) {
  937. *exact_match = (macro_prefix_len-j == ndn_len-i);
  938. ret_code = i + macro_prefix_len -j;
  939. done = 1;
  940. } else {
  941. /* not a prefix not a match */
  942. *exact_match = 0;
  943. ret_code = -1;
  944. done = 1;
  945. }
  946. }
  947. }
  948. }else {
  949. /*
  950. * Is another * component, so:
  951. * 1. match that component in macro_prefix (at pos k say)
  952. * with the corresponding compoent (at pos l say ) in ndn
  953. *
  954. * 2. match the intervening string ndn[i..l] and
  955. * macro_prefix[j..k].
  956. */
  957. /* First, find the start of the component in macro_prefix. */
  958. t++; /* move to the--this way we will look for "ou=" in ndn */
  959. k = acl_find_comp_start(macro_prefix, t);
  960. /* Now identify that component in ndn--if it's not there no match */
  961. tmp_str = slapi_ch_malloc(t-k+1);
  962. strncpy(tmp_str, &macro_prefix[k], t-k);
  963. tmp_str[t-k] = '\0';
  964. l = acl_strstr((char*)&ndn[i], tmp_str);
  965. if (l == -1) {
  966. *exact_match = 0;
  967. ret_code = -1;
  968. done = 1;
  969. } else {
  970. /*
  971. * Found the comp in ndn, so the comp matches.
  972. * Now test the intervening string segments:
  973. * ndn[i..l] and macro_prefix[j..k]
  974. */
  975. if ( k-j != l-i ) {
  976. *exact_match = 0;
  977. ret_code = -1;
  978. done = 1;
  979. } else{
  980. if (strncasecmp(&macro_prefix[j], &ndn[i], k-j) != 0) {
  981. *exact_match = 0;
  982. ret_code = -1;
  983. done = 1;
  984. } else {
  985. /* Matched, so bump i and j and keep going.*/
  986. i += acl_find_comp_end((char*)&ndn[l]);
  987. j += acl_find_comp_end((char*)&macro_prefix[k]);
  988. }
  989. }
  990. }
  991. slapi_ch_free((void **)&tmp_str);
  992. }
  993. }/* while */
  994. return(ret_code);
  995. }
  996. /*
  997. * returns the index in s of where the component at position
  998. * s[pos] starts.
  999. * This is the index of the character after the first unescaped comma
  1000. * moving backwards in s from pos.
  1001. * If this is not found then return 0, ie. the start of the string.
  1002. * If the index returned is > strlen(s) then could not find it.
  1003. * only such case is if you pass ",", in which case there is no component start.
  1004. */
  1005. static int
  1006. acl_find_comp_start(char * s, int pos ) {
  1007. int i =0;
  1008. int comp_start = 0;
  1009. i = pos;
  1010. while( i > 0 && (s[i] != ',' ||
  1011. s[i-1] == '\\')) {
  1012. i--;
  1013. }
  1014. /*
  1015. * i == 0 || (s[i] == ',' && s[i-1] != '\\')
  1016. */
  1017. if (i==0) {
  1018. /* Got all the way with no unescaped comma */
  1019. if (s[i] == ',') {
  1020. comp_start = i+1;
  1021. } else {
  1022. comp_start = i;
  1023. }
  1024. } else { /* Found an unescaped comma */
  1025. comp_start = i + 1;
  1026. }
  1027. return( comp_start);
  1028. }
  1029. /*
  1030. * returns the index in s of the first character after the
  1031. * first unescaped comma.
  1032. * If ther is no such character, returns strlen(s);
  1033. */
  1034. int
  1035. acl_find_comp_end( char * s) {
  1036. int i = 0;
  1037. int s_len = 0;
  1038. s_len = strlen(s);
  1039. if ( s_len == 0 || s_len == 1) {
  1040. return(s_len);
  1041. }
  1042. /* inv: i+1<s_len && (s[i] == '\\' || s[i+1] != ',')*/
  1043. i = 0;
  1044. while( i+1 < s_len && (s[i] == '\\' ||
  1045. s[i+1] != ',')) {
  1046. i++;
  1047. }
  1048. if ( i + 1 == s_len) {
  1049. return(s_len);
  1050. } else {
  1051. return(i+2);
  1052. }
  1053. }
  1054. /*
  1055. * return the index in s where substr occurs, if none
  1056. * returns -1.
  1057. */
  1058. int
  1059. acl_strstr(char * s, char *substr) {
  1060. char *t = NULL;
  1061. char *tmp_str = NULL;
  1062. tmp_str = slapi_ch_strdup(s);
  1063. if ( (t = strstr(tmp_str, substr)) == NULL ) {
  1064. slapi_ch_free((void **)&tmp_str);
  1065. return(-1);
  1066. } else {
  1067. int l = 0;
  1068. *t = '\0';
  1069. l = strlen(tmp_str);
  1070. slapi_ch_free((void **)&tmp_str);
  1071. return(l);
  1072. }
  1073. }
  1074. /*
  1075. * replace all occurences of substr in s with replace_str.
  1076. *
  1077. * returns a malloced version of the patched string.
  1078. */
  1079. char *
  1080. acl_replace_str(char * s, char *substr, char* replace_with_str) {
  1081. char *str = NULL;
  1082. char *working_s, *suffix, *prefix, *patched;
  1083. int replace_with_len, substr_len, prefix_len, suffix_len;
  1084. if (strstr(s, substr) == NULL) {
  1085. return(slapi_ch_strdup(s));
  1086. } else {
  1087. replace_with_len = strlen(replace_with_str);
  1088. substr_len = strlen(substr);
  1089. working_s = slapi_ch_strdup(s);
  1090. prefix = working_s;
  1091. str = strstr(prefix, substr);
  1092. while (str != NULL) {
  1093. /*
  1094. * working_s is a copy of the string to be patched
  1095. * str points to a substr to be patched
  1096. * prefix points to working_s
  1097. */
  1098. *str = '\0';
  1099. suffix = &str[substr_len];
  1100. prefix_len = strlen(prefix);
  1101. suffix_len = strlen(suffix);
  1102. patched = (char *)slapi_ch_malloc(prefix_len +
  1103. replace_with_len +
  1104. suffix_len +1 );
  1105. strcpy(patched, prefix);
  1106. strcat(patched, replace_with_str);
  1107. strcat(patched, suffix);
  1108. slapi_ch_free((void **)&working_s);
  1109. working_s = patched;
  1110. prefix = working_s;
  1111. str = strstr(prefix, substr);
  1112. }
  1113. return(working_s);
  1114. }
  1115. }
  1116. /*
  1117. * Start at index and return a malloced string that is the
  1118. * next component in dn (eg. "ou=People"),
  1119. * or NULL if couldn't find the next one.
  1120. */
  1121. char *
  1122. get_next_component(char *dn, int *index) {
  1123. int dn_len = strlen(dn);
  1124. int start_next = -1;
  1125. int i = 0;
  1126. char *ret_comp;
  1127. if (*index>= dn_len) {
  1128. return(NULL);
  1129. }
  1130. start_next = acl_find_comp_end( &dn[*index]);
  1131. if ( start_next >= dn_len ) {
  1132. *index = start_next;
  1133. return(NULL); /* no next comp */
  1134. }
  1135. /*
  1136. *Here, start_next should be the start of the next
  1137. * component--so far have not run off the end.
  1138. */
  1139. i = acl_find_comp_end( &dn[start_next]);
  1140. /*
  1141. * Here, the matched string is all from start_next to i.
  1142. */
  1143. ret_comp = (char *)slapi_ch_malloc(i - start_next +1);
  1144. memcpy( ret_comp, &dn[start_next], i-start_next);
  1145. ret_comp[i-start_next] = '\0';
  1146. return(ret_comp);
  1147. }
  1148. char *
  1149. get_this_component(char *dn, int *index) {
  1150. int dn_len = strlen(dn);
  1151. int i = 0;
  1152. char *ret_comp;
  1153. if (*index>= dn_len) {
  1154. return(NULL);
  1155. }
  1156. if (dn_len == *index + 1) {
  1157. /* Just return a copy of the string. */
  1158. return(slapi_ch_strdup(dn));
  1159. }else {
  1160. /* *index + 1 < dn_len */
  1161. i = *index+1;
  1162. while( (dn[i] != '\0') && dn[i] != ',' && dn[i-1] != '\\') {
  1163. i += 1;
  1164. }
  1165. /*
  1166. * Here, the matched string is all from *index to i.
  1167. */
  1168. ret_comp = (char *)slapi_ch_malloc(i - *index +1);
  1169. memcpy( ret_comp, &dn[*index], i - *index);
  1170. ret_comp[i-*index] = '\0';
  1171. if (i < dn_len) {
  1172. /* Found a comma before the end */
  1173. *index = i + 1; /* skip it */
  1174. }
  1175. return(ret_comp);
  1176. }
  1177. }
  1178. /* acl hash table funcs */
  1179. /*
  1180. * Add the key adn value to the ht.
  1181. * If it already exists then remove the old one and free
  1182. * the value.
  1183. */
  1184. void acl_ht_add_and_freeOld(acl_ht_t * acl_ht,
  1185. PLHashNumber key,
  1186. char *value){
  1187. char *old_value = NULL;
  1188. uintptr_t pkey = (uintptr_t)key;
  1189. if ( (old_value = (char *)acl_ht_lookup( acl_ht, key)) != NULL ) {
  1190. acl_ht_remove( acl_ht, key);
  1191. slapi_ch_free((void **)&old_value);
  1192. }
  1193. PL_HashTableAdd( acl_ht, (const void *)pkey, value);
  1194. }
  1195. /*
  1196. * Return a new acl_ht_t *
  1197. */
  1198. acl_ht_t *acl_ht_new(void) {
  1199. return(PL_NewHashTable(30, acl_ht_hash, /* key hasher */
  1200. PL_CompareValues, /* keyCompare */
  1201. PL_CompareStrings, 0, 0)); /* value compare */
  1202. }
  1203. static PLHashNumber acl_ht_hash( const void *key) {
  1204. return( (PLHashNumber)((uintptr_t)key) );
  1205. }
  1206. /* Free all the values in the ht */
  1207. void acl_ht_free_all_entries_and_values( acl_ht_t *acl_ht) {
  1208. PL_HashTableEnumerateEntries( acl_ht, acl_ht_free_entry_and_value,
  1209. NULL);
  1210. }
  1211. static PRIntn
  1212. acl_ht_free_entry_and_value(PLHashEntry *he, PRIntn i, void *arg)
  1213. {
  1214. slapi_ch_free((void **)&he->value); /* free value */
  1215. /* Free this entry anfd go on to next one */
  1216. return ( HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE);
  1217. }
  1218. /* Free all the values in the ht */
  1219. void acl_ht_display_ht( acl_ht_t *acl_ht) {
  1220. #ifdef FOR_DEBUGGING
  1221. PL_HashTableEnumerateEntries( acl_ht, acl_ht_display_entry, NULL);
  1222. #endif
  1223. }
  1224. #ifdef FOR_DEBUGGING
  1225. static PRIntn
  1226. acl_ht_display_entry(PLHashEntry *he, PRIntn i, void *arg)
  1227. {
  1228. PLHashNumber aci_index = (PLHashNumber)he->key;
  1229. char *matched_val = (char *)he->value;
  1230. LDAPDebug(LDAP_DEBUG_ACL,"macro ht entry: key='%d' matched_val='%s'"
  1231. "keyhash='%d'\n",
  1232. aci_index, (matched_val ? matched_val: "NULL"),
  1233. (PLHashNumber)he->keyHash);
  1234. return HT_ENUMERATE_NEXT;
  1235. }
  1236. #endif
  1237. /* remove this entry from the ht--doesn't free the value.*/
  1238. void acl_ht_remove( acl_ht_t *acl_ht, PLHashNumber key) {
  1239. PL_HashTableRemove( acl_ht, (const void *)((uintptr_t)key) );
  1240. }
  1241. /* Retrieve a pointer to the value of the entry with key */
  1242. void *acl_ht_lookup( acl_ht_t *acl_ht,
  1243. PLHashNumber key) {
  1244. return( PL_HashTableLookup( acl_ht, (const void *)((uintptr_t)key)) );
  1245. }
  1246. /***************************************************************************/
  1247. /* E N D */
  1248. /***************************************************************************/