entry.c 110 KB

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