aclparse.c 75 KB

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