aclutil.c 38 KB

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