aclparse.c 55 KB

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