entry.c 104 KB

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