entry.c 121 KB

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