1
0

entry.c 89 KB

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