aclparse.c 59 KB

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