aclutil.c 37 KB

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