aclparse.c 54 KB

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