aclparse.c 57 KB

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