entry.c 117 KB

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