entry.c 109 KB

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