entry.c 120 KB

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