aclparse.c 64 KB

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