aclparse.c 58 KB

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