aclparse.c 53 KB

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