aclparse.c 66 KB

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