aclutil.c 36 KB

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