aclparse.c 53 KB

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