entry.c 128 KB

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