entry.c 129 KB

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