entry.c 120 KB

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