aclparse.c 66 KB

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