aclutil.c 37 KB

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