aclparse.c 60 KB

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