1
0

entry.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* entry.c - routines for dealing with entries */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <sys/types.h>
  17. #include <sys/socket.h>
  18. #undef DEBUG /* disable counters */
  19. #include <prcountr.h>
  20. #include "slap.h"
  21. #undef ENTRY_DEBUG
  22. #define DELETED_ATTR_STRING ";deletedattribute"
  23. #define DELETED_ATTR_STRSIZE 17 /* sizeof(";deletedattribute") */
  24. #define DELETED_VALUE_STRING ";deleted"
  25. #define DELETED_VALUE_STRSIZE 8 /* sizeof(";deleted") */
  26. /* a helper function to set special rdn to a tombstone entry */
  27. static int _entry_set_tombstone_rdn(Slapi_Entry *e, const char *normdn);
  28. /* computation of the size of the vattr in the entry */
  29. #define VATTR_READ_LOCK(e) slapi_rwlock_rdlock(e->e_virtual_lock)
  30. #define VATTR_READ_UNLOCK(e) slapi_rwlock_unlock(e->e_virtual_lock)
  31. #define VATTR_WRITE_LOCK(e) slapi_rwlock_wrlock(e->e_virtual_lock)
  32. #define VATTR_WRITE_UNLOCK(e) slapi_rwlock_unlock(e->e_virtual_lock)
  33. static size_t entry_vattr_size(Slapi_Entry *e);
  34. static struct _entry_vattr *entry_vattr_lookup_nolock(const Slapi_Entry *e, const char *attr_name);
  35. static void entry_vattr_add_nolock(Slapi_Entry *e, const char *type, Slapi_Attr *attr);
  36. static void entry_vattr_free_nolock(Slapi_Entry *e);
  37. /* protected attributes which are not included in the flattened entry,
  38. * which will be stored in the db. */
  39. static char **protected_attrs_all = NULL;
  40. /*
  41. * add or delete attr to or from protected_attr_all list depending on the flag.
  42. * flag: 0 -- add
  43. * 1 -- delete
  44. */
  45. void
  46. set_attr_to_protected_list(char *attr, int flag)
  47. {
  48. if (charray_inlist(protected_attrs_all, attr)) { /* attr is in the list */
  49. if (flag) { /* delete */
  50. charray_remove(protected_attrs_all, attr, 1);
  51. }
  52. } else { /* attr is not in the list */
  53. if (!flag) { /* add */
  54. charray_add(&protected_attrs_all, slapi_ch_strdup(attr));
  55. }
  56. }
  57. }
  58. #if defined(USE_OLD_UNHASHED)
  59. static char *forbidden_attrs [] = {PSEUDO_ATTR_UNHASHEDUSERPASSWORD,
  60. NULL};
  61. #endif
  62. /* Attributes which are put into the entry extension */
  63. struct attrs_in_extension attrs_in_extension[] =
  64. {
  65. {PSEUDO_ATTR_UNHASHEDUSERPASSWORD,
  66. slapi_pw_get_entry_ext,
  67. slapi_pw_set_entry_ext,
  68. pw_copy_entry_ext,
  69. pw_get_ext_size},
  70. {NULL, NULL, NULL, NULL, NULL}
  71. };
  72. /* Structure used to store the virtual attribute cache in each entry
  73. * If 'attr' is not NULL, the name of the attribute is taken from attr->a_type and so
  74. * attrname is set to NULL.
  75. * If 'attr' is NULL, the name of the attribute is stored in attrname
  76. */
  77. struct _entry_vattr {
  78. char *attrname; /* if NULL, the attribute name is the one in attr->a_type */
  79. Slapi_Attr *attr; /* attribute computed by a SP */
  80. struct _entry_vattr *next;
  81. };
  82. /*
  83. * An attribute name is of the form 'basename[;option]'.
  84. * The state informaion is encoded in options. For example:
  85. *
  86. * telephonenumber;vucsn-011111111222233334444: 1 650 937 5739
  87. *
  88. * This function strips out the csn options, leaving behind a
  89. * type with any non-csn options left intact.
  90. */
  91. /*
  92. * WARNING: s gets butchered... the base type remains.
  93. */
  94. static void
  95. str2entry_state_information_from_type(struct berval *atype,
  96. CSNSet **csnset,
  97. CSN **attributedeletioncsn,
  98. CSN **maxcsn,
  99. int *value_state,
  100. int *attr_state)
  101. {
  102. char *p = NULL;
  103. char *semicolonp = NULL;
  104. if ((NULL == atype) || (NULL == atype->bv_val)) {
  105. return;
  106. }
  107. p = PL_strchr(atype->bv_val, ';');
  108. *value_state= VALUE_PRESENT;
  109. *attr_state= ATTRIBUTE_PRESENT;
  110. while(p!=NULL)
  111. {
  112. if(p[3]=='c' && p[4]=='s' && p[5]=='n' && p[6]=='-')
  113. {
  114. CSNType t= CSN_TYPE_UNKNOWN;
  115. if(p[1]=='x' && p[2]=='1')
  116. {
  117. t= CSN_TYPE_UNKNOWN;
  118. }
  119. if(p[1]=='x' && p[2]=='2')
  120. {
  121. t= CSN_TYPE_NONE;
  122. }
  123. if(p[1]=='a' && p[2]=='d')
  124. {
  125. t= CSN_TYPE_ATTRIBUTE_DELETED;
  126. }
  127. if(p[1]=='v' && p[2]=='u')
  128. {
  129. t= CSN_TYPE_VALUE_UPDATED;
  130. }
  131. if(p[1]=='v' && p[2]=='d')
  132. {
  133. t= CSN_TYPE_VALUE_DELETED;
  134. }
  135. if(p[1]=='m' && p[2]=='d')
  136. {
  137. t= CSN_TYPE_VALUE_DISTINGUISHED;
  138. }
  139. p[0]='\0';
  140. if(t!=CSN_TYPE_ATTRIBUTE_DELETED)
  141. {
  142. CSN csn;
  143. csn_init_by_string(&csn,p+7);
  144. csnset_add_csn(csnset,t,&csn);
  145. if ( *maxcsn == NULL )
  146. {
  147. *maxcsn = csn_dup ( &csn );
  148. }
  149. else if ( csn_compare (*maxcsn, &csn) < 0 )
  150. {
  151. csn_init_by_csn ( *maxcsn, &csn );
  152. }
  153. }
  154. else
  155. {
  156. *attributedeletioncsn= csn_new_by_string(p+7);
  157. if ( *maxcsn == NULL )
  158. {
  159. *maxcsn = csn_dup ( *attributedeletioncsn );
  160. }
  161. else if ( csn_compare (*maxcsn, *attributedeletioncsn) < 0 )
  162. {
  163. csn_init_by_csn ( *maxcsn, *attributedeletioncsn );
  164. }
  165. }
  166. if (NULL == semicolonp) {
  167. semicolonp = p; /* the first semicolon */
  168. }
  169. }
  170. else if(strncmp(p+1,"deletedattribute", 16)==0)
  171. {
  172. p[0]='\0';
  173. *attr_state= ATTRIBUTE_DELETED;
  174. if (NULL == semicolonp) {
  175. semicolonp = p; /* the first semicolon */
  176. }
  177. }
  178. else if(strncmp(p+1,"deleted", 7)==0)
  179. {
  180. p[0]='\0';
  181. *value_state= VALUE_DELETED;
  182. if (NULL == semicolonp) {
  183. semicolonp = p; /* the first semicolon */
  184. }
  185. }
  186. p= strchr(p+1, ';');
  187. }
  188. if (semicolonp) {
  189. atype->bv_len = semicolonp - atype->bv_val;
  190. }
  191. }
  192. /* rawdn is not consumed. Caller needs to free it. */
  193. static Slapi_Entry *
  194. str2entry_fast( const char *rawdn, const Slapi_RDN *srdn, char *s, int flags, int read_stateinfo )
  195. {
  196. Slapi_Entry *e;
  197. char *next, *ptype=NULL;
  198. int nvals= 0;
  199. int del_nvals= 0;
  200. unsigned long attr_val_cnt = 0;
  201. CSN *attributedeletioncsn= NULL; /* Moved to this level so that the JCM csn_free call below gets useful */
  202. CSNSet *valuecsnset= NULL; /* Moved to this level so that the JCM csn_free call below gets useful */
  203. CSN *maxcsn = NULL;
  204. char *normdn = NULL;
  205. Slapi_Attr **a = NULL;
  206. #ifdef OBSOLETE_DN_SYNTAX_CHECK
  207. int strict = 0;
  208. /* Check if we should be performing strict validation. */
  209. strict = config_get_dn_validate_strict();
  210. #endif
  211. /*
  212. * In string format, an entry looks like either of these:
  213. *
  214. * dn: <dn>\n
  215. * [<attr>:[:] <value>\n]
  216. * [<tab><continuedvalue>\n]*
  217. * ...
  218. *
  219. * rdn: <rdn>\n
  220. * [<attr>:[:] <value>\n]
  221. * [<tab><continuedvalue>\n]*
  222. * ...
  223. *
  224. * If a double colon is used after a type, it means the
  225. * following value is encoded as a base 64 string. This
  226. * happens if the value contains a non-printing character
  227. * or newline.
  228. *
  229. * In case an entry starts with rdn:, dn must be provided.
  230. */
  231. LDAPDebug( LDAP_DEBUG_TRACE, "=> str2entry_fast\n", 0, 0, 0 );
  232. e = slapi_entry_alloc();
  233. slapi_entry_init(e,NULL,NULL);
  234. /* dn|rdn + attributes */
  235. next = s;
  236. /* get the read lock of name2asi for performance purpose.
  237. It reduces read locking by per-entry lock, instead of per-attribute.
  238. */
  239. /* attr_syntax_read_lock();
  240. * no longer needed since attr syntax is not initialized
  241. */
  242. while ( (s = ldif_getline( &next )) != NULL &&
  243. attr_val_cnt < ENTRY_MAX_ATTRIBUTE_VALUE_COUNT )
  244. {
  245. struct berval type = {0, NULL};
  246. struct berval value = {0, NULL};
  247. int freeval = 0;
  248. int value_state= VALUE_NOTFOUND;
  249. int attr_state= ATTRIBUTE_NOTFOUND;
  250. if ( *s == '\n' || *s == '\0' ) {
  251. break;
  252. }
  253. if ( slapi_ldif_parse_line( s, &type, &value, &freeval ) < 0 ) {
  254. LDAPDebug0Args(LDAP_DEBUG_TRACE, "<= str2entry_fast NULL (parse_line)\n");
  255. continue;
  256. }
  257. /*
  258. * Extract the attribute and value CSNs from the attribute type.
  259. */
  260. csn_free(&attributedeletioncsn); /* JCM - Do this more efficiently */
  261. csnset_free(&valuecsnset);
  262. value_state= VALUE_NOTFOUND;
  263. attr_state= ATTRIBUTE_NOTFOUND;
  264. str2entry_state_information_from_type(&type,
  265. &valuecsnset, &attributedeletioncsn,
  266. &maxcsn, &value_state, &attr_state);
  267. if(!read_stateinfo)
  268. {
  269. /* We are not maintaining state information */
  270. if(value_state==VALUE_DELETED || attr_state==ATTRIBUTE_DELETED)
  271. {
  272. /* ignore deleted values and attributes */
  273. /* the memory below was not allocated by the slapi_ch_ functions */
  274. if (freeval) slapi_ch_free_string(&value.bv_val);
  275. continue;
  276. }
  277. /* Ignore CSNs */
  278. csn_free(&attributedeletioncsn);
  279. csnset_free(&valuecsnset);
  280. }
  281. /*
  282. * We cache some stuff as we go around the loop.
  283. */
  284. if((ptype==NULL)||(PL_strcasecmp(type.bv_val,ptype) != 0))
  285. {
  286. slapi_ch_free_string(&ptype);
  287. ptype=PL_strndup(type.bv_val, type.bv_len);
  288. nvals = 0;
  289. del_nvals = 0;
  290. a = NULL;
  291. }
  292. if ( rawdn ) {
  293. if ( NULL == slapi_entry_get_dn_const( e )) {
  294. if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
  295. normdn =
  296. slapi_dn_normalize_original(slapi_ch_strdup(rawdn));
  297. } else {
  298. if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
  299. normdn = slapi_ch_strdup(rawdn);
  300. } else {
  301. normdn = slapi_create_dn_string("%s", rawdn);
  302. if (NULL == normdn) {
  303. LDAPDebug1Arg(LDAP_DEBUG_TRACE,
  304. "str2entry_fast: Invalid DN: %s\n", rawdn);
  305. slapi_entry_free( e );
  306. if (freeval) slapi_ch_free_string(&value.bv_val);
  307. e = NULL;
  308. goto done;
  309. }
  310. }
  311. }
  312. /* normdn is consumed in e */
  313. slapi_entry_set_normdn(e, normdn);
  314. }
  315. if ( NULL == slapi_entry_get_rdn_const( e )) {
  316. if (srdn) {
  317. /* we can use the rdn generated in entryrdn_lookup_dn */
  318. slapi_entry_set_srdn ( e, srdn );
  319. }else if (normdn) {
  320. /* normdn is just referred in slapi_entry_set_rdn. */
  321. slapi_entry_set_rdn(e, normdn);
  322. } else {
  323. if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
  324. normdn =
  325. slapi_dn_normalize_original(slapi_ch_strdup(rawdn));
  326. } else {
  327. if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
  328. normdn = slapi_ch_strdup(rawdn);
  329. } else {
  330. normdn = slapi_create_dn_string("%s", rawdn);
  331. if (NULL == normdn) {
  332. LDAPDebug1Arg(LDAP_DEBUG_TRACE,
  333. "str2entry_fast: Invalid DN: %s\n", rawdn);
  334. slapi_entry_free( e );
  335. if (freeval)
  336. slapi_ch_free_string(&value.bv_val);
  337. e = NULL;
  338. goto done;
  339. }
  340. }
  341. }
  342. /* normdn is just referred in slapi_entry_set_rdn. */
  343. slapi_entry_set_rdn(e, normdn);
  344. slapi_ch_free_string(&normdn);
  345. }
  346. }
  347. rawdn = NULL; /* Set once in the loop.
  348. This won't affect the caller's passed address. */
  349. }
  350. if ( type.bv_len == SLAPI_ATTR_DN_LENGTH && PL_strncasecmp( type.bv_val, SLAPI_ATTR_DN, type.bv_len ) == 0 ) {
  351. if ( slapi_entry_get_dn_const(e)!=NULL ) {
  352. char ebuf[ BUFSIZ ];
  353. LDAPDebug( LDAP_DEBUG_TRACE,
  354. "str2entry_fast: entry has multiple dns \"%s\" and "
  355. "\"%s\" (second ignored)\n",
  356. slapi_entry_get_dn_const(e),
  357. escape_string( value.bv_val, ebuf ), 0 );
  358. /* the memory below was not allocated by the slapi_ch_ functions */
  359. if (freeval) slapi_ch_free_string(&value.bv_val);
  360. continue;
  361. }
  362. if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
  363. normdn =
  364. slapi_ch_strdup(slapi_dn_normalize_original(value.bv_val));
  365. } else {
  366. normdn = slapi_create_dn_string("%s", value.bv_val);
  367. }
  368. if (NULL == normdn) {
  369. char ebuf[ BUFSIZ ];
  370. LDAPDebug1Arg(LDAP_DEBUG_TRACE,
  371. "str2entry_fast: Invalid DN: %s\n",
  372. escape_string( value.bv_val, ebuf ));
  373. slapi_entry_free( e );
  374. if (freeval) slapi_ch_free_string(&value.bv_val);
  375. e = NULL;
  376. goto done;
  377. }
  378. /* normdn is consumed in e */
  379. slapi_entry_set_normdn(e, normdn);
  380. /* the memory below was not allocated by the slapi_ch_ functions */
  381. if (freeval) slapi_ch_free_string(&value.bv_val);
  382. continue;
  383. }
  384. if ( type.bv_len == SLAPI_ATTR_RDN_LENGTH && PL_strncasecmp( type.bv_val, SLAPI_ATTR_RDN, type.bv_len ) == 0 ) {
  385. if ( NULL == slapi_entry_get_rdn_const( e )) {
  386. slapi_entry_set_rdn( e, value.bv_val );
  387. }
  388. /* the memory below was not allocated by the slapi_ch_ functions */
  389. if (freeval) slapi_ch_free_string(&value.bv_val);
  390. continue;
  391. }
  392. /* If SLAPI_STR2ENTRY_NO_ENTRYDN is set, skip entrydn */
  393. if ( (flags & SLAPI_STR2ENTRY_NO_ENTRYDN) &&
  394. type.bv_len == SLAPI_ATTR_ENTRYDN_LENGTH && PL_strncasecmp( type.bv_val, SLAPI_ATTR_ENTRYDN, type.bv_len ) == 0 ) {
  395. if (freeval) slapi_ch_free_string(&value.bv_val);
  396. continue;
  397. }
  398. /* retrieve uniqueid */
  399. if ((type.bv_len == SLAPI_ATTR_UNIQUEID_LENGTH) && (PL_strcasecmp (type.bv_val, SLAPI_ATTR_UNIQUEID) == 0)) {
  400. if (e->e_uniqueid != NULL){
  401. LDAPDebug (LDAP_DEBUG_TRACE,
  402. "str2entry_fast: entry has multiple uniqueids %s "
  403. "and %s (second ignored)\n",
  404. e->e_uniqueid, value.bv_val, 0);
  405. }else{
  406. /* name2asi will be locked in slapi_entry_set_uniqueid */
  407. /* attr_syntax_unlock_read(); */
  408. slapi_entry_set_uniqueid (e, PL_strndup(value.bv_val, value.bv_len));
  409. /* attr_syntax_read_lock();*/
  410. }
  411. /* the memory below was not allocated by the slapi_ch_ functions */
  412. if (freeval) slapi_ch_free_string(&value.bv_val);
  413. continue;
  414. }
  415. if (value_state == VALUE_PRESENT && type.bv_len >= SLAPI_ATTR_OBJECTCLASS_LENGTH
  416. && PL_strncasecmp(type.bv_val, SLAPI_ATTR_OBJECTCLASS, type.bv_len) == 0) {
  417. if (value.bv_len >= SLAPI_ATTR_VALUE_SUBENTRY_LENGTH && PL_strncasecmp(value.bv_val,SLAPI_ATTR_VALUE_SUBENTRY,value.bv_len) == 0)
  418. e->e_flags |= SLAPI_ENTRY_LDAPSUBENTRY;
  419. if (value.bv_len >= SLAPI_ATTR_VALUE_TOMBSTONE_LENGTH && PL_strncasecmp(value.bv_val, SLAPI_ATTR_VALUE_TOMBSTONE,value.bv_len) == 0)
  420. e->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
  421. }
  422. {
  423. Slapi_Value *svalue = NULL;
  424. if(a==NULL)
  425. {
  426. switch(attr_state)
  427. {
  428. case ATTRIBUTE_PRESENT:
  429. if(attrlist_append_nosyntax_init(&e->e_attrs, type.bv_val, &a)==0 /* Found */)
  430. {
  431. LDAPDebug (LDAP_DEBUG_ANY, "str2entry_fast: Error. Non-contiguous attribute values for %s\n", type.bv_val, 0, 0);
  432. PR_ASSERT(0);
  433. continue;
  434. }
  435. break;
  436. case ATTRIBUTE_DELETED:
  437. if(attrlist_append_nosyntax_init(&e->e_deleted_attrs, type.bv_val, &a)==0 /* Found */)
  438. {
  439. LDAPDebug (LDAP_DEBUG_ANY, "str2entry_fast: Error. Non-contiguous deleted attribute values for %s\n", type.bv_val, 0, 0);
  440. PR_ASSERT(0);
  441. continue;
  442. }
  443. break;
  444. case ATTRIBUTE_NOTFOUND:
  445. LDAPDebug (LDAP_DEBUG_ANY, "str2entry_fast: Error. Non-contiguous deleted attribute values for %s\n", type.bv_val, 0, 0);
  446. PR_ASSERT(0);
  447. continue;
  448. /* break; ??? */
  449. }
  450. }
  451. /* moved the value setting code here to check Slapi_Attr 'a'
  452. * to retrieve the attribute syntax info */
  453. svalue = value_new(NULL, CSN_TYPE_NONE, NULL);
  454. #ifdef OBSOLETE_DN_SYNTAX_CHECK
  455. if (slapi_attr_is_dn_syntax_attr(*a)) {
  456. int rc = 0;
  457. char *dn_aval = NULL;
  458. if (strict) {
  459. /* check that the dn is formatted correctly */
  460. rc = slapi_dn_syntax_check(NULL, value.bv_val, 1);
  461. if (rc) { /* syntax check failed */
  462. LDAPDebug2Args(LDAP_DEBUG_TRACE,
  463. "str2entry_fast: strict: Invalid DN value: %s: %s\n",
  464. type.bv_val, value.bv_val);
  465. slapi_entry_free( e );
  466. if (freeval) slapi_ch_free_string(&value.bv_val);
  467. e = NULL;
  468. goto done;
  469. }
  470. }
  471. if (flags & SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT) {
  472. dn_aval = slapi_dn_normalize_original(value.bv_val);
  473. slapi_value_set(svalue, dn_aval, strlen(dn_aval));
  474. } else {
  475. Slapi_DN *sdn = slapi_sdn_new_dn_byref(value.bv_val);
  476. /* Note: slapi_sdn_get_dn returns normalized DN with
  477. * case-intact. Thus, the length of dn_aval is
  478. * slapi_sdn_get_ndn_len(sdn). */
  479. dn_aval = (char *)slapi_sdn_get_dn(sdn);
  480. slapi_value_set(svalue, (void *)dn_aval,
  481. slapi_sdn_get_ndn_len(sdn));
  482. slapi_sdn_free(&sdn);
  483. }
  484. } else {
  485. slapi_value_set_berval(svalue, &value);
  486. }
  487. #endif
  488. slapi_value_set_berval(svalue, &value);
  489. /* the memory below was not allocated by the slapi_ch_ functions */
  490. if (freeval) slapi_ch_free_string(&value.bv_val);
  491. svalue->v_csnset = valuecsnset;
  492. valuecsnset = NULL;
  493. {
  494. const CSN *distinguishedcsn= csnset_get_csn_of_type(svalue->v_csnset,CSN_TYPE_VALUE_DISTINGUISHED);
  495. if(distinguishedcsn!=NULL)
  496. {
  497. entry_add_dncsn_ext(e,distinguishedcsn, ENTRY_DNCSN_INCREASING);
  498. }
  499. }
  500. if(value_state==VALUE_DELETED)
  501. {
  502. /* consumes the value */
  503. slapi_valueset_add_attr_value_ext(
  504. *a,
  505. &(*a)->a_deleted_values,
  506. svalue,
  507. SLAPI_VALUE_FLAG_PASSIN );
  508. del_nvals++;
  509. }
  510. else
  511. {
  512. /* consumes the value */
  513. slapi_valueset_add_attr_value_ext(
  514. *a,
  515. &(*a)->a_present_values,
  516. svalue,
  517. SLAPI_VALUE_FLAG_PASSIN);
  518. nvals++;
  519. }
  520. if(attributedeletioncsn!=NULL)
  521. {
  522. attr_set_deletion_csn(*a,attributedeletioncsn);
  523. }
  524. }
  525. csn_free(&attributedeletioncsn);
  526. csnset_free(&valuecsnset);
  527. attr_val_cnt++;
  528. }
  529. slapi_ch_free_string(&ptype);
  530. if ( attr_val_cnt >= ENTRY_MAX_ATTRIBUTE_VALUE_COUNT )
  531. {
  532. LDAPDebug( LDAP_DEBUG_ANY,
  533. "str2entry_fast: entry %s exceeded max attribute value cound %ld\n",
  534. slapi_entry_get_dn_const(e)?slapi_entry_get_dn_const(e):"unknown",
  535. attr_val_cnt, 0 );
  536. }
  537. if (read_stateinfo && maxcsn)
  538. {
  539. e->e_maxcsn = maxcsn;
  540. maxcsn = NULL;
  541. }
  542. /* release read lock of name2asi, per-entry lock */
  543. /* attr_syntax_unlock_read();
  544. * no longer locked since attr syntax is not initialized
  545. */
  546. /* If this is a tombstone, it requires a special treatment for rdn. */
  547. if (e->e_flags & SLAPI_ENTRY_FLAG_TOMBSTONE) {
  548. /* tombstone */
  549. if (_entry_set_tombstone_rdn(e, slapi_entry_get_dn_const(e))) {
  550. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "str2entry_fast: "
  551. "tombstone entry has badly formatted dn: %s\n",
  552. slapi_entry_get_dn_const(e) );
  553. slapi_entry_free( e ); e = NULL;
  554. goto done;
  555. }
  556. }
  557. /* check to make sure there was a dn: line */
  558. if ( slapi_entry_get_dn_const(e)==NULL ) {
  559. if (!(SLAPI_STR2ENTRY_INCLUDE_VERSION_STR & flags))
  560. LDAPDebug( LDAP_DEBUG_ANY, "str2entry_fast: entry has no dn\n",
  561. 0, 0, 0 );
  562. slapi_entry_free( e );
  563. e = NULL;
  564. }
  565. done:
  566. csnset_free(&valuecsnset);
  567. csn_free(&attributedeletioncsn);
  568. csn_free(&maxcsn);
  569. LDAPDebug( LDAP_DEBUG_TRACE, "<= str2entry_fast 0x%x\n",
  570. e, 0, 0 );
  571. return( e );
  572. }
  573. #define STR2ENTRY_SMALL_BUFFER_SIZE 64
  574. #define STR2ENTRY_INITIAL_BERVAL_ARRAY_SIZE 8
  575. #define STR2ENTRY_VALUE_DUPCHECK_THRESHOLD 5
  576. typedef struct _entry_attr_data {
  577. int ead_attrarrayindex;
  578. const char *ead_attrtypename;
  579. char ead_allocated; /* non-zero if this struct needs to be freed */
  580. } entry_attr_data;
  581. /* Structure which stores a tree for the attributes on the entry rather than the linked list on a regular entry struture */
  582. typedef struct _entry_attrs {
  583. Avlnode *ea_attrlist;
  584. int ea_attrdatacount;
  585. entry_attr_data ea_attrdata[ STR2ENTRY_SMALL_BUFFER_SIZE ];
  586. } entry_attrs;
  587. typedef struct _str2entry_attr {
  588. char *sa_type;
  589. int sa_state;
  590. struct slapi_value_set sa_present_values;
  591. struct slapi_value_set sa_deleted_values;
  592. int sa_numdups;
  593. value_compare_fn_type sa_comparefn;
  594. Avlnode *sa_vtree;
  595. CSN *sa_attributedeletioncsn;
  596. Slapi_Attr sa_attr;
  597. } str2entry_attr;
  598. static void
  599. entry_attr_init(str2entry_attr *sa, const char *type, int state)
  600. {
  601. sa->sa_type= slapi_ch_strdup(type);
  602. sa->sa_state= state;
  603. slapi_valueset_init(&sa->sa_present_values);
  604. slapi_valueset_init(&sa->sa_deleted_values);
  605. sa->sa_numdups= 0;
  606. sa->sa_comparefn = NULL;
  607. sa->sa_vtree= NULL;
  608. sa->sa_attributedeletioncsn= NULL;
  609. slapi_attr_init(&sa->sa_attr, type);
  610. }
  611. /*
  612. * Create a tree of attributes.
  613. */
  614. static int
  615. entry_attrs_new(entry_attrs **pea)
  616. {
  617. entry_attrs *tmp = (entry_attrs *)slapi_ch_calloc(1, sizeof(entry_attrs));
  618. if (NULL == tmp) {
  619. return -1;
  620. } else {
  621. *pea = tmp;
  622. return 0;
  623. }
  624. }
  625. /*
  626. * Delete an attribute type tree node.
  627. */
  628. static void
  629. attr_type_node_free( caddr_t data )
  630. {
  631. entry_attr_data *ea = (entry_attr_data *)data;
  632. if ( NULL != ea && ea->ead_allocated ) {
  633. slapi_ch_free( (void **)&ea );
  634. }
  635. }
  636. /*
  637. * Delete a tree of attributes.
  638. */
  639. static void
  640. entry_attrs_delete(entry_attrs **pea)
  641. {
  642. if (NULL != *pea) {
  643. /* Delete the AVL tree */
  644. avl_free((*pea)->ea_attrlist, attr_type_node_free);
  645. slapi_ch_free((void**)pea);
  646. }
  647. }
  648. static int
  649. attr_type_node_cmp( caddr_t d1, caddr_t d2 )
  650. {
  651. /*
  652. * A simple strcasecmp() will do here because we do not care
  653. * about subtypes, etc. The slapi_str2entry() function treats
  654. * subtypes as distinct attribute types, because that is how
  655. * they are stored within the Slapi_Entry structure.
  656. */
  657. entry_attr_data *ea1= (entry_attr_data *)d1;
  658. entry_attr_data *ea2= (entry_attr_data *)d2;
  659. PR_ASSERT( ea1 != NULL );
  660. PR_ASSERT( ea1->ead_attrtypename != NULL );
  661. PR_ASSERT( ea2 != NULL );
  662. PR_ASSERT( ea2->ead_attrtypename != NULL );
  663. return strcasecmp(ea1->ead_attrtypename,ea2->ead_attrtypename);
  664. }
  665. /*
  666. * Adds a new attribute to the attribute tree.
  667. */
  668. static void
  669. entry_attrs_add(entry_attrs *ea, const char *atname, int atarrayindex)
  670. {
  671. entry_attr_data *ead;
  672. if ( ea->ea_attrdatacount < STR2ENTRY_SMALL_BUFFER_SIZE ) {
  673. ead = &(ea->ea_attrdata[ ea->ea_attrdatacount ]);
  674. ead->ead_allocated = 0;
  675. } else {
  676. ead = (entry_attr_data *)slapi_ch_malloc( sizeof( entry_attr_data ));
  677. ead->ead_allocated = 1;
  678. }
  679. ++ea->ea_attrdatacount;
  680. ead->ead_attrarrayindex = atarrayindex;
  681. ead->ead_attrtypename = atname; /* a reference, not a strdup! */
  682. avl_insert( &(ea->ea_attrlist), ead, attr_type_node_cmp, avl_dup_error );
  683. }
  684. /*
  685. * Checks for an attribute in the tree. Returns the attr array index or -1
  686. * if not found;
  687. */
  688. static int
  689. entry_attrs_find(entry_attrs *ea,char *type)
  690. {
  691. entry_attr_data tmpead = {0};
  692. entry_attr_data *foundead;
  693. tmpead.ead_attrtypename = type;
  694. foundead = (entry_attr_data *)avl_find( ea->ea_attrlist, &tmpead,
  695. attr_type_node_cmp );
  696. return ( NULL != foundead ) ? foundead->ead_attrarrayindex : -1;
  697. }
  698. /* What's going on here then ?
  699. Well, originally duplicate value checking was done by taking each
  700. new value and comparing in turn against all the previous values.
  701. Needless to say this was costly when there were many values.
  702. So, new code was written which built a binary tree of index keys
  703. for the values, and the test was done against the tree.
  704. Nothing wrong with this, it speeded up the case where there were
  705. many values nicely.
  706. Unfortunately, when there are few values, it proved to be a significent
  707. performance sink.
  708. So, now we check the old way up 'till there's 5 attribute values, then
  709. switch to the tree-based scheme.
  710. Note that duplicate values are only checked for and ignored
  711. if flags contains SLAPI_STR2ENTRY_REMOVEDUPVALS.
  712. */
  713. /* dn is not consumed. Caller needs to free it. */
  714. static Slapi_Entry *
  715. str2entry_dupcheck( const char *rawdn, char *s, int flags, int read_stateinfo )
  716. {
  717. Slapi_Entry *e;
  718. str2entry_attr stack_attrs[STR2ENTRY_SMALL_BUFFER_SIZE];
  719. str2entry_attr *dyn_attrs = NULL;
  720. str2entry_attr *attrs = stack_attrs;
  721. str2entry_attr *prev_attr= NULL;
  722. int nattrs;
  723. int maxattrs = STR2ENTRY_SMALL_BUFFER_SIZE;
  724. char *type;
  725. struct berval bvtype;
  726. str2entry_attr *sa;
  727. int i;
  728. char *next=NULL;
  729. char *valuecharptr=NULL;
  730. struct berval bvvalue;
  731. int rc;
  732. entry_attrs *ea = NULL;
  733. int tree_attr_checking = 0;
  734. int big_entry_attr_presence_check = 0;
  735. int check_for_duplicate_values = ( 0 != ( flags & SLAPI_STR2ENTRY_REMOVEDUPVALS ));
  736. Slapi_Value *value = 0;
  737. CSN *attributedeletioncsn= NULL;
  738. CSNSet *valuecsnset= NULL;
  739. CSN *maxcsn= NULL;
  740. char *normdn = NULL;
  741. int strict = 0;
  742. /* Check if we should be performing strict validation. */
  743. strict = config_get_dn_validate_strict();
  744. LDAPDebug0Args(LDAP_DEBUG_TRACE, "=> str2entry_dupcheck\n");
  745. e = slapi_entry_alloc();
  746. slapi_entry_init(e,NULL,NULL);
  747. next = s;
  748. nattrs = 0;
  749. if (flags & SLAPI_STR2ENTRY_BIGENTRY)
  750. {
  751. big_entry_attr_presence_check = 1;
  752. }
  753. while ( (s = ldif_getline( &next )) != NULL )
  754. {
  755. int value_state= VALUE_NOTFOUND;
  756. int attr_state= VALUE_NOTFOUND;
  757. int freeval = 0;
  758. struct berval bv_null = {0, NULL};
  759. csn_free(&attributedeletioncsn);
  760. if ( *s == '\n' || *s == '\0' ) {
  761. break;
  762. }
  763. bvtype = bv_null;
  764. bvvalue = bv_null;
  765. if ( slapi_ldif_parse_line( s, &bvtype, &bvvalue, &freeval ) < 0 ) {
  766. LDAPDebug(LDAP_DEBUG_ANY,
  767. "Warning: Entry (%s), ignoring invalid line \"%s\"...\n",
  768. rawdn ? rawdn : "", s, 0);
  769. continue;
  770. }
  771. type = bvtype.bv_val;
  772. valuecharptr = bvvalue.bv_val;
  773. /*
  774. * Extract the attribute and value CSNs from the attribute type.
  775. */
  776. csnset_free(&valuecsnset);
  777. value_state= VALUE_NOTFOUND;
  778. attr_state= VALUE_NOTFOUND;
  779. str2entry_state_information_from_type(&bvtype,
  780. &valuecsnset, &attributedeletioncsn,
  781. &maxcsn, &value_state, &attr_state);
  782. if(!read_stateinfo)
  783. {
  784. /* We are not maintaining state information */
  785. if(value_state==VALUE_DELETED || attr_state==ATTRIBUTE_DELETED)
  786. {
  787. /* ignore deleted values and attributes */
  788. /* the memory below was not allocated by the slapi_ch_ functions */
  789. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  790. continue;
  791. }
  792. /* Ignore CSNs */
  793. csn_free(&attributedeletioncsn);
  794. csnset_free(&valuecsnset);
  795. }
  796. if ( rawdn ) {
  797. if ( NULL == slapi_entry_get_dn_const(e) ) {
  798. if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
  799. normdn = slapi_ch_strdup(rawdn);
  800. } else {
  801. normdn = slapi_create_dn_string("%s", rawdn);
  802. if (NULL == normdn) {
  803. LDAPDebug1Arg(LDAP_DEBUG_TRACE,
  804. "str2entry_dupcheck: Invalid DN: %s\n", rawdn);
  805. slapi_entry_free( e );
  806. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  807. csnset_free(&valuecsnset);
  808. csn_free(&attributedeletioncsn);
  809. csn_free(&maxcsn);
  810. return NULL;
  811. }
  812. }
  813. /* normdn is consumed in e */
  814. slapi_entry_set_normdn(e, normdn);
  815. }
  816. if ( NULL == slapi_entry_get_rdn_const(e) ) {
  817. if (normdn) {
  818. /* normdn is just referred in slapi_entry_set_rdn. */
  819. slapi_entry_set_rdn(e, normdn);
  820. } else {
  821. if (flags & SLAPI_STR2ENTRY_DN_NORMALIZED) {
  822. normdn = slapi_ch_strdup(rawdn);
  823. } else {
  824. normdn = slapi_create_dn_string("%s", rawdn);
  825. if (NULL == normdn) {
  826. LDAPDebug1Arg(LDAP_DEBUG_TRACE,
  827. "str2entry_dupcheck: Invalid DN: %s\n", rawdn);
  828. slapi_entry_free( e );
  829. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  830. csnset_free(&valuecsnset);
  831. csn_free(&attributedeletioncsn);
  832. csn_free(&maxcsn);
  833. return NULL;
  834. }
  835. }
  836. /* normdn is just referred in slapi_entry_set_rdn. */
  837. slapi_entry_set_rdn(e, normdn);
  838. slapi_ch_free_string(&normdn);
  839. }
  840. }
  841. rawdn = NULL; /* Set once in the loop.
  842. This won't affect the caller's passed address. */
  843. }
  844. if ( strcasecmp( type, "dn" ) == 0 ) {
  845. if ( slapi_entry_get_dn_const(e)!=NULL ) {
  846. char ebuf[ BUFSIZ ];
  847. LDAPDebug( LDAP_DEBUG_TRACE,
  848. "str2entry_dupcheck: entry has multiple dns \"%s\" "
  849. "and \"%s\" (second ignored)\n",
  850. slapi_entry_get_dn_const(e),
  851. escape_string( valuecharptr, ebuf ), 0 );
  852. /* the memory below was not allocated by the slapi_ch_ functions */
  853. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  854. continue;
  855. }
  856. normdn = slapi_create_dn_string("%s", valuecharptr);
  857. if (NULL == normdn) {
  858. LDAPDebug1Arg(LDAP_DEBUG_TRACE,
  859. "str2entry_dupcheck: Invalid DN: %s\n", valuecharptr);
  860. slapi_entry_free( e ); e = NULL;
  861. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  862. goto free_and_return;
  863. }
  864. /* normdn is consumed in e */
  865. slapi_entry_set_normdn(e, normdn);
  866. /* the memory below was not allocated by the slapi_ch_ functions */
  867. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  868. continue;
  869. }
  870. if ( strcasecmp( type, "rdn" ) == 0 ) {
  871. if ( NULL == slapi_entry_get_rdn_const( e )) {
  872. slapi_entry_set_rdn( e, valuecharptr );
  873. }
  874. /* the memory below was not allocated by the slapi_ch_ functions */
  875. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  876. continue;
  877. }
  878. /* If SLAPI_STR2ENTRY_NO_ENTRYDN is set, skip entrydn */
  879. if ( (flags & SLAPI_STR2ENTRY_NO_ENTRYDN) &&
  880. strcasecmp( type, "entrydn" ) == 0 ) {
  881. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  882. continue;
  883. }
  884. /* retrieve uniqueid */
  885. if ((bvtype.bv_len == SLAPI_ATTR_UNIQUEID_LENGTH) && (PL_strcasecmp (type, SLAPI_ATTR_UNIQUEID) == 0)) {
  886. if (e->e_uniqueid != NULL){
  887. LDAPDebug (LDAP_DEBUG_TRACE,
  888. "str2entry_dupcheck: entry has multiple uniqueids %s "
  889. "and %s (second ignored)\n",
  890. e->e_uniqueid, valuecharptr, 0);
  891. }else{
  892. slapi_entry_set_uniqueid (e, slapi_ch_strdup(valuecharptr));
  893. }
  894. /* the memory below was not allocated by the slapi_ch_ functions */
  895. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  896. continue;
  897. }
  898. if (strcasecmp(type,"objectclass") == 0) {
  899. if (strcasecmp(valuecharptr,"ldapsubentry") == 0)
  900. e->e_flags |= SLAPI_ENTRY_LDAPSUBENTRY;
  901. if (strcasecmp(valuecharptr, SLAPI_ATTR_VALUE_TOMBSTONE) == 0)
  902. e->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
  903. }
  904. /* Here we have a quick look to see if this attribute is a new
  905. value for the type we last processed or a new type.
  906. If not, we look to see if we've seen this attribute type before.
  907. */
  908. if ( prev_attr!=NULL && strcasecmp( type, prev_attr->sa_type ) != 0 )
  909. {
  910. /* Different attribute type - find it, or alloc new */
  911. prev_attr = NULL;
  912. /* The linear check below can take a while, so we change to use a tree if there are many attrs */
  913. if (!big_entry_attr_presence_check)
  914. {
  915. for ( i = 0; i < nattrs; i++ )
  916. {
  917. if (strcasecmp( type, attrs[i].sa_type ) == 0 )
  918. {
  919. prev_attr = &attrs[i];
  920. break;
  921. }
  922. }
  923. }
  924. else
  925. {
  926. int prev_index;
  927. /* Did we just switch checking mechanism ? */
  928. if (!tree_attr_checking)
  929. {
  930. /* If so then put the exising attrs into the tree */
  931. if (0 != entry_attrs_new(&ea))
  932. {
  933. /* Something very bad happened */
  934. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  935. csn_free(&attributedeletioncsn);
  936. csn_free(&maxcsn);
  937. csnset_free(&valuecsnset);
  938. return NULL;
  939. }
  940. for ( i = 0; i < nattrs; i++ )
  941. {
  942. entry_attrs_add(ea,attrs[i].sa_type, i);
  943. }
  944. tree_attr_checking = 1;
  945. }
  946. prev_index = entry_attrs_find(ea,type);
  947. if ( prev_index >= 0 ) {
  948. prev_attr = &attrs[prev_index];
  949. /* (prev_attr!=NULL) Means that we already had that one in the set */
  950. }
  951. }
  952. }
  953. if ( prev_attr==NULL )
  954. {
  955. /* Haven't seen this type yet */
  956. if ( nattrs == maxattrs )
  957. {
  958. /* Out of space - reallocate */
  959. maxattrs *= 2;
  960. if ( nattrs == STR2ENTRY_SMALL_BUFFER_SIZE ) {
  961. /* out of fixed space - switch to dynamic */
  962. PR_ASSERT( dyn_attrs == NULL );
  963. dyn_attrs = (str2entry_attr *)
  964. slapi_ch_malloc( sizeof( str2entry_attr ) *
  965. maxattrs );
  966. memcpy( dyn_attrs, stack_attrs,
  967. STR2ENTRY_SMALL_BUFFER_SIZE *
  968. sizeof( str2entry_attr ));
  969. attrs = dyn_attrs;
  970. } else {
  971. /* Need more dynamic space */
  972. dyn_attrs = (str2entry_attr *)
  973. slapi_ch_realloc( (char *) dyn_attrs,
  974. sizeof( str2entry_attr ) * maxattrs );
  975. attrs = dyn_attrs; /* realloc may change base pointer */
  976. }
  977. }
  978. /* Record the new type in the array */
  979. entry_attr_init(&attrs[nattrs], type, attr_state);
  980. if ( check_for_duplicate_values )
  981. {
  982. /* Get the comparison function for later use */
  983. attr_get_value_cmp_fn( &attrs[nattrs].sa_attr, &(attrs[nattrs].sa_comparefn));
  984. /*
  985. * If we are maintaining the attribute tree,
  986. * then add the new attribute to the tree.
  987. */
  988. if (big_entry_attr_presence_check && tree_attr_checking)
  989. {
  990. entry_attrs_add(ea,attrs[nattrs].sa_type, nattrs);
  991. }
  992. }
  993. prev_attr = &attrs[nattrs];
  994. nattrs++;
  995. } else { /* prev_attr != NULL */
  996. }
  997. sa = prev_attr; /* For readability */
  998. value= value_new(NULL, CSN_TYPE_NONE, NULL);
  999. if (slapi_attr_is_dn_syntax_attr(&(sa->sa_attr))) {
  1000. Slapi_DN *sdn = NULL;
  1001. const char *dn_aval = NULL;
  1002. if (strict) {
  1003. /* check that the dn is formatted correctly */
  1004. rc = slapi_dn_syntax_check(NULL, valuecharptr, 1);
  1005. if (rc) { /* syntax check failed */
  1006. LDAPDebug2Args(LDAP_DEBUG_ANY,
  1007. "str2entry_dupcheck: strict: Invalid DN value: %s: %s\n",
  1008. type, valuecharptr);
  1009. slapi_entry_free( e ); e = NULL;
  1010. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  1011. goto free_and_return;
  1012. }
  1013. }
  1014. sdn = slapi_sdn_new_dn_byref(bvvalue.bv_val);
  1015. /* Note: slapi_sdn_get_dn returns the normalized DN
  1016. * with case-intact. Thus, the length of dn_aval is
  1017. * slapi_sdn_get_ndn_len(sdn). */
  1018. dn_aval = slapi_sdn_get_dn(sdn);
  1019. slapi_value_set(value, (void *)dn_aval, slapi_sdn_get_ndn_len(sdn));
  1020. slapi_sdn_free(&sdn);
  1021. } else {
  1022. slapi_value_set_berval(value, &bvvalue);
  1023. }
  1024. /* the memory below was not allocated by the slapi_ch_ functions */
  1025. if (freeval) slapi_ch_free_string(&bvvalue.bv_val);
  1026. value->v_csnset= valuecsnset;
  1027. valuecsnset= NULL;
  1028. {
  1029. const CSN *distinguishedcsn= csnset_get_csn_of_type(value->v_csnset,CSN_TYPE_VALUE_DISTINGUISHED);
  1030. if(distinguishedcsn!=NULL)
  1031. {
  1032. entry_add_dncsn(e,distinguishedcsn);
  1033. }
  1034. }
  1035. if(value_state==VALUE_DELETED)
  1036. {
  1037. /*
  1038. * for deleted values, we do not want to perform a dupcheck against
  1039. * existing values.
  1040. */
  1041. rc = slapi_valueset_add_attr_value_ext(&sa->sa_attr, &sa->sa_deleted_values,value, SLAPI_VALUE_FLAG_PASSIN);
  1042. }
  1043. else
  1044. {
  1045. int flags = SLAPI_VALUE_FLAG_PASSIN;
  1046. if (check_for_duplicate_values) flags |= SLAPI_VALUE_FLAG_DUPCHECK;
  1047. rc = slapi_valueset_add_attr_value_ext(&sa->sa_attr, &sa->sa_present_values,value, flags);
  1048. }
  1049. if ( rc==LDAP_SUCCESS )
  1050. {
  1051. value= NULL; /* value was consumed */
  1052. if(attributedeletioncsn!=NULL)
  1053. {
  1054. sa->sa_attributedeletioncsn= attributedeletioncsn;
  1055. attributedeletioncsn= NULL; /* csn was consumed */
  1056. }
  1057. }
  1058. else if (rc==LDAP_TYPE_OR_VALUE_EXISTS)
  1059. {
  1060. sa->sa_numdups++;
  1061. csn_free(&attributedeletioncsn);
  1062. }
  1063. else
  1064. {
  1065. /* Failure adding to value tree */
  1066. LDAPDebug( LDAP_DEBUG_ANY, "str2entry_dupcheck: unexpected failure %d adding value\n", rc, 0, 0 );
  1067. slapi_value_free(&value); /* value not consumed - free it */
  1068. slapi_entry_free( e ); e = NULL;
  1069. goto free_and_return;
  1070. }
  1071. slapi_value_free(&value); /* if rc is error, value was not consumed - free it */
  1072. }
  1073. /* All done with parsing. Now create the entry. */
  1074. /* check to make sure there was a dn: line */
  1075. if ( slapi_entry_get_dn_const(e)==NULL )
  1076. {
  1077. if (!(SLAPI_STR2ENTRY_INCLUDE_VERSION_STR & flags))
  1078. LDAPDebug( LDAP_DEBUG_ANY, "str2entry_dupcheck: entry has no dn\n",
  1079. 0, 0, 0 );
  1080. slapi_entry_free( e ); e = NULL;
  1081. goto free_and_return;
  1082. }
  1083. /* get the read lock of name2asi for performance purpose.
  1084. It reduces read locking by per-entry lock, instead of per-attribute.
  1085. */
  1086. attr_syntax_read_lock();
  1087. /*
  1088. * For each unique attribute in the array,
  1089. * Create a Slapi_Attr and set it's present and deleted values.
  1090. */
  1091. for ( i = 0; i < nattrs; i++ )
  1092. {
  1093. sa = &attrs[i];
  1094. if ( sa->sa_numdups > 0 )
  1095. {
  1096. if ( sa->sa_numdups > 1 ) {
  1097. LDAPDebug( LDAP_DEBUG_ANY, "str2entry_dupcheck: %d duplicate values for attribute "
  1098. "type %s detected in entry %s. Extra values ignored.\n",
  1099. sa->sa_numdups, sa->sa_type, slapi_entry_get_dn_const(e) );
  1100. } else {
  1101. LDAPDebug( LDAP_DEBUG_ANY, "str2entry_dupcheck: Duplicate value for attribute "
  1102. "type %s detected in entry %s. Extra value ignored.\n",
  1103. sa->sa_type, slapi_entry_get_dn_const(e), 0 );
  1104. }
  1105. }
  1106. {
  1107. Slapi_Attr **alist= NULL;
  1108. if(sa->sa_state==ATTRIBUTE_DELETED)
  1109. {
  1110. if(read_stateinfo)
  1111. {
  1112. alist= &e->e_deleted_attrs;
  1113. }
  1114. else
  1115. {
  1116. /*
  1117. * if we are not maintaining state info,
  1118. * ignore the deleted attributes
  1119. */
  1120. }
  1121. }
  1122. else
  1123. {
  1124. alist= &e->e_attrs;
  1125. }
  1126. if(alist!=NULL)
  1127. {
  1128. Slapi_Attr **a= NULL;
  1129. attrlist_find_or_create_locking_optional(alist, sa->sa_type, &a, PR_FALSE);
  1130. slapi_valueset_add_attr_valuearray_ext(
  1131. *a,
  1132. &(*a)->a_present_values,
  1133. sa->sa_present_values.va,
  1134. sa->sa_present_values.num,
  1135. SLAPI_VALUE_FLAG_PASSIN, NULL);
  1136. sa->sa_present_values.num= 0; /* The values have been consumed */
  1137. slapi_ch_free((void **)&sa->sa_present_values.va);
  1138. slapi_valueset_add_attr_valuearray_ext(
  1139. *a,
  1140. &(*a)->a_deleted_values,
  1141. sa->sa_deleted_values.va,
  1142. sa->sa_deleted_values.num,
  1143. SLAPI_VALUE_FLAG_PASSIN, NULL);
  1144. sa->sa_deleted_values.num= 0; /* The values have been consumed */
  1145. slapi_ch_free((void **)&sa->sa_deleted_values.va);
  1146. if(sa->sa_attributedeletioncsn!=NULL)
  1147. {
  1148. attr_set_deletion_csn(*a,sa->sa_attributedeletioncsn);
  1149. csn_free(&sa->sa_attributedeletioncsn);
  1150. }
  1151. }
  1152. }
  1153. }
  1154. /* release read lock of name2asi, per-entry lock */
  1155. attr_syntax_unlock_read();
  1156. /* If this is a tombstone, it requires a special treatment for rdn. */
  1157. if (e->e_flags & SLAPI_ENTRY_FLAG_TOMBSTONE) {
  1158. /* tombstone */
  1159. if (_entry_set_tombstone_rdn(e, slapi_entry_get_dn_const(e))) {
  1160. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "str2entry_dupcheck: "
  1161. "tombstone entry has badly formatted dn: %s\n",
  1162. slapi_entry_get_dn_const(e) );
  1163. slapi_entry_free( e ); e = NULL;
  1164. goto free_and_return;
  1165. }
  1166. }
  1167. /* Add the RDN values, if asked, and if not already present */
  1168. if ( flags & SLAPI_STR2ENTRY_ADDRDNVALS ) {
  1169. if ( slapi_entry_add_rdn_values( e ) != LDAP_SUCCESS ) {
  1170. LDAPDebug( LDAP_DEBUG_TRACE,
  1171. "str2entry_dupcheck: entry has badly formatted dn\n",
  1172. 0, 0, 0 );
  1173. slapi_entry_free( e ); e = NULL;
  1174. goto free_and_return;
  1175. }
  1176. }
  1177. if (read_stateinfo)
  1178. {
  1179. e->e_maxcsn = maxcsn;
  1180. maxcsn = NULL;
  1181. }
  1182. free_and_return:
  1183. for ( i = 0; i < nattrs; i++ )
  1184. {
  1185. slapi_ch_free((void **) &(attrs[ i ].sa_type));
  1186. slapi_valueset_done(&attrs[ i ].sa_present_values);
  1187. slapi_valueset_done(&attrs[ i ].sa_deleted_values);
  1188. attr_done( &attrs[ i ].sa_attr );
  1189. }
  1190. if (tree_attr_checking)
  1191. {
  1192. entry_attrs_delete(&ea);
  1193. }
  1194. slapi_ch_free((void **) &dyn_attrs );
  1195. if (value) slapi_value_free(&value);
  1196. csnset_free(&valuecsnset);
  1197. csn_free(&attributedeletioncsn);
  1198. csn_free(&maxcsn);
  1199. LDAPDebug( LDAP_DEBUG_TRACE, "<= str2entry_dupcheck 0x%x \"%s\"\n",
  1200. e, slapi_sdn_get_dn (slapi_entry_get_sdn_const(e)), 0 );
  1201. return e;
  1202. }
  1203. /*
  1204. *
  1205. * Convert an entry in LDIF format into a
  1206. * Slapi_Entry structure. If we can assume that the
  1207. * LDIF is well-formed we call str2entry_fast(),
  1208. * which does no error checking.
  1209. * Otherwise we do not assume well-formed LDIF, and
  1210. * call str2entry_dupcheck(), which checks for
  1211. * duplicate attribute values and does not assume
  1212. * that values are all contiguous.
  1213. *
  1214. * Well-formed LDIF has the following characteristics:
  1215. * 1) There are no duplicate attribute values
  1216. * 2) The RDN is an attribute of the entry
  1217. * 3) All values for a given attribute type are
  1218. * contiguous.
  1219. */
  1220. #define SLAPI_STRENTRY_FLAGS_HANDLED_IN_SLAPI_STR2ENTRY \
  1221. ( SLAPI_STR2ENTRY_IGNORE_STATE \
  1222. | SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES \
  1223. | SLAPI_STR2ENTRY_TOMBSTONE_CHECK \
  1224. | SLAPI_STR2ENTRY_USE_OBSOLETE_DNFORMAT \
  1225. | SLAPI_STR2ENTRY_NO_ENTRYDN \
  1226. | SLAPI_STR2ENTRY_DN_NORMALIZED \
  1227. )
  1228. #define SLAPI_STRENTRY_FLAGS_HANDLED_BY_STR2ENTRY_FAST \
  1229. ( SLAPI_STR2ENTRY_INCLUDE_VERSION_STR \
  1230. | SLAPI_STRENTRY_FLAGS_HANDLED_IN_SLAPI_STR2ENTRY \
  1231. )
  1232. /*
  1233. * If well-formed LDIF has not been provided OR if a flag that is
  1234. * not handled by str2entry_fast() has been passed in, call the
  1235. * slower but more forgiving str2entry_dupcheck() function.
  1236. */
  1237. #define STR2ENTRY_CANNOT_USE_FAST(flags) \
  1238. (((flags) & SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF) || \
  1239. ((flags) & ~SLAPI_STRENTRY_FLAGS_HANDLED_BY_STR2ENTRY_FAST))
  1240. Slapi_Entry *
  1241. slapi_str2entry( char *s, int flags )
  1242. {
  1243. Slapi_Entry *e;
  1244. int read_stateinfo= ~( flags & SLAPI_STR2ENTRY_IGNORE_STATE );
  1245. LDAPDebug( LDAP_DEBUG_ARGS,
  1246. "slapi_str2entry: flags=0x%x, entry=\"%.50s...\"\n",
  1247. flags, s, 0 );
  1248. /*
  1249. * If well-formed LDIF has not been provided OR if a flag that is
  1250. * not handled by str2entry_fast() has been passed in, call the
  1251. * slower but more forgiving str2entry_dupcheck() function.
  1252. */
  1253. if (STR2ENTRY_CANNOT_USE_FAST(flags))
  1254. {
  1255. e= str2entry_dupcheck( NULL/*dn*/, s, flags, read_stateinfo );
  1256. }
  1257. else
  1258. {
  1259. e= str2entry_fast( NULL/*dn*/, NULL/*rdn*/, s, flags, read_stateinfo );
  1260. }
  1261. if (!e)
  1262. return e; /* e == NULL */
  1263. if ( flags & SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES )
  1264. {
  1265. if ( flags & SLAPI_STR2ENTRY_NO_SCHEMA_LOCK )
  1266. {
  1267. schema_expand_objectclasses_nolock( e );
  1268. }
  1269. else
  1270. {
  1271. slapi_schema_expand_objectclasses( e );
  1272. }
  1273. }
  1274. if ( flags & SLAPI_STR2ENTRY_TOMBSTONE_CHECK )
  1275. {
  1276. /*
  1277. * Check if the entry is a tombstone.
  1278. */
  1279. if(slapi_entry_attr_hasvalue(e, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE))
  1280. {
  1281. e->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
  1282. }
  1283. }
  1284. return e;
  1285. }
  1286. /*
  1287. * string s does not include dn.
  1288. * NOTE: the first arg "dn" should have been normalized before passing.
  1289. */
  1290. Slapi_Entry *
  1291. slapi_str2entry_ext( const char *normdn, const Slapi_RDN *srdn, char *s, int flags )
  1292. {
  1293. Slapi_Entry *e;
  1294. int read_stateinfo= ~( flags & SLAPI_STR2ENTRY_IGNORE_STATE );
  1295. if (NULL == normdn)
  1296. {
  1297. return slapi_str2entry( s, flags );
  1298. }
  1299. LDAPDebug( LDAP_DEBUG_ARGS,
  1300. "slapi_str2entry_ext: flags=0x%x, dn=\"%s\", entry=\"%.50s...\"\n",
  1301. flags, normdn, s );
  1302. /*
  1303. * If well-formed LDIF has not been provided OR if a flag that is
  1304. * not handled by str2entry_fast() has been passed in, call the
  1305. * slower but more forgiving str2entry_dupcheck() function.
  1306. */
  1307. if (STR2ENTRY_CANNOT_USE_FAST(flags))
  1308. {
  1309. e = str2entry_dupcheck( normdn, s,
  1310. flags|SLAPI_STR2ENTRY_DN_NORMALIZED, read_stateinfo );
  1311. }
  1312. else
  1313. {
  1314. e = str2entry_fast( normdn, srdn, s,
  1315. flags|SLAPI_STR2ENTRY_DN_NORMALIZED, read_stateinfo );
  1316. }
  1317. if (!e)
  1318. return e; /* e == NULL */
  1319. if ( flags & SLAPI_STR2ENTRY_EXPAND_OBJECTCLASSES )
  1320. {
  1321. if ( flags & SLAPI_STR2ENTRY_NO_SCHEMA_LOCK )
  1322. {
  1323. schema_expand_objectclasses_nolock( e );
  1324. }
  1325. else
  1326. {
  1327. slapi_schema_expand_objectclasses( e );
  1328. }
  1329. }
  1330. if ( flags & SLAPI_STR2ENTRY_TOMBSTONE_CHECK )
  1331. {
  1332. /*
  1333. * Check if the entry is a tombstone.
  1334. */
  1335. if(slapi_entry_attr_hasvalue(e, SLAPI_ATTR_OBJECTCLASS, SLAPI_ATTR_VALUE_TOMBSTONE))
  1336. {
  1337. e->e_flags |= SLAPI_ENTRY_FLAG_TOMBSTONE;
  1338. }
  1339. }
  1340. return e;
  1341. }
  1342. /*
  1343. * If the attribute type is in the protected list, it returns size 0.
  1344. */
  1345. static size_t
  1346. entry2str_internal_size_value( const char *attrtype, const Slapi_Value *v,
  1347. int entry2str_ctrl, int attribute_state,
  1348. int value_state )
  1349. {
  1350. size_t elen = 0;
  1351. size_t attrtypelen;
  1352. if((NULL == attrtype) || is_type_protected(attrtype)) {
  1353. goto bail;
  1354. }
  1355. attrtypelen = strlen(attrtype);
  1356. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1357. {
  1358. attrtypelen+= csnset_string_size(v->v_csnset);
  1359. if (attribute_state==ATTRIBUTE_DELETED)
  1360. {
  1361. attrtypelen += DELETED_ATTR_STRSIZE;
  1362. }
  1363. if(value_state==VALUE_DELETED)
  1364. {
  1365. attrtypelen += DELETED_VALUE_STRSIZE;
  1366. }
  1367. }
  1368. elen = LDIF_SIZE_NEEDED(attrtypelen, slapi_value_get_berval(v)->bv_len);
  1369. bail:
  1370. return elen;
  1371. }
  1372. static size_t
  1373. entry2str_internal_size_valueset( const char *attrtype, const Slapi_ValueSet *vs, int entry2str_ctrl, int attribute_state, int value_state )
  1374. {
  1375. size_t elen= 0;
  1376. if(!valueset_isempty(vs))
  1377. {
  1378. int i;
  1379. Slapi_Value **va= valueset_get_valuearray(vs);
  1380. for (i = 0; va[i]; i++)
  1381. {
  1382. elen+= entry2str_internal_size_value(attrtype, va[i], entry2str_ctrl,
  1383. attribute_state, value_state );
  1384. }
  1385. }
  1386. return elen;
  1387. }
  1388. static size_t
  1389. entry2str_internal_size_attrlist( const Slapi_Attr *attrlist, int entry2str_ctrl, int attribute_state )
  1390. {
  1391. size_t elen= 0;
  1392. const Slapi_Attr *a;
  1393. for (a= attrlist; a; a = a->a_next)
  1394. {
  1395. /* skip operational attributes if not requested */
  1396. if ((entry2str_ctrl & SLAPI_DUMP_NOOPATTRS) &&
  1397. slapi_attr_flag_is_set(a, SLAPI_ATTR_FLAG_OPATTR))
  1398. continue;
  1399. /* Count the space required for the present and deleted values */
  1400. elen+= entry2str_internal_size_valueset(a->a_type, &a->a_present_values,
  1401. entry2str_ctrl, attribute_state,
  1402. VALUE_PRESENT);
  1403. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1404. {
  1405. elen+= entry2str_internal_size_valueset(a->a_type, &a->a_deleted_values,
  1406. entry2str_ctrl, attribute_state,
  1407. VALUE_DELETED);
  1408. /* ";adcsn-" + a->a_deletioncsn */
  1409. if ( a->a_deletioncsn )
  1410. {
  1411. elen += 1 + LDIF_CSNPREFIX_MAXLENGTH + CSN_STRSIZE;
  1412. }
  1413. if ( valueset_isempty(&a->a_deleted_values)) {
  1414. /* this means the entry is deleted and has no more attributes,
  1415. * when writing the attr to disk we would loose the AD-csn.
  1416. * Add an empty value to the set of deleted values. This will
  1417. * never be seen by any client. It will never be moved to the
  1418. * present values and is only used to preserve the AD-csn
  1419. * We need to add the size for that.
  1420. */
  1421. elen += 1 + LDIF_CSNPREFIX_MAXLENGTH + CSN_STRSIZE;
  1422. /* need also space for ";deletedattribute;deleted" */
  1423. elen += DELETED_ATTR_STRSIZE + DELETED_VALUE_STRSIZE;
  1424. }
  1425. }
  1426. }
  1427. return elen;
  1428. }
  1429. static void
  1430. entry2str_internal_put_value( const char *attrtype, const CSN *attrcsn, CSNType attrcsntype, int attr_state, const Slapi_Value *v, int value_state, char **ecur, char **typebuf, size_t *typebuf_len, int entry2str_ctrl )
  1431. {
  1432. const char *type;
  1433. unsigned long options = 0;
  1434. const struct berval *bvp;
  1435. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1436. {
  1437. char *p;
  1438. size_t attrtypelen= strlen(attrtype);
  1439. size_t attrcsnlen= 0;
  1440. size_t valuecsnlen= 0;
  1441. size_t need= attrtypelen+1;
  1442. if(attrcsn!=NULL)
  1443. {
  1444. /* ; csntype csn */
  1445. attrcsnlen= 1 + csn_string_size();
  1446. need+= attrcsnlen;
  1447. }
  1448. if(v->v_csnset!=NULL)
  1449. {
  1450. /* +(; csntype csn) */
  1451. valuecsnlen= csnset_string_size(v->v_csnset);
  1452. need+= valuecsnlen;
  1453. }
  1454. if(attr_state==ATTRIBUTE_DELETED)
  1455. {
  1456. need+= DELETED_ATTR_STRSIZE;
  1457. }
  1458. if(value_state==VALUE_DELETED)
  1459. {
  1460. need+= DELETED_VALUE_STRSIZE; /* ;deleted */
  1461. }
  1462. if(*typebuf_len<need)
  1463. {
  1464. *typebuf= (char*)slapi_ch_realloc(*typebuf,need);
  1465. *typebuf_len= need;
  1466. }
  1467. p= *typebuf;
  1468. type= p;
  1469. strcpy(p,attrtype);
  1470. p+= attrtypelen;
  1471. if(attrcsn!=NULL)
  1472. {
  1473. csn_as_attr_option_string(attrcsntype,attrcsn,p);
  1474. p+= attrcsnlen;
  1475. }
  1476. if(v->v_csnset!=NULL)
  1477. {
  1478. csnset_as_string(v->v_csnset,p);
  1479. p+= valuecsnlen;
  1480. }
  1481. if(attr_state==ATTRIBUTE_DELETED)
  1482. {
  1483. strcpy(p,DELETED_ATTR_STRING);
  1484. p+= DELETED_ATTR_STRSIZE;
  1485. }
  1486. if(value_state==VALUE_DELETED)
  1487. {
  1488. strcpy(p,DELETED_VALUE_STRING);
  1489. }
  1490. }
  1491. else
  1492. {
  1493. type= attrtype;
  1494. }
  1495. bvp = slapi_value_get_berval(v);
  1496. if (entry2str_ctrl & SLAPI_DUMP_NOWRAP)
  1497. options |= LDIF_OPT_NOWRAP;
  1498. if (entry2str_ctrl & SLAPI_DUMP_MINIMAL_ENCODING)
  1499. options |= LDIF_OPT_MINIMAL_ENCODING;
  1500. slapi_ldif_put_type_and_value_with_options( ecur, type, bvp->bv_val, bvp->bv_len, options );
  1501. }
  1502. static void
  1503. entry2str_internal_put_valueset( const char *attrtype, const CSN *attrcsn, CSNType attrcsntype, int attr_state, const Slapi_ValueSet *vs, int value_state, char **ecur, char **typebuf, size_t *typebuf_len, int entry2str_ctrl )
  1504. {
  1505. if(!valueset_isempty(vs))
  1506. {
  1507. int i;
  1508. Slapi_Value **va= valueset_get_valuearray(vs);
  1509. for ( i = 0; va[i] != NULL; i++ )
  1510. {
  1511. /* Attach the attribute deletion csn on the first value */
  1512. if((entry2str_ctrl & SLAPI_DUMP_STATEINFO) && i==0)
  1513. {
  1514. entry2str_internal_put_value( attrtype, attrcsn, attrcsntype, attr_state, va[i], value_state, ecur, typebuf, typebuf_len, entry2str_ctrl );
  1515. }
  1516. else
  1517. {
  1518. entry2str_internal_put_value( attrtype, NULL, CSN_TYPE_UNKNOWN, attr_state, va[i], value_state, ecur, typebuf, typebuf_len, entry2str_ctrl );
  1519. }
  1520. }
  1521. }
  1522. }
  1523. int
  1524. is_type_protected(const char *type)
  1525. {
  1526. char **paap = NULL;
  1527. for (paap = protected_attrs_all; paap && *paap; paap++) {
  1528. if (0 == strcasecmp(type, *paap)) {
  1529. return 1;
  1530. }
  1531. }
  1532. return 0;
  1533. }
  1534. #if defined(USE_OLD_UNHASHED)
  1535. int
  1536. is_type_forbidden(const char *type)
  1537. {
  1538. char **paap = NULL;
  1539. for (paap = forbidden_attrs; paap && *paap; paap++) {
  1540. if (0 == strcasecmp(type, *paap)) {
  1541. return 1;
  1542. }
  1543. }
  1544. return 0;
  1545. }
  1546. #endif
  1547. static void
  1548. entry2str_internal_put_attrlist( const Slapi_Attr *attrlist, int attr_state, int entry2str_ctrl, char **ecur, char **typebuf, size_t *typebuf_len)
  1549. {
  1550. const Slapi_Attr *a;
  1551. /* Put the present attributes */
  1552. for (a= attrlist; a; a = a->a_next)
  1553. {
  1554. /* skip operational attributes if not requested */
  1555. if ((entry2str_ctrl & SLAPI_DUMP_NOOPATTRS) &&
  1556. slapi_attr_flag_is_set(a, SLAPI_ATTR_FLAG_OPATTR))
  1557. continue;
  1558. /* don't dump uniqueid if not asked */
  1559. if (!(strcasecmp(a->a_type, SLAPI_ATTR_UNIQUEID) == 0 &&
  1560. !(SLAPI_DUMP_UNIQUEID & entry2str_ctrl)) &&
  1561. !is_type_protected(a->a_type))
  1562. {
  1563. /* Putting present attribute values */
  1564. /* put "<type>:[:] <value>" line for each value */
  1565. int present_values= !valueset_isempty(&a->a_present_values);
  1566. if(present_values)
  1567. {
  1568. entry2str_internal_put_valueset(a->a_type, a->a_deletioncsn, CSN_TYPE_ATTRIBUTE_DELETED, attr_state, &a->a_present_values, VALUE_PRESENT, ecur, typebuf, typebuf_len, entry2str_ctrl);
  1569. }
  1570. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1571. {
  1572. /* Putting deleted attribute values */
  1573. if(present_values)
  1574. {
  1575. entry2str_internal_put_valueset(a->a_type, NULL, CSN_TYPE_NONE, attr_state, &a->a_deleted_values, VALUE_DELETED, ecur, typebuf, typebuf_len, entry2str_ctrl);
  1576. }
  1577. else
  1578. {
  1579. /* There were no present values on which to place the ADCSN, so we put it on the first deleted value. */
  1580. if ( valueset_isempty(&a->a_deleted_values)) {
  1581. /* this means the entry is deleted and has no more attributes,
  1582. * when writing the attr to disk we would loose the AD-csn.
  1583. * Add an empty value to the set of deleted values. This will
  1584. * never be seen by any client. It will never be moved to the
  1585. * present values and is only used to preserve the AD-csn
  1586. */
  1587. valueset_add_string (a, (Slapi_ValueSet *)&a->a_deleted_values, "", CSN_TYPE_VALUE_DELETED, a->a_deletioncsn);
  1588. }
  1589. entry2str_internal_put_valueset(a->a_type, a->a_deletioncsn, CSN_TYPE_ATTRIBUTE_DELETED, attr_state, &a->a_deleted_values, VALUE_DELETED, ecur, typebuf, typebuf_len, entry2str_ctrl);
  1590. }
  1591. }
  1592. }
  1593. }
  1594. }
  1595. static char *
  1596. entry2str_internal( Slapi_Entry *e, int *len, int entry2str_ctrl )
  1597. {
  1598. char *ebuf;
  1599. char *ecur;
  1600. size_t elen = 0;
  1601. size_t typebuf_len= 64;
  1602. char *typebuf= (char *)slapi_ch_malloc(typebuf_len);
  1603. Slapi_Value dnvalue;
  1604. /*
  1605. * In string format, an entry looks like this:
  1606. * dn: <dn>\n
  1607. * [<attr>: <value>\n]*
  1608. */
  1609. ecur = ebuf = NULL;
  1610. value_init(&dnvalue,NULL,CSN_TYPE_NONE,NULL);
  1611. /* find length of buffer needed to hold this entry */
  1612. if (slapi_entry_get_dn_const(e)!=NULL)
  1613. {
  1614. slapi_value_set_string(&dnvalue,slapi_entry_get_dn_const(e));
  1615. elen+= entry2str_internal_size_value( "dn", &dnvalue, entry2str_ctrl,
  1616. ATTRIBUTE_PRESENT, VALUE_PRESENT );
  1617. }
  1618. /* Count the space required for the present attributes */
  1619. elen+= entry2str_internal_size_attrlist( e->e_attrs, entry2str_ctrl, ATTRIBUTE_PRESENT );
  1620. /* Count the space required for the deleted attributes */
  1621. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1622. {
  1623. elen+= entry2str_internal_size_attrlist( e->e_deleted_attrs, entry2str_ctrl,
  1624. ATTRIBUTE_DELETED );
  1625. }
  1626. elen += 1;
  1627. ecur = ebuf = (char *)slapi_ch_malloc(elen);
  1628. /* put the dn */
  1629. if ( slapi_entry_get_dn_const(e)!=NULL)
  1630. {
  1631. /* put "dn: <dn>" */
  1632. entry2str_internal_put_value("dn", NULL, CSN_TYPE_NONE, ATTRIBUTE_PRESENT, &dnvalue, VALUE_PRESENT, &ecur, &typebuf, &typebuf_len, entry2str_ctrl);
  1633. }
  1634. /* Put the present attributes */
  1635. entry2str_internal_put_attrlist( e->e_attrs, ATTRIBUTE_PRESENT, entry2str_ctrl, &ecur, &typebuf, &typebuf_len );
  1636. /* Put the deleted attributes */
  1637. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1638. {
  1639. entry2str_internal_put_attrlist( e->e_deleted_attrs, ATTRIBUTE_DELETED, entry2str_ctrl, &ecur, &typebuf, &typebuf_len );
  1640. }
  1641. *ecur = '\0';
  1642. if ( (size_t)(ecur - ebuf + 1) > elen )
  1643. {
  1644. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  1645. "entry2str_internal: array boundary wrote: bufsize=%ld wrote=%ld\n",
  1646. (long int)elen, (long int)(ecur - ebuf + 1));
  1647. }
  1648. if ( NULL != len ) {
  1649. *len = ecur - ebuf;
  1650. }
  1651. slapi_ch_free((void**)&typebuf);
  1652. value_done(&dnvalue);
  1653. return ebuf;
  1654. }
  1655. static char *
  1656. entry2str_internal_ext( Slapi_Entry *e, int *len, int entry2str_ctrl)
  1657. {
  1658. if (entry2str_ctrl & SLAPI_DUMP_RDN_ENTRY) /* dump rdn: ... */
  1659. {
  1660. char *ebuf;
  1661. char *ecur;
  1662. size_t elen = 0;
  1663. size_t typebuf_len = 64;
  1664. char *typebuf = (char *)slapi_ch_malloc(typebuf_len);
  1665. Slapi_Value rdnvalue;
  1666. /*
  1667. * In string format, an entry looks like this:
  1668. * rdn: <normalized rdn>\n
  1669. * [<attr>: <value>\n]*
  1670. */
  1671. ecur = ebuf = NULL;
  1672. value_init(&rdnvalue, NULL, CSN_TYPE_NONE, NULL);
  1673. /* find length of buffer needed to hold this entry */
  1674. if (NULL == slapi_entry_get_rdn_const(e) &&
  1675. NULL != slapi_entry_get_dn_const(e))
  1676. {
  1677. /* e_srdn is not filled in, use e_sdn */
  1678. slapi_rdn_init_all_sdn(&e->e_srdn, slapi_entry_get_sdn_const(e));
  1679. }
  1680. if (NULL != slapi_entry_get_rdn_const(e))
  1681. {
  1682. slapi_value_set_string(&rdnvalue, slapi_entry_get_rdn_const(e));
  1683. elen += entry2str_internal_size_value("rdn", &rdnvalue,
  1684. entry2str_ctrl,
  1685. ATTRIBUTE_PRESENT,
  1686. VALUE_PRESENT);
  1687. }
  1688. /* Count the space required for the present attributes */
  1689. elen += entry2str_internal_size_attrlist(e->e_attrs, entry2str_ctrl,
  1690. ATTRIBUTE_PRESENT);
  1691. /* Count the space required for the deleted attributes */
  1692. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1693. {
  1694. elen += entry2str_internal_size_attrlist(e->e_deleted_attrs,
  1695. entry2str_ctrl,
  1696. ATTRIBUTE_DELETED);
  1697. }
  1698. elen += 1;
  1699. ecur = ebuf = (char *)slapi_ch_malloc(elen);
  1700. /* put the rdn */
  1701. if (slapi_entry_get_rdn_const(e) != NULL)
  1702. {
  1703. /* put "rdn: <normalized rdn>" */
  1704. entry2str_internal_put_value("rdn", NULL, CSN_TYPE_NONE,
  1705. ATTRIBUTE_PRESENT, &rdnvalue,
  1706. VALUE_PRESENT, &ecur, &typebuf,
  1707. &typebuf_len, entry2str_ctrl);
  1708. }
  1709. /* Put the present attributes */
  1710. entry2str_internal_put_attrlist(e->e_attrs, ATTRIBUTE_PRESENT,
  1711. entry2str_ctrl, &ecur,
  1712. &typebuf, &typebuf_len );
  1713. /* Put the deleted attributes */
  1714. if(entry2str_ctrl & SLAPI_DUMP_STATEINFO)
  1715. {
  1716. entry2str_internal_put_attrlist(e->e_deleted_attrs,
  1717. ATTRIBUTE_DELETED, entry2str_ctrl,
  1718. &ecur, &typebuf, &typebuf_len );
  1719. }
  1720. *ecur = '\0';
  1721. if ((size_t)(ecur - ebuf + 1) > elen)
  1722. {
  1723. /* this should not happen */
  1724. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  1725. "entry2str_internal_ext: array boundary wrote: "
  1726. "bufsize=%ld wrote=%ld\n",
  1727. (long int)elen, (long int)(ecur - ebuf + 1));
  1728. }
  1729. if ( NULL != len ) {
  1730. *len = ecur - ebuf;
  1731. }
  1732. slapi_ch_free((void**)&typebuf);
  1733. value_done(&rdnvalue);
  1734. return ebuf;
  1735. }
  1736. else /* dump "dn: ..." */
  1737. {
  1738. return entry2str_internal( e, len, entry2str_ctrl );
  1739. }
  1740. }
  1741. /*
  1742. * This function converts an entry to the entry string starting with "dn: ..."
  1743. */
  1744. char *
  1745. slapi_entry2str( Slapi_Entry *e, int *len )
  1746. {
  1747. return entry2str_internal(e, len, 0);
  1748. }
  1749. /*
  1750. * This function converts an entry to the entry string starting with "dn: ..."
  1751. */
  1752. char *
  1753. slapi_entry2str_dump_uniqueid( Slapi_Entry *e, int *len )
  1754. {
  1755. return entry2str_internal(e, len, SLAPI_DUMP_UNIQUEID);
  1756. }
  1757. /*
  1758. * This function converts an entry to the entry string starting with "dn: ..."
  1759. */
  1760. char *
  1761. slapi_entry2str_no_opattrs( Slapi_Entry *e, int *len )
  1762. {
  1763. return entry2str_internal(e, len, SLAPI_DUMP_NOOPATTRS);
  1764. }
  1765. /*
  1766. * This function converts an entry to the entry string starting with "dn: ..."
  1767. * by default. If option includes SLAPI_DUMP_RDN_ENTRY bit, it does the entry
  1768. * to "rdn: ..."
  1769. */
  1770. char *
  1771. slapi_entry2str_with_options( Slapi_Entry *e, int *len, int options )
  1772. {
  1773. return entry2str_internal_ext(e, len, options);
  1774. }
  1775. static int entry_type = -1; /* The type number assigned by the Factory for 'Entry' */
  1776. int
  1777. get_entry_object_type()
  1778. {
  1779. if(entry_type==-1)
  1780. {
  1781. /* The factory is given the name of the object type, in
  1782. * return for a type handle. Whenever the object is created
  1783. * or destroyed the factory is called with the handle so
  1784. * that it may call the constructors or destructors registered
  1785. * with it.
  1786. */
  1787. entry_type= factory_register_type(SLAPI_EXT_ENTRY,offsetof(Slapi_Entry,e_extension));
  1788. }
  1789. return entry_type;
  1790. }
  1791. /* ====== Slapi_Entry functions ====== */
  1792. #ifdef ENTRY_DEBUG
  1793. static void entry_dump( const Slapi_Entry *e, const char *text);
  1794. #define ENTRY_DUMP(e,name) entry_dump(e,name)
  1795. #else
  1796. #define ENTRY_DUMP(e,name) ((void)0)
  1797. #endif
  1798. static int counters_created= 0;
  1799. PR_DEFINE_COUNTER(slapi_entry_counter_created);
  1800. PR_DEFINE_COUNTER(slapi_entry_counter_deleted);
  1801. PR_DEFINE_COUNTER(slapi_entry_counter_exist);
  1802. Slapi_Entry *
  1803. slapi_entry_alloc()
  1804. {
  1805. Slapi_Entry *e= (Slapi_Entry *) slapi_ch_calloc( 1, sizeof(struct slapi_entry) );
  1806. slapi_sdn_init(&e->e_sdn);
  1807. slapi_rdn_init(&e->e_srdn);
  1808. e->e_extension = factory_create_extension(get_entry_object_type(),e,NULL);
  1809. if(!counters_created)
  1810. {
  1811. PR_CREATE_COUNTER(slapi_entry_counter_created,"Slapi_Entry","created","");
  1812. PR_CREATE_COUNTER(slapi_entry_counter_deleted,"Slapi_Entry","deleted","");
  1813. PR_CREATE_COUNTER(slapi_entry_counter_exist,"Slapi_Entry","exist","");
  1814. counters_created= 1;
  1815. }
  1816. PR_INCREMENT_COUNTER(slapi_entry_counter_created);
  1817. PR_INCREMENT_COUNTER(slapi_entry_counter_exist);
  1818. ENTRY_DUMP(e,"slapi_entry_alloc");
  1819. return e;
  1820. }
  1821. /*
  1822. * WARNING - The DN is passed in *not* copied.
  1823. */
  1824. void
  1825. slapi_entry_init(Slapi_Entry *e, char *dn, Slapi_Attr *a)
  1826. {
  1827. slapi_sdn_set_dn_passin(slapi_entry_get_sdn(e), dn);
  1828. e->e_uniqueid= NULL;
  1829. e->e_attrs= a;
  1830. e->e_dncsnset= NULL;
  1831. e->e_maxcsn= NULL;
  1832. e->e_deleted_attrs= NULL;
  1833. e->e_virtual_attrs= NULL;
  1834. e->e_virtual_watermark= 0;
  1835. e->e_virtual_lock= slapi_new_rwlock();
  1836. e->e_flags= 0;
  1837. }
  1838. void
  1839. slapi_entry_init_ext(Slapi_Entry *e, Slapi_DN *sdn, Slapi_Attr *a)
  1840. {
  1841. slapi_sdn_copy(sdn, slapi_entry_get_sdn(e));
  1842. e->e_uniqueid= NULL;
  1843. e->e_attrs= a;
  1844. e->e_dncsnset= NULL;
  1845. e->e_maxcsn= NULL;
  1846. e->e_deleted_attrs= NULL;
  1847. e->e_virtual_attrs= NULL;
  1848. e->e_virtual_watermark= 0;
  1849. e->e_virtual_lock= slapi_new_rwlock();
  1850. e->e_flags= 0;
  1851. }
  1852. void
  1853. slapi_entry_free( Slapi_Entry *e ) /* JCM - Should be ** so that we can NULL the ptr */
  1854. {
  1855. if(e!=NULL)
  1856. {
  1857. ENTRY_DUMP(e,"slapi_entry_free");
  1858. factory_destroy_extension(get_entry_object_type(),e,NULL/*Parent*/,&(e->e_extension));
  1859. slapi_sdn_done(&e->e_sdn);
  1860. slapi_rdn_done(&e->e_srdn);
  1861. csnset_free(&e->e_dncsnset);
  1862. csn_free(&e->e_maxcsn);
  1863. slapi_ch_free((void **)&e->e_uniqueid);
  1864. attrlist_free(e->e_attrs);
  1865. attrlist_free(e->e_deleted_attrs);
  1866. VATTR_WRITE_LOCK(e);
  1867. entry_vattr_free_nolock(e);
  1868. VATTR_WRITE_UNLOCK(e);
  1869. if(e->e_virtual_lock)
  1870. slapi_destroy_rwlock(e->e_virtual_lock);
  1871. slapi_ch_free((void**)&e);
  1872. PR_INCREMENT_COUNTER(slapi_entry_counter_deleted);
  1873. PR_DECREMENT_COUNTER(slapi_entry_counter_exist);
  1874. }
  1875. }
  1876. static size_t slapi_attrlist_size(Slapi_Attr *attrs)
  1877. {
  1878. size_t size = 0;
  1879. Slapi_Attr *a;
  1880. for (a= attrs; a; a = a->a_next) {
  1881. if (a->a_type) size += strlen(a->a_type) + 1;
  1882. size += valueset_size(&a->a_present_values);
  1883. size += valueset_size(&a->a_deleted_values);
  1884. /* Don't bother with a_listtofree. This is only set
  1885. * by a call to slapi_attr_get_values, which should
  1886. * never be used on a cache entry since it can cause
  1887. * the entry to grow without bound.
  1888. */
  1889. if (a->a_deletioncsn) size += sizeof(CSN);
  1890. size += sizeof(Slapi_Attr);
  1891. }
  1892. return size;
  1893. }
  1894. /* return the approximate size of an entry --
  1895. * useful for checking cache sizes, etc
  1896. */
  1897. size_t
  1898. slapi_entry_size(Slapi_Entry *e)
  1899. {
  1900. size_t size = 0;
  1901. if (e->e_uniqueid) size += strlen(e->e_uniqueid) + 1;
  1902. if (e->e_dncsnset) size += csnset_size(e->e_dncsnset);
  1903. if (e->e_maxcsn) size += sizeof( CSN );
  1904. if (e->e_virtual_lock) size += slapi_rwlock_get_size();
  1905. /* Slapi_DN and RDN are included in Slapi_Entry */
  1906. size += (slapi_sdn_get_size(&e->e_sdn) - sizeof(Slapi_DN));
  1907. size += (slapi_rdn_get_size(&e->e_srdn) - sizeof(Slapi_RDN));
  1908. size += slapi_attrlist_size(e->e_attrs);
  1909. size += slapi_attrlist_size(e->e_deleted_attrs);
  1910. size += slapi_attrlist_size(e->e_aux_attrs);
  1911. size += entry_vattr_size(e);
  1912. if (e->e_extension) {
  1913. struct attrs_in_extension *aiep;
  1914. int cnt;
  1915. size_t extsiz = 0;
  1916. for (aiep = attrs_in_extension, cnt = 0;
  1917. aiep && aiep->ext_type; aiep++, cnt++) {
  1918. if (LDAP_SUCCESS == aiep->ext_get_size(e, &extsiz)) {
  1919. size += extsiz;
  1920. }
  1921. }
  1922. size += cnt * sizeof(void *);
  1923. }
  1924. size += sizeof(Slapi_Entry);
  1925. return size;
  1926. }
  1927. /*
  1928. * return a complete copy of entry pointed to by "e"
  1929. * entry extensions are duplicated, as well.
  1930. */
  1931. Slapi_Entry *
  1932. slapi_entry_dup( const Slapi_Entry *e )
  1933. {
  1934. Slapi_Entry *ec;
  1935. Slapi_Attr *a;
  1936. Slapi_Attr *lastattr= NULL;
  1937. struct attrs_in_extension *aiep;
  1938. PR_ASSERT( NULL != e );
  1939. ec = slapi_entry_alloc();
  1940. /*
  1941. * init the new entry--some things (eg. locks in the entry) are not dup'ed
  1942. */
  1943. slapi_entry_init(ec,NULL,NULL);
  1944. slapi_sdn_copy(slapi_entry_get_sdn_const(e),&ec->e_sdn);
  1945. slapi_srdn_copy(slapi_entry_get_srdn_const(e),&ec->e_srdn);
  1946. /* duplicate the dncsn also */
  1947. ec->e_dncsnset= csnset_dup(e->e_dncsnset);
  1948. ec->e_maxcsn= csn_dup(e->e_maxcsn);
  1949. /* don't use slapi_entry_set_uniqueid here because
  1950. it will cause uniqueid to be added twice to the
  1951. attribute list
  1952. */
  1953. if ( e->e_uniqueid != NULL )
  1954. {
  1955. ec->e_uniqueid = slapi_ch_strdup( e->e_uniqueid ); /* JCM - UniqueID Dup function? */
  1956. }
  1957. for ( a = e->e_attrs; a != NULL; a = a->a_next )
  1958. {
  1959. Slapi_Attr *newattr= slapi_attr_dup(a);
  1960. if(lastattr==NULL)
  1961. {
  1962. ec->e_attrs= newattr;
  1963. }
  1964. else
  1965. {
  1966. lastattr->a_next= newattr;
  1967. }
  1968. lastattr= newattr;
  1969. }
  1970. lastattr= NULL;
  1971. for ( a = e->e_deleted_attrs; a != NULL; a = a->a_next )
  1972. {
  1973. Slapi_Attr *newattr= slapi_attr_dup(a);
  1974. if(lastattr==NULL)
  1975. {
  1976. ec->e_deleted_attrs= newattr;
  1977. }
  1978. else
  1979. {
  1980. lastattr->a_next= newattr;
  1981. }
  1982. lastattr= newattr;
  1983. }
  1984. /* Copy flags as well */
  1985. ec->e_flags = e->e_flags;
  1986. /* Copy extension */
  1987. for (aiep = attrs_in_extension; aiep && aiep->ext_type; aiep++) {
  1988. aiep->ext_copy(e, ec);
  1989. }
  1990. ENTRY_DUMP(ec,"slapi_entry_dup");
  1991. return( ec );
  1992. }
  1993. #ifdef ENTRY_DEBUG
  1994. static void
  1995. entry_dump( const Slapi_Entry *e, const char *text)
  1996. {
  1997. const char *dn= slapi_entry_get_dn_const(e);
  1998. LDAPDebug( LDAP_DEBUG_ANY, "Entry %s ptr=%lx dn=%s\n", text, e, (dn==NULL?"NULL":dn));
  1999. }
  2000. #endif
  2001. char *
  2002. slapi_entry_get_dn( Slapi_Entry *e )
  2003. {
  2004. /* jcm - This is evil... we have to cast away the const. */
  2005. return (char*)(slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  2006. }
  2007. char *
  2008. slapi_entry_get_ndn( Slapi_Entry *e )
  2009. {
  2010. /* jcm - This is evil... we have to cast away the const. */
  2011. return (char*)(slapi_sdn_get_ndn(slapi_entry_get_sdn_const(e)));
  2012. }
  2013. const Slapi_DN *
  2014. slapi_entry_get_sdn_const( const Slapi_Entry *e )
  2015. {
  2016. return &e->e_sdn;
  2017. }
  2018. Slapi_DN *
  2019. slapi_entry_get_sdn( Slapi_Entry *e )
  2020. {
  2021. return &e->e_sdn;
  2022. }
  2023. const Slapi_RDN *
  2024. slapi_entry_get_srdn_const( const Slapi_Entry *e )
  2025. {
  2026. return &e->e_srdn;
  2027. }
  2028. Slapi_RDN *
  2029. slapi_entry_get_srdn( Slapi_Entry *e )
  2030. {
  2031. return &e->e_srdn;
  2032. }
  2033. const char *
  2034. slapi_entry_get_dn_const( const Slapi_Entry *e )
  2035. {
  2036. return (slapi_sdn_get_dn(slapi_entry_get_sdn_const(e)));
  2037. }
  2038. const char *
  2039. slapi_entry_get_rdn_const( const Slapi_Entry *e )
  2040. {
  2041. return (slapi_rdn_get_rdn(slapi_entry_get_srdn_const(e)));
  2042. }
  2043. /* slapi_rdn_get_nrdn could modify srdn in it. So, it cannot take const. */
  2044. const char *
  2045. slapi_entry_get_nrdn_const( const Slapi_Entry *e )
  2046. {
  2047. const char *nrdn =
  2048. slapi_rdn_get_nrdn(slapi_entry_get_srdn((Slapi_Entry *)e));
  2049. if (NULL == nrdn) {
  2050. const char *dn = slapi_entry_get_dn_const(e);
  2051. if (dn) {
  2052. /* cast away const */
  2053. slapi_rdn_init_all_dn((Slapi_RDN *)&e->e_srdn, dn);
  2054. nrdn = slapi_rdn_get_nrdn(slapi_entry_get_srdn((Slapi_Entry *)e));
  2055. }
  2056. }
  2057. return nrdn;
  2058. }
  2059. /*
  2060. * WARNING - The DN is passed in *not* copied.
  2061. */
  2062. void
  2063. slapi_entry_set_dn( Slapi_Entry *e, char *dn )
  2064. {
  2065. slapi_sdn_set_dn_passin(slapi_entry_get_sdn(e),dn);
  2066. }
  2067. void
  2068. slapi_entry_set_normdn( Slapi_Entry *e, char *dn )
  2069. {
  2070. slapi_sdn_set_normdn_passin(slapi_entry_get_sdn(e), dn);
  2071. }
  2072. /*
  2073. * WARNING - The DN is copied.
  2074. * The DN could be dn or RDN.
  2075. */
  2076. void
  2077. slapi_entry_set_rdn( Slapi_Entry *e, char *dn )
  2078. {
  2079. slapi_rdn_set_all_dn(slapi_entry_get_srdn(e),dn);
  2080. }
  2081. void
  2082. slapi_entry_set_sdn( Slapi_Entry *e, const Slapi_DN *sdn )
  2083. {
  2084. slapi_sdn_copy(sdn,slapi_entry_get_sdn(e));
  2085. }
  2086. void
  2087. slapi_entry_set_srdn( Slapi_Entry *e, const Slapi_RDN *srdn )
  2088. {
  2089. slapi_srdn_copy(srdn, slapi_entry_get_srdn(e));
  2090. }
  2091. const char *
  2092. slapi_entry_get_uniqueid( const Slapi_Entry *e )
  2093. {
  2094. return( e->e_uniqueid );
  2095. }
  2096. /*
  2097. * WARNING - The UniqueID is passed in *not* copied.
  2098. */
  2099. void
  2100. slapi_entry_set_uniqueid( Slapi_Entry *e, char *uniqueid )
  2101. {
  2102. e->e_uniqueid = uniqueid;
  2103. /* also add it to the list of attributes - it makes things easier */
  2104. slapi_entry_attr_set_charptr ( e, SLAPI_ATTR_UNIQUEID, uniqueid );
  2105. }
  2106. int
  2107. slapi_entry_first_attr( const Slapi_Entry *e, Slapi_Attr **a )
  2108. {
  2109. return slapi_entry_next_attr( e, NULL, a);
  2110. }
  2111. int
  2112. slapi_entry_next_attr( const Slapi_Entry *e, Slapi_Attr *prevattr, Slapi_Attr **a )
  2113. {
  2114. int done= 0;
  2115. /*
  2116. * We skip over any attributes that have no present values.
  2117. * Our state information storage scheme can cause this, since
  2118. * we have to hang onto the deleted value state information.
  2119. * <jcm - actually we don't do this any more... so this skipping
  2120. * may now be redundant.>
  2121. */
  2122. while(!done)
  2123. {
  2124. if(prevattr==NULL)
  2125. {
  2126. *a = e->e_attrs;
  2127. }
  2128. else
  2129. {
  2130. *a = prevattr->a_next;
  2131. }
  2132. if(*a!=NULL)
  2133. {
  2134. done= !valueset_isempty(&((*a)->a_present_values));
  2135. }
  2136. else
  2137. {
  2138. done= 1;
  2139. }
  2140. if(!done)
  2141. {
  2142. prevattr= *a;
  2143. }
  2144. }
  2145. return( *a ? 0 : -1 );
  2146. }
  2147. int
  2148. slapi_entry_attr_find( const Slapi_Entry *e, const char *type, Slapi_Attr **a )
  2149. {
  2150. int r = -1;
  2151. if(e == NULL){
  2152. return r;
  2153. }
  2154. *a = attrlist_find( e->e_attrs, type );
  2155. if (*a != NULL)
  2156. {
  2157. if(valueset_isempty(&((*a)->a_present_values)))
  2158. {
  2159. /*
  2160. * We ignore attributes that have no present values.
  2161. * Our state information storage scheme can cause this, since
  2162. * we have to hang onto the deleted value state information.
  2163. */
  2164. *a = NULL;
  2165. }
  2166. else
  2167. {
  2168. r = 0;
  2169. }
  2170. }
  2171. return r;
  2172. }
  2173. /* the following functions control virtual attribute cache invalidation */
  2174. static PRInt32 g_virtual_watermark = -1; /* good enough to init */
  2175. int slapi_entry_vattrcache_watermark_isvalid(const Slapi_Entry *e)
  2176. {
  2177. return e->e_virtual_watermark == g_virtual_watermark;
  2178. }
  2179. void slapi_entry_vattrcache_watermark_set(Slapi_Entry *e)
  2180. {
  2181. e->e_virtual_watermark = g_virtual_watermark;
  2182. }
  2183. void slapi_entry_vattrcache_watermark_invalidate(Slapi_Entry *e)
  2184. {
  2185. e->e_virtual_watermark = 0;
  2186. }
  2187. void slapi_entrycache_vattrcache_watermark_invalidate()
  2188. {
  2189. PR_AtomicIncrement(&g_virtual_watermark);
  2190. if (g_virtual_watermark == 0) {
  2191. PR_AtomicIncrement(&g_virtual_watermark);
  2192. }
  2193. }
  2194. /* The following functions control the virtual attribute cache
  2195. * stored in each entry (e_virtual_attrs). Access to that cache
  2196. * requires holding a lock (e_virtual_lock)
  2197. *
  2198. */
  2199. /* enumerate all the vattr attributes and compute their cumul size */
  2200. static size_t entry_vattr_size(Slapi_Entry *e)
  2201. {
  2202. size_t size = 0;
  2203. Slapi_Vattr *vattr = NULL;
  2204. VATTR_READ_LOCK(e);
  2205. for (vattr = e->e_virtual_attrs; vattr != NULL; vattr = vattr->next) {
  2206. if (vattr->attrname != NULL) {
  2207. size += strlen(vattr->attrname);
  2208. }
  2209. size += slapi_attrlist_size(vattr->attr);
  2210. size += sizeof(Slapi_Vattr);
  2211. }
  2212. VATTR_READ_UNLOCK(e);
  2213. return (size);
  2214. }
  2215. /* if attr_name has already been evaluated (and cached) then returns it
  2216. * Else it returns NULL
  2217. * The caller must hold e_virtual_lock in read or write
  2218. */
  2219. static Slapi_Vattr *entry_vattr_lookup_nolock(const Slapi_Entry *e, const char *attr_name)
  2220. {
  2221. Slapi_Vattr *vattr = NULL;
  2222. char *name;
  2223. for (vattr = e->e_virtual_attrs; vattr != NULL; vattr = vattr->next) {
  2224. /* take the attribute name where it was kept */
  2225. if (vattr->attrname != NULL) {
  2226. name = vattr->attrname;
  2227. } else if (vattr->attr != NULL) {
  2228. name = vattr->attr->a_type;
  2229. } else {
  2230. slapi_log_error(SLAPI_LOG_FATAL, "entry_vattr_lookup_nolock", "unable to retrieve attribute name %s\n", attr_name);
  2231. continue;
  2232. }
  2233. if (slapi_attr_type_cmp( (const char *) name , attr_name, SLAPI_TYPE_CMP_EXACT) == 0) {
  2234. break;
  2235. }
  2236. }
  2237. return (vattr);
  2238. }
  2239. /* It adds an attribute in the virtual attribute cache
  2240. * The caller must have checked that the attribute is not already cached.
  2241. * The caller must hold e_virtual_lock in write
  2242. */
  2243. static void entry_vattr_add_nolock(Slapi_Entry *e, const char * type, Slapi_Attr *attr)
  2244. {
  2245. Slapi_Vattr *vattr;
  2246. /* In order to remember that we already evaluated this attribute, add it into the vattr cache */
  2247. vattr = (Slapi_Vattr *) slapi_ch_calloc(1, sizeof (Slapi_Vattr));
  2248. vattr->attr = attr;
  2249. if (vattr->attr == NULL) {
  2250. /* This virtual attribute was evaluated but has no value
  2251. * keep the attribute name in attrname
  2252. */
  2253. vattr->attrname = attr_syntax_normalize_no_lookup(type);
  2254. } else {
  2255. vattr->attrname = NULL;
  2256. }
  2257. vattr->next = e->e_virtual_attrs;
  2258. e->e_virtual_attrs = vattr;
  2259. }
  2260. /* The caller must hold e_virtual_lock in write mode */
  2261. static void entry_vattr_free_nolock(Slapi_Entry *e)
  2262. {
  2263. Slapi_Vattr *vattr, *next;
  2264. for (vattr = e->e_virtual_attrs, next = NULL; vattr != NULL; vattr = next) {
  2265. next = vattr->next;
  2266. attrlist_free(vattr->attr);
  2267. slapi_ch_free((void **) &vattr->attrname);
  2268. slapi_ch_free((void **) &vattr);
  2269. }
  2270. e->e_virtual_attrs = NULL;
  2271. }
  2272. /*
  2273. * slapi_entry_vattrcache_findAndTest()
  2274. *
  2275. * returns:
  2276. * SLAPI_ENTRY_VATTR_NOT_RESOLVED--not found in vattrcache; *rc set to -1.
  2277. * SLAPI_ENTRY_VATTR_RESOLVED_ABSENT--present in vattrcache but empty value:
  2278. * means tjhat vattr type is not present in
  2279. * that entry.
  2280. * SLAPI_ENTRY_VATTR_RESOLVED_EXISTS--found vattr in the cache, in which
  2281. * case *rc contains the result of testing
  2282. * the filter f of type filter_type
  2283. * on the value of type in e.
  2284. * rc==-1=>not a filter match
  2285. * rc==0=>a filter match
  2286. * rc>0=>an LDAP error code.
  2287. */
  2288. int
  2289. slapi_entry_vattrcache_findAndTest( const Slapi_Entry *e, const char *type,
  2290. Slapi_Filter *f,
  2291. filter_type_t filter_type,
  2292. int *rc )
  2293. {
  2294. Slapi_Vattr *vattr;
  2295. int r= SLAPI_ENTRY_VATTR_NOT_RESOLVED; /* assume not resolved yet */
  2296. *rc = -1;
  2297. if (! slapi_entry_vattrcache_watermark_isvalid(e)) {
  2298. /* there is not virtual attribute cached or they are all invalid
  2299. * just return
  2300. */
  2301. return r;
  2302. }
  2303. /* Check if the attribute is already cached */
  2304. VATTR_READ_LOCK(e);
  2305. if ((vattr = entry_vattr_lookup_nolock(e, type))) {
  2306. /* That means this 'type' vattr was already evaluated */
  2307. if ((vattr->attr == NULL) || valueset_isempty(&(vattr->attr->a_present_values))) {
  2308. /* this means this is not a virtual attribute for that entry */
  2309. r = SLAPI_ENTRY_VATTR_RESOLVED_ABSENT;
  2310. } else {
  2311. /*
  2312. * this is a cached vattr--test the filter on it.
  2313. */
  2314. r = SLAPI_ENTRY_VATTR_RESOLVED_EXISTS;
  2315. if (filter_type == FILTER_TYPE_AVA) {
  2316. *rc = plugin_call_syntax_filter_ava(vattr->attr,
  2317. f->f_choice,
  2318. &f->f_ava);
  2319. } else if (filter_type == FILTER_TYPE_SUBSTRING) {
  2320. *rc = plugin_call_syntax_filter_sub(NULL, vattr->attr,
  2321. &f->f_sub);
  2322. } else if (filter_type == FILTER_TYPE_PRES) {
  2323. /* type is there, that's all we need to know. */
  2324. *rc = 0;
  2325. }
  2326. }
  2327. }
  2328. VATTR_READ_UNLOCK(e);
  2329. return r;
  2330. }
  2331. /*
  2332. * slapi_entry_vattrcache_find_values_and_type_ex()
  2333. *
  2334. * returns:
  2335. * SLAPI_ENTRY_VATTR_NOT_RESOLVED--not found in vattrcache.
  2336. * SLAPI_ENTRY_VATTR_RESOLVED_ABSENT--found in vattrcache but empty value
  2337. * ==>that vattr type is not present in the
  2338. * entry.
  2339. * SLAPI_ENTRY_VATTR_RESOLVED_EXISTS--found vattr in the vattr cache,
  2340. * in which case **results is a
  2341. * pointer to a duped Slapi_Valueset
  2342. * containing the values of type and
  2343. * **actual_type_name is the actual type
  2344. * name.
  2345. */
  2346. int
  2347. slapi_entry_vattrcache_find_values_and_type_ex( const Slapi_Entry *e,
  2348. const char *type,
  2349. Slapi_ValueSet ***results,
  2350. char ***actual_type_name)
  2351. {
  2352. Slapi_Vattr *vattr;
  2353. int r = SLAPI_ENTRY_VATTR_NOT_RESOLVED; /* assume not resolved yet */
  2354. if (! slapi_entry_vattrcache_watermark_isvalid(e)) {
  2355. /* there is not virtual attribute cached or they are all invalid
  2356. * just return
  2357. */
  2358. return r;
  2359. }
  2360. /* check if the attribute is not already cached */
  2361. VATTR_READ_LOCK(e);
  2362. if ((vattr = entry_vattr_lookup_nolock(e, type))) {
  2363. /* That means this 'type' vattr was already evaluated */
  2364. if ((vattr->attr == NULL) || valueset_isempty(&(vattr->attr->a_present_values))) {
  2365. /* this means this is not a virtual attribute for that entry */
  2366. r = SLAPI_ENTRY_VATTR_RESOLVED_ABSENT;
  2367. } else {
  2368. /*
  2369. * this is a cached vattr
  2370. * return a duped copy of the values and type
  2371. */
  2372. char *vattr_type = NULL;
  2373. r = SLAPI_ENTRY_VATTR_RESOLVED_EXISTS;
  2374. *results = (Slapi_ValueSet**) slapi_ch_calloc(1, sizeof (**results));
  2375. **results = valueset_dup(&(vattr->attr->a_present_values));
  2376. *actual_type_name =
  2377. (char**) slapi_ch_malloc(sizeof (**actual_type_name));
  2378. slapi_attr_get_type(vattr->attr, &vattr_type);
  2379. **actual_type_name = slapi_ch_strdup(vattr_type);
  2380. }
  2381. }
  2382. VATTR_READ_UNLOCK(e);
  2383. return r;
  2384. }
  2385. /*
  2386. * Deprecated in favour of slapi_entry_vattrcache_find_values_and_type_ex()
  2387. * which meshes better with slapi_vattr_values_get_sp_ex().
  2388. */
  2389. SLAPI_DEPRECATED int
  2390. slapi_entry_vattrcache_find_values_and_type( const Slapi_Entry *e,
  2391. const char *type,
  2392. Slapi_ValueSet **results,
  2393. char **actual_type_name)
  2394. {
  2395. Slapi_Vattr *vattr;
  2396. int r= SLAPI_ENTRY_VATTR_NOT_RESOLVED; /* assume not resolved yet */
  2397. if (! slapi_entry_vattrcache_watermark_isvalid(e)) {
  2398. /* there is not virtual attribute cached or they are all invalid
  2399. * just return
  2400. */
  2401. return r;
  2402. }
  2403. /* Check if the attribute is already cached */
  2404. VATTR_READ_LOCK(e);
  2405. if ((vattr = entry_vattr_lookup_nolock(e, type))) {
  2406. /* That means this 'type' vattr was already evaluated */
  2407. if ((vattr->attr == NULL) || valueset_isempty(&(vattr->attr->a_present_values))) {
  2408. /* this means this is not a virtual attribute for that entry */
  2409. r = SLAPI_ENTRY_VATTR_RESOLVED_ABSENT;
  2410. } else {
  2411. /*
  2412. * this is a cached vattr
  2413. * return a duped copy of the values and type
  2414. */
  2415. char *vattr_type = NULL;
  2416. r = SLAPI_ENTRY_VATTR_RESOLVED_EXISTS;
  2417. *results = valueset_dup(&(vattr->attr->a_present_values));
  2418. slapi_attr_get_type(vattr->attr, &vattr_type);
  2419. *actual_type_name = slapi_ch_strdup(vattr_type);
  2420. }
  2421. }
  2422. VATTR_READ_UNLOCK(e);
  2423. return r;
  2424. }
  2425. SLAPI_DEPRECATED int
  2426. slapi_entry_attr_merge( Slapi_Entry *e, const char *type, struct berval **vals )
  2427. {
  2428. Slapi_Value **values= NULL;
  2429. int rc=0;
  2430. valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */
  2431. rc = slapi_entry_attr_merge_sv(e, type, values);
  2432. valuearray_free(&values);
  2433. return(rc);
  2434. }
  2435. int
  2436. slapi_entry_attr_merge_sv(Slapi_Entry *e, const char *type, Slapi_Value **vals )
  2437. {
  2438. attrlist_merge_valuearray( &e->e_attrs, type, vals );
  2439. return 0;
  2440. }
  2441. /*
  2442. * Merge this valuset for type into e's vattrcache list.
  2443. * Creates the type if necessary.
  2444. * Dups valset.
  2445. * Only merge's in cacheable vattrs.
  2446. */
  2447. int
  2448. slapi_entry_vattrcache_merge_sv(Slapi_Entry *e, const char *type,
  2449. Slapi_ValueSet *valset, int buffer_flags)
  2450. {
  2451. Slapi_Value **vals = NULL;
  2452. Slapi_Vattr *vattr;
  2453. /* only attempt to merge if it's a cacheable attribute */
  2454. if ( slapi_vattrcache_iscacheable(type) || (buffer_flags & SLAPI_VIRTUALATTRS_VALUES_CACHEABLE)) {
  2455. VATTR_WRITE_LOCK(e);
  2456. if(!slapi_entry_vattrcache_watermark_isvalid(e))
  2457. {
  2458. /* free the previous set of vattrs */
  2459. entry_vattr_free_nolock(e);
  2460. }
  2461. if(valset)
  2462. vals = valueset_get_valuearray(valset);
  2463. /* Add the vals in the virtual attribute cache */
  2464. vattr = entry_vattr_lookup_nolock(e, type);
  2465. if (vattr) {
  2466. if (vattr->attr) {
  2467. /* virtual attribute already cached, add the value */
  2468. valueset_add_valuearray(&vattr->attr->a_present_values, vals);
  2469. } else if (vals) {
  2470. /* This is not a normal situation, a first SP cached
  2471. * an empty value for this attribute, but now a second SP
  2472. * returns a non NULL value.
  2473. * Possibly watermark should have been updated to clear the cache
  2474. */
  2475. slapi_log_error(SLAPI_LOG_FATAL, "slapi_entry_vattrcache_merge_sv",
  2476. "Virtual attribute %s already cached with empty value, unwilling to cache a different value (%s) \n",
  2477. type, slapi_entry_get_dn(e));
  2478. }
  2479. } else {
  2480. Slapi_Attr *attr = NULL;
  2481. if (vals) {
  2482. /* Create the new virtual attribute */
  2483. attr = slapi_attr_new();
  2484. slapi_attr_init(attr, type);
  2485. /* now add the value */
  2486. valueset_add_valuearray(&attr->a_present_values, vals);
  2487. }
  2488. /* put attr into the virtual attribute cache */
  2489. entry_vattr_add_nolock(e, type, attr);
  2490. }
  2491. slapi_entry_vattrcache_watermark_set(e);
  2492. VATTR_WRITE_UNLOCK(e);
  2493. }
  2494. return 0;
  2495. }
  2496. int
  2497. slapi_entry_attr_delete( Slapi_Entry *e, const char *type )
  2498. {
  2499. return( attrlist_delete(&e->e_attrs, type) );
  2500. }
  2501. SLAPI_DEPRECATED int
  2502. slapi_entry_attr_replace( Slapi_Entry *e, const char *type, struct berval **vals )
  2503. {
  2504. slapi_entry_attr_delete(e, type);
  2505. slapi_entry_attr_merge(e, type, vals);
  2506. return 0;
  2507. }
  2508. int
  2509. slapi_entry_attr_replace_sv( Slapi_Entry *e, const char *type, Slapi_Value **vals )
  2510. {
  2511. slapi_entry_attr_delete(e, type);
  2512. slapi_entry_attr_merge_sv(e, type, vals);
  2513. return 0;
  2514. }
  2515. int
  2516. slapi_entry_add_value (Slapi_Entry *e, const char *type, const Slapi_Value *value)
  2517. {
  2518. Slapi_Attr **a= NULL;
  2519. attrlist_find_or_create(&e->e_attrs, type, &a);
  2520. if(value != (Slapi_Value *) NULL) {
  2521. slapi_valueset_add_attr_value_ext(*a, &(*a)->a_present_values, (Slapi_Value *)value, 0);
  2522. }
  2523. return 0;
  2524. }
  2525. int
  2526. slapi_entry_add_string(Slapi_Entry *e, const char *type, const char *value)
  2527. {
  2528. Slapi_Attr **a= NULL;
  2529. attrlist_find_or_create(&e->e_attrs, type, &a);
  2530. valueset_add_string ( *a, &(*a)->a_present_values, value, CSN_TYPE_UNKNOWN, NULL);
  2531. return 0;
  2532. }
  2533. int
  2534. slapi_entry_delete_string(Slapi_Entry *e, const char *type, const char *value)
  2535. {
  2536. Slapi_Attr *a= attrlist_find(e->e_attrs, type);
  2537. if (a != NULL)
  2538. valueset_remove_string(a,&a->a_present_values, value);
  2539. return 0;
  2540. }
  2541. /* caller must free with slapi_ch_array_free */
  2542. char **
  2543. slapi_entry_attr_get_charray( const Slapi_Entry* e, const char *type)
  2544. {
  2545. int ignore;
  2546. return slapi_entry_attr_get_charray_ext(e, type, &ignore);
  2547. }
  2548. /*
  2549. * The extension also gathers the number of values.
  2550. * The caller must free with slapi_ch_array_free
  2551. */
  2552. char **
  2553. slapi_entry_attr_get_charray_ext( const Slapi_Entry* e, const char *type, int *numVals)
  2554. {
  2555. char **parray = NULL;
  2556. Slapi_Attr* attr = NULL;
  2557. int count = 0;
  2558. slapi_entry_attr_find(e, type, &attr);
  2559. if(numVals == NULL){
  2560. return NULL;
  2561. }
  2562. if(attr!=NULL){
  2563. int hint;
  2564. Slapi_Value *v = NULL;
  2565. for (hint = slapi_attr_first_value(attr, &v);
  2566. hint != -1;
  2567. hint = slapi_attr_next_value(attr, hint, &v))
  2568. {
  2569. const struct berval *bvp = slapi_value_get_berval(v);
  2570. char *p = slapi_ch_malloc(bvp->bv_len + 1);
  2571. memcpy(p, bvp->bv_val, bvp->bv_len);
  2572. p[bvp->bv_len]= '\0';
  2573. charray_add(&parray, p);
  2574. count++;
  2575. }
  2576. }
  2577. *numVals = count;
  2578. return parray;
  2579. }
  2580. char *
  2581. slapi_entry_attr_get_charptr( const Slapi_Entry* e, const char *type)
  2582. {
  2583. char *p= NULL;
  2584. Slapi_Attr* attr = NULL;
  2585. slapi_entry_attr_find(e, type, &attr);
  2586. if(attr!=NULL)
  2587. {
  2588. Slapi_Value *v;
  2589. const struct berval *bvp;
  2590. slapi_valueset_first_value( &attr->a_present_values, &v);
  2591. bvp = slapi_value_get_berval(v);
  2592. p= slapi_ch_malloc(bvp->bv_len + 1);
  2593. memcpy(p, bvp->bv_val, bvp->bv_len);
  2594. p[bvp->bv_len]= '\0';
  2595. }
  2596. return p;
  2597. }
  2598. /* returned value: attribute value as an integer type */
  2599. int
  2600. slapi_entry_attr_get_int( const Slapi_Entry* e, const char *type)
  2601. {
  2602. int r = 0;
  2603. Slapi_Attr* attr = NULL;
  2604. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2605. Slapi_Value *v;
  2606. slapi_valueset_first_value( &attr->a_present_values, &v);
  2607. r= slapi_value_get_int(v);
  2608. }
  2609. return r;
  2610. }
  2611. /* returned value: attribute value as an unsigned integer type */
  2612. unsigned int
  2613. slapi_entry_attr_get_uint( const Slapi_Entry* e, const char *type)
  2614. {
  2615. unsigned int r = 0;
  2616. Slapi_Attr* attr = NULL;
  2617. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2618. Slapi_Value *v;
  2619. slapi_valueset_first_value( &attr->a_present_values, &v);
  2620. r= slapi_value_get_uint(v);
  2621. }
  2622. return r;
  2623. }
  2624. /* returned value: attribute value as a long integer type */
  2625. long
  2626. slapi_entry_attr_get_long( const Slapi_Entry* e, const char *type)
  2627. {
  2628. long r = 0;
  2629. Slapi_Attr* attr = NULL;
  2630. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2631. Slapi_Value *v;
  2632. slapi_valueset_first_value( &attr->a_present_values, &v);
  2633. r = slapi_value_get_long(v);
  2634. }
  2635. return r;
  2636. }
  2637. /* returned value: attribute value as an unsigned long integer type */
  2638. unsigned long
  2639. slapi_entry_attr_get_ulong( const Slapi_Entry* e, const char *type)
  2640. {
  2641. unsigned long r = 0;
  2642. Slapi_Attr* attr = NULL;
  2643. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2644. Slapi_Value *v;
  2645. slapi_valueset_first_value( &attr->a_present_values, &v);
  2646. r = slapi_value_get_ulong(v);
  2647. }
  2648. return r;
  2649. }
  2650. /* returned value: attribute value as a long long integer type */
  2651. long long
  2652. slapi_entry_attr_get_longlong( const Slapi_Entry* e, const char *type)
  2653. {
  2654. long long r = 0;
  2655. Slapi_Attr* attr = NULL;
  2656. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2657. Slapi_Value *v;
  2658. slapi_valueset_first_value( &attr->a_present_values, &v);
  2659. r = slapi_value_get_longlong(v);
  2660. }
  2661. return r;
  2662. }
  2663. /* returned value: attribute value as an unsigned long long integer type */
  2664. unsigned long long
  2665. slapi_entry_attr_get_ulonglong( const Slapi_Entry* e, const char *type)
  2666. {
  2667. unsigned long long r = 0;
  2668. Slapi_Attr* attr = NULL;
  2669. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2670. Slapi_Value *v;
  2671. slapi_valueset_first_value( &attr->a_present_values, &v);
  2672. r = slapi_value_get_ulonglong(v);
  2673. }
  2674. return r;
  2675. }
  2676. /* returned value: attribute value as a boolean type */
  2677. PRBool
  2678. slapi_entry_attr_get_bool_ext(const Slapi_Entry* e, const char *type, PRBool default_value)
  2679. {
  2680. PRBool r = default_value; /* default if no attr */
  2681. Slapi_Attr* attr = NULL;
  2682. if ((0 == slapi_entry_attr_find(e, type, &attr)) && attr) {
  2683. Slapi_Value *v;
  2684. const struct berval *bvp;
  2685. slapi_valueset_first_value( &attr->a_present_values, &v);
  2686. bvp = slapi_value_get_berval(v);
  2687. if ((bvp == NULL) || (bvp->bv_len == 0)) { /* none or empty == false */
  2688. r = PR_FALSE;
  2689. } else if (!PL_strncasecmp(bvp->bv_val, "on", bvp->bv_len)) {
  2690. r = PR_TRUE;
  2691. } else if (!PL_strncasecmp(bvp->bv_val, "off", bvp->bv_len)) {
  2692. r = PR_FALSE;
  2693. } else if (!PL_strncasecmp(bvp->bv_val, "true", bvp->bv_len)) {
  2694. r = PR_TRUE;
  2695. } else if (!PL_strncasecmp(bvp->bv_val, "false", bvp->bv_len)) {
  2696. r = PR_FALSE;
  2697. } else if (!PL_strncasecmp(bvp->bv_val, "yes", bvp->bv_len)) {
  2698. r = PR_TRUE;
  2699. } else if (!PL_strncasecmp(bvp->bv_val, "no", bvp->bv_len)) {
  2700. r = PR_FALSE;
  2701. } else if (!PL_strncmp(bvp->bv_val, "1", bvp->bv_len)) {
  2702. r = PR_TRUE;
  2703. } else if (!PL_strncmp(bvp->bv_val, "0", bvp->bv_len)) {
  2704. r = PR_FALSE;
  2705. } else { /* assume numeric: 0 - false: non-zero - true */
  2706. r = (PRBool)slapi_value_get_ulong(v);
  2707. }
  2708. }
  2709. return r;
  2710. }
  2711. PRBool
  2712. slapi_entry_attr_get_bool(const Slapi_Entry* e, const char *type)
  2713. {
  2714. return slapi_entry_attr_get_bool_ext(e, type, PR_FALSE);
  2715. }
  2716. void
  2717. slapi_entry_attr_set_charptr( Slapi_Entry* e, const char *type, const char *value)
  2718. {
  2719. struct berval bv;
  2720. struct berval *bvals[2];
  2721. if (value) {
  2722. bvals[0] = &bv;
  2723. bvals[1] = NULL;
  2724. bv.bv_val = (char*)value;
  2725. bv.bv_len = strlen( value );
  2726. slapi_entry_attr_replace( e, type, bvals );
  2727. } else {
  2728. slapi_entry_attr_delete( e, type );
  2729. }
  2730. }
  2731. void
  2732. slapi_entry_attr_set_int( Slapi_Entry* e, const char *type, int l)
  2733. {
  2734. char value[16];
  2735. struct berval bv;
  2736. struct berval *bvals[2];
  2737. bvals[0] = &bv;
  2738. bvals[1] = NULL;
  2739. sprintf(value,"%d",l);
  2740. bv.bv_val = value;
  2741. bv.bv_len = strlen( value );
  2742. slapi_entry_attr_replace( e, type, bvals );
  2743. }
  2744. void
  2745. slapi_entry_attr_set_uint( Slapi_Entry* e, const char *type, unsigned int l)
  2746. {
  2747. char value[16];
  2748. struct berval bv;
  2749. struct berval *bvals[2];
  2750. bvals[0] = &bv;
  2751. bvals[1] = NULL;
  2752. sprintf(value,"%u",l);
  2753. bv.bv_val = value;
  2754. bv.bv_len = strlen( value );
  2755. slapi_entry_attr_replace( e, type, bvals );
  2756. }
  2757. void
  2758. slapi_entry_attr_set_long( Slapi_Entry* e, const char *type, long l)
  2759. {
  2760. char value[16];
  2761. struct berval bv;
  2762. struct berval *bvals[2];
  2763. bvals[0] = &bv;
  2764. bvals[1] = NULL;
  2765. sprintf(value,"%ld",l);
  2766. bv.bv_val = value;
  2767. bv.bv_len = strlen( value );
  2768. slapi_entry_attr_replace( e, type, bvals );
  2769. }
  2770. void
  2771. slapi_entry_attr_set_longlong( Slapi_Entry* e, const char *type, long long l)
  2772. {
  2773. char value[20];
  2774. struct berval bv;
  2775. struct berval *bvals[2];
  2776. bvals[0] = &bv;
  2777. bvals[1] = NULL;
  2778. sprintf(value,"%lld",l);
  2779. bv.bv_val = value;
  2780. bv.bv_len = strlen( value );
  2781. slapi_entry_attr_replace( e, type, bvals );
  2782. }
  2783. void
  2784. slapi_entry_attr_set_ulong( Slapi_Entry* e, const char *type, unsigned long l)
  2785. {
  2786. char value[16];
  2787. struct berval bv;
  2788. struct berval *bvals[2];
  2789. bvals[0] = &bv;
  2790. bvals[1] = NULL;
  2791. sprintf(value,"%lu",l);
  2792. bv.bv_val = value;
  2793. bv.bv_len = strlen( value );
  2794. slapi_entry_attr_replace( e, type, bvals );
  2795. }
  2796. int
  2797. slapi_entry_attr_exists(Slapi_Entry *e, const char *type)
  2798. {
  2799. Slapi_Attr *attr;
  2800. if(slapi_entry_attr_find(e, type, &attr) == 0){
  2801. return 1;
  2802. }
  2803. return 0;
  2804. }
  2805. /* JCM: The strcasecmp below should really be a bervalcmp
  2806. * deprecatred in favour of slapi_entry_attr_has_syntax_value
  2807. * which does respect the syntax of the attribute type.
  2808. */
  2809. SLAPI_DEPRECATED int
  2810. slapi_entry_attr_hasvalue(const Slapi_Entry *e, const char *type, const char *value) /* JCM - (const char *) => (struct berval *) */
  2811. {
  2812. int r= 0;
  2813. Slapi_Attr *attr;
  2814. Slapi_Value *sval;
  2815. if(slapi_entry_attr_find(e, type, &attr)==0)
  2816. {
  2817. int i= slapi_attr_first_value( attr, &sval );
  2818. while(!r && i!=-1)
  2819. {
  2820. const struct berval *val= slapi_value_get_berval(sval);
  2821. r= (strcasecmp(val->bv_val,value)==0);
  2822. i= slapi_attr_next_value( attr, i, &sval );
  2823. }
  2824. }
  2825. return r;
  2826. }
  2827. /*
  2828. * Checks if e contains an attr type with a value
  2829. * of value.
  2830. * Unlike slapi_entry_attr_hasvalue(), it does teh comparison
  2831. * respecting the syntax of type.
  2832. *
  2833. * returns non-zero if type has value in e, zero otherwise.
  2834. *
  2835. *
  2836. */
  2837. int
  2838. slapi_entry_attr_has_syntax_value(const Slapi_Entry *e,
  2839. const char *type,
  2840. const Slapi_Value *value)
  2841. {
  2842. int r = 0;
  2843. Slapi_Attr *attr;
  2844. if(e == NULL){
  2845. return r;
  2846. }
  2847. if(slapi_entry_attr_find(e, type, &attr)==0)
  2848. {
  2849. const struct berval *bv = slapi_value_get_berval(value);
  2850. if ( bv != NULL) {
  2851. r = (slapi_attr_value_find(attr, bv) == 0);
  2852. }
  2853. }
  2854. return r;
  2855. }
  2856. int
  2857. slapi_entry_rdn_values_present( const Slapi_Entry *e )
  2858. {
  2859. char **dns, **rdns;
  2860. int i, rc;
  2861. Slapi_Attr *attr;
  2862. struct ava ava;
  2863. const char *dn = slapi_entry_get_dn_const(e);
  2864. if (slapi_is_rootdse(dn))
  2865. return 1; /* the root dse has no RDN, so it should default to TRUE */
  2866. /* JCM Use the Slapi_RDN code */
  2867. rc = 1;
  2868. if ( (dns = slapi_ldap_explode_dn( slapi_entry_get_dn_const(e), 0 )) != NULL )
  2869. {
  2870. if ( (rdns = slapi_ldap_explode_rdn( dns[0], 0 )) != NULL )
  2871. {
  2872. for ( i = 0; rdns[i] != NULL; i++ )
  2873. {
  2874. if ( rdn2ava( rdns[i], &ava ) == 0 )
  2875. {
  2876. char *type = slapi_attr_syntax_normalize( ava.ava_type );
  2877. if ( slapi_entry_attr_find( e, type, &attr ) != 0 )
  2878. {
  2879. rc = 0;
  2880. }
  2881. slapi_ch_free((void **)&type);
  2882. if ( 0 == rc ) { /* attribute not found */
  2883. break;
  2884. }
  2885. if ( slapi_attr_value_find( attr, &(ava.ava_value) ) != 0 )
  2886. {
  2887. rc = 0;
  2888. break; /* value not found */
  2889. }
  2890. }
  2891. }
  2892. slapi_ldap_value_free( rdns );
  2893. } else {
  2894. rc = 0; /* Failure: the RDN seems invalid */
  2895. }
  2896. slapi_ldap_value_free( dns );
  2897. }
  2898. else
  2899. {
  2900. rc = 0; /* failure: the RDN seems to be invalid */
  2901. }
  2902. return( rc );
  2903. }
  2904. int
  2905. slapi_entry_add_rdn_values( Slapi_Entry *e )
  2906. {
  2907. const char *dn;
  2908. char **dns, **rdns;
  2909. const Slapi_DN *sdn;
  2910. int i, rc = LDAP_SUCCESS;
  2911. Slapi_Value *foundVal;
  2912. Slapi_Attr *attr;
  2913. if (NULL == e || NULL == (sdn = slapi_entry_get_sdn_const(e))) {
  2914. return( LDAP_SUCCESS );
  2915. }
  2916. /* Preserve the original in case the RDN is missing as an attr-val pair
  2917. * in the entry. */
  2918. dn = slapi_sdn_get_udn(sdn);
  2919. if (slapi_is_rootdse(dn)) {
  2920. return( LDAP_SUCCESS );
  2921. }
  2922. /* JCM Use the Slapi_RDN code */
  2923. /* make sure RDN values are also in the entry */
  2924. if ( (dns = slapi_ldap_explode_dn( dn, 0 )) == NULL ) {
  2925. return( LDAP_INVALID_DN_SYNTAX );
  2926. }
  2927. if ( (rdns = slapi_ldap_explode_rdn( dns[0], 0 )) == NULL ) {
  2928. slapi_ldap_value_free( dns );
  2929. return( LDAP_INVALID_DN_SYNTAX );
  2930. }
  2931. slapi_ldap_value_free( dns );
  2932. for ( i = 0; rdns[i] != NULL && rc == LDAP_SUCCESS; i++ ) {
  2933. struct ava ava;
  2934. char *type;
  2935. if ( rdn2ava( rdns[i], &ava ) != 0 ) {
  2936. slapi_ldap_value_free( rdns );
  2937. return( LDAP_INVALID_DN_SYNTAX );
  2938. }
  2939. foundVal = NULL;
  2940. type = slapi_attr_syntax_normalize( ava.ava_type );
  2941. if ( slapi_entry_attr_find( e, type, &attr ) == 0 ) {
  2942. rc = plugin_call_syntax_filter_ava_sv(attr, LDAP_FILTER_EQUALITY,
  2943. &ava, &foundVal, 0);
  2944. if (rc == 0 && foundVal != NULL) {
  2945. const struct berval *bv = slapi_value_get_berval(foundVal);
  2946. /*
  2947. * A subtlety to consider is that LDAP does not
  2948. * allow two values which compare the same for
  2949. * equality in an attribute at once.
  2950. */
  2951. if ((ava.ava_value.bv_len != bv->bv_len) ||
  2952. (memcmp(ava.ava_value.bv_val, bv->bv_val, bv->bv_len) != 0)) {
  2953. /* bytes not identical so reject */
  2954. LDAPDebug(LDAP_DEBUG_TRACE, "RDN value is not identical to entry value for type %s in entry %s\n",
  2955. type, dn ? dn : "<null>", 0 );
  2956. #if 0
  2957. /*
  2958. * This would be the right thing to do except that
  2959. * it breaks our own clients.
  2960. */
  2961. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  2962. #endif
  2963. }
  2964. /* exact same ava already present in entry, that's OK */
  2965. }
  2966. }
  2967. if (foundVal == NULL) {
  2968. struct berval *vals[2];
  2969. vals[0] = &ava.ava_value;
  2970. vals[1] = NULL;
  2971. rc = slapi_entry_add_values( e, type, vals );
  2972. }
  2973. slapi_ch_free( (void **)&type );
  2974. }
  2975. slapi_ldap_value_free( rdns );
  2976. return( rc );
  2977. }
  2978. /*
  2979. * Function: slapi_entry_has_children
  2980. *
  2981. * Returns: 0 if "p" has no children, 1 if "p" has children.
  2982. *
  2983. * Description: We (RJP+DB) modified this code to take advantage
  2984. * of the subordinatecount operational attribute that
  2985. * each entry now has.
  2986. *
  2987. * Author/Modifier: RJP
  2988. */
  2989. int
  2990. slapi_entry_has_children_ext(const Slapi_Entry *entry, int include_tombstone)
  2991. {
  2992. Slapi_Attr *attr;
  2993. int count = 0;
  2994. LDAPDebug( LDAP_DEBUG_TRACE, "=> slapi_has_children( %s )\n", slapi_entry_get_dn_const(entry), 0, 0);
  2995. /*If the subordinatecount exists, and it's nonzero, then return 1.*/
  2996. if (slapi_entry_attr_find( entry, "numsubordinates", &attr) == 0)
  2997. {
  2998. Slapi_Value *sval;
  2999. slapi_attr_first_value( attr, &sval );
  3000. if(sval!=NULL)
  3001. {
  3002. const struct berval *bval = slapi_value_get_berval( sval );
  3003. if(bval!=NULL)
  3004. {
  3005. /* The entry has the attribute, and it's non-zero */
  3006. count = strtol(bval->bv_val, (char **)NULL, 10);
  3007. if (count > 0) {
  3008. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "<= slapi_has_children %d\n", count);
  3009. return count;
  3010. }
  3011. }
  3012. }
  3013. }
  3014. /*If the subordinatecount exists, and it's nonzero, then return 1.*/
  3015. if (include_tombstone && (slapi_entry_attr_find( entry, "tombstonenumsubordinates", &attr) == 0))
  3016. {
  3017. Slapi_Value *sval;
  3018. slapi_attr_first_value( attr, &sval );
  3019. if(sval!=NULL)
  3020. {
  3021. const struct berval *bval = slapi_value_get_berval( sval );
  3022. if(bval!=NULL)
  3023. {
  3024. /* The entry has the attribute, and it's non-zero */
  3025. count = strtol(bval->bv_val, (char **)NULL, 10);
  3026. if (count > 0) {
  3027. LDAPDebug1Arg( LDAP_DEBUG_TRACE, "<= slapi_has_tombstone_children %d\n", count);
  3028. return count;
  3029. }
  3030. }
  3031. }
  3032. }
  3033. LDAPDebug( LDAP_DEBUG_TRACE, "<= slapi_has_children 0\n", 0, 0, 0 );
  3034. return(0);
  3035. }
  3036. int
  3037. slapi_entry_has_children(const Slapi_Entry *entry)
  3038. {
  3039. return slapi_entry_has_children_ext(entry, 0);
  3040. }
  3041. /*
  3042. * Renames an entry to simulate a MODRDN operation
  3043. */
  3044. int
  3045. slapi_entry_rename(Slapi_Entry *e, const char *newrdn, int deleteoldrdn, Slapi_DN *newsuperior)
  3046. {
  3047. int err = LDAP_SUCCESS;
  3048. Slapi_DN *olddn = NULL;
  3049. Slapi_Mods *smods = NULL;
  3050. Slapi_DN newsrdn;
  3051. LDAPDebug( LDAP_DEBUG_TRACE, "=> slapi_entry_rename\n", 0, 0, 0 );
  3052. slapi_sdn_init(&newsrdn);
  3053. /* Check if entry or newrdn are NULL. */
  3054. if (!e || !newrdn) {
  3055. err = LDAP_PARAM_ERROR;
  3056. goto done;
  3057. }
  3058. /* Get the old DN. */
  3059. olddn = slapi_entry_get_sdn(e);
  3060. /* If deleteoldrdn, find old RDN values and remove them from the entry. */
  3061. if (deleteoldrdn) {
  3062. char *type = NULL;
  3063. char * val = NULL;
  3064. int num_rdns = 0;
  3065. int i = 0;
  3066. Slapi_RDN *oldrdn = slapi_rdn_new_sdn(olddn);
  3067. /* Create mods based on the number of rdn elements. */
  3068. num_rdns = slapi_rdn_get_num_components(oldrdn);
  3069. smods = slapi_mods_new();
  3070. slapi_mods_init(smods, num_rdns + 2);
  3071. /* Loop through old rdns and construct a mod to remove the value. */
  3072. for (i = 0; i < num_rdns; i++) {
  3073. if (slapi_rdn_get_next(oldrdn, i, &type, &val) != -1) {
  3074. slapi_mods_add(smods, LDAP_MOD_DELETE, type, strlen(val), val);
  3075. }
  3076. }
  3077. slapi_rdn_free(&oldrdn);
  3078. /* Apply the mods to the entry. */
  3079. if ((err = slapi_entry_apply_mods(e, slapi_mods_get_ldapmods_byref(smods))) != LDAP_SUCCESS) {
  3080. /* A problem was encountered applying the mods. Bail. */
  3081. goto done;
  3082. }
  3083. }
  3084. /* We remove the parentid and entrydn since the backend will change these.
  3085. * We don't want to give the caller an inconsistent entry. */
  3086. slapi_entry_attr_delete(e, "parentid");
  3087. slapi_entry_attr_delete(e, "entrydn");
  3088. /* Build new DN. If newsuperior is set, just use "newrdn,newsuperior". If
  3089. * newsuperior is not set, need to add newrdn to old superior. */
  3090. slapi_sdn_init_dn_byref(&newsrdn, newrdn);
  3091. if (newsuperior) {
  3092. slapi_sdn_set_parent(&newsrdn, newsuperior);
  3093. } else {
  3094. Slapi_DN oldparent;
  3095. slapi_sdn_init(&oldparent);
  3096. slapi_sdn_get_parent(olddn, &oldparent);
  3097. slapi_sdn_set_parent(&newsrdn, &oldparent);
  3098. slapi_sdn_done(&oldparent);
  3099. }
  3100. /* Set the new DN in the entry. This hands off the memory used by newdn to the entry. */
  3101. slapi_entry_set_sdn(e, &newsrdn);
  3102. /* Set the RDN in the entry. */
  3103. /* note - there isn't a slapi_entry_set_rdn_from_sdn function */
  3104. slapi_rdn_done(slapi_entry_get_srdn(e));
  3105. slapi_rdn_init_all_sdn(slapi_entry_get_srdn(e),&newsrdn);
  3106. /* Add RDN values to entry. */
  3107. err = slapi_entry_add_rdn_values(e);
  3108. done:
  3109. slapi_mods_free(&smods);
  3110. slapi_sdn_done(&newsrdn);
  3111. LDAPDebug( LDAP_DEBUG_TRACE, "<= slapi_entry_rename\n", 0, 0, 0 );
  3112. return err;
  3113. }
  3114. /*
  3115. * Apply a set of modifications to an entry
  3116. */
  3117. int
  3118. slapi_entry_apply_mods( Slapi_Entry *e, LDAPMod **mods )
  3119. {
  3120. return entry_apply_mods(e, mods);
  3121. }
  3122. /*
  3123. * Apply a single mod to an entry
  3124. */
  3125. int slapi_entry_apply_mod( Slapi_Entry *e, LDAPMod *mod )
  3126. {
  3127. return entry_apply_mod(e, mod);
  3128. }
  3129. int
  3130. entry_apply_mods( Slapi_Entry *e, LDAPMod **mods )
  3131. {
  3132. return entry_apply_mods_ignore_error(e, mods, -1);
  3133. }
  3134. int
  3135. entry_apply_mods_ignore_error( Slapi_Entry *e, LDAPMod **mods, int ignore_error )
  3136. {
  3137. int err;
  3138. LDAPMod **mp = NULL;
  3139. LDAPDebug( LDAP_DEBUG_TRACE, "=> entry_apply_mods\n", 0, 0, 0 );
  3140. err = LDAP_SUCCESS;
  3141. for ( mp = mods; mp && *mp; mp++ )
  3142. {
  3143. err = entry_apply_mod( e, *mp );
  3144. if(err == ignore_error){
  3145. (*mp)->mod_op = LDAP_MOD_IGNORE;
  3146. } else if ( err != LDAP_SUCCESS ) {
  3147. break;
  3148. }
  3149. }
  3150. LDAPDebug( LDAP_DEBUG_TRACE, "<= entry_apply_mods %d\n", err, 0, 0 );
  3151. return( err );
  3152. }
  3153. /*
  3154. * apply mod and store the result in the extension
  3155. * return value: 1 - mod is applied and stored in extension
  3156. * -1 - mod is applied and failed
  3157. * 0 - mod is nothing to do with extension
  3158. */
  3159. int
  3160. slapi_entry_apply_mod_extension(Slapi_Entry *e, const LDAPMod *mod, int modcnt)
  3161. {
  3162. Slapi_Value **vals = NULL;
  3163. struct attrs_in_extension *aiep;
  3164. int err = LDAP_SUCCESS;
  3165. int rc = 0; /* by default, mod is nothing to do with extension */
  3166. if ((NULL == e) || (NULL == mod)) {
  3167. return err;
  3168. }
  3169. if (modcnt < 0) {
  3170. int i;
  3171. for (i = 0; mod->mod_bvalues && mod->mod_bvalues[i]; i++) ;
  3172. modcnt = i;
  3173. }
  3174. for (aiep = attrs_in_extension; aiep && aiep->ext_type; aiep++) {
  3175. vals = NULL;
  3176. if (0 == strcasecmp(mod->mod_type, aiep->ext_type)) {
  3177. rc = 1;
  3178. switch (mod->mod_op & ~LDAP_MOD_BVALUES) {
  3179. case LDAP_MOD_ADD:
  3180. if (modcnt > 0) {
  3181. valuearray_init_bervalarray(mod->mod_bvalues, &vals);
  3182. if (vals) {
  3183. /* vals is consumed if successful. */
  3184. err = aiep->ext_set(e, vals, SLAPI_EXT_SET_ADD);
  3185. if (err) {
  3186. slapi_log_error(SLAPI_LOG_FATAL, "entry_apply_mod",
  3187. "ADD: Failed to set %s to extension\n",
  3188. aiep->ext_type);
  3189. valuearray_free(&vals);
  3190. goto bail;
  3191. }
  3192. } else {
  3193. slapi_log_error(SLAPI_LOG_FATAL, "entry_apply_mod",
  3194. "ADD: %s has no values\n",
  3195. aiep->ext_type);
  3196. goto bail;
  3197. }
  3198. }
  3199. break;
  3200. case LDAP_MOD_DELETE:
  3201. if (modcnt > 0) {
  3202. err = aiep->ext_get(e, &vals);
  3203. if (err) {
  3204. slapi_log_error(SLAPI_LOG_FATAL, "entry_apply_mod",
  3205. "DEL: Failed to get %s from extension\n",
  3206. aiep->ext_type);
  3207. goto bail;
  3208. }
  3209. if (vals && *vals) {
  3210. Slapi_Value **myvals = NULL;
  3211. valuearray_add_valuearray(&myvals, vals, 0);
  3212. err = valuearray_subtract_bvalues(myvals,
  3213. mod->mod_bvalues);
  3214. if (err > 0) { /* err values are subtracted */
  3215. /*
  3216. * mvals contains original values minus
  3217. * to-be-deleted value. ext_set replaces the
  3218. * original value with the delta.
  3219. */
  3220. /* myvals is consumed if successful. */
  3221. err = aiep->ext_set(e, myvals, SLAPI_EXT_SET_REPLACE);
  3222. if (err) {
  3223. slapi_log_error(SLAPI_LOG_FATAL,
  3224. "entry_apply_mod",
  3225. "DEL: Failed to set %s "
  3226. "to extension\n",
  3227. aiep->ext_type);
  3228. valuearray_free(&myvals);
  3229. goto bail;
  3230. }
  3231. }
  3232. }
  3233. } else {
  3234. /* ext_set replaces the existing value with NULL */
  3235. err = aiep->ext_set(e, NULL, SLAPI_EXT_SET_REPLACE);
  3236. if (err) {
  3237. slapi_log_error(SLAPI_LOG_FATAL, "entry_apply_mod",
  3238. "DEL: Failed to set %s to extension\n",
  3239. aiep->ext_type);
  3240. goto bail;
  3241. }
  3242. }
  3243. break;
  3244. case LDAP_MOD_REPLACE:
  3245. if (modcnt > 0) {
  3246. /* ext_set replaces the existing value with the new value */
  3247. valuearray_init_bervalarray(mod->mod_bvalues, &vals);
  3248. if (vals) {
  3249. /* vals is consumed if successful. */
  3250. err = aiep->ext_set(e, vals, SLAPI_EXT_SET_REPLACE);
  3251. if (err) {
  3252. slapi_log_error(SLAPI_LOG_FATAL, "entry_apply_mod",
  3253. "REPLACE: Failed to set %s to extension\n",
  3254. aiep->ext_type);
  3255. valuearray_free(&vals);
  3256. goto bail;
  3257. }
  3258. } else {
  3259. slapi_log_error(SLAPI_LOG_FATAL, "entry_apply_mod",
  3260. "REPLACE: %s has no values\n",
  3261. aiep->ext_type);
  3262. goto bail;
  3263. }
  3264. }
  3265. break;
  3266. default:
  3267. rc = 0;
  3268. break;
  3269. }
  3270. }
  3271. }
  3272. bail:
  3273. if (rc > 0) {
  3274. if (err) {
  3275. rc = -1;
  3276. } else {
  3277. rc = 1;
  3278. }
  3279. }
  3280. return rc;
  3281. }
  3282. /*
  3283. * Apply a modification to an entry
  3284. */
  3285. int
  3286. entry_apply_mod( Slapi_Entry *e, const LDAPMod *mod )
  3287. {
  3288. int i;
  3289. int bvcnt;
  3290. int err = LDAP_SUCCESS;
  3291. PRBool sawsubentry=PR_FALSE;
  3292. for ( i = 0; mod->mod_bvalues != NULL && mod->mod_bvalues[i] != NULL; i++ ) {
  3293. if((strcasecmp(mod->mod_type,"objectclass") == 0)
  3294. && (strncasecmp((const char *)mod->mod_bvalues[i]->bv_val,"ldapsubentry",mod->mod_bvalues[i]->bv_len) == 0))
  3295. sawsubentry=PR_TRUE;
  3296. LDAPDebug( LDAP_DEBUG_ARGS, " %s: %s\n", mod->mod_type, mod->mod_bvalues[i]->bv_val, 0 );
  3297. }
  3298. bvcnt = i;
  3299. /*
  3300. * If err == 0, apply mod.
  3301. * If err == 1, mod is successfully set to extension.
  3302. * If err == -1, setting mod to extension failed.
  3303. */
  3304. err = slapi_entry_apply_mod_extension(e, mod, bvcnt);
  3305. if (err) {
  3306. if (1 == err) {
  3307. err = LDAP_SUCCESS;
  3308. } else {
  3309. err = LDAP_OPERATIONS_ERROR;
  3310. }
  3311. goto done;
  3312. }
  3313. switch ( mod->mod_op & ~LDAP_MOD_BVALUES )
  3314. {
  3315. case LDAP_MOD_ADD:
  3316. LDAPDebug( LDAP_DEBUG_ARGS, " add: %s\n", mod->mod_type, 0, 0 );
  3317. if(sawsubentry) e->e_flags |= SLAPI_ENTRY_LDAPSUBENTRY;
  3318. err = slapi_entry_add_values( e, mod->mod_type, mod->mod_bvalues );
  3319. break;
  3320. case LDAP_MOD_DELETE:
  3321. LDAPDebug( LDAP_DEBUG_ARGS, " delete: %s\n", mod->mod_type, 0, 0 );
  3322. if(sawsubentry) e->e_flags |= 0;
  3323. err = slapi_entry_delete_values( e, mod->mod_type, mod->mod_bvalues );
  3324. break;
  3325. case LDAP_MOD_REPLACE:
  3326. LDAPDebug( LDAP_DEBUG_ARGS, " replace: %s\n", mod->mod_type, 0, 0 );
  3327. err = entry_replace_values( e, mod->mod_type, mod->mod_bvalues );
  3328. break;
  3329. }
  3330. done:
  3331. LDAPDebug( LDAP_DEBUG_ARGS, " -\n", 0, 0, 0 );
  3332. return( err );
  3333. }
  3334. /*
  3335. * Add an array of "vals" to entry "e".
  3336. */
  3337. SLAPI_DEPRECATED int
  3338. slapi_entry_add_values(
  3339. Slapi_Entry *e,
  3340. const char *type,
  3341. struct berval **vals
  3342. )
  3343. {
  3344. Slapi_Value **values= NULL;
  3345. int rc=0;
  3346. valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */
  3347. rc=slapi_entry_add_values_sv(e,type,values);
  3348. valuearray_free(&values);
  3349. return(rc);
  3350. }
  3351. /*
  3352. * Add an array of "vals" to entry "e".
  3353. */
  3354. int
  3355. slapi_entry_add_values_sv(Slapi_Entry *e,
  3356. const char *type,
  3357. Slapi_Value **vals)
  3358. {
  3359. int rc= LDAP_SUCCESS;
  3360. if (valuearray_isempty(vals))
  3361. {
  3362. /*
  3363. * No values to add (unexpected but acceptable).
  3364. */
  3365. }
  3366. else
  3367. {
  3368. Slapi_Attr **a= NULL;
  3369. Slapi_Attr **alist= &e->e_attrs;
  3370. attrlist_find_or_create(alist, type, &a);
  3371. if (slapi_attr_is_dn_syntax_attr(*a)) {
  3372. valuearray_dn_normalize_value(vals);
  3373. (*a)->a_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CES;
  3374. }
  3375. rc= attr_add_valuearray(*a,vals,slapi_entry_get_dn_const(e));
  3376. }
  3377. return( rc );
  3378. }
  3379. /*
  3380. * Add a value set of "vs" to entry "e".
  3381. *
  3382. * 0 is success anything else failure.
  3383. */
  3384. int
  3385. slapi_entry_add_valueset(Slapi_Entry *e, const char *type, Slapi_ValueSet *vs)
  3386. {
  3387. Slapi_Value *v;
  3388. int i= slapi_valueset_first_value(vs,&v);
  3389. while(i!=-1) {
  3390. slapi_entry_add_value( e, type, v);
  3391. i= slapi_valueset_next_value(vs,i,&v);
  3392. }/* while */
  3393. return(0);
  3394. }
  3395. /*
  3396. * Delete an array of bervals from entry.
  3397. *
  3398. * Note that if this function fails, it leaves the values for "type" within
  3399. * "e" in an indeterminate state. The present value set may be truncated.
  3400. */
  3401. SLAPI_DEPRECATED int
  3402. slapi_entry_delete_values(
  3403. Slapi_Entry *e,
  3404. const char *type,
  3405. struct berval **vals
  3406. )
  3407. {
  3408. Slapi_Value **values= NULL;
  3409. int rc=0;
  3410. valuearray_init_bervalarray(vals,&values); /* JCM SLOW FUNCTION */
  3411. rc=slapi_entry_delete_values_sv(e,type,values);
  3412. valuearray_free(&values);
  3413. return(rc);
  3414. }
  3415. static int
  3416. delete_values_sv_internal(
  3417. Slapi_Entry *e,
  3418. const char *type,
  3419. Slapi_Value **valuestodelete,
  3420. int flags
  3421. )
  3422. {
  3423. Slapi_Attr *a;
  3424. int retVal= LDAP_SUCCESS;
  3425. /*
  3426. * If type is in the protected_attrs_all list, we could ignore the failure,
  3427. * as the attribute could only exist in the entry in the memory when the
  3428. * add/mod operation is done, while the retried entry from the db does not
  3429. * contain the attribute.
  3430. */
  3431. #if defined(USE_OLD_UNHASHED)
  3432. if (is_type_protected(type) || is_type_forbidden(type))
  3433. #else
  3434. if (is_type_protected(type))
  3435. #endif
  3436. {
  3437. flags |= SLAPI_VALUE_FLAG_IGNOREERROR;
  3438. }
  3439. /* delete the entire attribute */
  3440. if ( valuestodelete == NULL || valuestodelete[0] == NULL ){
  3441. LDAPDebug( LDAP_DEBUG_ARGS, "removing entire attribute %s\n",
  3442. type, 0, 0 );
  3443. retVal = attrlist_delete( &e->e_attrs, type);
  3444. if (flags & SLAPI_VALUE_FLAG_IGNOREERROR) {
  3445. return LDAP_SUCCESS;
  3446. }
  3447. return(retVal ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_SUCCESS);
  3448. }
  3449. /* delete specific values - find the attribute first */
  3450. a= attrlist_find(e->e_attrs, type);
  3451. if ( a == NULL ) {
  3452. LDAPDebug( LDAP_DEBUG_ARGS, "could not find attribute %s\n",
  3453. type, 0, 0 );
  3454. if (flags & SLAPI_VALUE_FLAG_IGNOREERROR) {
  3455. return LDAP_SUCCESS;
  3456. }
  3457. return( LDAP_NO_SUCH_ATTRIBUTE );
  3458. }
  3459. {
  3460. retVal= valueset_remove_valuearray(&a->a_present_values, a, valuestodelete, flags, NULL);
  3461. if(retVal==LDAP_SUCCESS)
  3462. {
  3463. /*
  3464. * all values have been deleted -- remove entire attribute
  3465. */
  3466. if ( valueset_isempty(&a->a_present_values) )
  3467. {
  3468. attrlist_delete( &e->e_attrs, a->a_type );
  3469. }
  3470. }
  3471. else
  3472. {
  3473. /* Failed
  3474. * - Duplicate value
  3475. * - Value not found
  3476. * - Operations error
  3477. */
  3478. if ( retVal==LDAP_OPERATIONS_ERROR )
  3479. {
  3480. LDAPDebug( LDAP_DEBUG_ANY, "Possible existing duplicate "
  3481. "value for attribute type %s found in "
  3482. "entry %s\n", a->a_type, slapi_entry_get_dn_const(e), 0 );
  3483. }
  3484. if (flags & SLAPI_VALUE_FLAG_IGNOREERROR) {
  3485. retVal = LDAP_SUCCESS;
  3486. }
  3487. }
  3488. }
  3489. return( retVal );
  3490. }
  3491. /*
  3492. * Delete an array of present values from an entry.
  3493. *
  3494. * Note that if this function fails, it leaves the values for "type" within
  3495. * "e" in an indeterminate state. The present value set may be truncated.
  3496. */
  3497. int
  3498. slapi_entry_delete_values_sv(
  3499. Slapi_Entry *e,
  3500. const char *type,
  3501. Slapi_Value **valuestodelete
  3502. )
  3503. {
  3504. return( delete_values_sv_internal( e, type, valuestodelete,
  3505. 0 /* Do Not Ignore Errors */ ));
  3506. }
  3507. int
  3508. entry_replace_values(
  3509. Slapi_Entry *e,
  3510. const char *type,
  3511. struct berval **vals
  3512. )
  3513. {
  3514. return attrlist_replace( &e->e_attrs, type, vals );
  3515. }
  3516. int
  3517. entry_replace_values_with_flags(
  3518. Slapi_Entry *e,
  3519. const char *type,
  3520. struct berval **vals,
  3521. int flags
  3522. )
  3523. {
  3524. return attrlist_replace_with_flags( &e->e_attrs, type, vals, flags );
  3525. }
  3526. int
  3527. slapi_entry_flag_is_set( const Slapi_Entry *e, unsigned char flag )
  3528. {
  3529. return( e->e_flags & flag );
  3530. }
  3531. void slapi_entry_set_flag( Slapi_Entry *e, unsigned char flag)
  3532. {
  3533. e->e_flags |= flag;
  3534. }
  3535. void slapi_entry_clear_flag( Slapi_Entry *e, unsigned char flag)
  3536. {
  3537. e->e_flags &= ~flag;
  3538. }
  3539. /*
  3540. * Add the missing values in `vals' to an entry.
  3541. *
  3542. * Note that if this function fails, it leaves the values for "type" within
  3543. * "e" in an indeterminate state. The present value set may be truncated.
  3544. */
  3545. int
  3546. slapi_entry_merge_values_sv(
  3547. Slapi_Entry *e,
  3548. const char *type,
  3549. Slapi_Value **vals
  3550. )
  3551. {
  3552. int rc;
  3553. rc = delete_values_sv_internal( e, type, vals, SLAPI_VALUE_FLAG_IGNOREERROR );
  3554. if ( rc == LDAP_SUCCESS || rc == LDAP_NO_SUCH_ATTRIBUTE ) {
  3555. rc = slapi_entry_attr_merge_sv( e, type, vals );
  3556. }
  3557. return( rc );
  3558. }
  3559. void
  3560. send_referrals_from_entry(Slapi_PBlock *pb, Slapi_Entry *referral)
  3561. {
  3562. Slapi_Value *val=NULL;
  3563. Slapi_Attr *attr=NULL;
  3564. int i=0, numValues=0;
  3565. struct berval **refscopy=NULL;
  3566. struct berval **url=NULL;
  3567. slapi_entry_attr_find( referral, "ref", &attr );
  3568. if(attr != NULL) {
  3569. slapi_attr_get_numvalues(attr, &numValues );
  3570. if(numValues > 0) {
  3571. url=(struct berval **) slapi_ch_malloc((numValues + 1) * sizeof(struct berval*));
  3572. }
  3573. for (i = slapi_attr_first_value(attr, &val); i != -1;
  3574. i = slapi_attr_next_value(attr, i, &val)) {
  3575. url[i]=(struct berval*)slapi_value_get_berval(val);
  3576. }
  3577. url[numValues]=NULL;
  3578. }
  3579. refscopy = ref_adjust(pb, url, slapi_entry_get_sdn(referral), 0);
  3580. send_ldap_result(pb, LDAP_REFERRAL,
  3581. slapi_entry_get_dn(referral), NULL, 0, refscopy );
  3582. if(url != NULL) {
  3583. slapi_ch_free( (void **)&url );
  3584. }
  3585. if ( refscopy != NULL ) {
  3586. ber_bvecfree( refscopy );
  3587. }
  3588. }
  3589. /*
  3590. * slapi_entry_diff: perform diff between entry e1 and e2
  3591. * and set mods to smods which updates e1 to e2.
  3592. * diff_ctrl: SLAPI_DUMP_NOOPATTRS => skip operational attributes
  3593. */
  3594. void
  3595. slapi_entry_diff(Slapi_Mods *smods, Slapi_Entry *e1, Slapi_Entry *e2, int diff_ctrl)
  3596. {
  3597. Slapi_Attr *e1_attr = NULL;
  3598. Slapi_Attr *e2_attr = NULL;
  3599. char *e1_attr_name = NULL;
  3600. char *e2_attr_name = NULL;
  3601. int rval = 0;
  3602. slapi_mods_init(smods, 0);
  3603. for (slapi_entry_first_attr(e1, &e1_attr); e1_attr;
  3604. slapi_entry_next_attr(e1, e1_attr, &e1_attr))
  3605. {
  3606. /* skip operational attributes if not requested */
  3607. if ((diff_ctrl & SLAPI_DUMP_NOOPATTRS) &&
  3608. slapi_attr_flag_is_set(e1_attr, SLAPI_ATTR_FLAG_OPATTR))
  3609. continue;
  3610. slapi_attr_get_type(e1_attr, &e1_attr_name);
  3611. rval = slapi_entry_attr_find(e2, e1_attr_name, &e2_attr);
  3612. if (0 == rval)
  3613. {
  3614. int i;
  3615. Slapi_Value *e1_val;
  3616. /* attr e1_attr_names is shared with e2 */
  3617. /* XXX: not very efficient.
  3618. * needs to be rewritten for the schema w/ lots of attributes
  3619. */
  3620. for (i = slapi_attr_first_value(e1_attr, &e1_val); i != -1;
  3621. i = slapi_attr_next_value(e1_attr, i, &e1_val))
  3622. {
  3623. if (0 != slapi_attr_value_find(e2_attr,
  3624. slapi_value_get_berval(e1_val)))
  3625. {
  3626. /* attr-value e1_val not found in e2_attr; add it */
  3627. LDAPDebug(LDAP_DEBUG_TRACE,
  3628. "slapi_entry_diff: attr-val of %s is not in e2; "
  3629. "add it\n",
  3630. e1_attr_name, 0, 0);
  3631. slapi_mods_add(smods, LDAP_MOD_ADD, e1_attr_name,
  3632. e1_val->bv.bv_len, e1_val->bv.bv_val);
  3633. }
  3634. }
  3635. }
  3636. else
  3637. {
  3638. /* attr e1_attr_names not found in e2 */
  3639. LDAPDebug(LDAP_DEBUG_TRACE,
  3640. "slapi_entry_diff: attr %s is not in e2; add it\n",
  3641. e1_attr_name, 0, 0);
  3642. slapi_mods_add_mod_values(smods, LDAP_MOD_ADD,
  3643. e1_attr_name,
  3644. attr_get_present_values(e1_attr));
  3645. }
  3646. }
  3647. /* if the attribute is multi-valued, the untouched values should be put */
  3648. for (slapi_entry_first_attr(e2, &e2_attr); e2_attr;
  3649. slapi_entry_next_attr(e2, e2_attr, &e2_attr)) {
  3650. /* skip operational attributes if not requested */
  3651. if ((diff_ctrl & SLAPI_DUMP_NOOPATTRS) &&
  3652. slapi_attr_flag_is_set(e2_attr, SLAPI_ATTR_FLAG_OPATTR))
  3653. continue;
  3654. slapi_attr_get_type(e2_attr, &e2_attr_name);
  3655. rval = slapi_entry_attr_find(e1, e2_attr_name, &e1_attr);
  3656. if (0 == rval)
  3657. {
  3658. int i;
  3659. Slapi_Value *e2_val;
  3660. /* attr e2_attr_names is shared with e1 */
  3661. /* XXX: not very efficient.
  3662. * needs to be rewritten for the schema w/ lots of attributes
  3663. */
  3664. for (i = slapi_attr_first_value(e2_attr, &e2_val); i != -1;
  3665. i = slapi_attr_next_value(e2_attr, i, &e2_val))
  3666. {
  3667. if (0 != slapi_attr_value_find(e1_attr,
  3668. slapi_value_get_berval(e2_val)))
  3669. {
  3670. /* attr-value e2_val not found in e1_attr; delete it */
  3671. LDAPDebug(LDAP_DEBUG_TRACE,
  3672. "slapi_entry_diff: attr-val of %s is not in e1; "
  3673. "delete it\n",
  3674. e2_attr_name, 0, 0);
  3675. slapi_mods_add(smods, LDAP_MOD_DELETE, e2_attr_name,
  3676. e2_val->bv.bv_len, e2_val->bv.bv_val);
  3677. }
  3678. }
  3679. }
  3680. else
  3681. {
  3682. /* attr e2_attr_names not in e1 */
  3683. LDAPDebug(LDAP_DEBUG_TRACE,
  3684. "slapi_entry_diff: attr %s is not in e1; delete it\n",
  3685. e2_attr_name, 0, 0);
  3686. slapi_mods_add_mod_values(smods, LDAP_MOD_DELETE, e2_attr_name, NULL);
  3687. }
  3688. }
  3689. return;
  3690. }
  3691. /* delete the entry (and sub entries if any) specified with dn */
  3692. static void
  3693. delete_subtree(Slapi_PBlock *pb, const char *dn, void *plg_id)
  3694. {
  3695. Slapi_PBlock mypb;
  3696. int ret = 0;
  3697. int opresult;
  3698. slapi_search_internal_set_pb(pb, dn, LDAP_SCOPE_SUBTREE, "(objectclass=*)",
  3699. NULL, 0, NULL, NULL, plg_id, 0);
  3700. slapi_search_internal_pb(pb);
  3701. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret);
  3702. if (ret == LDAP_SUCCESS) {
  3703. Slapi_Entry **entries = NULL;
  3704. Slapi_Entry **ep = NULL;
  3705. Slapi_DN *rootDN = slapi_sdn_new_dn_byval(dn);
  3706. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  3707. for (ep = entries; ep && *ep; ep++) {
  3708. const Slapi_DN *sdn = slapi_entry_get_sdn_const(*ep);
  3709. if (slapi_sdn_compare(sdn, rootDN) == 0)
  3710. continue;
  3711. pblock_init(&mypb);
  3712. slapi_delete_internal_set_pb(&mypb, slapi_sdn_get_dn(sdn),
  3713. NULL, NULL, plg_id, 0);
  3714. slapi_delete_internal_pb(&mypb);
  3715. slapi_pblock_get(&mypb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  3716. pblock_done(&mypb);
  3717. }
  3718. slapi_sdn_free(&rootDN);
  3719. }
  3720. pblock_done(pb);
  3721. pblock_init(pb);
  3722. slapi_delete_internal_set_pb(pb, dn, NULL, NULL, plg_id, 0);
  3723. slapi_delete_internal_pb(pb);
  3724. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  3725. pblock_done(pb);
  3726. }
  3727. /*
  3728. * slapi_entries_diff: diff between entry array old_entries and curr_entries
  3729. * (testall == 0) => return immediately after the 1st diff
  3730. * (testall != 0) => scan all the entries
  3731. * (force_update == 0) => just print the diff info
  3732. * (force_update != 0) => force to go back to old
  3733. *
  3734. * return 0, if identical
  3735. * return 1, otherwise
  3736. */
  3737. int
  3738. slapi_entries_diff(Slapi_Entry **old_entries, Slapi_Entry **curr_entries,
  3739. int testall, const char *logging_prestr,
  3740. const int force_update, void *plg_id)
  3741. {
  3742. char *my_logging_prestr = "";
  3743. Slapi_Entry **oep, **cep;
  3744. int rval = 0;
  3745. Slapi_PBlock pb;
  3746. #define SLAPI_ENTRY_FLAG_DIFF_IN_BOTH 0x80
  3747. if (NULL != logging_prestr && '\0' != *logging_prestr)
  3748. {
  3749. my_logging_prestr = slapi_ch_smprintf("%s ", logging_prestr);
  3750. }
  3751. for (oep = old_entries; oep != NULL && *oep != NULL; oep++) {
  3752. for (cep = curr_entries; cep != NULL && *cep != NULL; cep++) {
  3753. if (!slapi_sdn_compare(slapi_entry_get_sdn_const(*oep),
  3754. slapi_entry_get_sdn_const(*cep))) {
  3755. Slapi_Mods *smods = slapi_mods_new();
  3756. LDAPMod *mod;
  3757. int isfirst = 1;
  3758. /* check the attr diff and do modify */
  3759. slapi_entry_diff(smods, *oep, *cep, SLAPI_DUMP_NOOPATTRS);
  3760. for (mod = slapi_mods_get_first_mod(smods);
  3761. mod != NULL;
  3762. mod = slapi_mods_get_next_mod(smods))
  3763. {
  3764. rval = 1;
  3765. if (isfirst)
  3766. {
  3767. LDAPDebug(LDAP_DEBUG_ANY, "%sEntry %s\n", my_logging_prestr,
  3768. slapi_entry_get_dn_const(*oep), 0);
  3769. isfirst = 0;
  3770. }
  3771. switch (mod->mod_op & ~LDAP_MOD_BVALUES)
  3772. {
  3773. case LDAP_MOD_DELETE:
  3774. LDAPDebug(LDAP_DEBUG_ANY,
  3775. " Del Attribute %s Value %s\n",
  3776. mod->mod_type, mod->mod_bvalues?
  3777. mod->mod_bvalues[0]->bv_val:"N/A", 0);
  3778. break;
  3779. case LDAP_MOD_ADD:
  3780. LDAPDebug(LDAP_DEBUG_ANY,
  3781. " Add Attribute %s Value %s\n",
  3782. mod->mod_type, mod->mod_bvalues[0]->bv_val, 0);
  3783. break;
  3784. case LDAP_MOD_REPLACE:
  3785. LDAPDebug(LDAP_DEBUG_ANY,
  3786. " Rep Attribute %s Value %s\n",
  3787. mod->mod_type, mod->mod_bvalues[0]->bv_val, 0);
  3788. break;
  3789. default:
  3790. LDAPDebug(LDAP_DEBUG_ANY,
  3791. " Unknown op %d Attribute %s\n",
  3792. mod->mod_op & ~LDAP_MOD_BVALUES,
  3793. mod->mod_type, 0);
  3794. break;
  3795. }
  3796. if (!testall)
  3797. {
  3798. slapi_mods_free(&smods);
  3799. goto out;
  3800. }
  3801. }
  3802. if (0 == isfirst && force_update && testall)
  3803. {
  3804. pblock_init(&pb);
  3805. slapi_modify_internal_set_pb_ext(&pb,
  3806. slapi_entry_get_sdn_const(*oep),
  3807. slapi_mods_get_ldapmods_byref(smods),
  3808. NULL, NULL, plg_id, 0);
  3809. slapi_modify_internal_pb(&pb);
  3810. pblock_done(&pb);
  3811. }
  3812. slapi_mods_free(&smods);
  3813. slapi_entry_set_flag(*oep, SLAPI_ENTRY_FLAG_DIFF_IN_BOTH);
  3814. slapi_entry_set_flag(*cep, SLAPI_ENTRY_FLAG_DIFF_IN_BOTH);
  3815. }
  3816. }
  3817. }
  3818. for (oep = old_entries; oep != NULL && *oep != NULL; oep++) {
  3819. if (slapi_entry_flag_is_set(*oep, SLAPI_ENTRY_FLAG_DIFF_IN_BOTH)) {
  3820. slapi_entry_clear_flag(*oep, SLAPI_ENTRY_FLAG_DIFF_IN_BOTH);
  3821. } else {
  3822. rval = 1;
  3823. LDAPDebug(LDAP_DEBUG_ANY, "Add %sEntry %s\n",
  3824. my_logging_prestr, slapi_entry_get_dn_const(*oep), 0);
  3825. if (testall)
  3826. {
  3827. if (force_update)
  3828. {
  3829. LDAPMod **mods;
  3830. slapi_entry2mods(*oep, NULL, &mods);
  3831. pblock_init(&pb);
  3832. slapi_add_internal_set_pb(&pb,
  3833. slapi_entry_get_dn_const(*oep), mods, NULL, plg_id, 0);
  3834. slapi_add_internal_pb(&pb);
  3835. freepmods(mods);
  3836. pblock_done(&pb);
  3837. }
  3838. }
  3839. else
  3840. {
  3841. goto out;
  3842. }
  3843. }
  3844. }
  3845. for (cep = curr_entries; cep != NULL && *cep != NULL; cep++) {
  3846. if (slapi_entry_flag_is_set(*cep, SLAPI_ENTRY_FLAG_DIFF_IN_BOTH)) {
  3847. slapi_entry_clear_flag(*cep, SLAPI_ENTRY_FLAG_DIFF_IN_BOTH);
  3848. } else {
  3849. rval = 1;
  3850. LDAPDebug(LDAP_DEBUG_ANY, "Del %sEntry %s\n",
  3851. my_logging_prestr, slapi_entry_get_dn_const(*cep), 0);
  3852. if (testall)
  3853. {
  3854. if (force_update) {
  3855. pblock_init(&pb);
  3856. delete_subtree(&pb, slapi_entry_get_dn_const(*cep), plg_id);
  3857. pblock_done(&pb);
  3858. }
  3859. }
  3860. else
  3861. {
  3862. goto out;
  3863. }
  3864. }
  3865. }
  3866. out:
  3867. if (NULL != logging_prestr && '\0' != *logging_prestr)
  3868. slapi_ch_free_string(&my_logging_prestr);
  3869. return rval;
  3870. }
  3871. /* a helper function to set special rdn to a tombstone entry */
  3872. /* Since this a tombstone, it requires a special treatment for rdn*/
  3873. static int
  3874. _entry_set_tombstone_rdn(Slapi_Entry *e, const char *normdn)
  3875. {
  3876. int rc = 0;
  3877. char *tombstone_rdn = slapi_ch_strdup(normdn);
  3878. if ((0 == PL_strncasecmp(tombstone_rdn, SLAPI_ATTR_UNIQUEID,
  3879. sizeof(SLAPI_ATTR_UNIQUEID) - 1)) &&
  3880. (NULL == PL_strstr(tombstone_rdn, RUV_STORAGE_ENTRY_UNIQUEID))) {
  3881. /* dn starts with "nsuniqueid=" and this is not an RUV */
  3882. char *sepp = PL_strchr(tombstone_rdn, ',');
  3883. /* dn looks like this:
  3884. * nsuniqueid=042d8081-...-ca8fe9f7,uid=tuser,o=abc.com
  3885. * create a new srdn for the original dn
  3886. * uid=tuser,o=abc.com
  3887. */
  3888. if (sepp) {
  3889. Slapi_RDN mysrdn = {0};
  3890. rc = slapi_rdn_init_all_dn(&mysrdn, sepp + 1);
  3891. if (rc) {
  3892. slapi_log_error(SLAPI_LOG_FATAL, "_entry_set_tombstone_rdn",
  3893. "Failed to convert DN %s to RDN\n", sepp + 1);
  3894. slapi_rdn_done(&mysrdn);
  3895. goto bail;
  3896. }
  3897. sepp = PL_strchr(sepp + 1, ',');
  3898. if (sepp) {
  3899. /* nsuniqueid=042d8081-...-ca8fe9f7,uid=tuser, */
  3900. /* ^ */
  3901. *sepp = '\0';
  3902. slapi_rdn_replace_rdn(&mysrdn, tombstone_rdn);
  3903. slapi_entry_set_srdn(e, &mysrdn);
  3904. }
  3905. slapi_rdn_done(&mysrdn);
  3906. }
  3907. }
  3908. bail:
  3909. slapi_ch_free_string(&tombstone_rdn);
  3910. return rc;
  3911. }