entry.c 104 KB

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