aclparse.c 61 KB

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