aclutil.c 37 KB

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