aclparse.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  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. #include "acl.h"
  39. /****************************************************************************/
  40. /* prototypes */
  41. /****************************************************************************/
  42. static int __aclp__parse_aci(char *str, aci_t *aci_item);
  43. static int __aclp__sanity_check_acltxt(aci_t *aci_item, char *str);
  44. static char * __aclp__normalize_acltxt (aci_t *aci_item, char *str);
  45. static char * __aclp__getNextLASRule(aci_t *aci_item, char *str,
  46. char **endOfCurrRule);
  47. static char * __aclp__dn_normalize( char *dn , char *end);
  48. static int __aclp__get_aci_right ( char *str);
  49. static int __aclp__init_targetattr (aci_t *aci, char *attr_val);
  50. static int __acl__init_targetattrfilters( aci_t *aci_item, char *str);
  51. static int process_filter_list( Targetattrfilter ***attrfilterarray,
  52. char * str);
  53. static int __acl_init_targetattrfilter( Targetattrfilter *attrfilter, char *str );
  54. static void __aclp_chk_paramRules ( aci_t *aci_item, char *start,
  55. char *end);
  56. static void __acl_strip_trailing_space( char *str);
  57. static void __acl_strip_leading_space( char **str);
  58. static char * __acl_trim_filterstr( char * str );
  59. static int acl_verify_exactly_one_attribute( char *attr_name, Slapi_Filter *f);
  60. static int type_compare( Slapi_Filter *f, void *arg);
  61. static int acl_check_for_target_macro( aci_t *aci_item, char *value);
  62. static int get_acl_rights_as_int( char * strValue);
  63. /***************************************************************************
  64. *
  65. * acl_parse
  66. *
  67. * Parses the input string and copies the information into the
  68. * correct place in the aci.
  69. *
  70. *
  71. * Input:
  72. * char *str - Input string which has the ACL
  73. * This is a duped copy, so here we have
  74. * the right to stich '\0' characters into str for
  75. * processing purposes. If you want to keep
  76. * a piece of str, you'll need to dup it
  77. * as it gets freed outside the scope of acl_parse.
  78. * aci_t *item - the aci item where the ACL info will be
  79. * - stored.
  80. *
  81. * Returns:
  82. * 0 -- Parsed okay
  83. * < 0 -- error codes
  84. *
  85. * Error Handling:
  86. * None.
  87. *
  88. **************************************************************************/
  89. int
  90. acl_parse(char * str, aci_t *aci_item)
  91. {
  92. int rv=0;
  93. char *next;
  94. char *save;
  95. while(*str) {
  96. __acl_strip_leading_space( &str );
  97. if (*str == '\0') break;
  98. if (*str == '(') {
  99. if ((next = slapi_find_matching_paren(str)) == NULL) {
  100. return(ACL_SYNTAX_ERR);
  101. }
  102. } else {
  103. /* then we have done all the processing */
  104. return 0;
  105. }
  106. LDAP_UTF8INC(str); /* skip the "(" */
  107. save = next;
  108. LDAP_UTF8INC(next);
  109. *save = '\0';
  110. /* Now we have a "str)" */
  111. if ( 0 != (rv = __aclp__parse_aci(str, aci_item))) {
  112. return(rv);
  113. }
  114. /* Move to the next */
  115. str = next;
  116. }
  117. /* check if have a ACLTXT or not */
  118. if (!(aci_item->aci_type & ACI_ACLTXT))
  119. return ACL_SYNTAX_ERR;
  120. if (aci_item->target) {
  121. Slapi_Filter *f;
  122. /* Make sure that the target is a valid target.
  123. ** Example: ACL is located in
  124. ** "ou=engineering, o=ace industry, c=us
  125. ** but if the target is "o=ace industry, c=us",
  126. ** then it's an ERROR.
  127. */
  128. f = aci_item->target;
  129. if (aci_item->aci_type & ACI_TARGET_DN) {
  130. char *avaType;
  131. struct berval *avaValue;
  132. const char *dn;
  133. dn = slapi_sdn_get_ndn ( aci_item->aci_sdn );
  134. slapi_filter_get_ava ( f, &avaType, &avaValue );
  135. if (!slapi_dn_issuffix( avaValue->bv_val, dn))
  136. return ACL_INVALID_TARGET;
  137. }
  138. }
  139. /*
  140. ** We need to keep the taregetFilterStr for anyone ACL only.
  141. ** same for targetValueFilterStr.
  142. ** We need to keep it for macros too as it needs to be expnaded at eval time.
  143. **
  144. */
  145. if ( (aci_item->aci_elevel != ACI_ELEVEL_USERDN_ANYONE) &&
  146. !(aci_item->aci_type & ACI_TARGET_MACRO_DN) ) {
  147. slapi_ch_free ( (void **) & aci_item->targetFilterStr );
  148. }
  149. /*
  150. * If we parsed the aci and there was a ($dn) on the user side
  151. * but none in hte taget then that's an error as the user side
  152. * value is derived from the target side value.
  153. */
  154. if (!(aci_item->aci_type & ACI_TARGET_MACRO_DN) &&
  155. (aci_item->aci_ruleType & ACI_PARAM_DNRULE)) {
  156. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  157. "acl_parse: A macro in a subject ($dn) must have a macro in the target.\n");
  158. return(ACL_INVALID_TARGET);
  159. }
  160. return 0;
  161. }
  162. /***************************************************************************
  163. *
  164. * __aclp__parse_aci
  165. *
  166. * Parses Each individual subset of information/
  167. *
  168. * Input:
  169. * char *str - Input string which has the ACL like "str)"
  170. * aci_t *item - the aci item where the ACL info will be
  171. * - stored.
  172. *
  173. * Returns:
  174. * 0 -- Parsed okay
  175. * < 0 -- error codes
  176. *
  177. * Error Handling:
  178. * None.
  179. *
  180. **************************************************************************/
  181. static int
  182. __aclp__parse_aci (char *str, aci_t *aci_item)
  183. {
  184. int len;
  185. int rv;
  186. int type;
  187. char *tmpstr;
  188. char *s = NULL;
  189. char *value = NULL;
  190. Slapi_Filter *f = NULL;
  191. int targetattrlen = strlen(aci_targetattr);
  192. int targetdnlen = strlen (aci_targetdn);
  193. int tfilterlen = strlen(aci_targetfilter);
  194. int targetattrfilterslen = strlen(aci_targetattrfilters);
  195. __acl_strip_leading_space( &str );
  196. if (*str == '\0') {
  197. return(ACL_SYNTAX_ERR);
  198. }
  199. /* The first letter should tell us something */
  200. switch(*str) {
  201. case 'v':
  202. type = ACI_ACLTXT;
  203. if ( 0 != (rv= __aclp__sanity_check_acltxt(aci_item, str ) ) ) {
  204. return rv;
  205. }
  206. break;
  207. case 't':
  208. if (strncmp(str, aci_targetattrfilters,targetattrfilterslen ) == 0) {
  209. type = ACI_TARGET_ATTR;
  210. /*
  211. * The targetattrfilters bit looks like this:
  212. * (targetattrfilters="add= attr1:F1 && attr2:F2 ... && attrn:Fn,
  213. * del= attr1:F1 && attr2:F2... && attrn:Fn")
  214. */
  215. if ( 0 != (rv= __acl__init_targetattrfilters(
  216. aci_item, str))) {
  217. return rv;
  218. }
  219. } else if (strncmp(str, aci_targetattr,targetattrlen ) == 0) {
  220. type = ACI_TARGET_ATTR;
  221. if ( (s = strstr( str, "!=" )) != NULL ) {
  222. type |= ACI_TARGET_ATTR_NOT;
  223. strncpy(s, " ", 1);
  224. }
  225. /* Get individual components of the targetattr.
  226. * (targetattr = "cn || u* || phone ||tel:add:(tel=1234)
  227. * || sn:del:(gn=5678)")
  228. * If it contains a value filter, the type will also be
  229. * ACI_TARGET_VALUE_ATTR.
  230. */
  231. if ( 0 != (rv= __aclp__init_targetattr(
  232. aci_item, str))) {
  233. return rv;
  234. }
  235. } else if (strncmp(str, aci_targetfilter,tfilterlen ) == 0) {
  236. if ( aci_item->targetFilter)
  237. return ACL_SYNTAX_ERR;
  238. type = ACI_TARGET_FILTER;
  239. /* we need to remove the targetfilter stuff*/
  240. if ( (s = strstr( str, "!=" )) != NULL ) {
  241. type |= ACI_TARGET_FILTER_NOT;
  242. }
  243. /*
  244. * If it's got a macro in the targetfilter then it must
  245. * have a target and it must have a macro.
  246. */
  247. if ((s = strstr (str, ACL_RULE_MACRO_DN_KEY)) != NULL ||
  248. ((s = strstr(str, ACL_RULE_MACRO_DN_LEVELS_KEY)) != NULL)) {
  249. /* Must have a targetmacro */
  250. if ( !(aci_item->aci_type & ACI_TARGET_MACRO_DN)) {
  251. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  252. "acl_parse: A macro in a targetfilter ($dn) must have a macro in the target.\n");
  253. return(ACL_SYNTAX_ERR);
  254. }
  255. type|= ACI_TARGET_FILTER_MACRO_DN;
  256. }
  257. tmpstr = strchr(str, '=');
  258. tmpstr++;
  259. __acl_strip_leading_space(&tmpstr);
  260. /*
  261. * Trim off enclosing quotes and enclosing
  262. * superfluous brackets.
  263. * The result has been duped so it can be kept.
  264. */
  265. tmpstr = __acl_trim_filterstr( tmpstr );
  266. f = slapi_str2filter(tmpstr);
  267. /* save the filter string */
  268. aci_item->targetFilterStr = tmpstr;
  269. } else if (strncmp(str, aci_targetdn, targetdnlen) == 0) {
  270. char *tstr = NULL;
  271. const size_t LDAP_URL_prefix_len = strlen (LDAP_URL_prefix);
  272. char *tt;
  273. type = ACI_TARGET_DN;
  274. /* Keep a copy of the target attr */
  275. if (aci_item->target) {
  276. return (ACL_SYNTAX_ERR);
  277. }
  278. if ( (s = strstr( str, "!=" )) != NULL ) {
  279. type |= ACI_TARGET_NOT;
  280. strncpy(s, " ", 1);
  281. }
  282. /* Convert it to lower as slapi_dn_normalize() does not */
  283. for (tt = str; *tt; tt++) *tt = TOLOWER ( *tt );
  284. if ( (s = strchr( str, '=' )) != NULL ) {
  285. value = s + 1;
  286. slapi_dn_normalize(value);
  287. len = strlen ( value );
  288. if (*value == '"' && value[len-1] == '"'){
  289. value[len-1] = '\0';
  290. value++;
  291. }
  292. __acl_strip_leading_space(&value);
  293. } else {
  294. return ( ACL_SYNTAX_ERR );
  295. }
  296. if ( strncasecmp ( value, LDAP_URL_prefix , LDAP_URL_prefix_len) )
  297. return ( ACL_SYNTAX_ERR );
  298. value += LDAP_URL_prefix_len;
  299. len = strlen ( value );
  300. tstr = (char *) slapi_ch_malloc ( targetdnlen + len + 4 );
  301. sprintf ( tstr, "(target=%s)", value);
  302. if ( (rv = acl_check_for_target_macro( aci_item, value)) == -1) {
  303. slapi_ch_free ( (void **) &tstr );
  304. return(ACL_SYNTAX_ERR);
  305. } else if ( rv > 0) {
  306. /* is present, so the type is now ACL_TARGET_MACRO_DN */
  307. type = ACI_TARGET_MACRO_DN;
  308. } else {
  309. /* it's a normal target with no macros inside */
  310. f = slapi_str2filter ( tstr );
  311. }
  312. slapi_ch_free ( (void **) &tstr );
  313. } else {
  314. /* did start with a 't' but was not a recognsied keyword */
  315. return(ACL_SYNTAX_ERR);
  316. }
  317. /*
  318. * Here, it was a recognised keyword that started with 't'.
  319. * Check that the filter associated with ACI_TARGET_DN and
  320. * ACI_TARGET_FILTER are OK.
  321. */
  322. if (f == NULL) {
  323. /* The following types require a filter to have been created */
  324. if (type & ACI_TARGET_DN)
  325. return ACL_TARGET_FILTER_ERR;
  326. else if (type & ACI_TARGET_FILTER)
  327. return ACL_TARGETFILTER_ERR;
  328. } else {
  329. int filterChoice;
  330. filterChoice = slapi_filter_get_choice ( f );
  331. if ( (type & ACI_TARGET_DN) &&
  332. ( filterChoice == LDAP_FILTER_PRESENT)) {
  333. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  334. "acl__parse_aci: Unsupported filter type:%d\n", filterChoice);
  335. return(ACL_SYNTAX_ERR);
  336. } else if (( filterChoice == LDAP_FILTER_SUBSTRINGS) &&
  337. (type & ACI_TARGET_DN)) {
  338. type &= ~ACI_TARGET_DN;
  339. type |= ACI_TARGET_PATTERN;
  340. }
  341. }
  342. if ((type & ACI_TARGET_DN) ||
  343. (type & ACI_TARGET_PATTERN)) {
  344. if (aci_item->target) {
  345. /* There is something already. ERROR */
  346. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  347. "Multiple targets in the ACL syntax\n",
  348. 0,0,0);
  349. slapi_filter_free(f, 1);
  350. return(ACL_SYNTAX_ERR);
  351. } else {
  352. aci_item->target = f;
  353. }
  354. } else if ( type & ACI_TARGET_FILTER) {
  355. if (aci_item->targetFilter) {
  356. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  357. "Multiple target Filters in the ACL Syntax\n",
  358. 0,0,0);
  359. slapi_filter_free(f, 1);
  360. return(ACL_SYNTAX_ERR);
  361. } else {
  362. aci_item->targetFilter = f;
  363. }
  364. }
  365. break; /* 't' */
  366. default:
  367. /* Here the keyword did not start with 'v' ot 't' so error */
  368. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  369. "Unknown keyword at \"%s\"\n Expecting"
  370. " \"target\", \"targetattr\", \"targetfilter\", \"targattrfilters\""
  371. " or \"version\"\n", str, 0, 0);
  372. return(ACL_SYNTAX_ERR);
  373. }/* switch() */
  374. /* Store the type info */
  375. aci_item->aci_type |= type;
  376. return 0;
  377. }
  378. /***************************************************************************
  379. * acl__sanity_check_acltxt
  380. *
  381. * Check the input ACL text. Reports any errors. Also forgivs if certain
  382. * things are missing.
  383. *
  384. * Input:
  385. * char *str - String containg the acl text
  386. * int *err - error status
  387. *
  388. * Returns:
  389. * 0 --- good status
  390. * <0 --- error
  391. *
  392. * Error Handling:
  393. * None.
  394. *
  395. *
  396. **************************************************************************/
  397. static int
  398. __aclp__sanity_check_acltxt (aci_t *aci_item, char *str)
  399. {
  400. NSErr_t errp;
  401. char *s;
  402. ACLListHandle_t *handle = NULL;
  403. char *newstr = NULL;
  404. char *word;
  405. char *next;
  406. memset (&errp, 0, sizeof(NSErr_t));
  407. newstr = str;
  408. while ((s = strstr(newstr, "authenticate")) != NULL) {
  409. char *next;
  410. next = s + 12;
  411. s--;
  412. while (s != str && ldap_utf8isspace(s)) LDAP_UTF8DEC(s);
  413. if (s && *s == ';') {
  414. /* We don't support authenticate stuff */
  415. return ACL_INVALID_AUTHORIZATION;
  416. } else {
  417. newstr = next;
  418. }
  419. }
  420. newstr = slapi_ch_strdup (str);
  421. word = ldap_utf8strtok_r(newstr, " ", &next);
  422. if (strcasecmp (word, "version") == 0) {
  423. word = ldap_utf8strtok_r(NULL, " ", &next);
  424. if (atoi(word) != 3) {
  425. slapi_ch_free ( (void **) &newstr );
  426. return ACL_INCORRECT_ACI_VERSION;
  427. }
  428. }
  429. slapi_ch_free ( (void **) &newstr );
  430. /* We need to normalize the DNs in the userdn and group dn
  431. ** so that, it's only done once.
  432. */
  433. if ((newstr = __aclp__normalize_acltxt (aci_item, str )) == NULL) {
  434. return ACL_SYNTAX_ERR;
  435. }
  436. slapi_log_error(SLAPI_LOG_ACL, plugin_name, "Normalized String:%s\n", newstr, 0,0);
  437. /* check for acl syntax error */
  438. if ((handle = (ACLListHandle_t *) ACL_ParseString(&errp,
  439. newstr)) == NULL) {
  440. acl_print_acllib_err(&errp, str);
  441. slapi_ch_free ( (void **) &newstr );
  442. return ACL_SYNTAX_ERR;
  443. } else {
  444. /* get the rights and the aci type */
  445. aci_item->aci_handle = handle;
  446. nserrDispose(&errp);
  447. slapi_ch_free ( (void **) &newstr );
  448. return 0;
  449. }
  450. }
  451. /******************************************************************************
  452. *
  453. * acl__normalize_acltxt
  454. *
  455. *
  456. * XXXrbyrne this routine should be re-written when someone eventually
  457. * gets sick enough of it. Same for getNextLAS() below.
  458. *
  459. * Normalize the acltxt i.e normalize all the DNs specified in the
  460. * Userdn and Groupdn rule so that we normalize once here and not
  461. * over and over again at the runtime in the LASes. We have to normalize
  462. * before we generate the handle otherwise it's of no use.
  463. * Also convert deny to deny absolute
  464. *
  465. * The string that comes in is something like:
  466. * version 3.0; acl "Dept domain administration"; allow (all)
  467. * groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP"; )
  468. *
  469. * Returns NULL on error.
  470. *
  471. ******************************************************************************/
  472. static char *
  473. __aclp__normalize_acltxt ( aci_t * aci_item, char * str )
  474. {
  475. char *s, *p;
  476. char *end;
  477. char *aclstr, *s_aclstr;
  478. char *ret_str = NULL;
  479. int len;
  480. char *ptr, *aclName;
  481. char *nextACE;
  482. char *tmp_str = NULL;
  483. char *acestr = NULL;
  484. char *s_acestr = NULL;
  485. int aci_rights_val = 0; /* bug 389975 */
  486. /* make a copy first */
  487. s_aclstr = aclstr = slapi_ch_strdup ( str );
  488. /* The rules are like this version 3.0; acl "xyz"; rule1; rule2; */
  489. s = strchr (aclstr, ';');
  490. if ( NULL == s) {
  491. slapi_ch_free ( (void **) &s_aclstr );
  492. return NULL;
  493. }
  494. aclstr = ++s;
  495. /* From DS 4.0, we support both aci (or aci) "name" -- we have to change to acl
  496. ** as libaccess will not like it
  497. */
  498. s = aclstr;
  499. while (s && ldap_utf8isspace(s)) LDAP_UTF8INC(s);
  500. *(s+2 ) = 'l';
  501. aclName = s+3;
  502. s = strchr (aclstr, ';');
  503. if ( NULL == s) {
  504. slapi_ch_free ( (void **) &s_aclstr );
  505. return NULL;
  506. }
  507. aclstr = s;
  508. LDAP_UTF8INC(aclstr);
  509. *s = '\0';
  510. /* Here aclName is the acl description string */
  511. aci_item->aclName = slapi_ch_strdup ( aclName );
  512. aclutil_str_appened (&ret_str, s_aclstr);
  513. aclutil_str_appened (&ret_str, ";");
  514. /* start with the string */
  515. acestr = aclstr;
  516. /*
  517. * Here acestr is something like:
  518. *
  519. * " allow (all) groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP";)"
  520. *
  521. *
  522. */
  523. normalize_nextACERule:
  524. /* now we are in the rule part */
  525. tmp_str = acestr;
  526. s = strchr (tmp_str, ';');
  527. if ( s == NULL) {
  528. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  529. slapi_ch_free ( (void **) &s_aclstr );
  530. return NULL;
  531. }
  532. nextACE = s;
  533. LDAP_UTF8INC(nextACE);
  534. *s = '\0';
  535. /* acestr now will hold copy of the ACE. Also add
  536. ** some more space in case we need to add "absolute"
  537. ** for deny rule. We will never need more 2 times
  538. ** the len.
  539. */
  540. len = strlen (tmp_str);
  541. s_acestr = acestr = slapi_ch_calloc ( 1, 2 * len);
  542. __acl_strip_leading_space(&tmp_str);
  543. /*
  544. * Now it's something like:
  545. * allow (all) groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP";
  546. */
  547. if (strncasecmp(tmp_str, "allow", 5) == 0) {
  548. memcpy(acestr, tmp_str, len);
  549. tmp_str += 5;
  550. /* gather the rights */
  551. aci_rights_val = __aclp__get_aci_right (tmp_str);/* bug 389975 */
  552. aci_item->aci_type |= ACI_HAS_ALLOW_RULE;
  553. } else if (strncasecmp(tmp_str, "deny", 4) == 0) {
  554. char *d_rule ="deny absolute";
  555. /* Then we have to add "absolute" to the deny rule
  556. ** What we are doing here is to tackle this situation.
  557. **
  558. ** allow -- deny -- allow
  559. ** deny -- allow
  560. **
  561. ** by using deny absolute we force the precedence rule
  562. ** i.e deny has a precedence over allow. Since there doesn't
  563. ** seem to be an easy to detect the mix, forcing this
  564. ** to all the deny rules will do the job.
  565. */
  566. __acl_strip_leading_space(&tmp_str);
  567. tmp_str += 4;
  568. /* We might have an absolute there already */
  569. if ((s = strstr (tmp_str, "absolute")) != NULL) {
  570. tmp_str = s;
  571. tmp_str += 8;
  572. }
  573. /* gather the rights */
  574. aci_rights_val = __aclp__get_aci_right (tmp_str);/* bug 389975 */
  575. aci_item->aci_type |= ACI_HAS_DENY_RULE;
  576. len = strlen ( d_rule );
  577. memcpy (acestr, d_rule, len );
  578. memcpy (acestr+len, tmp_str, strlen (tmp_str) );
  579. } else {
  580. /* wrong syntax */
  581. aci_rights_val = -1 ;
  582. }
  583. if (aci_rights_val == -1 )
  584. {
  585. /* wrong syntax */
  586. slapi_ch_free ( (void **) &ret_str );
  587. slapi_ch_free ( (void **) &s_acestr );
  588. slapi_ch_free ( (void **) &s_aclstr );
  589. return NULL;
  590. } else
  591. aci_item->aci_access |= aci_rights_val;
  592. /* Normalize all the DNs in the userdn rule */
  593. /*
  594. *
  595. * Here acestr starts like this:
  596. * " allow (all) groupdn = "ldap:///cn=Domain Administrators, o=$dn.o, o=ISP"
  597. */
  598. s = __aclp__getNextLASRule(aci_item, acestr, &end);
  599. while ( s ) {
  600. if ( 0 == strncmp ( s, DS_LAS_USERDNATTR, 10) ||
  601. ( 0 == strncmp ( s, DS_LAS_USERATTR, 8))) {
  602. /*
  603. ** For userdnattr/userattr rule, the resources changes and hence
  604. ** we cannot cache the result. See above for more comments.
  605. */
  606. aci_item->aci_elevel = ACI_ELEVEL_USERDNATTR;
  607. } else if ( 0== strncmp ( s, DS_LAS_USERDN, 6)) {
  608. p = strstr ( s, "=");
  609. p--;
  610. if ( strncmp (p, "!=", 2) == 0)
  611. aci_item->aci_type |= ACI_CONTAIN_NOT_USERDN;
  612. /* XXXrbyrne
  613. * Here we need to scan for more ldap:/// within
  614. * this userdn rule type:
  615. * eg. userdn = "ldap:///cn=joe,o=sun.com || ldap:///self"
  616. * This is handled correctly in DS_LASUserDnEval
  617. * but the bug here is not setting ACI_USERDN_SELFRULE
  618. * which would ensure that acl info is not cached from
  619. * one resource entry to the next. (bug 558519)
  620. */
  621. p = strstr ( p, "ldap");
  622. if (p == NULL) {
  623. /* must start with ldap */
  624. if (s_acestr) slapi_ch_free ( (void **) &s_acestr );
  625. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  626. slapi_ch_free ( (void **) &s_aclstr );
  627. return (NULL);
  628. }
  629. p += 8; /* for ldap:/// */
  630. if( __aclp__dn_normalize (p, end) == NULL) {
  631. if (s_acestr) slapi_ch_free ( (void **) &s_acestr );
  632. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  633. slapi_ch_free ( (void **) &s_aclstr );
  634. return (NULL);
  635. }
  636. /* we have a rule like userdn = "ldap:///blah". s points to blah now.
  637. ** let's find if we have a SELF rule like userdn = "ldap:///self".
  638. ** Since the resource changes on entry basis, we can't cache the
  639. ** evalation of handle for all time. The cache result is valid
  640. ** within the evaluation of that resource.
  641. */
  642. if (strncasecmp(p, "self", 4) == 0) {
  643. aci_item->aci_ruleType |= ACI_USERDN_SELFRULE;
  644. } else if ( strncasecmp(p, "anyone", 6) == 0 ) {
  645. aci_item->aci_elevel = ACI_ELEVEL_USERDN_ANYONE;
  646. } else if ( strncasecmp(p, "all", 3) == 0 ) {
  647. if ( aci_item->aci_elevel > ACI_ELEVEL_USERDN_ALL )
  648. aci_item->aci_elevel = ACI_ELEVEL_USERDN_ALL;
  649. } else {
  650. if ( aci_item->aci_elevel > ACI_ELEVEL_USERDN )
  651. aci_item->aci_elevel = ACI_ELEVEL_USERDN;
  652. }
  653. /* See if we have a parameterized rule */
  654. __aclp_chk_paramRules ( aci_item, p, end );
  655. } else if ( 0 == strncmp ( s, DS_LAS_GROUPDNATTR, 11)) {
  656. /*
  657. ** For groupdnattr rule, the resources changes and hence
  658. ** we cannot cache the result. See above for more comments.
  659. */
  660. /* Find out if we have a URL type of rule */
  661. if ((p= strstr (s, "ldap")) != NULL) {
  662. if ( aci_item->aci_elevel > ACI_ELEVEL_GROUPDNATTR_URL )
  663. aci_item->aci_elevel = ACI_ELEVEL_GROUPDNATTR_URL;
  664. } else if ( aci_item->aci_elevel > ACI_ELEVEL_GROUPDNATTR ) {
  665. aci_item->aci_elevel = ACI_ELEVEL_GROUPDNATTR;
  666. }
  667. aci_item->aci_ruleType |= ACI_GROUPDNATTR_RULE;
  668. } else if ( 0 == strncmp ( s, DS_LAS_GROUPDN, 7)) {
  669. p = strstr ( s, "=");
  670. p--;
  671. if ( strncmp (p, "!=", 2) == 0)
  672. aci_item->aci_type |= ACI_CONTAIN_NOT_GROUPDN;
  673. p = strstr ( s, "ldap");
  674. if (p == NULL) {
  675. /* must start with ldap */
  676. if (s_acestr) slapi_ch_free ( (void **) &s_acestr );
  677. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  678. slapi_ch_free ( (void **) &s_aclstr );
  679. return (NULL);
  680. }
  681. p += 8;
  682. if (__aclp__dn_normalize (p, end) == NULL) {
  683. if (s_acestr) slapi_ch_free ( (void **) &s_acestr );
  684. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  685. slapi_ch_free ( (void **) &s_aclstr );
  686. return (NULL);
  687. }
  688. /* check for param rules */
  689. __aclp_chk_paramRules ( aci_item, p, end );
  690. if ( aci_item->aci_elevel > ACI_ELEVEL_GROUPDN )
  691. aci_item->aci_elevel = ACI_ELEVEL_GROUPDN;
  692. aci_item->aci_ruleType |= ACI_GROUPDN_RULE;
  693. } else if ( 0 == strncmp ( s, DS_LAS_ROLEDN, 6)) {
  694. p = strstr ( s, "=");
  695. p--;
  696. if ( strncmp (p, "!=", 2) == 0)
  697. aci_item->aci_type |= ACI_CONTAIN_NOT_ROLEDN;
  698. p = strstr ( s, "ldap");
  699. if (p == NULL) {
  700. /* must start with ldap */
  701. if (s_acestr) slapi_ch_free ( (void **) &s_acestr );
  702. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  703. slapi_ch_free ( (void **) &s_aclstr );
  704. return (NULL);
  705. }
  706. p += 8;
  707. if (__aclp__dn_normalize (p, end) == NULL) {
  708. if (s_acestr) slapi_ch_free ( (void **) &s_acestr );
  709. if (ret_str) slapi_ch_free ( (void **) &ret_str );
  710. slapi_ch_free ( (void **) &s_aclstr );
  711. return (NULL);
  712. }
  713. /* check for param rules */
  714. __aclp_chk_paramRules ( aci_item, p, end );
  715. /* XXX need this for roledn ?
  716. if ( aci_item->aci_elevel > ACI_ELEVEL_GROUPDN )
  717. aci_item->aci_elevel = ACI_ELEVEL_GROUPDN;*/
  718. aci_item->aci_ruleType |= ACI_ROLEDN_RULE;
  719. }
  720. s = ++end;
  721. s = __aclp__getNextLASRule(aci_item, s, &end);
  722. }/* while */
  723. /* get the head of the string */
  724. acestr = s_acestr;
  725. len = strlen( acestr);
  726. ptr = acestr +len-1;
  727. while (*ptr && *ptr != '\"' && *ptr != ')' ) *ptr-- = ' ';
  728. ptr++;
  729. *ptr = ';';
  730. aclutil_str_appened (&ret_str, acestr);
  731. if (s_acestr) {
  732. slapi_ch_free ( (void **) &s_acestr );
  733. }
  734. s_acestr = NULL;
  735. if (nextACE) {
  736. s = strstr (nextACE, "allow");
  737. if (s == NULL) s = strstr (nextACE, "deny");
  738. if (s == NULL) {
  739. if (nextACE && *nextACE != '\0')
  740. aclutil_str_appened (&ret_str, nextACE);
  741. slapi_ch_free ( (void **) &s_aclstr );
  742. return (ret_str);
  743. }
  744. acestr = nextACE;
  745. goto normalize_nextACERule;
  746. }
  747. slapi_ch_free ( (void **) &s_aclstr );
  748. return (ret_str);
  749. }
  750. /*
  751. *
  752. * acl__getNextLASRule
  753. * Find the next rule.
  754. *
  755. * Returns:
  756. * endOfCurrRule - end of current rule
  757. * nextRule - start of next rule
  758. */
  759. static char *
  760. __aclp__getNextLASRule (aci_t *aci_item, char *original_str , char **endOfCurrRule)
  761. {
  762. char *newstr, *word, *next, *start, *end;
  763. char *ruleStart = NULL;
  764. int len, ruleLen;
  765. int in_dn_expr = 0;
  766. *endOfCurrRule = NULL;
  767. end = start = NULL;
  768. newstr = slapi_ch_strdup (original_str);
  769. if ( (strncasecmp(newstr, "allow", 5) == 0) ||
  770. (strncasecmp(newstr, "deny", 4) == 0) ) {
  771. word = ldap_utf8strtok_r(newstr, ")", &next);
  772. }
  773. else {
  774. word = ldap_utf8strtok_r(newstr, " ", &next);
  775. }
  776. /*
  777. * The first word is of no interest -- skip it
  778. * it's allow or deny followed by the rights (<rights>),
  779. * so skip over the rights as well or it's 'and', 'or',....
  780. */
  781. while ( (word = ldap_utf8strtok_r(NULL, " ", &next)) != NULL) {
  782. int got_rule = 0;
  783. int ruleType = 0;
  784. /*
  785. ** The next word must be one of these to be considered
  786. ** a valid rule.
  787. ** This is making me crazy. We might have a case like
  788. ** "((userdn=". strtok is returning me that word.
  789. */
  790. len = strlen ( word );
  791. word [len] = '\0';
  792. if ( (ruleStart= strstr(word, DS_LAS_USERDNATTR)) != NULL) {
  793. ruleType |= ACI_USERDNATTR_RULE;
  794. ruleLen = strlen ( DS_LAS_USERDNATTR) ;
  795. } else if ( (ruleStart = strstr(word, DS_LAS_USERDN)) != NULL) {
  796. ruleType = ACI_USERDN_RULE;
  797. ruleLen = strlen ( DS_LAS_USERDN);
  798. in_dn_expr = 1;
  799. } else if ( (ruleStart = strstr(word, DS_LAS_GROUPDNATTR)) != NULL) {
  800. ruleType = ACI_GROUPDNATTR_RULE;
  801. ruleLen = strlen ( DS_LAS_GROUPDNATTR) ;
  802. } else if ((ruleStart= strstr(word, DS_LAS_GROUPDN)) != NULL) {
  803. ruleType = ACI_GROUPDN_RULE;
  804. ruleLen = strlen ( DS_LAS_GROUPDN) ;
  805. in_dn_expr = 1;
  806. } else if ((ruleStart = strstr(word, DS_LAS_USERATTR)) != NULL) {
  807. ruleType = ACI_USERATTR_RULE;
  808. ruleLen = strlen ( DS_LAS_USERATTR) ;
  809. } else if ((ruleStart= strstr(word, DS_LAS_ROLEDN)) != NULL) {
  810. ruleType = ACI_ROLEDN_RULE;
  811. ruleLen = strlen ( DS_LAS_ROLEDN);
  812. in_dn_expr = 1;
  813. } else if ((ruleStart= strstr(word, DS_LAS_AUTHMETHOD)) != NULL) {
  814. ruleType = ACI_AUTHMETHOD_RULE;
  815. ruleLen = strlen ( DS_LAS_AUTHMETHOD);
  816. } else if ((ruleStart = strstr(word, ACL_ATTR_IP)) != NULL) {
  817. ruleType = ACI_IP_RULE;
  818. ruleLen = strlen ( ACL_ATTR_IP) ;
  819. } else if ((ruleStart = strstr(word, DS_LAS_TIMEOFDAY)) != NULL) {
  820. ruleType = ACI_TIMEOFDAY_RULE;
  821. ruleLen = strlen ( DS_LAS_TIMEOFDAY) ;
  822. } else if ((ruleStart = strstr(word, DS_LAS_DAYOFWEEK)) != NULL) {
  823. ruleType = ACI_DAYOFWEEK_RULE;
  824. ruleLen = strlen ( DS_LAS_DAYOFWEEK) ;
  825. } else if ((ruleStart = strstr(word, ACL_ATTR_DNS)) != NULL) {
  826. ruleType = ACI_DNS_RULE;
  827. ruleLen = strlen ( ACL_ATTR_DNS) ;
  828. }
  829. /* Here, we've found a space...if we were in in_dn_expr mode
  830. * and we'vve found a closure for that ie.a '"' or a ')'
  831. * eg. "'ldap:///all"' or 'ldap:///all")' then exit in_dn_expr mode.
  832. */
  833. if ( in_dn_expr && (word[len-1] == '"' ||
  834. len>1 && word[len-2] == '"' ||
  835. len>2 && word[len-3] == '"')) {
  836. in_dn_expr = 0;
  837. }
  838. /*
  839. * ruleStart may be NULL as word could be (all) for example.
  840. * this word will just be skipped--we're really waiting for
  841. * userdn or groupdn or...
  842. */
  843. if ( ruleStart && ruleType ) {
  844. /* Look in the current word for "=" or else look into
  845. ** the next word -- if none of them are true, then this
  846. ** is not the start of the rule
  847. */
  848. char *tmpStr = ruleStart + ruleLen;
  849. if ( strchr ( tmpStr, '=') ||
  850. ((word = ldap_utf8strtok_r(NULL, " ", &next) ) &&
  851. word && ((strncmp ( word, "=", 1) == 0 ) ||
  852. (strncmp ( word, "!=",2) ==0) ||
  853. (strncmp ( word, ">", 1) == 0 ) ||
  854. (strncmp ( word, "<", 1) == 0 ) ||
  855. (strncmp ( word, "<", 1) == 0 ) ||
  856. (strncmp ( word, "<=",2) ==0 ) ||
  857. (strncmp ( word, ">=",2) ==0) ||
  858. (strncmp ( word, "=>",2) ==0) ||
  859. (strncmp ( word, "=<",2) ==0))
  860. ) ){
  861. aci_item->aci_ruleType |= ruleType;
  862. got_rule = 1;
  863. }
  864. }
  865. if ( NULL == start && got_rule ) {
  866. /*
  867. * We've just found a rule start--keep going though because
  868. * we need to return the end of this rule too.
  869. */
  870. start= ruleStart;
  871. got_rule = 0;
  872. } else {
  873. /*
  874. * Here, we have a candidate for the end of the rule we've found
  875. * (the start of which is currently in start).
  876. * But we need to be sure it really is the end and not a
  877. * "fake end" due to a keyword bbeing embeded in a dn.
  878. */
  879. if (word && !in_dn_expr &&
  880. ((strcasecmp(word, "and") == 0) ||
  881. (strcasecmp(word, "or") == 0) ||
  882. (strcasecmp(word, "not") == 0) ||
  883. (strcasecmp(word, ";") == 0))) {
  884. /* If we have start, then it really is the end */
  885. word--;
  886. if (start) {
  887. end = word;
  888. break;
  889. } else {
  890. /* We found a fake end, but we've no start so keep going */
  891. }
  892. }
  893. }
  894. } /* while */
  895. if ( end ) {
  896. /* Found an end to the rule and it's not the last rule */
  897. len = end - newstr;
  898. end = original_str +len;
  899. while ( (end != original_str) && *end != '\"') end--;
  900. *endOfCurrRule = end;
  901. len = start - newstr;
  902. ruleStart = original_str + len;
  903. } else {
  904. /* Walked off the end of the string so it's the last rule */
  905. end = original_str + strlen(original_str)-1;
  906. while ( (end != original_str) && *end != '\"') end--;
  907. *endOfCurrRule = end;
  908. }
  909. if ( start ) {
  910. /* Got a rule, fixup the pointer */
  911. len = start - newstr;
  912. ruleStart = original_str + len;
  913. }
  914. slapi_ch_free ( (void **) &newstr );
  915. /*
  916. * Here, ruleStart points to the start of the next rule in original_str.
  917. * end points to the end of this rule.
  918. */
  919. return ( ruleStart );
  920. }
  921. /******************************************************************************
  922. *
  923. * __aclp__dn_normalize
  924. *
  925. * Normalize the DN INPLACE. This routine is similar to slapi_dn_normalize()
  926. * except various small stuff at the end.
  927. * Normalize until the "end" and not to the end of string.
  928. *
  929. ******************************************************************************/
  930. static char *
  931. __aclp__dn_normalize( char *dn , char *end)
  932. {
  933. char *d;
  934. if ((end - dn) < 0) {
  935. return(NULL);
  936. }
  937. d = slapi_dn_normalize_to_end ( dn, end );
  938. /* Do I have the quotes already */
  939. if (*d != '\"' ) {
  940. /*
  941. ** We are taking care of this situation
  942. ** " ") ". We need to remove the space
  943. ** infront and tack it after the quote like this.
  944. ** "" ) ".
  945. */
  946. *d = '\"';
  947. d++;
  948. while (*d && *d != '\"') *d++ = ' ';
  949. *d = ' ';
  950. }
  951. return( dn );
  952. }
  953. /***************************************************************************
  954. * acl__get_aci_right
  955. *
  956. * Go thru the one acl text str and figure our the rights declared.
  957. *
  958. *****************************************************************************/
  959. static int
  960. __aclp__get_aci_right (char *str)
  961. {
  962. char *sav_str = slapi_ch_strdup(str);
  963. char *t, *tt;
  964. int type = 0;
  965. char *delimiter = ",";
  966. char *val = NULL;
  967. int aclval = 0;
  968. t = sav_str;
  969. __acl_strip_leading_space( &t );
  970. if (*t == '(' ) {
  971. if ((tt = slapi_find_matching_paren(t)) == NULL) {
  972. slapi_ch_free ( (void **) &sav_str );
  973. return -1;
  974. } else {
  975. t++; /* skip the first character which is ( */
  976. *tt = '\0';
  977. }
  978. } else {
  979. slapi_ch_free ( (void **) &sav_str );
  980. return -1;
  981. }
  982. /* get the tokens separated by "," */
  983. val = ldap_utf8strtok_r(t,delimiter, &tt);
  984. if (val == NULL )
  985. {
  986. slapi_ch_free ( (void **) &sav_str );
  987. return -1;
  988. }
  989. while (val != NULL)
  990. {
  991. /* get the corresponding integer value */
  992. aclval = get_acl_rights_as_int(val);
  993. if (aclval == -1 )
  994. {
  995. type = -1;
  996. break;
  997. }
  998. type |= aclval;
  999. val = ldap_utf8strtok_r(NULL,delimiter, &tt); /* get the next token */
  1000. }
  1001. slapi_ch_free ( (void **) &sav_str );
  1002. return type;
  1003. }
  1004. static int get_acl_rights_as_int( char * strValue)
  1005. {
  1006. if (strValue == NULL )
  1007. return -1;
  1008. /* First strip out the leading and trailing spaces */
  1009. __acl_strip_leading_space( &strValue );
  1010. __acl_strip_trailing_space( strValue );
  1011. /* We have to do a strcasecmp (case insensitive cmp) becuase we should return
  1012. only if it is exact match. */
  1013. if (strcasecmp (strValue, "read") == 0 )
  1014. return SLAPI_ACL_READ;
  1015. else if (strcasecmp (strValue, "write") == 0 )
  1016. return SLAPI_ACL_WRITE;
  1017. else if (strcasecmp (strValue, "search") == 0 )
  1018. return SLAPI_ACL_SEARCH;
  1019. else if (strcasecmp (strValue, "compare") == 0 )
  1020. return SLAPI_ACL_COMPARE;
  1021. else if (strcasecmp (strValue, "add") == 0 )
  1022. return SLAPI_ACL_ADD;
  1023. else if (strcasecmp (strValue, "delete") == 0 )
  1024. return SLAPI_ACL_DELETE;
  1025. else if (strcasecmp (strValue, "proxy") == 0 )
  1026. return SLAPI_ACL_PROXY;
  1027. else if (strcasecmp (strValue, "selfwrite") == 0 )
  1028. return (SLAPI_ACL_SELF | SLAPI_ACL_WRITE);
  1029. else if (strcasecmp (strValue, "all") == 0 )
  1030. return SLAPI_ACL_ALL;
  1031. else
  1032. return -1; /* error */
  1033. }
  1034. /***************************************************************************
  1035. *
  1036. * acl_access2str
  1037. *
  1038. * Convert the access bits into character strings.
  1039. * Example: "read, self read"
  1040. *
  1041. * Input:
  1042. *
  1043. * int access - The access in bits
  1044. * char **rights - rights in chars
  1045. *
  1046. * Returns:
  1047. * NULL - No rights to start with
  1048. * right - rights converted.
  1049. *
  1050. * Error Handling:
  1051. * None.
  1052. *
  1053. **************************************************************************/
  1054. char *
  1055. acl_access2str(int access)
  1056. {
  1057. if ( access & SLAPI_ACL_COMPARE ) {
  1058. return access_str_compare;
  1059. } else if ( access & SLAPI_ACL_SEARCH ) {
  1060. return access_str_search;
  1061. } else if ( access & SLAPI_ACL_READ ) {
  1062. return access_str_read;
  1063. } else if ( access & SLAPI_ACL_DELETE) {
  1064. return access_str_delete;
  1065. } else if ( access & SLAPI_ACL_ADD) {
  1066. return access_str_add;
  1067. } else if ( (access & SLAPI_ACL_WRITE ) && (access & SLAPI_ACL_SELF)) {
  1068. return access_str_selfwrite;
  1069. } else if (access & SLAPI_ACL_WRITE ) {
  1070. return access_str_write;
  1071. } else if (access & SLAPI_ACL_PROXY ) {
  1072. return access_str_proxy;
  1073. }
  1074. return NULL;
  1075. }
  1076. /***************************************************************************
  1077. *
  1078. * __aclp__init_targetattr
  1079. *
  1080. * Parse the targetattr string and create a array of attrs. This will
  1081. * help us to do evaluation at run time little faster.
  1082. * entry.
  1083. * Here, also extract any target value filters.
  1084. *
  1085. * Input:
  1086. * aci_t *aci -- The aci item
  1087. * char *str -- the targetattr string
  1088. *
  1089. * Returns:
  1090. * ACL_OK - everything ok
  1091. * ACL_SYNTAX_ERROR - in case of error.
  1092. *
  1093. *
  1094. ***************************************************************************/
  1095. static int
  1096. __aclp__init_targetattr (aci_t *aci, char *attr_val)
  1097. {
  1098. int numattr=0;
  1099. Targetattr **attrArray;
  1100. char *s, *end_attr, *str;
  1101. int len;
  1102. Targetattr *attr = NULL;
  1103. s = strchr (attr_val, '=');
  1104. s++;
  1105. __acl_strip_leading_space(&s);
  1106. len = strlen(s);
  1107. if (*s == '"' && s[len-1] == '"') {
  1108. s[len-1] = '\0';
  1109. s++;
  1110. }
  1111. str = s;
  1112. attrArray = aci->targetAttr;
  1113. if (attrArray[0] != NULL) {
  1114. /*
  1115. ** That means we are visiting more than once.
  1116. ** Syntax error. We have a case like: (targetattr) (targetattr)
  1117. */
  1118. return ACL_SYNTAX_ERR;
  1119. }
  1120. while (str != 0 && *str != 0) {
  1121. __acl_strip_leading_space(&str);
  1122. if ((end_attr = strstr(str, "||")) != NULL) {
  1123. /* skip the two '|' chars */
  1124. auto char *t = end_attr;
  1125. LDAP_UTF8INC(end_attr);
  1126. LDAP_UTF8INC(end_attr);
  1127. *t = 0;
  1128. }
  1129. __acl_strip_trailing_space(str);
  1130. /*
  1131. * Here:
  1132. * end_attr points to the next attribute thing.
  1133. *
  1134. * str points to the current one to be processed and it looks like this:
  1135. * rbyrneXXX Watchout is it OK to use : as the speperator ?
  1136. * cn
  1137. * c*n*
  1138. * *
  1139. * The attribute goes in the attrTarget list.
  1140. *
  1141. */
  1142. attr = (Targetattr *) slapi_ch_malloc (sizeof (Targetattr));
  1143. memset (attr, 0, sizeof(Targetattr));
  1144. if (strchr(str, '*')) {
  1145. /* It contains a * so it's something like * or cn* */
  1146. if (strcmp(str, "*" ) != 0) {
  1147. char line[100];
  1148. char *lineptr = &line[0];
  1149. char *newline = NULL;
  1150. int lenstr = 0;
  1151. struct slapi_filter *f = NULL;
  1152. if ((lenstr = strlen(str)) > 91) { /* 100 - 8 for "(attr =%s)" */
  1153. newline = slapi_ch_malloc(lenstr + 9);
  1154. lineptr = newline;
  1155. }
  1156. attr->attr_type = ACL_ATTR_FILTER;
  1157. sprintf (lineptr, "(attr =%s)", str);
  1158. f = slapi_str2filter (lineptr);
  1159. if (f == NULL) {
  1160. slapi_log_error(SLAPI_LOG_FATAL, plugin_name,
  1161. "__aclp__init_targetattr:Unable to generate filter (%s)\n", lineptr,0,0);
  1162. } else {
  1163. attr->u.attr_filter = f;
  1164. }
  1165. if (newline) slapi_ch_free((void **) &newline);
  1166. } else {
  1167. attr->attr_type = ACL_ATTR_STAR;
  1168. attr->u.attr_str = slapi_ch_strdup (str);
  1169. }
  1170. } else {
  1171. attr->u.attr_str = slapi_ch_strdup (str);
  1172. attr->attr_type = ACL_ATTR_STRING;
  1173. }
  1174. /*
  1175. * Add the attr to the targetAttr list
  1176. */
  1177. attrArray[numattr] = attr;
  1178. numattr++;
  1179. if (!(numattr % ACL_INIT_ATTR_ARRAY)) {
  1180. aci->targetAttr = (Targetattr **) slapi_ch_realloc (
  1181. (void *) aci->targetAttr,
  1182. (numattr+ACL_INIT_ATTR_ARRAY) *
  1183. sizeof(Targetattr *));
  1184. attrArray = aci->targetAttr;
  1185. }
  1186. /* Move on to the next attribute in the list */
  1187. str = end_attr;
  1188. } /* while */
  1189. /* NULL teminate the list */
  1190. attrArray[numattr] = NULL;
  1191. return 0;
  1192. }
  1193. void
  1194. acl_strcpy_special (char *d, char *s)
  1195. {
  1196. for (; *s; LDAP_UTF8INC(s)) {
  1197. switch (*s) {
  1198. case '.':
  1199. case '\\':
  1200. case '[':
  1201. case ']':
  1202. case '*':
  1203. case '+':
  1204. case '^':
  1205. case '$':
  1206. *d = '\\';
  1207. LDAP_UTF8INC(d);
  1208. /* FALL */
  1209. default:
  1210. d += LDAP_UTF8COPY(d, s);
  1211. }
  1212. }
  1213. *d = '\0';
  1214. }
  1215. /***************************************************************************
  1216. *
  1217. * acl_verify_aci_syntax
  1218. * verify if the aci's being added for the entry has a valid syntax or not.
  1219. *
  1220. * Input:
  1221. * Slapi_Entry *e - The Slapi_Entry itself
  1222. * char **errbuf; -- error message
  1223. *
  1224. * Returns:
  1225. * -1 (ACL_ERR) - Syntax error
  1226. * 0 - No error
  1227. *
  1228. * Error Handling:
  1229. * None.
  1230. *
  1231. **************************************************************************/
  1232. int
  1233. acl_verify_aci_syntax (Slapi_Entry *e, char **errbuf)
  1234. {
  1235. if (e != NULL) {
  1236. Slapi_DN *e_sdn;
  1237. int rv;
  1238. Slapi_Attr *attr = NULL;
  1239. Slapi_Value *sval=NULL;
  1240. const struct berval *attrVal;
  1241. int i;
  1242. e_sdn = slapi_entry_get_sdn ( e );
  1243. slapi_entry_attr_find (e, aci_attr_type, &attr);
  1244. if (! attr ) return 0;
  1245. i= slapi_attr_first_value ( attr,&sval );
  1246. while ( i != -1 ) {
  1247. attrVal = slapi_value_get_berval ( sval );
  1248. rv=acl_verify_syntax ( e_sdn, attrVal);
  1249. if ( 0 != rv ) {
  1250. aclutil_print_err(rv, e_sdn, attrVal, errbuf);
  1251. return ACL_ERR;
  1252. }
  1253. i = slapi_attr_next_value ( attr, i, &sval );
  1254. }
  1255. }
  1256. return(0);
  1257. }
  1258. /***************************************************************************
  1259. *
  1260. * acl__verify_syntax
  1261. * Called from slapi_acl_check_mods() to verify if the new aci being
  1262. * added/replaced has the right syntax or not.
  1263. *
  1264. * Input:
  1265. * Slapi_DN *e_sdn - sdn of the entry
  1266. * berval *bval - The berval containg the aci value
  1267. *
  1268. * Returns:
  1269. * return values from acl__parse_aci()
  1270. *
  1271. * Error Handling:
  1272. * None.
  1273. *
  1274. **************************************************************************/
  1275. int
  1276. acl_verify_syntax(const Slapi_DN *e_sdn, const struct berval *bval)
  1277. {
  1278. aci_t *aci_item;
  1279. int rv = 0;
  1280. char *str;
  1281. aci_item = acllist_get_aci_new ();
  1282. slapi_sdn_set_ndn_byval ( aci_item->aci_sdn, slapi_sdn_get_ndn ( e_sdn ) );
  1283. /* make a copy the the string */
  1284. str = slapi_ch_strdup(bval->bv_val);
  1285. rv = acl_parse(str, aci_item);
  1286. /* cleanup before you leave ... */
  1287. acllist_free_aci (aci_item);
  1288. slapi_ch_free ( (void **) &str );
  1289. return(rv);
  1290. }
  1291. static void
  1292. __aclp_chk_paramRules ( aci_t *aci_item, char *start, char *end)
  1293. {
  1294. size_t len;
  1295. char *str;
  1296. char *p, *s;
  1297. len = end - start;
  1298. s = str = (char *) slapi_ch_calloc(1, len + 1);
  1299. memcpy ( str, start, len);
  1300. while ( (p= strchr ( s, '$')) != NULL) {
  1301. p++; /* skip the $ */
  1302. if ( 0 == strncasecmp ( p, "dn", 2))
  1303. aci_item->aci_ruleType |= ACI_PARAM_DNRULE;
  1304. else if ( 0 == strncasecmp ( p, "attr", 4))
  1305. aci_item->aci_ruleType |= ACI_PARAM_ATTRRULE;
  1306. s = p;
  1307. }
  1308. slapi_ch_free ( (void **) &str );
  1309. }
  1310. /*
  1311. * Check for an ocurrence of a macro aci in the target.
  1312. * value is the normalized target string.
  1313. *
  1314. * this is something like:
  1315. * (target="ldap:///cn=*,ou=people,($dn),o=sun.com")
  1316. *
  1317. *
  1318. * returns 1 if there is a $dn present.
  1319. * returns 0 if not.
  1320. * returns -1 is syntax error.
  1321. * If succes then:
  1322. * ACI_TARGET_MACRO_DN is the type.
  1323. * type can also include, ACI_TARGET_PATTERN, ACI_TARGET_NOT.
  1324. * Also aci_item->aci_macro->match_this is set to be
  1325. * cn=*,ou=people,($dn),o=sun.com, to be used later.
  1326. *
  1327. * . we allow at most one ($dn) in a target.
  1328. * . if a "*" accurs with it, it must be the first component and at most
  1329. * once.
  1330. * . it's ok for ($dn) to occur on it's own in a target, but if it appears in
  1331. * a user rule, then it must be in the target.
  1332. *
  1333. *
  1334. *
  1335. */
  1336. static int
  1337. acl_check_for_target_macro( aci_t *aci_item, char *value)
  1338. {
  1339. char *str = NULL;
  1340. str = strstr( value, ACL_TARGET_MACRO_DN_KEY);
  1341. if (str != NULL) {
  1342. aci_item->aci_type &= ~ACI_TARGET_DN;
  1343. aci_item->aci_type |= ACI_TARGET_MACRO_DN;
  1344. aci_item->aci_macro = (aciMacro *)slapi_ch_malloc(sizeof(aciMacro));
  1345. aci_item->aci_macro->match_this = slapi_ch_strdup(value);
  1346. aci_item->aci_macro->macro_ptr = strstr( aci_item->aci_macro->match_this,
  1347. ACL_TARGET_MACRO_DN_KEY);
  1348. return(1);
  1349. }
  1350. return(0);
  1351. }
  1352. /* Strip trailing spaces from str by writing '\0' into them */
  1353. static void
  1354. __acl_strip_trailing_space( char *str) {
  1355. char *ptr = NULL;
  1356. int len = 0;
  1357. if (*str) {
  1358. /* ignore trailing whitespace */
  1359. len = strlen(str);
  1360. ptr = str+len-1;
  1361. while(ldap_utf8isspace(ptr)){ *ptr = '\0'; LDAP_UTF8DEC(ptr); }
  1362. }
  1363. }
  1364. /*
  1365. * Strip leading spaces by resetting str to point to the first
  1366. * non-space charater.
  1367. */
  1368. static void
  1369. __acl_strip_leading_space( char **str) {
  1370. char *tmp_ptr = NULL;
  1371. tmp_ptr = *str;
  1372. while ( *tmp_ptr && ldap_utf8isspace( tmp_ptr ) ) LDAP_UTF8INC(tmp_ptr);
  1373. *str = tmp_ptr;
  1374. }
  1375. /*
  1376. * str is a string containing an LDAP filter.
  1377. * Trim off enclosing quotes and enclosing
  1378. * superfluous brackets.
  1379. * The result is duped so it can be kept.
  1380. */
  1381. static char *
  1382. __acl_trim_filterstr( char * str ) {
  1383. char *tmpstr;
  1384. int len;
  1385. char *end;
  1386. tmpstr = str;
  1387. /* If the last char is a "," take it out */
  1388. len = strlen (tmpstr);
  1389. if (len>0 && (tmpstr[len-1] == ',')) {
  1390. tmpstr [len-1] = '\0';
  1391. }
  1392. /* Does it have quotes around it */
  1393. len = strlen (tmpstr);
  1394. if (*tmpstr == '"' && tmpstr[len-1] == '"') {
  1395. tmpstr [len-1] = '\0';
  1396. tmpstr++;
  1397. }
  1398. str = tmpstr;
  1399. /* If we have a filter like
  1400. ** (((&(...) (...)))), we need to get rid of the
  1401. ** multiple parens or slapi_str2filter will not
  1402. ** evaluate properly. Need to package like
  1403. ** (filter ). probably I should fix str2filter
  1404. ** code.
  1405. */
  1406. while (*tmpstr++ == '(' && *tmpstr == '(') {
  1407. if ((end = slapi_find_matching_paren( str )) != NULL) {
  1408. *end = '\0';
  1409. str++;
  1410. }
  1411. }
  1412. return( slapi_ch_strdup(str));
  1413. }
  1414. /*
  1415. * Here str points to a targetattrfilters thing which looks tlike this:
  1416. *
  1417. * targetattrfilters="add=attr1:F1 && attr2:F2 ... && attrn:Fn,
  1418. * del=attr1:F1 && attr2:F2... && attrn:Fn")
  1419. *
  1420. *
  1421. */
  1422. static int __acl__init_targetattrfilters( aci_t *aci, char *input_str) {
  1423. char *s, *str;
  1424. int len;
  1425. char *addlistptr = NULL;
  1426. char *dellistptr = NULL;
  1427. if (aci->targetAttrAddFilters != NULL ||
  1428. aci->targetAttrDelFilters != NULL) {
  1429. /*
  1430. ** That means we are visiting more than once.
  1431. ** Syntax error.
  1432. ** We have a case like: (targetattrfilters) (targetattrfilters)
  1433. */
  1434. return ACL_SYNTAX_ERR;
  1435. }
  1436. /* First, skip the "targetattrfilters" */
  1437. s = strchr (input_str, '=');
  1438. s++; /* skip the = */
  1439. __acl_strip_leading_space(&s); /* skip to next significant character */
  1440. len = strlen(s); /* Knock off the " and trailing ) */
  1441. if (*s == '"' && s[len-1] == '"') {
  1442. s[len-1] = '\0';
  1443. s++; /* skip the first " */
  1444. } else { /* No matching quotes */
  1445. return (ACL_SYNTAX_ERR);
  1446. }
  1447. str = s;
  1448. /*
  1449. * Here str looks like add=attr1:F1...attrn:Fn,
  1450. * del=attr1:F1...attrn:Fn
  1451. *
  1452. * extract the add and del filter lists and process each one
  1453. * in turn.
  1454. */
  1455. s = strchr (str, '=');
  1456. *s = '\0';
  1457. s++; /* skip the = */
  1458. __acl_strip_leading_space(&s); /* start of the first filter list */
  1459. /*
  1460. * Now str is add or del
  1461. * s points to the first filter list.
  1462. */
  1463. if (strcmp(str, "add") == 0) {
  1464. aci->aci_type |= ACI_TARGET_ATTR_ADD_FILTERS;
  1465. addlistptr = s;
  1466. /* Now isolate the first filter list. */
  1467. if ((str = strstr(s , "del=")) || ((str = strstr(s , "del ="))) ) {
  1468. str--;
  1469. *str = '\0';
  1470. *str++;
  1471. }
  1472. } else if (strcmp(str, "del") == 0) {
  1473. aci->aci_type |= ACI_TARGET_ATTR_DEL_FILTERS;
  1474. dellistptr = s;
  1475. /* Now isolate the first filter list. */
  1476. if ((str = strstr(s , "add=")) || ((str = strstr(s , "add ="))) ) {
  1477. str--;
  1478. *str = '\0';
  1479. *str++;
  1480. }
  1481. } else {
  1482. return(ACL_SYNTAX_ERR);
  1483. }
  1484. __acl_strip_trailing_space(s);
  1485. /*
  1486. * Here, we have isolated the first filter list.
  1487. * There may be a second one.
  1488. * Now, str points to the start of the
  1489. * string that contains the second filter list.
  1490. * If there is none then str is NULL.
  1491. */
  1492. if (str != NULL ){
  1493. __acl_strip_leading_space(&str);
  1494. s = strchr (str, '=');
  1495. *s = '\0';
  1496. s++;
  1497. __acl_strip_trailing_space(str);
  1498. __acl_strip_leading_space(&s);
  1499. /*
  1500. * s points to the start of the second filter list.
  1501. * str is add or del
  1502. */
  1503. if (aci->aci_type & ACI_TARGET_ATTR_ADD_FILTERS) {
  1504. if (strcmp(str, "del") == 0) {
  1505. aci->aci_type |= ACI_TARGET_ATTR_DEL_FILTERS;
  1506. dellistptr = s;
  1507. } else {
  1508. return(ACL_SYNTAX_ERR);
  1509. }
  1510. } else if ( aci->aci_type & ACI_TARGET_ATTR_DEL_FILTERS ) {
  1511. if (strcmp(str, "add") == 0) {
  1512. aci->aci_type |= ACI_TARGET_ATTR_ADD_FILTERS;
  1513. addlistptr = s;
  1514. } else {
  1515. return(ACL_SYNTAX_ERR);
  1516. }
  1517. }
  1518. }
  1519. /*
  1520. * addlistptr points to the add filter list.
  1521. * dellistptr points to the del filter list.
  1522. * In both cases the strings have been leading and trailing space
  1523. * stripped.
  1524. * Either may be NULL.
  1525. */
  1526. if (process_filter_list( &aci->targetAttrAddFilters, addlistptr)
  1527. == ACL_SYNTAX_ERR) {
  1528. return( ACL_SYNTAX_ERR);
  1529. }
  1530. if (process_filter_list( &aci->targetAttrDelFilters, dellistptr)
  1531. == ACL_SYNTAX_ERR) {
  1532. return( ACL_SYNTAX_ERR);
  1533. }
  1534. return(0);
  1535. }
  1536. /*
  1537. * We have a list of filters that looks like this:
  1538. * attr1:F1 &&....attrn:Fn
  1539. *
  1540. * We need to put each component into a targetattrfilter component of
  1541. * the array.
  1542. *
  1543. */
  1544. static int process_filter_list( Targetattrfilter ***input_attrFilterArray,
  1545. char * input_str) {
  1546. char *str, *end_attr, *tmp_attr;
  1547. Targetattrfilter *attrfilter = NULL;
  1548. int numattr=0;
  1549. Targetattrfilter **attrFilterArray = NULL;
  1550. str = input_str;
  1551. while (str != 0 && *str != 0) {
  1552. if ((end_attr = strstr(str, "&&")) != NULL) {
  1553. /* skip the two '|' chars */
  1554. auto char *t = end_attr;
  1555. LDAP_UTF8INC(end_attr);
  1556. LDAP_UTF8INC(end_attr);
  1557. *t = 0;
  1558. }
  1559. __acl_strip_trailing_space(str);
  1560. __acl_strip_leading_space(&str);
  1561. /*
  1562. * Here:
  1563. * end_attr points to the next attribute thing.
  1564. *
  1565. * str points to the current one to be processed and it looks like
  1566. * this:
  1567. *
  1568. * attr1:F1
  1569. *
  1570. */
  1571. attrfilter = (Targetattrfilter *) slapi_ch_malloc (sizeof (Targetattrfilter));
  1572. memset (attrfilter, 0, sizeof(Targetattrfilter));
  1573. if ((tmp_attr = strstr( str,":")) != NULL) {
  1574. if ( __acl_init_targetattrfilter( attrfilter, str ) != 0 ) {
  1575. slapi_ch_free((void**)&attrfilter);
  1576. return(ACL_SYNTAX_ERR);
  1577. }
  1578. } else {
  1579. slapi_ch_free((void**)&attrfilter);
  1580. return(ACL_SYNTAX_ERR);
  1581. }
  1582. /*
  1583. * Add the attrfilte to the targetAttrFilter list
  1584. */
  1585. attrFilterArray = (Targetattrfilter **) slapi_ch_realloc (
  1586. (void *) attrFilterArray,
  1587. ((numattr+1)*sizeof(Targetattrfilter *)) );
  1588. attrFilterArray[numattr] = attrfilter;
  1589. numattr++;
  1590. /* Move on to the next attribute in the list */
  1591. str = end_attr;
  1592. }/* while */
  1593. /* NULL terminate the list */
  1594. attrFilterArray = (Targetattrfilter **) slapi_ch_realloc (
  1595. (void *) attrFilterArray,
  1596. ((numattr+1)*sizeof(Targetattrfilter *)) );
  1597. attrFilterArray[numattr] = NULL;
  1598. *input_attrFilterArray = attrFilterArray;
  1599. return 0;
  1600. }
  1601. /*
  1602. * Take str and put it into the attrfilter component.
  1603. *
  1604. * str looks as follows: attr1:F1
  1605. *
  1606. * It has had leading and trailing space stripped.
  1607. */
  1608. static int __acl_init_targetattrfilter( Targetattrfilter *attrfilter,
  1609. char *str ) {
  1610. char *tmp_ptr, *s, *filter_ptr;
  1611. Slapi_Filter *f = NULL;
  1612. s = str;
  1613. /* First grab the attribute name */
  1614. if ( (tmp_ptr = strstr( str, ":")) == NULL ) {
  1615. /* No :, syntax error */
  1616. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1617. "Bad targetattrfilter %s:%s\n",
  1618. str,"Expecting \":\"",0);
  1619. return(ACL_SYNTAX_ERR);
  1620. }
  1621. *tmp_ptr = '\0';
  1622. LDAP_UTF8INC(tmp_ptr);
  1623. __acl_strip_trailing_space(s);
  1624. /* s should be the attribute name-make sure it's non-empty. */
  1625. if ( *s == '\0' ) {
  1626. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1627. "No attribute name in targattrfilters\n",
  1628. 0,0);
  1629. return(ACL_SYNTAX_ERR);
  1630. }
  1631. attrfilter->attr_str = slapi_ch_strdup (s);
  1632. /* Now grab the filter */
  1633. filter_ptr = tmp_ptr;
  1634. __acl_strip_leading_space(&filter_ptr);
  1635. __acl_strip_trailing_space(filter_ptr);
  1636. /* trim dups the string, so we need to free it later if it's not kept. */
  1637. tmp_ptr = __acl_trim_filterstr(filter_ptr);
  1638. if ((f = slapi_str2filter(tmp_ptr)) == NULL) {
  1639. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1640. "Bad targetattr filter for attribute %s:%s\n",
  1641. attrfilter->attr_str,tmp_ptr,0);
  1642. slapi_ch_free( (void **) &attrfilter->attr_str);
  1643. slapi_ch_free( (void **) &tmp_ptr);
  1644. return(ACL_SYNTAX_ERR);
  1645. }
  1646. /*
  1647. * Here verify that the named attribute is the only one
  1648. * that appears in the filter.
  1649. */
  1650. if (acl_verify_exactly_one_attribute( attrfilter->attr_str, f) !=
  1651. SLAPI_FILTER_SCAN_NOMORE) {
  1652. slapi_log_error(SLAPI_LOG_ACL, plugin_name,
  1653. "Exactly one attribute type per filter allowed in targattrfilters (%s)\n",
  1654. attrfilter->attr_str, 0);
  1655. slapi_ch_free( (void **) &attrfilter->attr_str);
  1656. slapi_ch_free( (void **) &tmp_ptr);
  1657. slapi_filter_free( f, 1 );
  1658. return(ACL_SYNTAX_ERR);
  1659. }
  1660. /* free the tmp_ptr */
  1661. slapi_ch_free( (void **) &tmp_ptr);
  1662. attrfilter->filterStr = slapi_ch_strdup (filter_ptr);
  1663. attrfilter->filter = f;
  1664. return(LDAP_SUCCESS);
  1665. }
  1666. /*
  1667. * Returns 0 if attr_name is the only attribute name to
  1668. * appear in original_filter AND it appears at least once.
  1669. * Otherwise returns STOP_FILTER_SCAN.
  1670. */
  1671. static int acl_verify_exactly_one_attribute( char *attr_name,
  1672. Slapi_Filter *original_filter) {
  1673. int error_code;
  1674. return( slapi_filter_apply( original_filter, type_compare,
  1675. (void *)attr_name, &error_code));
  1676. }
  1677. static int type_compare( Slapi_Filter *f, void *arg) {
  1678. /* Compare only the base names: eg cn and cn;lang-eb will be the same. */
  1679. char *t = (char *)arg;
  1680. char *filter_type;
  1681. int rc = SLAPI_FILTER_SCAN_STOP;
  1682. if (slapi_filter_get_attribute_type( f, &filter_type) == 0) {
  1683. t = slapi_attr_syntax_normalize(t);
  1684. filter_type = slapi_attr_syntax_normalize(filter_type);
  1685. if (slapi_attr_type_cmp(filter_type, t, 1) == 0) {
  1686. rc = SLAPI_FILTER_SCAN_CONTINUE;
  1687. }
  1688. slapi_ch_free( (void **)&t );
  1689. slapi_ch_free( (void **)&filter_type );
  1690. }
  1691. return rc;
  1692. }