ldapfct.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063
  1. #ident "ldclt @(#)ldapfct.c 1.68 01/05/04"
  2. /** BEGIN COPYRIGHT BLOCK
  3. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  4. * Copyright (C) 2006 Red Hat, Inc.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. /*
  14. FILE : ldapfct.c
  15. AUTHOR : Jean-Luc SCHWING
  16. VERSION : 1.0
  17. DATE : 04 December 1998
  18. DESCRIPTION :
  19. This file contains the ldap part of this tool.
  20. */
  21. #include <stdio.h> /* printf(), etc... */
  22. #include <string.h> /* strcpy(), etc... */
  23. #include <errno.h> /* errno, etc... */
  24. #include <stdlib.h> /* malloc(), etc... */
  25. #include <lber.h> /* ldap C-API BER declarations */
  26. #include <ldap.h> /* ldap C-API declarations */
  27. #ifdef LDAP_H_FROM_QA_WKA
  28. #include <proto-ldap.h> /* ldap C-API prototypes */
  29. #endif
  30. #include <unistd.h> /* close(), etc... */
  31. #include <pthread.h> /* pthreads(), etc... */
  32. #include "port.h" /* Portability definitions */ /*JLS 29-11-00*/
  33. #include "ldclt.h" /* This tool's include file */
  34. #include "utils.h" /* Utilities functions */ /*JLS 14-11-00*/
  35. #include <sasl.h>
  36. #include "ldaptool-sasl.h"
  37. #if !defined(USE_OPENLDAP)
  38. #include <ldap_ssl.h> /* ldapssl_init(), etc... */
  39. #endif
  40. #include <prprf.h>
  41. #include <plstr.h>
  42. #include <nspr.h>
  43. #include <nss.h>
  44. #include <ssl.h>
  45. #include <pk11pub.h>
  46. #define LDCLT_DEREF_ATTR "secretary"
  47. int ldclt_create_deref_control( LDAP *ld, char *derefAttr, char **attrs, LDAPControl **ctrlp );
  48. #if !defined(USE_OPENLDAP)
  49. int ldclt_build_control( char *oid, BerElement *ber, int freeber, char iscritical, LDAPControl **ctrlp );
  50. #endif
  51. int ldclt_alloc_ber( LDAP *ld, BerElement **berp );
  52. static SSLVersionRange enabledNSSVersions;
  53. /* ****************************************************************************
  54. FUNCTION : my_ldap_err2string
  55. PURPOSE : This function is targeted to encapsulate the standard
  56. function ldap_err2string(), that sometimes returns
  57. a NULL pointer and thus crashes the appicaliton :-(
  58. INPUT : err = error to decode
  59. OUTPUT : None.
  60. RETURN : A string that describes the error.
  61. DESCRIPTION :
  62. *****************************************************************************/
  63. char *
  64. my_ldap_err2string (
  65. int err)
  66. {
  67. if (ldap_err2string (err) == NULL)
  68. return ("ldap_err2string() returns a NULL pointer !!!");
  69. else
  70. return (ldap_err2string(err));
  71. }
  72. /* ****************************************************************************
  73. FUNCTION : dnFromMessage
  74. PURPOSE : Extract the matcheddnp value from an LDAP (error)
  75. message.
  76. INPUT : tttctx = thread context.
  77. res = result to parse
  78. OUTPUT : None.
  79. RETURN : The matcheddnp or an error string.
  80. DESCRIPTION :
  81. *****************************************************************************/
  82. char *
  83. dnFromMessage (
  84. thread_context *tttctx,
  85. LDAPMessage *res)
  86. {
  87. static char *notFound = "*** not found by ldclt ***";
  88. int ret;
  89. int errcodep;
  90. /*
  91. * Maybe a previous call to free...
  92. */
  93. if (tttctx->matcheddnp)
  94. ldap_memfree (tttctx->matcheddnp);
  95. /*
  96. * Get the requested information
  97. */
  98. ret = ldap_parse_result (tttctx->ldapCtx, res, &errcodep,
  99. &(tttctx->matcheddnp), NULL, NULL, NULL, 0);
  100. switch (ret)
  101. {
  102. case LDAP_SUCCESS:
  103. case LDAP_MORE_RESULTS_TO_RETURN:
  104. return (tttctx->matcheddnp);
  105. case LDAP_NO_RESULTS_RETURNED:
  106. case LDAP_DECODING_ERROR:
  107. case LDAP_PARAM_ERROR:
  108. case LDAP_NO_MEMORY:
  109. default:
  110. tttctx->matcheddnp = NULL;
  111. printf ("ldclt[%d]: T%03d: Cannot ldap_parse_result(), error=%d (%s)\n",
  112. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  113. fflush (stdout);
  114. return (notFound);
  115. }
  116. }
  117. /* New function */ /*JLS 03-05-01*/
  118. /* ****************************************************************************
  119. FUNCTION : getBindAndPasswdFromFile
  120. PURPOSE : Get the new bindDN and passwd to use from a dlf.
  121. INPUT : tttctx = this thread context
  122. OUTPUT : None.
  123. RETURN : -1 if error, 0 else.
  124. DESCRIPTION :
  125. *****************************************************************************/
  126. int
  127. getBindAndPasswdFromFile (
  128. thread_context *tttctx)
  129. {
  130. int num; /* Temp. number */
  131. int i; /* For the loops */
  132. /*
  133. * The bind DN is before the first '\t'
  134. */
  135. num = (lrand48() % mctx.rndBindDlf->strNb);
  136. for (i=0 ; mctx.rndBindDlf->str[num][i]!='\0' &&
  137. mctx.rndBindDlf->str[num][i]!='\t' ; i++);
  138. if (mctx.rndBindDlf->str[num][i] == '\0')
  139. {
  140. printf ("ldclt[%d]: %s: No bind DN find line %d of %s\n",
  141. mctx.pid, tttctx->thrdId, num+1, mctx.rndBindFname);
  142. return (-1);
  143. }
  144. strncpy (tttctx->bufBindDN, mctx.rndBindDlf->str[num], i);
  145. tttctx->bufBindDN[i] = '\0';
  146. /*
  147. * Skip the '\t' to find the password.
  148. * The password is from this place up to the end of the line.
  149. */
  150. while (mctx.rndBindDlf->str[num][i]!='\0' &&
  151. mctx.rndBindDlf->str[num][i]=='\t')
  152. i++;
  153. if (mctx.rndBindDlf->str[num][i] == '\0')
  154. {
  155. printf ("ldclt[%d]: %s: No password find line %d of %s\n",
  156. mctx.pid, tttctx->thrdId, num+1, mctx.rndBindFname);
  157. return (-1);
  158. }
  159. strcpy (tttctx->bufPasswd, &(mctx.rndBindDlf->str[num][i]));
  160. return (0);
  161. }
  162. /* ****************************************************************************
  163. FUNCTION : buildNewBindDN
  164. PURPOSE : Purpose of the fct
  165. INPUT : tttctx = this thread context
  166. OUTPUT : None.
  167. RETURN : -1 if error, 0 else.
  168. DESCRIPTION :
  169. *****************************************************************************/
  170. int
  171. buildNewBindDN (
  172. thread_context *tttctx)
  173. {
  174. /*
  175. * Maybe we should get the bindDN and passwd from a file ?
  176. */
  177. if (mctx.mod2 & M2_RNDBINDFILE)
  178. return (getBindAndPasswdFromFile (tttctx));
  179. /*
  180. * If we shouldn't operate with a variable bind DN, then the buffers
  181. * are already initiated with the fixed values...
  182. */
  183. if (!(mctx.mode & RANDOM_BINDDN))
  184. return (0);
  185. /*
  186. * Generate the random value we will use for both the bind DN
  187. * and the password.
  188. */
  189. if (mctx.mode & STRING)
  190. (void) randomString (tttctx, mctx.bindDNNbDigit);
  191. else
  192. rnd (tttctx->buf2, mctx.bindDNLow, mctx.bindDNHigh, (mctx.mod2 & M2_NOZEROPAD) ? 0 : mctx.bindDNNbDigit);
  193. /*
  194. * First, randomize the bind DN.
  195. */
  196. strncpy (&(tttctx->bufBindDN[tttctx->startBindDN]), tttctx->buf2,
  197. mctx.bindDNNbDigit);
  198. if (mctx.mode & VERY_VERBOSE)
  199. printf ("ldclt[%d]: T%03d: bind DN=\"%s\"\n",
  200. mctx.pid, tttctx->thrdNum, tttctx->bufBindDN);
  201. /*
  202. * Second, randomize the bind password.
  203. */
  204. strncpy (&(tttctx->bufPasswd[tttctx->startPasswd]), tttctx->buf2,
  205. mctx.passwdNbDigit);
  206. if (mctx.mode & VERY_VERBOSE)
  207. printf ("ldclt[%d]: T%03d: bind passwd=\"%s\"\n",
  208. mctx.pid, tttctx->thrdNum, tttctx->bufPasswd);
  209. /*
  210. * No problem found.
  211. */
  212. return (0);
  213. }
  214. #if defined(USE_OPENLDAP)
  215. int
  216. refRebindProc(
  217. LDAP *ldapCtx,
  218. const char *url,
  219. ber_tag_t request,
  220. ber_int_t msgid,
  221. void *arg
  222. )
  223. {
  224. thread_context *tttctx;
  225. struct berval cred;
  226. tttctx = (thread_context *)arg;
  227. cred.bv_val = tttctx->bufPasswd;
  228. cred.bv_len = strlen(tttctx->bufPasswd);
  229. return ldap_sasl_bind_s(ldapCtx, tttctx->bufBindDN, LDAP_SASL_SIMPLE,
  230. &cred, NULL, NULL, NULL);
  231. }
  232. #else /* !USE_OPENLDAP */
  233. /* New function */ /*JLS 08-03-01*/
  234. /* ****************************************************************************
  235. FUNCTION : refRebindProc
  236. PURPOSE : This function is intended to perform the rebind when
  237. a referral requires it.
  238. INPUT : None.
  239. OUTPUT : None.
  240. RETURN : Always LDAP_SUCCESS for the moment...
  241. DESCRIPTION :
  242. *****************************************************************************/
  243. int
  244. refRebindProc (
  245. LDAP *ldapCtx,
  246. char **dnp,
  247. char **passwdp,
  248. int *authmethodp,
  249. int freeit,
  250. void *arg)
  251. {
  252. thread_context *tttctx;
  253. tttctx = (thread_context *)arg;
  254. /*
  255. * We will assume here that the same DN and passwd will be used to
  256. * bind to the referred server, so we will just get the values used
  257. * previously from the thread's context.
  258. */
  259. *dnp = tttctx->bufBindDN;
  260. *passwdp = tttctx->bufPasswd;
  261. *authmethodp = LDAP_AUTH_SIMPLE;
  262. /*
  263. * What should we do with the "freeit" argument ? I do not have any
  264. * memory to free, so let's just ignore it.
  265. */
  266. return (LDAP_SUCCESS);
  267. }
  268. #endif /* !USE_OPENLDAP */
  269. /* ****************************************************************************
  270. FUNCTION : referralSetup
  271. PURPOSE : Initiates referral features. This function is called
  272. once after the ldap_init().
  273. INPUT : tttctx = this thread's thread_context
  274. OUTPUT : None.
  275. RETURN : -1 if error, 0 else.
  276. DESCRIPTION :
  277. *****************************************************************************/
  278. int
  279. referralSetup (
  280. thread_context *tttctx)
  281. {
  282. int ret; /* Return value */
  283. void *ref; /* Referral settings */
  284. /*
  285. * Set the referral options...
  286. */
  287. if (mctx.referral == REFERRAL_OFF)
  288. ref = LDAP_OPT_OFF;
  289. else
  290. ref = LDAP_OPT_ON;
  291. ret = ldap_set_option (tttctx->ldapCtx, LDAP_OPT_REFERRALS, &ref);
  292. if (ret < 0)
  293. {
  294. printf ("ldclt[%d]: %s: Cannot ldap_set_option(LDAP_OPT_REFERRALS)\n",
  295. mctx.pid, tttctx->thrdId);
  296. fflush (stdout);
  297. return (-1);
  298. }
  299. /*
  300. * Maybe the user would like to have an authenticated referral rebind ?
  301. * Note : at 09-03-01 ldap_set_rebind_proc() is a void return function
  302. * Note : cannot compile on _WIN32 without the cast... even if I cast to
  303. * the same thing !!!!
  304. */
  305. if (mctx.referral == REFERRAL_REBIND)
  306. ldap_set_rebind_proc (tttctx->ldapCtx, refRebindProc, (void *)tttctx);
  307. /*
  308. * Normal end
  309. */
  310. return (0);
  311. }
  312. #if defined(USE_OPENLDAP)
  313. /* ****************************************************************************
  314. FUNCTION : dirname
  315. PURPOSE : given a relative or absolute path name, return
  316. the name of the directory containing the path
  317. INPUT : path
  318. OUTPUT : none
  319. RETURN : directory part of path or "."
  320. DESCRIPTION : caller must free return value when done
  321. *****************************************************************************/
  322. static char *
  323. ldclt_dirname(const char *path) {
  324. char sep = PR_GetDirectorySeparator();
  325. char *ptr = NULL;
  326. char *ret = NULL;
  327. if (path && ((ptr = strrchr(path, sep))) && *(ptr+1)) {
  328. ret = PL_strndup(path, ptr-path);
  329. } else {
  330. ret = PL_strdup(".");
  331. }
  332. return ret;
  333. }
  334. static char *
  335. ldclt_get_sec_pwd(PK11SlotInfo *slot, PRBool retry, void *arg)
  336. {
  337. char *pwd = (char *)arg;
  338. return PL_strdup(pwd);
  339. }
  340. static int
  341. ldclt_clientauth(thread_context *tttctx, LDAP *ld, const char *path, const char *certname, const char *pwd)
  342. {
  343. const char *colon = NULL;
  344. char *token_name = NULL;
  345. PK11SlotInfo *slot = NULL;
  346. int rc = 0;
  347. int thrdNum = 0;
  348. if (tttctx) {
  349. thrdNum = tttctx->thrdNum;
  350. }
  351. if ((colon = PL_strchr(certname, ':' ))) {
  352. token_name = PL_strndup(certname, colon-certname);
  353. }
  354. if (token_name) {
  355. slot = PK11_FindSlotByName(token_name);
  356. } else {
  357. slot = PK11_GetInternalKeySlot();
  358. }
  359. if (!slot) {
  360. printf ("ldclt[%d]: T%03d: Cannot find slot for token %s - %d\n",
  361. mctx.pid, thrdNum,
  362. token_name ? token_name : "internal", PR_GetError());
  363. fflush(stdout);
  364. goto done;
  365. }
  366. NSS_SetDomesticPolicy();
  367. PK11_SetPasswordFunc(ldclt_get_sec_pwd);
  368. rc = PK11_Authenticate(slot, PR_FALSE, (void *)pwd);
  369. if (rc != SECSuccess) {
  370. printf ("ldclt[%d]: T%03d: Cannot authenticate to token %s - %d\n",
  371. mctx.pid, thrdNum,
  372. token_name ? token_name : "internal", PR_GetError());
  373. fflush(stdout);
  374. goto done;
  375. }
  376. if ((rc = ldap_set_option(ld, LDAP_OPT_X_TLS_CERTFILE, certname))) {
  377. printf ("ldclt[%d]: T%03d: Cannot ldap_set_option(ld, LDAP_OPT_X_CERTFILE, %s), errno=%d ldaperror=%d:%s\n",
  378. mctx.pid, thrdNum, certname, errno, rc, my_ldap_err2string(rc));
  379. fflush (stdout);
  380. goto done;
  381. }
  382. if ((rc = ldap_set_option(ld, LDAP_OPT_X_TLS_KEYFILE, pwd))) {
  383. printf ("ldclt[%d]: T%03d: Cannot ldap_set_option(ld, LDAP_OPT_X_KEYFILE, %s), errno=%d ldaperror=%d:%s\n",
  384. mctx.pid, thrdNum, pwd, errno, rc, my_ldap_err2string(rc));
  385. fflush (stdout);
  386. goto done;
  387. }
  388. done:
  389. PL_strfree(token_name);
  390. if (slot) {
  391. PK11_FreeSlot(slot);
  392. }
  393. return rc;
  394. }
  395. /* need mutex around ldap_initialize - see https://fedorahosted.org/389/ticket/348 */
  396. static PRCallOnceType ol_init_callOnce = {0,0};
  397. static PRLock *ol_init_lock = NULL;
  398. static PRStatus
  399. internal_ol_init_init(void)
  400. {
  401. PR_ASSERT(NULL == ol_init_lock);
  402. if ((ol_init_lock = PR_NewLock()) == NULL) {
  403. PRErrorCode errorCode = PR_GetError();
  404. printf("internal_ol_init_init PR_NewLock failed %d\n", errorCode);
  405. return PR_FAILURE;
  406. }
  407. return PR_SUCCESS;
  408. }
  409. #endif /* USE_OPENLDAP */
  410. /* mctx is a global */
  411. LDAP *
  412. connectToLDAP(thread_context *tttctx, const char *bufBindDN, const char *bufPasswd, unsigned int mode, unsigned int mod2)
  413. {
  414. LDAP *ld = NULL;
  415. struct berval cred = {0, NULL};
  416. int v2v3 = LDAP_VERSION3;
  417. const char *passwd = NULL;
  418. #if defined(USE_OPENLDAP)
  419. char *ldapurl = NULL;
  420. #endif
  421. int thrdNum = 0;
  422. int ret = -1;
  423. int binded = 0;
  424. SSLVersionRange range;
  425. if (tttctx) {
  426. thrdNum = tttctx->thrdNum;
  427. }
  428. #if defined(USE_OPENLDAP)
  429. if (mctx.ldapurl != NULL) {
  430. ldapurl = PL_strdup(mctx.ldapurl);
  431. } else {
  432. ldapurl = PR_smprintf("ldap%s://%s:%d/",
  433. (mode & SSL) ? "s" : "",
  434. mctx.hostname, mctx.port);
  435. }
  436. if (PR_SUCCESS != PR_CallOnce(&ol_init_callOnce, internal_ol_init_init)) {
  437. printf("Could not perform internal ol_init init\n");
  438. goto done;
  439. }
  440. PR_Lock(ol_init_lock);
  441. if ((ret = ldap_initialize(&ld, ldapurl))) {
  442. PR_Unlock(ol_init_lock);
  443. printf ("ldclt[%d]: T%03d: Cannot ldap_initialize (%s), errno=%d ldaperror=%d:%s\n",
  444. mctx.pid, thrdNum, ldapurl, errno, ret, my_ldap_err2string(ret));
  445. fflush (stdout);
  446. goto done;
  447. }
  448. PR_Unlock(ol_init_lock);
  449. PR_smprintf_free(ldapurl);
  450. ldapurl = NULL;
  451. if (mode & SSL) {
  452. int optval = 0;
  453. /* bad, but looks like the tools expect to be able to use an ip address
  454. for the hostname, so have to defeat fqdn checking in cn of subject of server cert */
  455. int ssl_strength = LDAP_OPT_X_TLS_NEVER;
  456. char *certdir = ldclt_dirname(mctx.certfile);
  457. if ((ret = ldap_set_option(ld, LDAP_OPT_X_TLS_REQUIRE_CERT, &ssl_strength))) {
  458. printf ("ldclt[%d]: T%03d: Cannot ldap_set_option(ld, LDAP_OPT_X_TLS_REQUIRE_CERT), errno=%d ldaperror=%d:%s\n",
  459. mctx.pid, thrdNum, errno, ret, my_ldap_err2string(ret));
  460. fflush (stdout);
  461. free(certdir);
  462. goto done;
  463. }
  464. /* tell it where our cert db is */
  465. if ((ret = ldap_set_option(ld, LDAP_OPT_X_TLS_CACERTDIR, certdir))) {
  466. printf ("ldclt[%d]: T%03d: Cannot ldap_set_option(ld, LDAP_OPT_X_CACERTDIR, %s), errno=%d ldaperror=%d:%s\n",
  467. mctx.pid, thrdNum, certdir, errno, ret, my_ldap_err2string(ret));
  468. fflush (stdout);
  469. free(certdir);
  470. goto done;
  471. }
  472. /* Initialize NSS */
  473. ret = NSS_Initialize(certdir, "", "", SECMOD_DB, NSS_INIT_READONLY);
  474. if (ret != SECSuccess) {
  475. printf ("ldclt[%d]: T%03d: Cannot NSS_Initialize(%s) %d\n",
  476. mctx.pid, thrdNum, certdir, PR_GetError());
  477. fflush(stdout);
  478. goto done;
  479. }
  480. /* Set supported SSL version range. */
  481. SSL_VersionRangeGetSupported(ssl_variant_stream, &enabledNSSVersions);
  482. range.min = enabledNSSVersions.min;
  483. range.max = enabledNSSVersions.max;
  484. SSL_VersionRangeSetDefault(ssl_variant_stream, &range);
  485. if ((mode & CLTAUTH) &&
  486. (ret = ldclt_clientauth(tttctx, ld, certdir, mctx.cltcertname, mctx.keydbpin))) {
  487. free(certdir);
  488. goto done;
  489. }
  490. if ((ret = ldap_set_option(ld, LDAP_OPT_X_TLS_NEWCTX, &optval))) {
  491. printf ("ldclt[%d]: T%03d: Cannot ldap_set_option(ld, LDAP_OPT_X_TLS_NEWCTX), errno=%d ldaperror=%d:%s\n",
  492. mctx.pid, thrdNum, errno, ret, my_ldap_err2string(ret));
  493. fflush (stdout);
  494. free(certdir);
  495. goto done;
  496. }
  497. free(certdir);
  498. }
  499. #else /* !USE_OPENLDAP */
  500. /*
  501. * SSL is enabled ?
  502. */
  503. if (mode & SSL) {
  504. /*
  505. * LDAP session initialization in SSL mode
  506. * added by: B Kolics (11/10/00)
  507. */
  508. ld = ldapssl_init(mctx.hostname, mctx.port, 1);
  509. if (mode & VERY_VERBOSE)
  510. printf ("ldclt[%d]: T%03d: After ldapssl_init (%s, %d), ldapCtx=0x%p\n",
  511. mctx.pid, thrdNum, mctx.hostname, mctx.port,
  512. ld);
  513. if (ld == NULL) {
  514. printf ("ldclt[%d]: T%03d: Cannot ldapssl_init (%s, %d), errno=%d\n",
  515. mctx.pid, thrdNum, mctx.hostname, mctx.port, errno);
  516. fflush (stdout);
  517. ret = -1;
  518. goto done;
  519. }
  520. /*
  521. * Client authentication is used ?
  522. */
  523. if (mode & CLTAUTH) {
  524. ret = ldapssl_enable_clientauth(ld, "", mctx.keydbpin, mctx.cltcertname);
  525. if (mode & VERY_VERBOSE)
  526. printf
  527. ("ldclt[%d]: T%03d: After ldapssl_enable_clientauth (ldapCtx=0x%p, %s, %s)",
  528. mctx.pid, thrdNum, ld, mctx.keydbpin,
  529. mctx.cltcertname);
  530. if (ret < 0) {
  531. printf
  532. ("ldclt[%d]: T%03d: Cannot ldapssl_enable_clientauth (ldapCtx=0x%p, %s, %s)",
  533. mctx.pid, thrdNum, ld, mctx.keydbpin, mctx.cltcertname);
  534. ldap_perror(ld, "ldapssl_enable_clientauth");
  535. fflush (stdout);
  536. goto done;
  537. }
  538. }
  539. } else {
  540. /*
  541. * connection initialization in normal, unencrypted mode
  542. */
  543. ld = ldap_init (mctx.hostname, mctx.port);
  544. if (mode & VERY_VERBOSE)
  545. printf ("ldclt[%d]: T%03d: After ldap_init (%s, %d), ldapCtx=0x%p\n",
  546. mctx.pid, thrdNum, mctx.hostname, mctx.port,
  547. ld);
  548. if (ld == NULL) {
  549. printf ("ldclt[%d]: T%03d: Cannot ldap_init (%s, %d), errno=%d\n",
  550. mctx.pid, thrdNum, mctx.hostname, mctx.port, errno);
  551. fflush (stdout);
  552. ret = -1;
  553. goto done;
  554. }
  555. }
  556. #endif /* !USE_OPENLDAP */
  557. if (mode & CLTAUTH) {
  558. passwd = NULL;
  559. } else {
  560. passwd = bufPasswd?bufPasswd:mctx.passwd;
  561. if (passwd) {
  562. cred.bv_val = (char *)passwd;
  563. cred.bv_len = strlen(passwd);
  564. }
  565. }
  566. if (mode & LDAP_V2)
  567. v2v3 = LDAP_VERSION2;
  568. else
  569. v2v3 = LDAP_VERSION3;
  570. ret = ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &v2v3);
  571. if (ret < 0) {
  572. printf ("ldclt[%d]: T%03d: Cannot ldap_set_option(LDAP_OPT_PROTOCOL_VERSION)\n",
  573. mctx.pid, thrdNum);
  574. fflush (stdout);
  575. ret = -1;
  576. goto done;
  577. }
  578. /*
  579. * Set the referral options...
  580. */
  581. if (tttctx && (referralSetup (tttctx) < 0)) {
  582. ret = -1;
  583. goto done;
  584. }
  585. /*
  586. * Let's save some time here... If no bindDN is provided, the tool is
  587. * working in anonymous mode, i.e. we may consider it is always
  588. * binded.
  589. * NOTE : maybe some cleanup is needed with the tests mctx.bindDN!=NULL
  590. * below in this function ?
  591. * 03-05-01 : no cleanup I think, cf M2_RNDBINDFILE
  592. */
  593. if ((bufBindDN == NULL) && (mctx.bindDN == NULL) &&
  594. ((!(mod2 & M2_RNDBINDFILE)) && (!(mod2 & M2_SASLAUTH)))) {/*JLS 05-03-01*/
  595. if (tttctx) {
  596. tttctx->binded = 1; /*JLS 05-03-01*/
  597. }
  598. ret = 0;
  599. goto done;
  600. } /*JLS 05-03-01*/
  601. /*
  602. * Maybe we should bind ?
  603. */
  604. /*
  605. * for SSL client authentication, SASL BIND is used
  606. */
  607. if (tttctx) {
  608. binded = tttctx->binded;
  609. }
  610. if ((mode & CLTAUTH) && ((!(binded)) ||
  611. (mode & BIND_EACH_OPER))) {
  612. if (mode & VERY_VERBOSE)
  613. printf ("ldclt[%d]: T%03d: Before ldap_sasl_bind_s\n",
  614. mctx.pid, thrdNum);
  615. ret = ldap_sasl_bind_s (ld, "", "EXTERNAL", NULL, NULL, NULL,
  616. NULL);
  617. if (mode & VERY_VERBOSE)
  618. printf ("ldclt[%d]: T%03d: After ldap_sasl_bind_s\n",
  619. mctx.pid, thrdNum);
  620. if (ret == LDAP_SUCCESS) { /*JLS 18-12-00*/
  621. if (tttctx) {
  622. tttctx->binded = 1; /*JLS 18-12-00*/
  623. }
  624. } else { /*JLS 18-12-00*/
  625. if (tttctx) {
  626. tttctx->binded = 0; /*JLS 18-12-00*/
  627. }
  628. if (ignoreError (ret)) { /*JLS 18-12-00*/
  629. if (!(mode & QUIET)) { /*JLS 18-12-00*/
  630. printf ("ldclt[%d]: T%03d: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
  631. mctx.pid, thrdNum, ret, my_ldap_err2string (ret));
  632. fflush (stdout); /*JLS 18-12-00*/
  633. } /*JLS 18-12-00*/
  634. if (addErrorStat (ret) < 0) /*JLS 18-12-00*/
  635. ret = -1;
  636. else
  637. ret = 0;
  638. goto done;
  639. } else { /*JLS 18-12-00*/
  640. printf ("ldclt[%d]: T%03d: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
  641. mctx.pid, thrdNum, ret, my_ldap_err2string (ret));
  642. fflush (stdout); /*JLS 18-12-00*/
  643. if (tttctx)
  644. tttctx->exitStatus = EXIT_NOBIND; /*JLS 18-12-00*/
  645. (void)addErrorStat(ret);
  646. ret = -1;
  647. goto done;
  648. } /*JLS 18-12-00*/
  649. }
  650. } else if ((mod2 & M2_SASLAUTH) && ((!(binded)) ||
  651. (mode & BIND_EACH_OPER))) {
  652. void *defaults;
  653. char *my_saslauthid = NULL;
  654. if ( mctx.sasl_mech == NULL) {
  655. fprintf( stderr, "Please specify the SASL mechanism name when "
  656. "using SASL options\n");
  657. ret = -1;
  658. goto done;
  659. }
  660. if ( mctx.sasl_secprops != NULL) {
  661. ret = ldap_set_option( ld, LDAP_OPT_X_SASL_SECPROPS,
  662. (void *) mctx.sasl_secprops );
  663. if ( ret != LDAP_SUCCESS ) {
  664. fprintf( stderr, "Unable to set LDAP_OPT_X_SASL_SECPROPS: %s\n",
  665. mctx.sasl_secprops );
  666. goto done;
  667. }
  668. }
  669. /*
  670. * Generate the random authid if set up so
  671. */
  672. if ((mod2 & M2_RANDOM_SASLAUTHID) && tttctx) {
  673. rnd (tttctx->buf2, mctx.sasl_authid_low, mctx.sasl_authid_high,
  674. (mctx.mod2 & M2_NOZEROPAD) ? 0 : mctx.sasl_authid_nbdigit);
  675. strncpy (&(tttctx->bufSaslAuthid[tttctx->startSaslAuthid]),
  676. tttctx->buf2, mctx.sasl_authid_nbdigit);
  677. my_saslauthid = tttctx->bufSaslAuthid;
  678. if (mode & VERY_VERBOSE)
  679. printf ("ldclt[%d]: T%03d: Sasl Authid=\"%s\"\n",
  680. mctx.pid, thrdNum, tttctx->bufSaslAuthid);
  681. } else {
  682. my_saslauthid = mctx.sasl_authid;
  683. }
  684. defaults = ldaptool_set_sasl_defaults( ld, mctx.sasl_flags, mctx.sasl_mech,
  685. my_saslauthid, mctx.sasl_username, mctx.passwd, mctx.sasl_realm );
  686. if (defaults == NULL) {
  687. perror ("malloc");
  688. exit (LDAP_NO_MEMORY);
  689. }
  690. #if defined(USE_OPENLDAP)
  691. ret = ldap_sasl_interactive_bind_s( ld, mctx.bindDN, mctx.sasl_mech,
  692. NULL, NULL, mctx.sasl_flags,
  693. ldaptool_sasl_interact, defaults );
  694. #else
  695. ret = ldap_sasl_interactive_bind_ext_s( ld, mctx.bindDN, mctx.sasl_mech,
  696. NULL, NULL, mctx.sasl_flags,
  697. ldaptool_sasl_interact, defaults, NULL );
  698. #endif
  699. if (ret != LDAP_SUCCESS ) {
  700. if (tttctx) {
  701. tttctx->binded = 0;
  702. }
  703. if (!(mode & QUIET)) {
  704. fprintf(stderr, "Error: could not bind: %d:%s\n",
  705. ret, my_ldap_err2string(ret));
  706. }
  707. if (addErrorStat (ret) < 0)
  708. goto done;
  709. } else {
  710. if (tttctx) {
  711. tttctx->binded = 1;
  712. }
  713. }
  714. ldaptool_free_defaults( defaults );
  715. } else {
  716. if (((mctx.bindDN != NULL) || (mod2 & M2_RNDBINDFILE)) && /*03-05-01*/
  717. ((!(binded)) || (mode & BIND_EACH_OPER))) {
  718. struct berval *servercredp = NULL;
  719. const char *binddn = NULL;
  720. const char *passwd = NULL;
  721. if (tttctx && (buildNewBindDN (tttctx) < 0)) { /*JLS 05-01-01*/
  722. ret = -1;
  723. goto done;
  724. }
  725. if (tttctx && tttctx->bufPasswd) {
  726. binddn = tttctx->bufBindDN;
  727. passwd = tttctx->bufPasswd;
  728. } else if (bufPasswd) {
  729. binddn = bufBindDN;
  730. passwd = bufPasswd;
  731. } else if (mctx.passwd) {
  732. binddn = mctx.bindDN;
  733. passwd = mctx.passwd;
  734. }
  735. if (passwd) {
  736. cred.bv_val = (char *)passwd;
  737. cred.bv_len = strlen(passwd);
  738. }
  739. if (mode & VERY_VERBOSE)
  740. printf ("ldclt[%d]: T%03d: Before ldap_simple_bind_s (%s, %s)\n",
  741. mctx.pid, thrdNum, binddn,
  742. passwd?passwd:"NO PASSWORD PROVIDED");
  743. ret = ldap_sasl_bind_s (ld, binddn,
  744. LDAP_SASL_SIMPLE, &cred, NULL, NULL, &servercredp); /*JLS 05-01-01*/
  745. ber_bvfree(servercredp);
  746. if (mode & VERY_VERBOSE)
  747. printf ("ldclt[%d]: T%03d: After ldap_simple_bind_s (%s, %s)\n",
  748. mctx.pid, thrdNum, binddn,
  749. passwd?passwd:"NO PASSWORD PROVIDED");
  750. if (ret == LDAP_SUCCESS) { /*JLS 18-12-00*/
  751. if (tttctx) {
  752. tttctx->binded = 1; /*JLS 18-12-00*/
  753. }
  754. } else { /*JLS 18-12-00*/
  755. if (tttctx) {
  756. tttctx->binded = 0; /*JLS 18-12-00*/
  757. }
  758. if (ignoreError (ret)) { /*JLS 18-12-00*/
  759. if (!(mode & QUIET)) { /*JLS 18-12-00*/
  760. printf("ldclt[%d]: T%03d: Cannot ldap_simple_bind_s (%s, %s), error=%d (%s)\n",
  761. mctx.pid, thrdNum, binddn,
  762. passwd?passwd:"NO PASSWORD PROVIDED",
  763. ret, my_ldap_err2string (ret));
  764. fflush (stdout); /*JLS 18-12-00*/
  765. } /*JLS 18-12-00*/
  766. if (addErrorStat (ret) < 0) { /*JLS 18-12-00*/
  767. ret = -1;
  768. } else {
  769. ret = 0;
  770. }
  771. goto done;
  772. } else { /*JLS 18-12-00*/
  773. printf ("ldclt[%d]: T%03d: Cannot ldap_simple_bind_s (%s, %s), error=%d (%s)\n",
  774. mctx.pid, thrdNum, binddn,
  775. passwd?passwd:"NO PASSWORD PROVIDED",
  776. ret, my_ldap_err2string (ret));
  777. fflush (stdout); /*JLS 18-12-00*/
  778. if (tttctx)
  779. tttctx->exitStatus = EXIT_NOBIND; /*JLS 18-12-00*/
  780. (void)addErrorStat(ret);
  781. ret = -1;
  782. goto done;
  783. } /*JLS 18-12-00*/
  784. }
  785. }
  786. }
  787. done:
  788. if (ret) {
  789. ldap_unbind_ext(ld, NULL, NULL);
  790. ld = NULL;
  791. }
  792. #if defined(USE_OPENLDAP)
  793. if (ldapurl) {
  794. PR_smprintf_free(ldapurl);
  795. ldapurl = NULL;
  796. }
  797. #endif
  798. return ld;
  799. }
  800. /* ****************************************************************************
  801. FUNCTION : connectToServer
  802. PURPOSE : Realise the connection to the server.
  803. If requested by the user, it also realize the
  804. disconnection prior to connect.
  805. INPUT : tttctx = this thread's thread_context
  806. OUTPUT : None.
  807. RETURN : -1 if error, 0 else.
  808. DESCRIPTION :
  809. *****************************************************************************/
  810. int
  811. connectToServer (
  812. thread_context *tttctx)
  813. {
  814. int ret; /* Return value */
  815. LBER_SOCKET fd; /* LDAP cnx's fd */
  816. /*
  817. * Maybe close the connection ?
  818. * We will do this *here* to keep the cnx the longest time open.
  819. */
  820. if ((mctx.mode & BIND_EACH_OPER) && (tttctx->ldapCtx != NULL))
  821. {
  822. /*
  823. * Maybe the user want the connection to be *closed* rather than
  824. * being kindly unbinded ?
  825. */
  826. if (mctx.mode & CLOSE_FD)
  827. {
  828. /*
  829. * Get the corresponding fd
  830. */
  831. #ifdef WORKAROUND_4197228
  832. if (getFdFromLdapSession (tttctx->ldapCtx, &fd) < 0)
  833. {
  834. printf ("ldclt[%d]: T%03d: Cannot extract fd from ldap session\n",
  835. mctx.pid, tttctx->thrdNum);
  836. fflush (stdout);
  837. return (-1);
  838. }
  839. #else
  840. ret = ldap_get_option (tttctx->ldapCtx, LDAP_OPT_DESC, &fd);
  841. if (ret < 0)
  842. {
  843. printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_DESC)\n",
  844. mctx.pid, tttctx->thrdNum);
  845. fflush (stdout);
  846. return (-1);
  847. }
  848. #endif
  849. #ifdef TRACE_FD_GET_OPTION_BUG
  850. printf ("ldclt[%d]: T%03d: fd=%d\n", mctx.pid, tttctx->thrdNum, (int)fd);
  851. #endif
  852. if (close ((int)fd) < 0)
  853. {
  854. perror ("ldctx");
  855. printf ("ldclt[%d]: T%03d: cannot close(fd=%d), error=%d (%s)\n",
  856. mctx.pid, tttctx->thrdNum, (int)fd, errno, strerror (errno));
  857. return (-1);
  858. }
  859. }
  860. /*
  861. * Ok, anyway, we must ldap_unbind() to release our contextes
  862. * at the client side, otherwise this process will rocket through
  863. * the ceiling.
  864. * But don't be afraid, the UNBIND operation never reach the
  865. * server that will only see a suddent socket disconnection.
  866. */
  867. ret = ldap_unbind_ext (tttctx->ldapCtx, NULL, NULL);
  868. if (ret != LDAP_SUCCESS)
  869. {
  870. fprintf (stderr, "ldclt[%d]: T%03d: cannot ldap_unbind(), error=%d (%s)\n",
  871. mctx.pid, tttctx->thrdNum, ret,strerror (ret));
  872. fflush (stderr);
  873. (void)addErrorStat(ret);
  874. return (-1);
  875. }
  876. tttctx->ldapCtx = NULL;
  877. }
  878. /*
  879. * Maybe create the LDAP context ?
  880. */
  881. if (tttctx->ldapCtx == NULL)
  882. {
  883. tttctx->ldapCtx = connectToLDAP(tttctx, tttctx->bufBindDN, tttctx->bufPasswd,
  884. mctx.mode, mctx.mod2);
  885. if (!tttctx->ldapCtx) {
  886. return (-1);
  887. }
  888. }
  889. /*
  890. * Normal end
  891. */
  892. return (0);
  893. }
  894. /* ****************************************************************************
  895. FUNCTION : buildVersatileAttribute
  896. PURPOSE : Build a new attribute value using the definitions of
  897. the parameters.
  898. The pointer returned is always to a safe buffer inside
  899. the attribute.
  900. INPUT : tttctx = thread context
  901. object = object definition
  902. attrib = attribute to build
  903. OUTPUT : None.
  904. RETURN : NULL if error, pointer to the new value else.
  905. DESCRIPTION :
  906. *****************************************************************************/
  907. char *
  908. buildVersatileAttribute (
  909. thread_context *tttctx,
  910. vers_object *object,
  911. vers_attribute *attrib)
  912. {
  913. vers_field *field; /* To parse the fields */
  914. int num; /* Temp. number */
  915. /*
  916. * Maybe this attribute has a constant value ?
  917. * (i.e. only one field, constant value)
  918. */
  919. if (attrib->buf == NULL)
  920. return (attrib->field->cst);
  921. /*
  922. * Well, it looks like we will have to build the new value
  923. */
  924. attrib->buf[0] = '\0'; /* No field yet */
  925. for (field=attrib->field ; field!=NULL ; field=field->next)
  926. {
  927. switch (field->how)
  928. {
  929. case HOW_CONSTANT:
  930. strcat (attrib->buf, field->cst);
  931. break;
  932. case HOW_INCR_FROM_FILE:
  933. if (mctx.mode & COMMON_COUNTER)
  934. {
  935. num = incrementCommonCounterObject (tttctx, field->commonField);
  936. if (num < 0)
  937. return (NULL);
  938. }
  939. else
  940. {
  941. if (field->cnt > field->high)
  942. field->cnt = field->low;
  943. num = field->cnt;
  944. field->cnt++; /* Next value for next loop */
  945. }
  946. strcat (attrib->buf, field->dlf->str[num]);
  947. if (field->var != -1)
  948. strcpy (object->var[field->var], field->dlf->str[num]);
  949. break;
  950. case HOW_INCR_FROM_FILE_NL:
  951. if (mctx.mode & COMMON_COUNTER)
  952. {
  953. num = incrementCommonCounterObject (tttctx, field->commonField);
  954. if (num < 0)
  955. return (NULL);
  956. }
  957. else
  958. {
  959. if (field->cnt > field->high)
  960. {
  961. printf ("ldclt[%d]: %s: Hit top incrementeal value\n",
  962. mctx.pid, tttctx->thrdId);
  963. return(NULL);
  964. }
  965. num = field->cnt;
  966. field->cnt++; /* Next value for next loop */
  967. }
  968. strcat (attrib->buf, field->dlf->str[num]);
  969. if (field->var != -1)
  970. strcpy (object->var[field->var], tttctx->buf2);
  971. break;
  972. case HOW_INCR_NB:
  973. if (mctx.mode & COMMON_COUNTER)
  974. {
  975. num = incrementCommonCounterObject (tttctx, field->commonField);
  976. if (num < 0)
  977. return (NULL);
  978. }
  979. else
  980. {
  981. if (field->cnt > field->high)
  982. field->cnt = field->low;
  983. num = field->cnt;
  984. field->cnt++; /* Next value for next loop */
  985. }
  986. sprintf (tttctx->buf2, "%0*d", (mctx.mod2 & M2_NOZEROPAD) ? 0 : field->nb, num);
  987. strcat (attrib->buf, tttctx->buf2);
  988. if (field->var != -1)
  989. strcpy (object->var[field->var], tttctx->buf2);
  990. break;
  991. case HOW_INCR_NB_NOLOOP:
  992. if (mctx.mode & COMMON_COUNTER)
  993. {
  994. num = incrementCommonCounterObject (tttctx, field->commonField);
  995. if (num < 0)
  996. return (NULL);
  997. }
  998. else
  999. {
  1000. if (field->cnt > field->high)
  1001. {
  1002. printf ("ldclt[%d]: %s: Hit top incrementeal value\n",
  1003. mctx.pid, tttctx->thrdId);
  1004. return(NULL);
  1005. }
  1006. num = field->cnt;
  1007. field->cnt++; /* Next value for next loop */
  1008. }
  1009. sprintf (tttctx->buf2, "%0*d", (mctx.mod2 & M2_NOZEROPAD) ? 0 : field->nb, num);
  1010. strcat (attrib->buf, tttctx->buf2);
  1011. if (field->var != -1)
  1012. strcpy (object->var[field->var], tttctx->buf2);
  1013. break;
  1014. case HOW_RND_FROM_FILE:
  1015. num = (lrand48() % field->dlf->strNb);
  1016. strcat (attrib->buf, field->dlf->str[num]);
  1017. if (field->var != -1)
  1018. strcpy (object->var[field->var], field->dlf->str[num]);
  1019. break;
  1020. case HOW_RND_NUMBER:
  1021. rnd (tttctx->buf2, field->low, field->high, (mctx.mod2 & M2_NOZEROPAD) ? 0 : field->nb);
  1022. strcat (attrib->buf, tttctx->buf2);
  1023. if (field->var != -1)
  1024. strcpy (object->var[field->var], tttctx->buf2);
  1025. break;
  1026. case HOW_RND_STRING:
  1027. (void) randomString (tttctx, field->nb);
  1028. strcat (attrib->buf, tttctx->buf2);
  1029. if (field->var != -1)
  1030. strcpy (object->var[field->var], tttctx->buf2);
  1031. break;
  1032. case HOW_VARIABLE:
  1033. if (object->var[field->var] == NULL) /*JLS 11-04-01*/
  1034. { /*JLS 11-04-01*/
  1035. printf ("ldclt[%d]: %s: Error : unset variable %c in %s\n",
  1036. mctx.pid, tttctx->thrdId,
  1037. 'A'+field->var, attrib->src); /*JLS 11-04-01*/
  1038. return(NULL); /*JLS 11-04-01*/
  1039. } /*JLS 11-04-01*/
  1040. strcat (attrib->buf, object->var[field->var]);
  1041. break;
  1042. default:
  1043. /*
  1044. * Should not happen, unless new variant parsed and not
  1045. * integrated here, or "jardinage"....
  1046. */
  1047. field = NULL;
  1048. field->how = 22; /* Crash !!! */
  1049. break;
  1050. }
  1051. }
  1052. /*
  1053. * Return the new value.
  1054. */
  1055. return (attrib->buf);
  1056. }
  1057. /* ****************************************************************************
  1058. FUNCTION : buildRandomRdnOrFilter
  1059. PURPOSE : This function will build a random string (rdn or filter)
  1060. to be used by ldap_search() or ldap_add() or etc...
  1061. The result is in tttctx->bufFilter
  1062. INPUT : tttctx = thread context
  1063. OUTPUT : None.
  1064. RETURN : -1 if error, 0 else.
  1065. DESCRIPTION :
  1066. *****************************************************************************/
  1067. int
  1068. buildRandomRdnOrFilter (
  1069. thread_context *tttctx)
  1070. {
  1071. /*
  1072. * Maybe we will operate with a variable base DN ?
  1073. */
  1074. if (mctx.mode & RANDOM_BASE)
  1075. {
  1076. if (mctx.mode & STRING)
  1077. (void) randomString (tttctx, mctx.baseDNNbDigit);
  1078. else
  1079. rnd (tttctx->buf2, mctx.baseDNLow, mctx.baseDNHigh, /*JLS 14-11-00*/
  1080. (mctx.mod2 & M2_NOZEROPAD) ? 0 : mctx.baseDNNbDigit); /*JLS 14-11-00*/
  1081. strncpy (&(tttctx->bufBaseDN[tttctx->startBaseDN]),
  1082. tttctx->buf2, mctx.baseDNNbDigit);
  1083. if (mctx.mode & VERY_VERBOSE)
  1084. printf ("ldclt[%d]: T%03d: base DN=\"%s\"\n",
  1085. mctx.pid, tttctx->thrdNum, tttctx->bufBaseDN);
  1086. }
  1087. /*
  1088. * Maybe we must build a random attribute value ?
  1089. * We only support random string generation here.
  1090. */
  1091. if (mctx.mode & ATTR_REPLACE) /*JLS 21-11-00*/
  1092. { /*JLS 21-11-00*/
  1093. (void) randomString (tttctx, mctx.attrplNbDigit); /*JLS 21-11-00*/
  1094. strncpy (&(tttctx->bufAttrpl[tttctx->startAttrpl]), /*JLS 21-11-00*/
  1095. tttctx->buf2, mctx.attrplNbDigit); /*JLS 21-11-00*/
  1096. if (mctx.mode & VERY_VERBOSE) /*JLS 21-11-00*/
  1097. printf ("ldclt[%d]: T%03d: attrib=\"%s\"\n",
  1098. mctx.pid, tttctx->thrdNum, tttctx->bufAttrpl);
  1099. } /*JLS 21-11-00*/
  1100. /*
  1101. * Maybe we must use a variant-rdn style ?
  1102. */
  1103. if (mctx.mod2 & M2_RDN_VALUE) /*JLS 23-03-01*/
  1104. { /*JLS 23-03-01*/
  1105. char *buf; /* Temp for new value */ /*JLS 23-03-01*/
  1106. /*JLS 23-03-01*/
  1107. buf = buildVersatileAttribute (tttctx, /*JLS 23-03-01*/
  1108. tttctx->object, tttctx->object->rdn); /*JLS 23-03-01*/
  1109. if (buf == NULL) /*JLS 23-03-01*/
  1110. return (-1); /*JLS 23-03-01*/
  1111. strcpy (tttctx->bufFilter, tttctx->object->rdnName); /*JLS 23-03-01*/
  1112. strcat (tttctx->bufFilter, "="); /*JLS 23-03-01*/
  1113. strcat (tttctx->bufFilter, buf); /*JLS 23-03-01*/
  1114. if (mctx.mode & VERY_VERBOSE) /*JLS 28-03-01*/
  1115. printf ("ldclt[%d]: %s: rdn variant mode:filter=\"%s\"\n",
  1116. mctx.pid, tttctx->thrdId, tttctx->bufFilter);
  1117. } /*JLS 23-03-01*/
  1118. else /*JLS 23-03-01*/
  1119. { /*JLS 23-03-01*/
  1120. /*
  1121. * Build the new filter string
  1122. */
  1123. if (mctx.mode & RANDOM)
  1124. {
  1125. if (mctx.mode & STRING)
  1126. (void) randomString (tttctx, mctx.randomNbDigit);
  1127. else
  1128. rnd (tttctx->buf2, mctx.randomLow, mctx.randomHigh, /*JLS 14-11-00*/
  1129. (mctx.mod2 & M2_NOZEROPAD) ? 0 : mctx.randomNbDigit); /*JLS 14-11-00*/
  1130. strncpy (&(tttctx->bufFilter[tttctx->startRandom]),
  1131. tttctx->buf2, mctx.randomNbDigit);
  1132. if ((mctx.mod2 & M2_NOZEROPAD) && mctx.randomTail) {
  1133. strcat(tttctx->bufFilter, mctx.randomTail);
  1134. }
  1135. if (mctx.mode & VERY_VERBOSE)
  1136. printf ("ldclt[%d]: %s: random mode:filter=\"%s\"\n",
  1137. mctx.pid, tttctx->thrdId, tttctx->bufFilter);
  1138. }
  1139. if (mctx.mode & INCREMENTAL)
  1140. {
  1141. if (mctx.mode & COMMON_COUNTER) /*JLS 14-03-01*/
  1142. { /*JLS 14-03-01*/
  1143. int val = incrementCommonCounter (tttctx); /*JLS 14-03-01*/
  1144. if (val == -1) /*JLS 14-03-01*/
  1145. return (-1); /*JLS 14-03-01*/
  1146. sprintf (tttctx->buf2, "%0*d", (mctx.mod2 & M2_NOZEROPAD) ? 0 : mctx.randomNbDigit, val);/*JLS 14-03-01*/
  1147. } /*JLS 14-03-01*/
  1148. else if ((mctx.mode & NOLOOP) && ((tttctx->lastVal + mctx.incr) > mctx.randomHigh))
  1149. {
  1150. /*
  1151. * Well, there is no clean way to exit. Let's use the error
  1152. * condition and hope all will be ok.
  1153. */
  1154. printf ("ldclt[%d]: %s: Hit top incremental value %d > %d\n",
  1155. mctx.pid, tttctx->thrdId, (tttctx->lastVal + mctx.incr), mctx.randomHigh);
  1156. return (-1);
  1157. }
  1158. else
  1159. {
  1160. tttctx->lastVal = incr_and_wrap(tttctx->lastVal, mctx.randomLow, mctx.randomHigh, mctx.incr);
  1161. sprintf (tttctx->buf2, "%0*d", (mctx.mod2 & M2_NOZEROPAD) ? 0 : mctx.randomNbDigit, tttctx->lastVal);
  1162. } /*JLS 14-03-01*/
  1163. strncpy (&(tttctx->bufFilter[tttctx->startRandom]), tttctx->buf2,
  1164. mctx.randomNbDigit);
  1165. if ((mctx.mod2 & M2_NOZEROPAD) && mctx.randomTail) {
  1166. strcat(tttctx->bufFilter, mctx.randomTail);
  1167. }
  1168. if (mctx.mode & VERY_VERBOSE)
  1169. printf ("ldclt[%d]: %s: incremental mode:filter=\"%s\"\n",
  1170. mctx.pid, tttctx->thrdId, tttctx->bufFilter);
  1171. }
  1172. } /*JLS 23-03-01*/
  1173. return (0);
  1174. }
  1175. /* ****************************************************************************
  1176. FUNCTION : addAttrib
  1177. PURPOSE : Add a new attribute to the given LDAPMod array
  1178. INPUT : attrs = existing LDAPMod array
  1179. nb = number of entries in the array
  1180. newattr = new attribute to add to the list
  1181. OUTPUT : None.
  1182. RETURN : -1 if error, 0 else.
  1183. DESCRIPTION : Important note: the first time this function is called,
  1184. it must be with nb==0.
  1185. *****************************************************************************/
  1186. int
  1187. addAttrib (
  1188. LDAPMod **attrs,
  1189. int nb,
  1190. LDAPMod *newattr)
  1191. {
  1192. attrs[nb] = (LDAPMod *) malloc (sizeof (LDAPMod));
  1193. if (attrs[nb] == NULL) /*JLS 06-03-00*/
  1194. { /*JLS 06-03-00*/
  1195. printf ("ldclt[%d]: Txxx: cannot malloc(attrs[%d]), error=%d (%s)\n",
  1196. mctx.pid, nb, errno, strerror (errno));
  1197. return (-1); /*JLS 06-03-00*/
  1198. } /*JLS 06-03-00*/
  1199. memcpy (attrs[nb], newattr, sizeof (LDAPMod));
  1200. attrs[nb+1] = NULL;
  1201. return (0);
  1202. }
  1203. /* ****************************************************************************
  1204. FUNCTION : freeAttrib
  1205. PURPOSE : Free an array of addAttrib.
  1206. INPUT : attrs = LDAPMod array to free
  1207. OUTPUT : None.
  1208. RETURN : -1 if error, 0 else.
  1209. DESCRIPTION :
  1210. *****************************************************************************/
  1211. int
  1212. freeAttrib (
  1213. LDAPMod **attrs)
  1214. {
  1215. int i;
  1216. int j;
  1217. for (i=0 ; attrs[i]!=NULL ; i++) {
  1218. if (attrs[i]->mod_op & LDAP_MOD_BVALUES) {
  1219. for (j=0; attrs[i]->mod_bvalues[j] != NULL; j++) {
  1220. free (attrs[i]->mod_bvalues[j]);
  1221. }
  1222. free (attrs[i]->mod_bvalues);
  1223. } else {
  1224. free (attrs[i]->mod_values);
  1225. }
  1226. free (attrs[i]);
  1227. }
  1228. return (0);
  1229. }
  1230. /* ****************************************************************************
  1231. FUNCTION : strList1
  1232. PURPOSE : Create a list (array) of two strings
  1233. INPUT : str1 = the first string.
  1234. OUTPUT : None.
  1235. RETURN : Pointer to the char *str[2]
  1236. DESCRIPTION :
  1237. *****************************************************************************/
  1238. char **
  1239. strList1 (
  1240. char *str1)
  1241. {
  1242. char **p;
  1243. p = (char **) malloc (2 * sizeof (char *));
  1244. if (p == NULL) /*JLS 06-03-00*/
  1245. { /*JLS 06-03-00*/
  1246. printf ("ldclt[%d]: Txxx: cannot malloc(p), error=%d (%s)\n",
  1247. mctx.pid, errno, strerror (errno));
  1248. ldcltExit (EXIT_RESSOURCE); /*JLS 18-12-00*/
  1249. } /*JLS 06-03-00*/
  1250. p[0] = str1;
  1251. p[1] = NULL;
  1252. return (p);
  1253. }
  1254. /* ****************************************************************************
  1255. FUNCTION : printErrorFromLdap
  1256. PURPOSE : Print the error message returned by ldap.
  1257. INPUT : tttctx = thread context
  1258. res = LDAP result
  1259. errcode = error code
  1260. errmsg = error message
  1261. OUTPUT : None.
  1262. RETURN : -1 if error, 0 else.
  1263. DESCRIPTION :
  1264. *****************************************************************************/
  1265. int
  1266. printErrorFromLdap (
  1267. thread_context *tttctx,
  1268. LDAPMessage *res,
  1269. int errcode,
  1270. char *errmsg)
  1271. {
  1272. int ret; /* Return value */
  1273. char *addErrMsg; /* Additional error msg */
  1274. int errcodep; /* Async error code */ /*JLS 11-08-00*/
  1275. /*
  1276. * Print the error message and error code
  1277. */
  1278. printf ("ldclt[%d]: T%03d: %s, error=%d (%s",
  1279. mctx.pid, tttctx->thrdNum, errmsg,
  1280. errcode, my_ldap_err2string (errcode));
  1281. if (!res) {
  1282. printf (") -- NULL result\n");
  1283. return -1;
  1284. }
  1285. /*
  1286. * See if there is an additional error message...
  1287. */
  1288. ret = ldap_parse_result (tttctx->ldapCtx, res, &errcodep, /*JLS 11-08-00*/
  1289. NULL, &addErrMsg, NULL, NULL, 0);
  1290. if (ret != LDAP_SUCCESS)
  1291. {
  1292. printf (")\n");
  1293. printf ("ldclt[%d]: T%03d: errcodep = %d\n",
  1294. mctx.pid, tttctx->thrdNum, errcodep);
  1295. printf ("ldclt[%d]: T%03d: Cannot ldap_parse_result(), error=%d (%s)\n",
  1296. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  1297. return (-1);
  1298. }
  1299. /*
  1300. * Ok, we were able to retrieve more information...
  1301. * Well, the errcodep is the error of the operation parsed by
  1302. * ldap_parse_result(), so we will print it if different from
  1303. * the one given in argument to this function...
  1304. */
  1305. if (errcodep != errcode) /*JLS 11-08-00*/
  1306. printf ("ldclt[%d]: errcodep=%d (%s)",
  1307. mctx.pid, errcodep, my_ldap_err2string (errcodep));
  1308. if ((addErrMsg != NULL) && (*addErrMsg != '\0'))
  1309. {
  1310. printf (" - %s", addErrMsg);
  1311. ldap_memfree (addErrMsg);
  1312. }
  1313. printf (")\n");
  1314. /*
  1315. * Don't forget to flush !
  1316. */
  1317. fflush (stdout);
  1318. return (0);
  1319. }
  1320. /* ****************************************************************************
  1321. FUNCTION : buildNewModAttribFile
  1322. PURPOSE : Build a new (random or incremental) target DN and the
  1323. corresponding LDAPMod for attribute modification.
  1324. INPUT : tttctx = thread context
  1325. OUTPUT : newDN = DN of the new entry
  1326. attrs = attributes for the ldap_modify
  1327. RETURN : -1 if error, 0 else.
  1328. *****************************************************************************/
  1329. int
  1330. buildNewModAttribFile (
  1331. thread_context *tttctx,
  1332. char *newDn,
  1333. LDAPMod **attrs)
  1334. {
  1335. int nbAttribs; /* Nb of attributes */
  1336. LDAPMod attribute; /* To build the attributes */
  1337. struct berval *bv = malloc(sizeof(struct berval));
  1338. attribute.mod_bvalues = (struct berval **)malloc(2 * sizeof(struct berval *));
  1339. int rc = 0;
  1340. if ((bv == NULL) || (attribute.mod_bvalues == NULL)) {
  1341. rc = -1;
  1342. goto error;
  1343. }
  1344. /*
  1345. * Build the new DN
  1346. * We will assume that the filter (-f argument) is set to use it
  1347. * to build the rdn of the new entry.
  1348. * Note that the random new attribute is also build by this function.
  1349. */
  1350. if (buildRandomRdnOrFilter (tttctx) < 0) {
  1351. rc = -1;
  1352. goto error;
  1353. }
  1354. strcpy (newDn, tttctx->bufFilter);
  1355. strcat (newDn, ",");
  1356. strcat (newDn, tttctx->bufBaseDN);
  1357. /*
  1358. * Build the attributes modification
  1359. */
  1360. bv->bv_len = mctx.attrplFileSize;
  1361. bv->bv_val = mctx.attrplFileContent;
  1362. attrs[0] = NULL; /* No attributes yet */
  1363. nbAttribs = 0; /* No attributes yet */
  1364. attribute.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  1365. attribute.mod_type = mctx.attrplName;
  1366. attribute.mod_bvalues[0] = bv;
  1367. attribute.mod_bvalues[1] = NULL;
  1368. if (addAttrib (attrs, nbAttribs++, &attribute) < 0) {
  1369. rc = -1;
  1370. goto error;
  1371. }
  1372. goto done;
  1373. error:
  1374. if (bv != NULL) {
  1375. free(bv);
  1376. }
  1377. if (attribute.mod_bvalues != NULL) {
  1378. free(attribute.mod_bvalues);
  1379. }
  1380. done:
  1381. return rc;
  1382. }
  1383. /* ****************************************************************************
  1384. FUNCTION : buildNewModAttrib
  1385. PURPOSE : Build a new (random or incremental) target DN and the
  1386. corresponding LDAPMod for attribute modification.
  1387. INPUT : tttctx = thread context
  1388. OUTPUT : newDN = DN of the new entry
  1389. attrs = attributes for the ldap_modify
  1390. RETURN : -1 if error, 0 else.
  1391. DESCRIPTION :
  1392. *****************************************************************************/
  1393. int
  1394. buildNewModAttrib (
  1395. thread_context *tttctx,
  1396. char *newDn,
  1397. LDAPMod **attrs)
  1398. {
  1399. int nbAttribs; /* Nb of attributes */
  1400. LDAPMod attribute; /* To build the attributes */
  1401. /*
  1402. * Build the new DN
  1403. * We will assume that the filter (-f argument) is set to use it
  1404. * to build the rdn of the new entry.
  1405. * Note that the random new attribute is also build by this function.
  1406. */
  1407. if (buildRandomRdnOrFilter (tttctx) < 0)
  1408. return (-1);
  1409. strcpy (newDn, tttctx->bufFilter);
  1410. strcat (newDn, ",");
  1411. strcat (newDn, tttctx->bufBaseDN);
  1412. /*
  1413. * Build the attributes modification
  1414. */
  1415. attrs[0] = NULL; /* No attributes yet */
  1416. nbAttribs = 0; /* No attributes yet */
  1417. attribute.mod_op = LDAP_MOD_REPLACE;
  1418. attribute.mod_type = mctx.attrplName;
  1419. attribute.mod_values = strList1 (tttctx->bufAttrpl);
  1420. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1421. return (-1);
  1422. /*
  1423. * Normal end
  1424. */
  1425. return (0);
  1426. }
  1427. /* ****************************************************************************
  1428. FUNCTION : buildVersatileObject
  1429. PURPOSE : Build a new entry using the definitions in the object
  1430. given in parameter.
  1431. INPUT : tttctx = thread context
  1432. object = object definition
  1433. OUTPUT : attrs = resulting attributes.
  1434. RETURN : -1 if error, 0 else.
  1435. DESCRIPTION :
  1436. *****************************************************************************/
  1437. int
  1438. buildVersatileObject (
  1439. thread_context *tttctx,
  1440. vers_object *object,
  1441. LDAPMod **attrs)
  1442. {
  1443. int nbAttribs; /* Nb of attributes */
  1444. LDAPMod attribute; /* To build the attributes */
  1445. int i; /* For the loop */
  1446. char *newValue; /* New values for the attributes */
  1447. /*
  1448. * Initialization
  1449. */
  1450. attrs[0] = NULL; /* No attributes yet */
  1451. nbAttribs = 0; /* No attributes yet */
  1452. /*
  1453. * What is sure is that ttctx->bufFilter contains the rdn of the new entry !
  1454. * This rdn is build from the filter, and is independant from the
  1455. * object class.
  1456. */
  1457. for (i=0 ; tttctx->bufFilter[i] != '=' ; i++)
  1458. tttctx->buf2[i] = tttctx->bufFilter[i];
  1459. tttctx->buf2[i] = '\0';
  1460. strcpy (tttctx->bufObject1, tttctx->buf2);
  1461. attribute.mod_op = LDAP_MOD_ADD;
  1462. attribute.mod_type = tttctx->bufObject1;
  1463. attribute.mod_values = strList1 (&(tttctx->bufFilter[i+1]));
  1464. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1465. return (-1);
  1466. /*
  1467. * We are certain that there is enough space in attrs
  1468. */
  1469. for (i=0 ; i<object->attribsNb ; i++)
  1470. {
  1471. attribute.mod_op = LDAP_MOD_ADD;
  1472. attribute.mod_type = object->attribs[i].name;
  1473. newValue = buildVersatileAttribute (tttctx, object, &(object->attribs[i]));
  1474. if (newValue == NULL)
  1475. return (-1);
  1476. attribute.mod_values = strList1 (newValue);
  1477. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1478. return (-1);
  1479. }
  1480. return (0);
  1481. }
  1482. /* ****************************************************************************
  1483. FUNCTION : buildNewEntry
  1484. PURPOSE : Build a new (random or incremental) entry, to be used
  1485. for ldap_add() or ldap_modify() operations.
  1486. INPUT : tttctx = thread context
  1487. OUTPUT : newDn = DN of the new entry
  1488. attrs = attributes of the new entry
  1489. RETURN : -1 if error, 0 else.
  1490. DESCRIPTION :
  1491. *****************************************************************************/
  1492. int
  1493. buildNewEntry (
  1494. thread_context *tttctx,
  1495. char *newDn,
  1496. LDAPMod **attrs)
  1497. {
  1498. int nbAttribs; /* Nb of attributes */
  1499. LDAPMod attribute; /* To build the attributes */
  1500. int i; /* To loop */
  1501. /*
  1502. * Build the new DN
  1503. * We will assume that the filter (-f argument) is set to use it
  1504. * to build the rdn of the new entry.
  1505. */
  1506. if (buildRandomRdnOrFilter (tttctx) < 0)
  1507. return (-1);
  1508. strcpy (newDn, tttctx->bufFilter);
  1509. strcat (newDn, ",");
  1510. strcat (newDn, tttctx->bufBaseDN);
  1511. if (mctx.mode & VERY_VERBOSE)
  1512. printf ("ldclt[%d]: %s: tttctx->bufFilter=\"%s\"\n",
  1513. mctx.pid, tttctx->thrdId, tttctx->bufFilter);
  1514. /*
  1515. * Maybe we are using the new versatile object definition ?
  1516. */
  1517. if (mctx.mod2 & M2_OBJECT) /*JLS 19-03-01*/
  1518. { /*JLS 19-03-01*/
  1519. if (buildVersatileObject(tttctx, tttctx->object, attrs) < 0)/*JLS 19-03-01*/
  1520. return (-1); /*JLS 19-03-01*/
  1521. if (mctx.mode & VERY_VERBOSE) /*JLS 19-03-01*/
  1522. { /*JLS 19-03-01*/
  1523. for (i=0 ; attrs[i] != NULL ; i++) /*JLS 19-03-01*/
  1524. printf ("ldclt[%d]: %s: attrs[%d]=(\"%s\" , \"%s\")\n",
  1525. mctx.pid, tttctx->thrdId, i,
  1526. attrs[i]->mod_type, attrs[i]->mod_values[0]); /*JLS 19-03-01*/
  1527. } /*JLS 19-03-01*/
  1528. return (0); /*JLS 19-03-01*/
  1529. } /*JLS 19-03-01*/
  1530. /*
  1531. * Build the attributes
  1532. * First, the class...
  1533. * The classe depends of course on the user's choice.
  1534. * Up to now, we only accept person, or one of its subclasses, emailPerson.
  1535. * The difference is that emailPerson contains no other mandatory attributes,
  1536. * but an optionnal one caled "jpegPhoto". This one will be added at the end
  1537. * of this function.
  1538. * NOTE: When other classes will be managed, this function will be splitted
  1539. * to do this.
  1540. */
  1541. attrs[0] = NULL; /* No attributes yet */
  1542. nbAttribs = 0; /* No attributes yet */
  1543. attribute.mod_op = LDAP_MOD_ADD;
  1544. attribute.mod_type = "objectclass";
  1545. if (mctx.mode & OC_PERSON)
  1546. attribute.mod_values = strList1 ("person");
  1547. if (mctx.mode & OC_EMAILPERSON)
  1548. attribute.mod_values = strList1 ("emailPerson");
  1549. if (mctx.mode & OC_INETORGPRSON) /*JLS 07-11-00*/
  1550. attribute.mod_values = strList1 ("inetOrgPerson"); /*JLS 07-11-00*/
  1551. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1552. return (-1);
  1553. /*
  1554. * What is sure is that ttctx->bufFilter contains the rdn of the new entry !
  1555. * This rdn is build from the filter, and is independant from the
  1556. * object class.
  1557. */
  1558. for (i=0 ; tttctx->bufFilter[i] != '=' ; i++)
  1559. tttctx->buf2[i] = tttctx->bufFilter[i];
  1560. tttctx->buf2[i] = '\0';
  1561. attribute.mod_op = LDAP_MOD_ADD;
  1562. attribute.mod_type = tttctx->buf2;
  1563. attribute.mod_values = strList1 (&(tttctx->bufFilter[i+1]));
  1564. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1565. return (-1);
  1566. /*
  1567. * The other attributes...
  1568. */
  1569. if (mctx.mode & (OC_PERSON|OC_EMAILPERSON|OC_INETORGPRSON)) /*JLS 07-11-00*/
  1570. {
  1571. if (strcmp (tttctx->buf2, "cn"))
  1572. {
  1573. attribute.mod_op = LDAP_MOD_ADD;
  1574. attribute.mod_type = "cn";
  1575. attribute.mod_values = strList1 ("toto cn");
  1576. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1577. return (-1);
  1578. }
  1579. if (strcmp (tttctx->buf2, "sn"))
  1580. {
  1581. attribute.mod_op = LDAP_MOD_ADD;
  1582. attribute.mod_type = "sn";
  1583. attribute.mod_values = strList1 ("toto sn");
  1584. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1585. return (-1);
  1586. }
  1587. if ((mctx.mode & OC_INETORGPRSON) && (mctx.mod2 & M2_DEREF))
  1588. {
  1589. attribute.mod_op = LDAP_MOD_ADD;
  1590. attribute.mod_type = LDCLT_DEREF_ATTR;
  1591. /* refer itself */
  1592. attribute.mod_values = strList1 (newDn);
  1593. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1594. return (-1);
  1595. }
  1596. }
  1597. /*
  1598. * This object class is used because it contains an attribute photo...
  1599. */
  1600. if (mctx.mode & (OC_EMAILPERSON|OC_INETORGPRSON)) /*JLS 07-11-00*/
  1601. {
  1602. attribute.mod_op = (LDAP_MOD_ADD|LDAP_MOD_BVALUES);
  1603. attribute.mod_type = "jpegPhoto";
  1604. if (getImage (&attribute) < 0)
  1605. return (-1);
  1606. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1607. return (-1);
  1608. }
  1609. /*
  1610. * No more attributes. Dump the attributes...
  1611. */
  1612. if (mctx.mode & VERY_VERBOSE)
  1613. {
  1614. for (i=0 ; attrs[i] != NULL ; i++)
  1615. printf ("ldclt[%d]: T%03d: attrs[%d]=(\"%s\" , \"%s\")\n",
  1616. mctx.pid, tttctx->thrdNum, i,
  1617. attrs[i]->mod_type, attrs[i]->mod_values[0]);
  1618. }
  1619. return (0);
  1620. }
  1621. /* ****************************************************************************
  1622. FUNCTION : createMissingNodes
  1623. PURPOSE : Create the missing intermediate nodes.
  1624. INPUT : tttctx = thread context
  1625. newDN = new DN that was rejected due to error 32
  1626. LDAP_NO_SUCH_OBJECT
  1627. cnx = ldap connection. NULL if not connected.
  1628. OUTPUT : None.
  1629. RETURN : -1 if error, 0 else.
  1630. DESCRIPTION : This function will call itself recursively while it is
  1631. impossible to create the node, as long as the error is
  1632. LDAP_NO_SUCH_OBJECT (aka 32).
  1633. This function connects to the server to perform its
  1634. task, using the same DN and passwd as in the mctx, i.e.
  1635. the same as the rest of this tool. Hence it is possible
  1636. that it is impossible to initiate a database if the
  1637. user is not cn=admin and the database is empty, because
  1638. only cn=admin may create the root entry.
  1639. *****************************************************************************/
  1640. int
  1641. createMissingNodes (
  1642. thread_context *tttctx,
  1643. char *newDN,
  1644. LDAP *cnx)
  1645. {
  1646. int i, j; /* For the loops */
  1647. int ret; /* Return value */
  1648. char *nodeDN; /* Node to create */
  1649. char attrName[256]; /* nodeDN's rdn attribute name */
  1650. char attrVal[256]; /* nodeDN's rdn attribute value */
  1651. char *objClass; /* Object class to create */
  1652. int nbAttribs; /* Nb of attributes */
  1653. LDAPMod attribute; /* To build the attributes */
  1654. LDAPMod *attrs[4]; /* Attributes of this entry */
  1655. /*
  1656. * Skip the rdn of the given newDN, that was rejected.
  1657. * Don't forget to skip the leading spaces...
  1658. */
  1659. nodeDN=newDN;
  1660. while (*nodeDN != '\0')
  1661. {
  1662. if (*nodeDN == ',')
  1663. break;
  1664. if (*nodeDN == '\\')
  1665. {
  1666. nodeDN++;
  1667. if (*nodeDN == '\0')
  1668. break;
  1669. }
  1670. nodeDN++;
  1671. }
  1672. if (*nodeDN == ',')
  1673. nodeDN++; /* Skip the ',' */
  1674. while ((*nodeDN == ' ') && (*nodeDN != '\0'))
  1675. nodeDN++;
  1676. if (*nodeDN == '\0')
  1677. {
  1678. printf ("ldclt[%d]: T%03d: Reach top of DN for %s\n",
  1679. mctx.pid, tttctx->thrdNum, newDN);
  1680. fflush (stdout);
  1681. return (-1);
  1682. }
  1683. if (mctx.mode & VERY_VERBOSE) /*JLS 14-12-00*/
  1684. printf ("ldclt[%d]: T%03d: nodeDN: %s\n",
  1685. mctx.pid, tttctx->thrdNum, nodeDN);
  1686. /*
  1687. * Found the naming attribute used for nodeDN's rdn.
  1688. */
  1689. for (i=0 ; (nodeDN[i] != '=') && (nodeDN[i] != '\0') ; i++);
  1690. if (nodeDN[i] == '\0')
  1691. {
  1692. printf ("ldclt[%d]: T%03d: Cannot extract naming attribute from %s\n",
  1693. mctx.pid, tttctx->thrdNum, nodeDN);
  1694. fflush (stdout);
  1695. return (-1);
  1696. }
  1697. strncpy (attrName, nodeDN, i);
  1698. attrName[i] = '\0';
  1699. /*
  1700. * Get the value of this rdn
  1701. */
  1702. for (j=i ; (nodeDN[j] != ',') && (nodeDN[j] != '\0') ; j++);
  1703. if (nodeDN[j] == '\0')
  1704. {
  1705. printf ("ldclt[%d]: T%03d: Cannot extract naming attribute from %s\n",
  1706. mctx.pid, tttctx->thrdNum, nodeDN);
  1707. fflush (stdout);
  1708. return (-1);
  1709. }
  1710. strncpy (attrVal, nodeDN+i+1, j-i-1);
  1711. attrVal[j-i-1] = '\0';
  1712. /*
  1713. * What kind of entry should be create ?
  1714. */
  1715. if (!strcmp (attrName, "o"))
  1716. objClass = "organization";
  1717. else if (!strcmp (attrName, "ou"))
  1718. objClass = "organizationalUnit";
  1719. else if (!strcmp (attrName, "cn"))
  1720. objClass = "organizationalRole";
  1721. else
  1722. {
  1723. printf ("ldclt[%d]: T%03d: Don't know how to create entry when rdn is \"%s=%s\"\n",
  1724. mctx.pid, tttctx->thrdNum, attrName, attrVal);
  1725. fflush (stdout);
  1726. return (-1);
  1727. }
  1728. /*
  1729. * Maybe connect to the server ?
  1730. */
  1731. if (cnx == NULL)
  1732. {
  1733. unsigned int mode = mctx.mode;
  1734. unsigned int mod2 = mctx.mod2;
  1735. /* clear bits not applicable to this mode */
  1736. mod2 &= ~M2_RNDBINDFILE;
  1737. mod2 &= ~M2_SASLAUTH;
  1738. mod2 &= ~M2_RANDOM_SASLAUTHID;
  1739. /* force bind to happen */
  1740. mode |= BIND_EACH_OPER;
  1741. if (mode & VERY_VERBOSE) /*JLS 14-12-00*/
  1742. printf ("ldclt[%d]: T%03d: must connect to the server.\n",
  1743. mctx.pid, tttctx->thrdNum);
  1744. tttctx->ldapCtx = connectToLDAP(tttctx, tttctx->bufBindDN, tttctx->bufPasswd, mode, mod2);
  1745. if (!tttctx->ldapCtx) {
  1746. return (-1);
  1747. }
  1748. cnx = tttctx->ldapCtx;
  1749. }
  1750. /*
  1751. * Create the entry
  1752. */
  1753. attrs[0] = NULL; /* No attributes yet */
  1754. nbAttribs = 0; /* No attributes yet */
  1755. attribute.mod_op = LDAP_MOD_ADD;
  1756. attribute.mod_type = "objectclass";
  1757. attribute.mod_values = strList1 (objClass);
  1758. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1759. return (-1);
  1760. attribute.mod_op = LDAP_MOD_ADD;
  1761. attribute.mod_type = attrName;
  1762. attribute.mod_values = strList1 (attrVal);
  1763. if (addAttrib (attrs, nbAttribs++, &attribute) < 0)
  1764. return (-1);
  1765. /*
  1766. * Add the entry
  1767. * If it doesn't work, we will recurse on the nodeDN
  1768. */
  1769. ret = ldap_add_ext_s (cnx, nodeDN, attrs, NULL, NULL);
  1770. if ((ret != LDAP_SUCCESS) && (ret != LDAP_ALREADY_EXISTS))
  1771. {
  1772. if (ret == LDAP_NO_SUCH_OBJECT)
  1773. { /*JLS 07-11-00*/
  1774. printf ("ldclt[%d]: T%03d: Parent of %s doesn't exist, looping\n",
  1775. mctx.pid, tttctx->thrdNum, nodeDN);
  1776. if (createMissingNodes (tttctx, nodeDN, cnx) < 0)
  1777. return (-1);
  1778. else
  1779. {
  1780. /*
  1781. * Well, the upper node is created. Maybe we should now
  1782. * create the node requested for this instance of the function.
  1783. * Two solutions, retry a ldap_add_s() or recursive call to
  1784. * createMissingNodes()... Let's be simple and recurse ;-)
  1785. * Don't forget that the cnx was released in the previous call.
  1786. */
  1787. cnx = NULL;
  1788. return (createMissingNodes (tttctx, newDN, cnx));
  1789. }
  1790. } /*JLS 07-11-00*/
  1791. /*
  1792. * Well, looks like it is more serious !
  1793. */
  1794. printf ("ldclt[%d]: T%03d: Cannot add (%s), error=%d (%s)\n",
  1795. mctx.pid, tttctx->thrdNum, nodeDN, ret, my_ldap_err2string (ret));
  1796. fflush (stdout);
  1797. (void)addErrorStat(ret);
  1798. return (-1);
  1799. }
  1800. /*
  1801. * Note that error this node may exist, i.e. being just created
  1802. * by another thread !
  1803. * Memorize this operation only if the entry was really created.
  1804. * Maybe we run in check mode, so be carreful...
  1805. */
  1806. if (ret != LDAP_ALREADY_EXISTS)
  1807. {
  1808. if (incrementNbOpers (tttctx) < 0)
  1809. return (-1);
  1810. #ifdef SOLARIS /*JLS 14-11-00*/
  1811. if (mctx.slavesNb > 0)
  1812. if (opAdd (tttctx, LDAP_REQ_ADD, nodeDN, attrs, NULL, NULL) < 0)
  1813. return (-1);
  1814. #endif /*JLS 14-11-00*/
  1815. }
  1816. else /*JLS 15-12-00*/
  1817. {
  1818. if (mctx.mode & COUNT_EACH) /*JLS 15-12-00*/
  1819. { /*JLS 15-12-00*/
  1820. if (incrementNbOpers (tttctx) < 0) /*JLS 15-12-00*/
  1821. return (-1); /*JLS 15-12-00*/
  1822. } /*JLS 15-12-00*/
  1823. }
  1824. /*
  1825. * Ok, we succeed to create the entry ! or it already exist.
  1826. * Don't forget to free the attributes and to release the cnx !!
  1827. */
  1828. if (freeAttrib (attrs) < 0)
  1829. return (-1);
  1830. /*
  1831. * Ouf ! End of this function.
  1832. */
  1833. return (0);
  1834. }
  1835. /* ****************************************************************************
  1836. FUNCTION : getPending
  1837. PURPOSE : Get the pending results, and perform some basic controls
  1838. on them.
  1839. INPUT : tttctx = thread context
  1840. timeout = how many times wait for a result.
  1841. OUTPUT : None.
  1842. RETURN : -1 if error, 0 else.
  1843. DESCRIPTION :
  1844. *****************************************************************************/
  1845. int
  1846. getPending (
  1847. thread_context *tttctx,
  1848. struct timeval *timeout)
  1849. {
  1850. LDAPMessage *res; /* LDAP async results */
  1851. int ret; /* Return values */
  1852. int expected = 0; /* Expect this type */
  1853. char *verb; /* LDAP verb expected */
  1854. int type; /* Message type */
  1855. int errcodep; /* Async error code */
  1856. int msgid; /* Async message id */
  1857. int msgOk; /* Message read. */
  1858. char *addErrMsg; /* Additional error msg */ /*JLS 03-08-00*/
  1859. /*
  1860. * Initialization
  1861. */
  1862. msgOk = 0; /* No message received */
  1863. if (tttctx->mode & ADD_ENTRIES)
  1864. {
  1865. expected = LDAP_RES_ADD;
  1866. verb = "ldap_add";
  1867. }
  1868. else if (tttctx->mode & DELETE_ENTRIES)
  1869. {
  1870. expected = LDAP_RES_DELETE;
  1871. verb = "ldap_delete";
  1872. }
  1873. else if (tttctx->mode & RENAME_ENTRIES)
  1874. {
  1875. expected = LDAP_RES_MODRDN;
  1876. verb = "ldap_rename";
  1877. }
  1878. else if (tttctx->mode & ATTR_REPLACE) /*JLS 21-11-00*/
  1879. {
  1880. expected = LDAP_RES_MODIFY; /*JLS 21-11-00*/
  1881. verb = "ldap_modify"; /*JLS 21-11-00*/
  1882. }
  1883. else
  1884. {
  1885. return (-1);
  1886. }
  1887. /*
  1888. * Here, we are in asynchronous mode...
  1889. * Too bad, lot of things to do here.
  1890. * First, let's see if we are above the reading threshold.
  1891. * This function may be called recursivelly to empty the input queue. When
  1892. * it is used this way, the timeout is set to zero.
  1893. */
  1894. if ((timeout == &(mctx.timevalZero)) ||
  1895. (tttctx->pendingNb >= mctx.asyncMin))
  1896. {
  1897. /*
  1898. * Retrieve the next pending request
  1899. * The result of ldap_result() may be -1 (error), 0 (timeout).
  1900. * If timeout, well... let's ignore it and continue.
  1901. */
  1902. ret = ldap_result (tttctx->ldapCtx, LDAP_RES_ANY, 1, timeout, &res);
  1903. if (ret != 0)
  1904. {
  1905. msgOk = 1;
  1906. if (ret < 0)
  1907. {
  1908. if (!ignoreError (ret))
  1909. {
  1910. msgOk = 0;
  1911. if (!(mctx.mode & QUIET))
  1912. {
  1913. printf ("ldclt[%d]: T%03d: Cannot ldap_result(), error=%d (%s)\n",
  1914. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  1915. fflush (stdout);
  1916. }
  1917. }
  1918. if (addErrorStat (ret) < 0)
  1919. return (-1);
  1920. }
  1921. else
  1922. {
  1923. /*
  1924. * Ensure it is what we expect to see...
  1925. */
  1926. type = ldap_msgtype (res);
  1927. msgid = ldap_msgid (res);
  1928. if (type != expected)
  1929. {
  1930. msgOk = 0;
  1931. printf ("ldclt[%d]: T%03d: bad result type 0x%02x\n",
  1932. mctx.pid, tttctx->thrdNum, type);
  1933. fflush (stdout);
  1934. if (msgIdDel (tttctx, msgid, 1) < 0)
  1935. return (-1);
  1936. return (0);
  1937. }
  1938. /*
  1939. * Parse the result
  1940. */
  1941. addErrMsg = NULL; /*JLS 03-08-00*/
  1942. ret = ldap_parse_result (tttctx->ldapCtx, res, &errcodep,
  1943. NULL, &addErrMsg, NULL, NULL, 0); /*JLS 03-08-00*/
  1944. if (ret < 0)
  1945. {
  1946. if (!ignoreError (ret))
  1947. {
  1948. msgOk = 0;
  1949. if (!(mctx.mode & QUIET))
  1950. {
  1951. printf ("ldclt[%d]: T%03d: Cannot ldap_parse_result(%s), error=%d (%s",
  1952. mctx.pid, tttctx->thrdNum, msgIdStr (tttctx, msgid),
  1953. ret, my_ldap_err2string (ret));
  1954. if ((addErrMsg != NULL) && (*addErrMsg != '\0')) /*JLS 03-08-00*/
  1955. { /*JLS 03-08-00*/
  1956. printf (" - %s", addErrMsg); /*JLS 03-08-00*/
  1957. ldap_memfree (addErrMsg); /*JLS 03-08-00*/
  1958. } /*JLS 03-08-00*/
  1959. printf (")\n"); /*JLS 03-08-00*/
  1960. fflush (stdout);
  1961. }
  1962. }
  1963. if (msgIdDel (tttctx, msgid, 1) < 0)
  1964. return (-1);
  1965. if (addErrorStat (ret) < 0)
  1966. return (-1);
  1967. }
  1968. /*
  1969. * Ensure the operation was well performed
  1970. * Note that an error to be ignored should be considered as
  1971. * a good message received (cf boolean msgOk).
  1972. */
  1973. if (errcodep != LDAP_SUCCESS)
  1974. {
  1975. if (!ignoreError (ret))
  1976. {
  1977. msgOk = 0;
  1978. if (!(mctx.mode & QUIET))
  1979. {
  1980. printf ("ldclt[%d]: T%03d: Cannot %s(%s), error=%d (%s)\n",
  1981. mctx.pid, tttctx->thrdNum, verb, msgIdStr (tttctx, msgid),
  1982. errcodep, my_ldap_err2string (errcodep));
  1983. fflush (stdout);
  1984. }
  1985. }
  1986. /*
  1987. * Maybe we must create the intermediate nodes ?
  1988. */
  1989. if (((expected == LDAP_RES_ADD) || (expected == LDAP_RES_MODRDN)) &&
  1990. (errcodep == LDAP_NO_SUCH_OBJECT))
  1991. {
  1992. /*
  1993. * Attention, for the rename operation we will memorize the new
  1994. * parent node and not the entry itself.
  1995. */
  1996. if (createMissingNodes (tttctx, msgIdStr (tttctx, msgid), NULL) < 0)
  1997. {
  1998. printf ("ldclt[%d]: T%03d: Cannot create the intermediate nodes for %s\n",
  1999. mctx.pid, tttctx->thrdNum, msgIdStr (tttctx, msgid));
  2000. fflush (stdout);
  2001. return (-1);
  2002. }
  2003. if ((mctx.mode & VERBOSE) && (!(mctx.mode & QUIET)))
  2004. {
  2005. printf ("ldclt[%d]: T%03d: Intermediate nodes created for %s\n",
  2006. mctx.pid, tttctx->thrdNum, msgIdStr (tttctx, msgid));
  2007. fflush (stdout);
  2008. }
  2009. }
  2010. /*
  2011. * Free the message's data
  2012. */
  2013. if (msgIdDel (tttctx, msgid, 1) < 0)
  2014. return (-1);
  2015. if (addErrorStat (errcodep) < 0)
  2016. return (-1);
  2017. }
  2018. else
  2019. {
  2020. /*
  2021. * Ok, the operation is well performed !
  2022. * Maybe we are running in check mode ?
  2023. */
  2024. if (mctx.slavesNb == 0)
  2025. {
  2026. if (msgIdDel (tttctx, msgid, 1) < 0)
  2027. return (-1);
  2028. }
  2029. #ifdef SOLARIS /*JLS 14-11-00*/
  2030. else
  2031. {
  2032. switch (expected)
  2033. {
  2034. case LDAP_RES_ADD:
  2035. if (opAdd (tttctx, LDAP_REQ_ADD, msgIdDN(tttctx, msgid),
  2036. msgIdAttribs(tttctx, msgid), NULL, NULL) < 0)
  2037. return (-1);
  2038. break;
  2039. case LDAP_RES_DELETE:
  2040. if (opAdd (tttctx, LDAP_REQ_DELETE, msgIdDN(tttctx, msgid),
  2041. NULL, NULL, NULL) < 0)
  2042. return (-1);
  2043. break;
  2044. case LDAP_RES_MODRDN:
  2045. if (opAdd (tttctx, LDAP_REQ_DELETE, msgIdDN(tttctx, msgid),
  2046. NULL, NULL, NULL) < 0)
  2047. /*
  2048. TBC : memorize the newRdn and newParent
  2049. */
  2050. return (-1);
  2051. break;
  2052. }
  2053. if (msgIdDel (tttctx, msgid, 1) < 0)
  2054. return (-1);
  2055. }
  2056. #endif /* SOLARIS */ /*JLS 14-11-00*/
  2057. }
  2058. /*
  2059. * Ok, it is a "SUCCESS" message.
  2060. * Don't forget to free the returned message !
  2061. */
  2062. tttctx->pendingNb--;
  2063. if ((ret = ldap_msgfree (res)) < 0)
  2064. {
  2065. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2066. {
  2067. printf ("ldclt[%d]: T%03d: Cannot ldap_msgfree(), error=%d (%s)\n",
  2068. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  2069. fflush (stdout);
  2070. }
  2071. if (addErrorStat (ret) < 0)
  2072. return (-1);
  2073. }
  2074. }
  2075. }
  2076. }
  2077. /*
  2078. * Maybe recurse to read the next message ?
  2079. */
  2080. if (msgOk)
  2081. return (getPending (tttctx, &(mctx.timevalZero)));
  2082. return (0);
  2083. }
  2084. /* ****************************************************************************
  2085. FUNCTION : doRename
  2086. PURPOSE : Perform an ldap_rename() operation.
  2087. INPUT : tttctx = thread context
  2088. OUTPUT : None.
  2089. RETURN : -1 if error, 0 else.
  2090. DESCRIPTION :
  2091. *****************************************************************************/
  2092. int
  2093. doRename (
  2094. thread_context *tttctx)
  2095. {
  2096. char oldDn[MAX_DN_LENGTH]; /* DN of the entry to rename */
  2097. int ret; /* Return values */
  2098. int retry; /* Retry after createMissingNodes() */
  2099. int retryed; /* If already retryed */
  2100. int msgid; /* For asynchronous mode */
  2101. /*
  2102. * Connection to the server
  2103. * The function connectToServer() will take care of the various connection/
  2104. * disconnection, bind/unbind/close etc... requested by the user.
  2105. * The cost is one more function call in this application, but the
  2106. * resulting source code will be much more easiest to maintain.
  2107. */
  2108. if (connectToServer (tttctx) < 0) /*JLS 18-12-00*/
  2109. return (-1); /*JLS 18-12-00*/
  2110. if (!(tttctx->binded)) /*JLS 18-12-00*/
  2111. return (0); /*JLS 18-12-00*/
  2112. /*
  2113. * Build a random entry name. Of course, we are not sure it exist
  2114. * but there is no other simple solution...
  2115. * Anyway, the result is: tttctx->bufFilter , tttctx->bufBaseDN
  2116. */
  2117. if (buildRandomRdnOrFilter (tttctx) < 0)
  2118. return (-1);
  2119. snprintf (oldDn, sizeof(oldDn), "%s,%s", tttctx->bufFilter, tttctx->bufBaseDN);
  2120. oldDn[sizeof(oldDn)-1] = '\0';
  2121. /*
  2122. * Now, build a random new name for this entry
  2123. */
  2124. if (buildRandomRdnOrFilter (tttctx) < 0)
  2125. return (-1);
  2126. /*
  2127. * Do the rename
  2128. * Maybe we are in synchronous mode ?
  2129. * We won't try to recover from errors in this function, because the
  2130. * way the library will tell the application that the target parent
  2131. * (tttctx->bufBaseDN) doesn't exist is LDAP_PROTOCOL_ERROR that may
  2132. * report as well three other problems.
  2133. */
  2134. if (!(mctx.mode & ASYNC))
  2135. {
  2136. /*
  2137. * We will retry with this entry...
  2138. * We need to memorize we have already retry to add an entry, because
  2139. * ldap_rename_s() returns LDAP_PROTOCOL_ERROR if the randomly chosen
  2140. * entry we try to rename does not exist ! If we don't, the program will
  2141. * loop infinitely on this.
  2142. * The only way is to decide that LDAP_PROTOCOL_ERROR is not a *real*
  2143. * error, that is detailed below :
  2144. * The possible meanings of LDAP_PROTOCOL_ERROR are :
  2145. * - BER problem, unlike to happen
  2146. * - newrdn is invalid, also unlike to happen
  2147. * - ldclt is not running ldap v3 - ok, we could leave with this.
  2148. * - the newparent is invalid. I thing that we could take this for
  2149. * "doesn't exist"...
  2150. */
  2151. retry = 1;
  2152. retryed = 0;
  2153. while (retry && !retryed)
  2154. {
  2155. if (mctx.mode & WITH_NEWPARENT) /*JLS 15-12-00*/
  2156. ret = ldap_rename_s (tttctx->ldapCtx, oldDn,
  2157. tttctx->bufFilter, tttctx->bufBaseDN, 1, NULL, NULL);
  2158. else /*JLS 15-12-00*/
  2159. ret = ldap_rename_s (tttctx->ldapCtx, oldDn, /*JLS 15-12-00*/
  2160. tttctx->bufFilter, NULL, 1, NULL, NULL);/*JLS 15-12-00*/
  2161. if (ret == LDAP_SUCCESS) /*JLS 15-12-00*/
  2162. {
  2163. retry = 0;
  2164. if (incrementNbOpers (tttctx) < 0) /* Memorize operation */
  2165. return (-1);
  2166. #ifdef SOLARIS /*JLS 14-11-00*/
  2167. if (mctx.slavesNb > 0)
  2168. if (opAdd (tttctx, LDAP_REQ_MODRDN, oldDn, NULL,
  2169. tttctx->bufFilter, tttctx->bufBaseDN) < 0)
  2170. return (-1);
  2171. #endif /*JLS 14-11-00*/
  2172. }
  2173. else /* Error */
  2174. {
  2175. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2176. {
  2177. printf ("ldclt[%d]: T%03d: Cannot rename (%s, %s, %s), error=%d (%s)\n",
  2178. mctx.pid, tttctx->thrdNum, oldDn, tttctx->bufFilter, tttctx->bufBaseDN,
  2179. ret, my_ldap_err2string (ret));
  2180. fflush (stdout);
  2181. }
  2182. if (addErrorStat (ret) < 0)
  2183. return (-1);
  2184. /*
  2185. * Check what to do depending on the error.
  2186. * From the c-sdk function description, if the parent node of the
  2187. * new dn (i.e. tttctx->bufBaseDN) doesn't exist, the error returned
  2188. * is LDAP_PROTOCOL_ERROR that may report as well three other problems.
  2189. * See discussion above.
  2190. */
  2191. /*
  2192. * Maybe we should count each operation ?
  2193. */
  2194. if ((mctx.mode & COUNT_EACH) && /*JLS 18-12-00*/
  2195. ((ret == LDAP_PROTOCOL_ERROR) || /*JLS 18-12-00*/
  2196. (ret == LDAP_NO_SUCH_OBJECT) || /*JLS 18-12-00*/
  2197. (ret == LDAP_ALREADY_EXISTS))) /*JLS 18-12-00*/
  2198. { /*JLS 18-12-00*/
  2199. if (incrementNbOpers (tttctx) < 0) /*JLS 18-12-00*/
  2200. return (-1); /*JLS 18-12-00*/
  2201. } /*JLS 18-12-00*/
  2202. /*
  2203. * Maybe we must create the intermediate nodes ?
  2204. */
  2205. if (ret != LDAP_PROTOCOL_ERROR) /*JLS 15-12-00*/
  2206. retry = 0;
  2207. else
  2208. {
  2209. if (createMissingNodes (tttctx, tttctx->bufBaseDN, NULL) < 0)
  2210. {
  2211. retry = 0;
  2212. printf ("ldclt[%d]: T%03d: Cannot create the intermediate nodes for %s\n",
  2213. mctx.pid, tttctx->thrdNum, tttctx->bufBaseDN);
  2214. fflush (stdout);
  2215. return (-1);
  2216. }
  2217. if ((mctx.mode & VERBOSE) && (!(mctx.mode & QUIET)))
  2218. {
  2219. printf ("ldclt[%d]: T%03d: Intermediate nodes created for %s\n",
  2220. mctx.pid, tttctx->thrdNum, tttctx->bufBaseDN);
  2221. fflush (stdout);
  2222. }
  2223. retryed = 1;
  2224. }
  2225. }
  2226. }
  2227. /*
  2228. * End of synchronous operations
  2229. */
  2230. return (0);
  2231. }
  2232. /*
  2233. * Here, we are in asynchronous mode...
  2234. * Too bad, lot of things to do here.
  2235. * First, let's see if we are above the reading threshold.
  2236. */
  2237. if (getPending (tttctx, &(mctx.timeval)) < 0)
  2238. return (-1);
  2239. /*
  2240. * Maybe we may send another request ?
  2241. * Well... there is no proper way to retrieve the error number for
  2242. * this, so I guess I may use direct access to the ldap context
  2243. * to read the field ld_errno.
  2244. */
  2245. if (tttctx->pendingNb > mctx.asyncMax)
  2246. {
  2247. if ((mctx.mode & VERBOSE) &&
  2248. (tttctx->asyncHit == 1) &&
  2249. (!(mctx.mode & SUPER_QUIET)))
  2250. {
  2251. tttctx->asyncHit = 1;
  2252. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  2253. mctx.pid, tttctx->thrdNum);
  2254. fflush (stdout);
  2255. }
  2256. }
  2257. else
  2258. {
  2259. if ((mctx.mode & VERBOSE) &&
  2260. (tttctx->asyncHit == 1) &&
  2261. (!(mctx.mode & SUPER_QUIET)))
  2262. {
  2263. tttctx->asyncHit = 0;
  2264. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  2265. mctx.pid, tttctx->thrdNum);
  2266. fflush (stdout);
  2267. }
  2268. if (mctx.mode & WITH_NEWPARENT) /*JLS 15-12-00*/
  2269. ret = ldap_rename (tttctx->ldapCtx, oldDn,
  2270. tttctx->bufFilter, tttctx->bufBaseDN,
  2271. 1, NULL, NULL, &msgid);
  2272. else /*JLS 15-12-00*/
  2273. ret = ldap_rename (tttctx->ldapCtx, oldDn, /*JLS 15-12-00*/
  2274. tttctx->bufFilter, NULL, /*JLS 15-12-00*/
  2275. 1, NULL, NULL, &msgid); /*JLS 15-12-00*/
  2276. if (ret < 0)
  2277. {
  2278. if (ldap_get_option (tttctx->ldapCtx, LDAP_OPT_ERROR_NUMBER, &ret) < 0)
  2279. {
  2280. printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
  2281. mctx.pid, tttctx->thrdNum);
  2282. fflush (stdout);
  2283. return (-1);
  2284. }
  2285. else
  2286. {
  2287. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2288. {
  2289. printf ("ldclt[%d]: T%03d: Cannot rename (%s, %s, %s), error=%d (%s)\n",
  2290. mctx.pid, tttctx->thrdNum, oldDn, tttctx->bufFilter, tttctx->bufBaseDN,
  2291. ret, my_ldap_err2string (ret));
  2292. fflush (stdout);
  2293. }
  2294. if (addErrorStat (ret) < 0)
  2295. return (-1);
  2296. /*
  2297. * Maybe we should count each operation ?
  2298. */
  2299. if ((mctx.mode & COUNT_EACH) && /*JLS 18-12-00*/
  2300. ((ret == LDAP_PROTOCOL_ERROR) || /*JLS 18-12-00*/
  2301. (ret == LDAP_NO_SUCH_OBJECT) || /*JLS 18-12-00*/
  2302. (ret == LDAP_ALREADY_EXISTS))) /*JLS 18-12-00*/
  2303. { /*JLS 18-12-00*/
  2304. if (incrementNbOpers (tttctx) < 0) /*JLS 18-12-00*/
  2305. return (-1); /*JLS 18-12-00*/
  2306. } /*JLS 18-12-00*/
  2307. /*
  2308. * Maybe we must create the intermediate nodes ?
  2309. * Question: is it likely probable that such error is returned
  2310. * by the server on a *asynchornous* operation ?
  2311. * See discussion about the error returned in the synchronous section
  2312. * of this function.
  2313. */
  2314. if (ret == LDAP_PROTOCOL_ERROR) /*JLS 15-12-00*/
  2315. { /*JLS 15-12-00*/
  2316. if (createMissingNodes (tttctx, tttctx->bufBaseDN, NULL) < 0)
  2317. return (-1);
  2318. } /*JLS 15-12-00*/
  2319. }
  2320. }
  2321. else
  2322. {
  2323. /*
  2324. * Memorize the operation
  2325. */
  2326. /*
  2327. TBC : I'm not sure what will happen if we call msgIdAdd() with a NULL
  2328. pointer as attribs !!!!!
  2329. */
  2330. if (msgIdAdd (tttctx, msgid, tttctx->bufBaseDN, oldDn, NULL) < 0)
  2331. return (-1);
  2332. if (incrementNbOpers (tttctx) < 0)
  2333. return (-1);
  2334. tttctx->pendingNb++;
  2335. }
  2336. }
  2337. if (mctx.mode & VERY_VERBOSE)
  2338. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  2339. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  2340. /*
  2341. * End of asynchronous operation... and also end of function.
  2342. */
  2343. return (0);
  2344. }
  2345. /* ****************************************************************************
  2346. FUNCTION : ldclt_write_genldif
  2347. PURPOSE : Implements buffered write to speed up -e genldif
  2348. implementation.
  2349. INPUT : str = string to write.
  2350. lgth = length of this string.
  2351. OUTPUT : None.
  2352. RETURN : None.
  2353. DESCRIPTION : Usual write() function is unbuffered on Solaris and
  2354. thus really slow down the whole process. Using this
  2355. function allow ldclt to perform 8 times faster.
  2356. We cannot use fprintf() because of portability issues
  2357. regarding large files support.
  2358. *****************************************************************************/
  2359. char *ldclt_write_genldif_buf = NULL;
  2360. char *ldclt_write_genldif_pt;
  2361. int ldclt_write_genldif_nb;
  2362. void
  2363. ldclt_flush_genldif (void)
  2364. {
  2365. write (mctx.genldifFile, ldclt_write_genldif_buf, ldclt_write_genldif_nb);
  2366. ldclt_write_genldif_pt = ldclt_write_genldif_buf;
  2367. ldclt_write_genldif_nb = 0;
  2368. }
  2369. void
  2370. ldclt_write_genldif (
  2371. char *str,
  2372. int lgth)
  2373. {
  2374. /*
  2375. * First call ?
  2376. */
  2377. if (ldclt_write_genldif_buf == NULL)
  2378. {
  2379. ldclt_write_genldif_buf = (char *) malloc (65536);
  2380. ldclt_write_genldif_pt = ldclt_write_genldif_buf;
  2381. ldclt_write_genldif_nb = 0;
  2382. }
  2383. /*
  2384. * Buffer full ?
  2385. */
  2386. if (ldclt_write_genldif_nb + lgth >= 65536)
  2387. ldclt_flush_genldif();
  2388. /*
  2389. * Add to the buffer
  2390. */
  2391. strncpy (ldclt_write_genldif_pt, str, lgth);
  2392. ldclt_write_genldif_pt += lgth;
  2393. ldclt_write_genldif_nb += lgth;
  2394. }
  2395. /* ****************************************************************************
  2396. FUNCTION : doGenldif
  2397. PURPOSE : Create a ldif file from the given parameters.
  2398. INPUT : tttctx = thread context
  2399. OUTPUT : None.
  2400. RETURN : -1 if error, 0 else.
  2401. DESCRIPTION :
  2402. *****************************************************************************/
  2403. int
  2404. doGenldif (
  2405. thread_context *tttctx)
  2406. {
  2407. char newDn[MAX_DN_LENGTH]; /* DN of the new entry */
  2408. LDAPMod *attrs[MAX_ATTRIBS]; /* Attributes of this entry */
  2409. int i; /* For the loop */
  2410. /*
  2411. * Build a new entry
  2412. */
  2413. if (buildNewEntry (tttctx, newDn, attrs) < 0)
  2414. return (-1);
  2415. /*
  2416. * Dump this entry.
  2417. * Using a buffer speeds writes 3 times faster.
  2418. */
  2419. ldclt_write_genldif ("dn: ", 4); /*JLS 02-04-01*/
  2420. ldclt_write_genldif (newDn, strlen (newDn)); /*JLS 02-04-01*/
  2421. ldclt_write_genldif ("\n", 1); /*JLS 02-04-01*/
  2422. for (i=0 ; attrs[i]!=NULL ; i++) /*JLS 02-04-01*/
  2423. { /*JLS 02-04-01*/
  2424. ldclt_write_genldif (attrs[i]->mod_type, /*JLS 02-04-01*/
  2425. strlen (attrs[i]->mod_type)); /*JLS 02-04-01*/
  2426. ldclt_write_genldif (": ", 2); /*JLS 02-04-01*/
  2427. ldclt_write_genldif (attrs[i]->mod_values[0], /*JLS 02-04-01*/
  2428. strlen (attrs[i]->mod_values[0])); /*JLS 02-04-01*/
  2429. ldclt_write_genldif ("\n", 1); /*JLS 02-04-01*/
  2430. } /*JLS 02-04-01*/
  2431. ldclt_write_genldif ("\n", 1); /*JLS 02-04-01*/
  2432. /*
  2433. * Increment counters
  2434. */
  2435. if (incrementNbOpers (tttctx) < 0) /*JLS 28-03-01*/
  2436. return (-1); /*JLS 28-03-01*/
  2437. /*
  2438. * Free the memory and return
  2439. */
  2440. if (freeAttrib (attrs) < 0)
  2441. return (-1);
  2442. return (0);
  2443. }
  2444. /* ****************************************************************************
  2445. FUNCTION : doAddEntry
  2446. PURPOSE : Perform an ldap_add() operation.
  2447. INPUT : tttctx = thread context
  2448. OUTPUT : None.
  2449. RETURN : -1 if error, 0 else.
  2450. DESCRIPTION :
  2451. *****************************************************************************/
  2452. int
  2453. doAddEntry (
  2454. thread_context *tttctx)
  2455. {
  2456. char newDn[MAX_DN_LENGTH]; /* DN of the new entry */
  2457. LDAPMod *attrs[MAX_ATTRIBS]; /* Attributes of this entry */
  2458. int ret; /* Return values */
  2459. int retry; /* Retry after createMissingNodes() */
  2460. /*
  2461. * Connection to the server
  2462. * The function connectToServer() will take care of the various connection/
  2463. * disconnection, bind/unbind/close etc... requested by the user.
  2464. * The cost is one more function call in this application, but the
  2465. * resulting source code will be much more easiest to maintain.
  2466. */
  2467. if (connectToServer (tttctx) < 0)
  2468. return (-1);
  2469. if (!(tttctx->binded)) /*JLS 18-12-00*/
  2470. return (0); /*JLS 18-12-00*/
  2471. /*
  2472. * Do the add
  2473. * Maybe we are in synchronous mode ?
  2474. */
  2475. if (!(mctx.mode & ASYNC))
  2476. {
  2477. /*
  2478. * Build the new entry
  2479. */
  2480. if (buildNewEntry (tttctx, newDn, attrs) < 0)
  2481. return (-1);
  2482. /*
  2483. * We will retry with this entry...
  2484. */
  2485. retry = 1;
  2486. while (retry)
  2487. {
  2488. ret = ldap_add_ext_s (tttctx->ldapCtx, newDn, attrs, NULL, NULL);
  2489. if (ret != LDAP_SUCCESS)
  2490. {
  2491. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2492. {
  2493. printf ("ldclt[%d]: T%03d: Cannot add (%s), error=%d (%s)\n",
  2494. mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
  2495. fflush (stdout);
  2496. }
  2497. if (addErrorStat (ret) < 0)
  2498. return (-1);
  2499. /*
  2500. * Maybe we must create the intermediate nodes ?
  2501. */
  2502. if (ret != LDAP_NO_SUCH_OBJECT)
  2503. {
  2504. if ((ret == LDAP_ALREADY_EXISTS) && /*JLS 15-12-00*/
  2505. (mctx.mode & COUNT_EACH)) /*JLS 15-12-00*/
  2506. { /*JLS 15-12-00*/
  2507. if (incrementNbOpers (tttctx) < 0) /*JLS 15-12-00*/
  2508. return (-1); /*JLS 15-12-00*/
  2509. } /*JLS 15-12-00*/
  2510. retry = 0;
  2511. }
  2512. else
  2513. {
  2514. if (createMissingNodes (tttctx, newDn, NULL) < 0)
  2515. {
  2516. retry = 0;
  2517. printf ("ldclt[%d]: T%03d: Cannot create the intermediate nodes for %s\n",
  2518. mctx.pid, tttctx->thrdNum, newDn);
  2519. fflush (stdout);
  2520. return (-1);
  2521. }
  2522. if ((mctx.mode & VERBOSE) && (!(mctx.mode & QUIET)))
  2523. {
  2524. printf ("ldclt[%d]: T%03d: Intermediate nodes created for %s\n",
  2525. mctx.pid, tttctx->thrdNum, newDn);
  2526. fflush (stdout);
  2527. }
  2528. }
  2529. }
  2530. else
  2531. {
  2532. retry = 0;
  2533. if (incrementNbOpers (tttctx) < 0) /* Memorize operation */
  2534. return (-1);
  2535. #ifdef SOLARIS /*JLS 14-11-00*/
  2536. if (mctx.slavesNb > 0)
  2537. if (opAdd (tttctx, LDAP_REQ_ADD, newDn, attrs, NULL, NULL) < 0)
  2538. return (-1);
  2539. #endif /*JLS 14-11-00*/
  2540. }
  2541. }
  2542. /*
  2543. * Free the attributes
  2544. */
  2545. if (freeAttrib (attrs) < 0)
  2546. return (-1);
  2547. /*
  2548. * End of synchronous operations
  2549. */
  2550. return (0);
  2551. }
  2552. /*
  2553. * Here, we are in asynchronous mode...
  2554. * Too bad, lot of things to do here.
  2555. * First, let's see if we are above the reading threshold.
  2556. */
  2557. if (getPending (tttctx, &(mctx.timeval)) < 0)
  2558. return (-1);
  2559. /*
  2560. * Maybe we may send another request ?
  2561. * Well... there is no proper way to retrieve the error number for
  2562. * this, so I guess I may use direct access to the ldap context
  2563. * to read the field ld_errno.
  2564. */
  2565. if (tttctx->pendingNb > mctx.asyncMax)
  2566. {
  2567. if ((mctx.mode & VERBOSE) &&
  2568. (tttctx->asyncHit == 1) &&
  2569. (!(mctx.mode & SUPER_QUIET)))
  2570. {
  2571. tttctx->asyncHit = 1;
  2572. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  2573. mctx.pid, tttctx->thrdNum);
  2574. fflush (stdout);
  2575. }
  2576. }
  2577. else
  2578. {
  2579. int msgid = 0;
  2580. if ((mctx.mode & VERBOSE) &&
  2581. (tttctx->asyncHit == 1) &&
  2582. (!(mctx.mode & SUPER_QUIET)))
  2583. {
  2584. tttctx->asyncHit = 0;
  2585. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  2586. mctx.pid, tttctx->thrdNum);
  2587. fflush (stdout);
  2588. }
  2589. /*
  2590. * Build the new entry
  2591. */
  2592. if (buildNewEntry (tttctx, newDn, attrs) < 0)
  2593. return (-1);
  2594. ret = ldap_add_ext (tttctx->ldapCtx, newDn, attrs, NULL, NULL, &msgid);
  2595. if (ret < 0)
  2596. {
  2597. if (ldap_get_option (tttctx->ldapCtx, LDAP_OPT_ERROR_NUMBER, &ret) < 0)
  2598. {
  2599. printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
  2600. mctx.pid, tttctx->thrdNum);
  2601. fflush (stdout);
  2602. return (-1);
  2603. }
  2604. else
  2605. {
  2606. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2607. {
  2608. printf ("ldclt[%d]: T%03d: Cannot add(), error=%d (%s)\n",
  2609. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  2610. fflush (stdout);
  2611. }
  2612. if (addErrorStat (ret) < 0)
  2613. return (-1);
  2614. /*
  2615. * Maybe we must create the intermediate nodes ?
  2616. * Question: is it likely probable that sush error is returned
  2617. * by the server on a *asynchornous* operation ?
  2618. */
  2619. if (ret == LDAP_NO_SUCH_OBJECT)
  2620. if (createMissingNodes (tttctx, newDn, NULL) < 0)
  2621. return (-1);
  2622. if ((ret == LDAP_ALREADY_EXISTS) && /*JLS 15-12-00*/
  2623. (mctx.mode & COUNT_EACH)) /*JLS 15-12-00*/
  2624. { /*JLS 15-12-00*/
  2625. if (incrementNbOpers (tttctx) < 0) /*JLS 15-12-00*/
  2626. return (-1); /*JLS 15-12-00*/
  2627. } /*JLS 15-12-00*/
  2628. }
  2629. }
  2630. else
  2631. {
  2632. /*
  2633. * Memorize the operation
  2634. */
  2635. if (msgIdAdd (tttctx, msgid, newDn, newDn, attrs) < 0)
  2636. return (-1);
  2637. if (incrementNbOpers (tttctx) < 0)
  2638. return (-1);
  2639. tttctx->pendingNb++;
  2640. }
  2641. }
  2642. if (mctx.mode & VERY_VERBOSE)
  2643. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  2644. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  2645. /*
  2646. * End of asynchronous operation... and also end of function.
  2647. */
  2648. return (0);
  2649. }
  2650. /* ****************************************************************************
  2651. FUNCTION : doAttrReplace
  2652. PURPOSE : Perform an ldap_modify() operation, to replace an
  2653. attribute of the entry.
  2654. INPUT : tttctx = thread context
  2655. OUTPUT : None.
  2656. RETURN : -1 if error, 0 else.
  2657. DESCRIPTION :
  2658. *****************************************************************************/
  2659. int
  2660. doAttrReplace (
  2661. thread_context *tttctx)
  2662. {
  2663. char newDn[MAX_DN_LENGTH]; /* DN of the new entry */
  2664. LDAPMod *attrs[MAX_ATTRIBS]; /* Attributes of this entry */
  2665. int ret; /* Return values */
  2666. int msgid; /* For asynchronous mode */
  2667. /*
  2668. * Connection to the server
  2669. * The function connectToServer() will take care of the various connection/
  2670. * disconnection, bind/unbind/close etc... requested by the user.
  2671. * The cost is one more function call in this application, but the
  2672. * resulting source code will be much more easiest to maintain.
  2673. */
  2674. if (connectToServer (tttctx) < 0)
  2675. return (-1);
  2676. if (!(tttctx->binded)) /*JLS 18-12-00*/
  2677. return (0); /*JLS 18-12-00*/
  2678. /*
  2679. * Do the modify
  2680. * Maybe we are in synchronous mode ?
  2681. */
  2682. if (!(mctx.mode & ASYNC))
  2683. {
  2684. /*
  2685. * Build the new entry
  2686. */
  2687. if (buildNewModAttrib (tttctx, newDn, attrs) < 0)
  2688. return (-1);
  2689. /*
  2690. * We will modify this entry
  2691. */
  2692. ret = ldap_modify_ext_s (tttctx->ldapCtx, newDn, attrs, NULL, NULL);
  2693. if (ret != LDAP_SUCCESS)
  2694. {
  2695. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2696. {
  2697. printf ("ldclt[%d]: T%03d: Cannot modify (%s), error=%d (%s)\n",
  2698. mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
  2699. fflush (stdout);
  2700. }
  2701. if (addErrorStat (ret) < 0)
  2702. return (-1);
  2703. }
  2704. else
  2705. {
  2706. if (incrementNbOpers (tttctx) < 0) /* Memorize operation */
  2707. return (-1);
  2708. }
  2709. /*
  2710. * Free the attributes
  2711. */
  2712. if (freeAttrib (attrs) < 0)
  2713. return (-1);
  2714. /*
  2715. * End of synchronous operations
  2716. */
  2717. return (0);
  2718. }
  2719. /*
  2720. * Here, we are in asynchronous mode...
  2721. * Too bad, lot of things to do here.
  2722. * First, let's see if we are above the reading threshold.
  2723. */
  2724. if (getPending (tttctx, &(mctx.timeval)) < 0)
  2725. return (-1);
  2726. /*
  2727. * Maybe we may send another request ?
  2728. * Well... there is no proper way to retrieve the error number for
  2729. * this, so I guess I may use direct access to the ldap context
  2730. * to read the field ld_errno.
  2731. */
  2732. if (tttctx->pendingNb > mctx.asyncMax)
  2733. {
  2734. if ((mctx.mode & VERBOSE) &&
  2735. (tttctx->asyncHit == 1) &&
  2736. (!(mctx.mode & SUPER_QUIET)))
  2737. {
  2738. tttctx->asyncHit = 1;
  2739. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  2740. mctx.pid, tttctx->thrdNum);
  2741. fflush (stdout);
  2742. }
  2743. }
  2744. else
  2745. {
  2746. if ((mctx.mode & VERBOSE) &&
  2747. (tttctx->asyncHit == 1) &&
  2748. (!(mctx.mode & SUPER_QUIET)))
  2749. {
  2750. tttctx->asyncHit = 0;
  2751. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  2752. mctx.pid, tttctx->thrdNum);
  2753. fflush (stdout);
  2754. }
  2755. /*
  2756. * Build the new entry
  2757. */
  2758. if (buildNewModAttrib (tttctx, newDn, attrs) < 0)
  2759. return (-1);
  2760. ret = ldap_modify_ext (tttctx->ldapCtx, newDn, attrs, NULL, NULL, &msgid);
  2761. if (ret != LDAP_SUCCESS)
  2762. {
  2763. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2764. {
  2765. printf ("ldclt[%d]: T%03d: Cannot modify(%s), error=%d (%s)\n",
  2766. mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
  2767. fflush (stdout);
  2768. }
  2769. if (addErrorStat (ret) < 0)
  2770. return (-1);
  2771. }
  2772. else
  2773. {
  2774. /*
  2775. * Memorize the operation
  2776. */
  2777. if (msgIdAdd (tttctx, msgid, newDn, newDn, attrs) < 0)
  2778. return (-1);
  2779. if (incrementNbOpers (tttctx) < 0)
  2780. return (-1);
  2781. tttctx->pendingNb++;
  2782. }
  2783. }
  2784. if (mctx.mode & VERY_VERBOSE)
  2785. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  2786. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  2787. /*
  2788. * End of asynchronous operation... and also end of function.
  2789. */
  2790. return (0);
  2791. }
  2792. /* ****************************************************************************
  2793. FUNCTION : doAttrFileReplace
  2794. PURPOSE : Perform an ldap_modify() operation, to replace an
  2795. attribute of the entry with content read from file .
  2796. INPUT : tttctx = thread context
  2797. OUTPUT : None.
  2798. RETURN : -1 if error, 0 else.
  2799. DESCRIPTION :
  2800. *****************************************************************************/
  2801. int
  2802. doAttrFileReplace (
  2803. thread_context *tttctx)
  2804. {
  2805. char newDn[MAX_DN_LENGTH]; /* DN of the new entry */
  2806. LDAPMod *attrs[MAX_ATTRIBS]; /* Attributes of this entry */
  2807. int ret; /* Return values */
  2808. int msgid; /* For asynchronous mode */
  2809. /*
  2810. * Connection to the server
  2811. * The function connectToServer() will take care of the various connection/
  2812. * disconnection, bind/unbind/close etc... requested by the user.
  2813. * The cost is one more function call in this application, but the
  2814. * resulting source code will be much more easiest to maintain.
  2815. */
  2816. if (connectToServer (tttctx) < 0) /* if connection is being established, */
  2817. return (-1); /* then tttctx->ldapCtx would exist and holds connection */
  2818. if (!(tttctx->binded))
  2819. return (0);
  2820. /*
  2821. * Do the modify
  2822. * Maybe we are in synchronous mode ?
  2823. */
  2824. if (!(mctx.mode & ASYNC))
  2825. {
  2826. /*
  2827. * Build the new entry
  2828. */
  2829. if (buildNewModAttribFile (tttctx, newDn, attrs) < 0)
  2830. return (-1);
  2831. /*
  2832. * We will modify this entry
  2833. */
  2834. ret = ldap_modify_ext_s (tttctx->ldapCtx, newDn, attrs, NULL, NULL);
  2835. if (ret != LDAP_SUCCESS)
  2836. {
  2837. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2838. {
  2839. printf ("ldclt[%d]: T%03d: AttriFileReplace Error Cannot modify (%s), error=%d (%s)\n",
  2840. mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
  2841. fflush (stdout);
  2842. }
  2843. if (addErrorStat (ret) < 0)
  2844. return (-1);
  2845. }
  2846. else
  2847. {
  2848. printf ("ldclt[%d]: T%03d: AttriFileReplace modify (%s) success ,\n",
  2849. mctx.pid, tttctx->thrdNum, newDn);
  2850. if (incrementNbOpers (tttctx) < 0) /* Memorize operation */
  2851. return (-1);
  2852. }
  2853. /*
  2854. * Free the attributes
  2855. */
  2856. if (freeAttrib (attrs) < 0)
  2857. return (-1);
  2858. /*
  2859. * End of synchronous operations
  2860. */
  2861. return (0);
  2862. }
  2863. /*
  2864. * Here, we are in asynchronous mode...
  2865. * Too bad, lot of things to do here.
  2866. * First, let's see if we are above the reading threshold.
  2867. */
  2868. if (getPending (tttctx, &(mctx.timeval)) < 0)
  2869. return (-1);
  2870. /*
  2871. * Maybe we may send another request ?
  2872. * Well... there is no proper way to retrieve the error number for
  2873. * this, so I guess I may use direct access to the ldap context
  2874. * to read the field ld_errno.
  2875. */
  2876. if (tttctx->pendingNb > mctx.asyncMax)
  2877. {
  2878. if ((mctx.mode & VERBOSE) &&
  2879. (tttctx->asyncHit == 1) &&
  2880. (!(mctx.mode & SUPER_QUIET)))
  2881. {
  2882. tttctx->asyncHit = 1;
  2883. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  2884. mctx.pid, tttctx->thrdNum);
  2885. fflush (stdout);
  2886. }
  2887. }
  2888. else
  2889. {
  2890. if ((mctx.mode & VERBOSE) &&
  2891. (tttctx->asyncHit == 1) &&
  2892. (!(mctx.mode & SUPER_QUIET)))
  2893. {
  2894. tttctx->asyncHit = 0;
  2895. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  2896. mctx.pid, tttctx->thrdNum);
  2897. fflush (stdout);
  2898. }
  2899. /*
  2900. * Build the new entry
  2901. */
  2902. if (buildNewModAttrib (tttctx, newDn, attrs) < 0)
  2903. return (-1);
  2904. ret = ldap_modify_ext (tttctx->ldapCtx, newDn, attrs, NULL, NULL, &msgid);
  2905. if (ret != LDAP_SUCCESS)
  2906. {
  2907. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2908. {
  2909. printf ("ldclt[%d]: T%03d: Cannot modify(%s), error=%d (%s)\n",
  2910. mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
  2911. fflush (stdout);
  2912. }
  2913. if (addErrorStat (ret) < 0)
  2914. return (-1);
  2915. }
  2916. else
  2917. {
  2918. /*
  2919. * Memorize the operation
  2920. */
  2921. if (msgIdAdd (tttctx, msgid, newDn, newDn, attrs) < 0)
  2922. return (-1);
  2923. if (incrementNbOpers (tttctx) < 0)
  2924. return (-1);
  2925. tttctx->pendingNb++;
  2926. }
  2927. }
  2928. if (mctx.mode & VERY_VERBOSE)
  2929. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  2930. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  2931. /*
  2932. * End of asynchronous operation... and also end of function.
  2933. */
  2934. return (0);
  2935. }
  2936. /* ****************************************************************************
  2937. FUNCTION : doDeleteEntry
  2938. PURPOSE : Perform an ldap_delete() operation.
  2939. INPUT : tttctx = thread context
  2940. OUTPUT : None.
  2941. RETURN : -1 if error, 0 else.
  2942. DESCRIPTION :
  2943. *****************************************************************************/
  2944. int
  2945. doDeleteEntry (
  2946. thread_context *tttctx)
  2947. {
  2948. int ret; /* Return values */
  2949. char delDn[MAX_DN_LENGTH]; /* The entry to delete */
  2950. /*
  2951. * Connection to the server
  2952. * The function connectToServer() will take care of the various connection/
  2953. * disconnection, bind/unbind/close etc... requested by the user.
  2954. * The cost is one more function call in this application, but the
  2955. * resulting source code will be much more easiest to maintain.
  2956. */
  2957. if (connectToServer (tttctx) < 0)
  2958. return (-1);
  2959. if (!(tttctx->binded)) /*JLS 18-12-00*/
  2960. return (0); /*JLS 18-12-00*/
  2961. /*
  2962. * Do the delete
  2963. * Maybe we are in synchronous mode ?
  2964. */
  2965. if (!(mctx.mode & ASYNC))
  2966. {
  2967. /*
  2968. * Random (or incremental) creation of the rdn to delete.
  2969. * The resulting rdn is in tttctx->bufFilter.
  2970. */
  2971. if (buildRandomRdnOrFilter (tttctx) < 0)
  2972. return (-1);
  2973. snprintf(delDn, sizeof(delDn), "%s,%s", tttctx->bufFilter, tttctx->bufBaseDN);
  2974. delDn[sizeof(delDn)-1] = '\0';
  2975. ret = ldap_delete_ext_s (tttctx->ldapCtx, delDn, NULL, NULL);
  2976. if (ret != LDAP_SUCCESS)
  2977. {
  2978. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  2979. {
  2980. printf ("ldclt[%d]: T%03d: Cannot ldap_delete (%s), error=%d (%s)\n",
  2981. mctx.pid, tttctx->thrdNum, delDn, ret, my_ldap_err2string (ret));
  2982. fflush (stdout);
  2983. }
  2984. if (addErrorStat (ret) < 0)
  2985. return (-1);
  2986. if ((ret == LDAP_NO_SUCH_OBJECT) && /*JLS 15-12-00*/
  2987. (mctx.mode & COUNT_EACH)) /*JLS 15-12-00*/
  2988. { /*JLS 15-12-00*/
  2989. if (incrementNbOpers (tttctx) < 0) /*JLS 15-12-00*/
  2990. return (-1); /*JLS 15-12-00*/
  2991. } /*JLS 15-12-00*/
  2992. }
  2993. else
  2994. {
  2995. if (incrementNbOpers (tttctx) < 0) /* Memorize operation */
  2996. return (-1);
  2997. #ifdef SOLARIS /*JLS 14-11-00*/
  2998. if (mctx.slavesNb > 0)
  2999. if (opAdd (tttctx, LDAP_REQ_DELETE, delDn, NULL, NULL, NULL) < 0)
  3000. return (-1);
  3001. #endif /*JLS 14-11-00*/
  3002. }
  3003. /*
  3004. * End of synchronous operations
  3005. */
  3006. return (0);
  3007. }
  3008. /*
  3009. * Here, we are in asynchronous mode...
  3010. * Too bad, lot of things to do here.
  3011. * First, let's see if we are above the reading threshold.
  3012. */
  3013. if (getPending (tttctx, &(mctx.timeval)) < 0)
  3014. return (-1);
  3015. /*
  3016. * Maybe we may send another request ?
  3017. * Well... there is no proper way to retrieve the error number for
  3018. * this, so I guess I may use direct access to the ldap context
  3019. * to read the field ld_errno.
  3020. */
  3021. if (tttctx->pendingNb > mctx.asyncMax)
  3022. {
  3023. if ((mctx.mode & VERBOSE) &&
  3024. (tttctx->asyncHit == 1) &&
  3025. (!(mctx.mode & SUPER_QUIET)))
  3026. {
  3027. tttctx->asyncHit = 1;
  3028. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  3029. mctx.pid, tttctx->thrdNum);
  3030. fflush (stdout);
  3031. }
  3032. }
  3033. else
  3034. {
  3035. int msgid = 0;
  3036. if ((mctx.mode & VERBOSE) &&
  3037. (tttctx->asyncHit == 1) &&
  3038. (!(mctx.mode & SUPER_QUIET)))
  3039. {
  3040. tttctx->asyncHit = 0;
  3041. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  3042. mctx.pid, tttctx->thrdNum);
  3043. fflush (stdout);
  3044. }
  3045. /*
  3046. * Random (or incremental) creation of the rdn to delete.
  3047. * The resulting rdn is in tttctx->bufFilter.
  3048. */
  3049. if (buildRandomRdnOrFilter (tttctx) < 0)
  3050. return (-1);
  3051. snprintf (delDn, sizeof(delDn), "%s,%s", tttctx->bufFilter, tttctx->bufBaseDN);
  3052. delDn[sizeof(delDn)-1] = '\0';
  3053. ret = ldap_delete_ext (tttctx->ldapCtx, delDn, NULL, NULL, &msgid);
  3054. if (ret < 0)
  3055. {
  3056. if (ldap_get_option (tttctx->ldapCtx, LDAP_OPT_ERROR_NUMBER, &ret) < 0)
  3057. {
  3058. printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
  3059. mctx.pid, tttctx->thrdNum);
  3060. fflush (stdout);
  3061. return (-1);
  3062. }
  3063. else
  3064. {
  3065. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3066. {
  3067. printf ("ldclt[%d]: T%03d: Cannot ldap_delete(), error=%d (%s)\n",
  3068. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  3069. fflush (stdout);
  3070. }
  3071. if (addErrorStat (ret) < 0)
  3072. return (-1);
  3073. if ((ret == LDAP_NO_SUCH_OBJECT) && /*JLS 15-12-00*/
  3074. (mctx.mode & COUNT_EACH)) /*JLS 15-12-00*/
  3075. { /*JLS 15-12-00*/
  3076. if (incrementNbOpers (tttctx) < 0) /*JLS 15-12-00*/
  3077. return (-1); /*JLS 15-12-00*/
  3078. } /*JLS 15-12-00*/
  3079. }
  3080. }
  3081. else
  3082. {
  3083. /*
  3084. * Memorize the operation
  3085. */
  3086. if (incrementNbOpers (tttctx) < 0)
  3087. return (-1);
  3088. tttctx->pendingNb++;
  3089. }
  3090. }
  3091. if (mctx.mode & VERY_VERBOSE)
  3092. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  3093. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  3094. /*
  3095. * End of asynchronous operation... and also end of doDeleteEntry().
  3096. */
  3097. return (0);
  3098. }
  3099. /* ****************************************************************************
  3100. FUNCTION : doBindOnly
  3101. PURPOSE : Perform only bind/unbind operations.
  3102. INPUT : tttctx = thread context
  3103. OUTPUT : None.
  3104. RETURN : -1 if error, 0 else.
  3105. DESCRIPTION :
  3106. *****************************************************************************/
  3107. int
  3108. doBindOnly (
  3109. thread_context *tttctx)
  3110. {
  3111. /*
  3112. * Connection to the server
  3113. * The function connectToServer() will take care of the various connection/
  3114. * disconnection, bind/unbind/close etc... requested by the user.
  3115. * The cost is one more function call in this application, but the
  3116. * resulting source code will be much more easiest to maintain.
  3117. */
  3118. if (connectToServer (tttctx) < 0)
  3119. return (-1);
  3120. /* don't count failed binds unless counteach option is used */
  3121. if (!(tttctx->binded) && !(mctx.mode & COUNT_EACH))
  3122. return (0);
  3123. if (incrementNbOpers (tttctx) < 0)
  3124. return (-1);
  3125. return (0);
  3126. }
  3127. /* ****************************************************************************
  3128. FUNCTION : doExactSearch
  3129. PURPOSE : Perform one exact search operation.
  3130. INPUT : tttctx = thread context
  3131. OUTPUT : None.
  3132. RETURN : -1 if error, 0 else.
  3133. DESCRIPTION :
  3134. *****************************************************************************/
  3135. int
  3136. doExactSearch (
  3137. thread_context *tttctx)
  3138. {
  3139. int ret; /* Return value */
  3140. LDAPMessage *res; /* LDAP results */
  3141. LDAPMessage *e; /* LDAP results */
  3142. char **attrlist; /* Attribs list */ /*JLS 15-03-01*/
  3143. LDAPControl **ctrlsp = NULL, *ctrls[2], *dctrl = NULL; /* derefence control */
  3144. /* the following variables are used for response parsing */
  3145. int msgtype, parse_rc , rc ; /* for search result parsing */
  3146. char *matcheddn, *errmsg, *dn ;
  3147. LDAPControl **resctrls;
  3148. LDAPControl **rcp;
  3149. /*
  3150. * Connection to the server
  3151. * The function connectToServer() will take care of the various connection/
  3152. * disconnection, bind/unbind/close etc... requested by the user.
  3153. * The cost is one more function call in this application, but the
  3154. * resulting source code will be much more easiest to maintain.
  3155. */
  3156. if (connectToServer (tttctx) < 0) /*JLS 18-12-00*/
  3157. return (-1); /*JLS 18-12-00*/
  3158. if (!(tttctx->binded)) /*JLS 18-12-00*/
  3159. return (0); /*JLS 18-12-00*/
  3160. /*
  3161. * Build the filter
  3162. */
  3163. if (buildRandomRdnOrFilter (tttctx) < 0)
  3164. return (-1);
  3165. /*
  3166. * Prepear the attribute list
  3167. */
  3168. if (mctx.attrlistNb == 0) /*JLS 15-03-01*/
  3169. attrlist = NULL; /*JLS 15-03-01*/
  3170. else /*JLS 15-03-01*/
  3171. if (mctx.mode & RANDOM_ATTRLIST) /*JLS 15-03-01*/
  3172. attrlist = selectRandomAttrList (tttctx); /*JLS 15-03-01*/
  3173. else /*JLS 15-03-01*/
  3174. attrlist = mctx.attrlist; /*JLS 15-03-01*/
  3175. if (mctx.mod2 & M2_DEREF) /* dereference */
  3176. {
  3177. char *attrs[2];
  3178. /* I have stored ref attr at mctx.attRef , deref attr at mctx.attRefDef */
  3179. /* using hard coded value for dereferenced attribute if no mctx.attRef is set */
  3180. if (mctx.attRef == NULL )
  3181. {
  3182. attrs[0] = "cn";
  3183. }else{
  3184. attrs[0] = mctx.attRefDef;
  3185. }
  3186. attrs[1] = NULL;
  3187. /* use pre-defined value if passin mctx.attrRefDef is null
  3188. * the pre-defined value LDCLT_DEREF_ATTR is "secretary"
  3189. */
  3190. if (mctx.attRef == NULL ){
  3191. ret = ldclt_create_deref_control(tttctx->ldapCtx,
  3192. LDCLT_DEREF_ATTR, attrs, &dctrl);
  3193. }else{
  3194. ret = ldclt_create_deref_control(tttctx->ldapCtx,
  3195. mctx.attRef , attrs, &dctrl);
  3196. }
  3197. /* dctrl contains the returned reference value */
  3198. if (LDAP_SUCCESS == ret)
  3199. {
  3200. ctrls[0] = dctrl;
  3201. ctrls[1] = NULL;
  3202. ctrlsp = ctrls;
  3203. }
  3204. else
  3205. {
  3206. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3207. fprintf (stderr,
  3208. "ldclt[%d]: T%03d: ldclt_create_deref_control() failed, error=%d\n",
  3209. mctx.pid, tttctx->thrdNum, ret);
  3210. if (dctrl) {
  3211. ldap_control_free(dctrl);
  3212. }
  3213. if (addErrorStat(ret) < 0)
  3214. return (-1);
  3215. }
  3216. }
  3217. /*
  3218. * Do the search
  3219. * Maybe we are in synchronous mode ? I hope so, it is really
  3220. * much simple ;-)
  3221. */
  3222. if (!(mctx.mode & ASYNC))
  3223. {
  3224. ret = ldap_search_ext_s (tttctx->ldapCtx, tttctx->bufBaseDN, mctx.scope,
  3225. tttctx->bufFilter, attrlist, /*JLS 15-03-01*/
  3226. mctx.attrsonly, ctrlsp, NULL, NULL, -1, &res); /*JLS 03-01-01*/
  3227. if (ret != LDAP_SUCCESS)
  3228. { /* if search failed */
  3229. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3230. (void) printErrorFromLdap (tttctx, res, ret, /*JLS 03-08-00*/
  3231. "Cannot ldap_search()"); /*JLS 03-08-00*/
  3232. if (addErrorStat (ret) < 0)
  3233. {
  3234. goto bail;
  3235. }
  3236. if ((ret == LDAP_NO_SUCH_OBJECT) && /*JLS 15-12-00*/
  3237. (mctx.mode & COUNT_EACH)) /*JLS 15-12-00*/
  3238. { /*JLS 15-12-00*/
  3239. if (incrementNbOpers (tttctx) < 0) /*JLS 15-12-00*/
  3240. {
  3241. goto bail;
  3242. }
  3243. } /*JLS 15-12-00*/
  3244. }
  3245. else
  3246. { /* if search success & we are in verbose mode */
  3247. int nentries = 0;
  3248. if (mctx.mode & VERBOSE)
  3249. {
  3250. for (e = ldap_first_message (tttctx->ldapCtx, res); e != NULL ; e = ldap_next_message (tttctx->ldapCtx, e) )
  3251. {
  3252. msgtype = ldap_msgtype (e);
  3253. switch (msgtype)
  3254. {
  3255. /* if this is an entry that returned */
  3256. case LDAP_RES_SEARCH_ENTRY:
  3257. nentries++;
  3258. /* get dereferenced value into resctrls: deref parsing */
  3259. parse_rc = ldap_get_entry_controls ( tttctx->ldapCtx, e, &resctrls );
  3260. if ( resctrls != NULL )
  3261. {/* parse it only when we have return saved in server control */
  3262. /* get dn */
  3263. if (( dn = ldap_get_dn (tttctx->ldapCtx, e)) != NULL )
  3264. {
  3265. for ( rcp = resctrls; rcp && *rcp; rcp++ )
  3266. {
  3267. /* if very_verbose */
  3268. if ( mctx.mode & VERY_VERBOSE )
  3269. {
  3270. printf("dn: [%s] -> deref oid: %s, value: %s\n",
  3271. dn,
  3272. (**rcp).ldctl_oid?(**rcp).ldctl_oid:"none",
  3273. (**rcp).ldctl_value.bv_val?(**rcp).ldctl_value.bv_val:"none");
  3274. }
  3275. }
  3276. ldap_controls_free( resctrls );
  3277. ldap_memfree (dn);
  3278. }
  3279. }
  3280. break; /*end of case LDAP_RES_SEARCH_ENTRY */
  3281. /* if this is an reference that returned */
  3282. case LDAP_RES_SEARCH_REFERENCE: /* we do nothing here */
  3283. break;
  3284. /* if we reach the end of search result */
  3285. case LDAP_RES_SEARCH_RESULT:
  3286. /* just free the returned msg */
  3287. parse_rc = ldap_parse_result (tttctx->ldapCtx, e, &rc, &matcheddn, &errmsg, NULL , NULL , 0);
  3288. if ( parse_rc != LDAP_SUCCESS )
  3289. {
  3290. printf("ldap_parse_result error: [%s]\n", ldap_err2string( parse_rc ));
  3291. }
  3292. if ( rc != LDAP_SUCCESS )
  3293. {
  3294. printf ("ldap_search_ext_s error: [%s]\n", ldap_err2string( rc ));
  3295. }
  3296. break; /* end of case LDAP_RES_SEARCH_RESULT */
  3297. default:
  3298. break;
  3299. }
  3300. } /*end of message retriving */
  3301. } /* end of verbose mode */
  3302. if ((mctx.srch_nentries > -1) && (mctx.srch_nentries != nentries)) {
  3303. printf ("Error: search returned %d entries not the requested %d entries\n", nentries, mctx.srch_nentries);
  3304. goto bail;
  3305. }
  3306. if (incrementNbOpers (tttctx) < 0)/* Memorize operation */
  3307. {
  3308. goto bail;
  3309. }
  3310. /*
  3311. * Don't forget to free the returned message !
  3312. */
  3313. if ((ret = ldap_msgfree (res)) < 0)
  3314. {
  3315. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3316. {
  3317. printf ("ldclt[%d]: T%03d: Cannot ldap_msgfree(), error=%d (%s)\n",
  3318. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  3319. fflush (stdout);
  3320. }
  3321. if (addErrorStat (ret) < 0)
  3322. {
  3323. goto bail;
  3324. }
  3325. }
  3326. }
  3327. /*
  3328. * End of synchronous operation
  3329. */
  3330. if (dctrl)
  3331. {
  3332. ldap_control_free(dctrl);
  3333. }
  3334. return (0);
  3335. }
  3336. /*
  3337. * Here, we are in asynchronous mode...
  3338. * Too bad, lot of things to do here.
  3339. * First, let's see if we are above the reading threshold.
  3340. */
  3341. if (tttctx->pendingNb >= mctx.asyncMin)
  3342. {
  3343. /*
  3344. * Retrieve the next pending request
  3345. */
  3346. ret = ldap_result (tttctx->ldapCtx, LDAP_RES_ANY, 1, &(mctx.timeval), &res);
  3347. if (ret < 0)
  3348. {
  3349. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3350. (void) printErrorFromLdap (tttctx, res, ret, /*JLS 03-08-00*/
  3351. "Cannot ldap_result()"); /*JLS 03-08-00*/
  3352. if (addErrorStat (ret) < 0)
  3353. {
  3354. goto bail;
  3355. }
  3356. }
  3357. else
  3358. {
  3359. tttctx->pendingNb--;
  3360. /*
  3361. * Don't forget to free the returned message !
  3362. */
  3363. if ((ret = ldap_msgfree (res)) < 0)
  3364. {
  3365. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3366. {
  3367. printf ("ldclt[%d]: T%03d: Cannot ldap_msgfree(), error=%d (%s)\n",
  3368. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  3369. fflush (stdout);
  3370. }
  3371. if (addErrorStat (ret) < 0)
  3372. {
  3373. goto bail;
  3374. }
  3375. }
  3376. }
  3377. }
  3378. /*
  3379. * Maybe we may send another request ?
  3380. * Well... there is no proper way to retrieve the error number for
  3381. * this, so I guess I may use direct access to the ldap context
  3382. * to read the field ld_errno.
  3383. */
  3384. if (tttctx->pendingNb > mctx.asyncMax)
  3385. {
  3386. if ((mctx.mode & VERBOSE) &&
  3387. (tttctx->asyncHit == 1) &&
  3388. (!(mctx.mode & SUPER_QUIET)))
  3389. {
  3390. tttctx->asyncHit = 1;
  3391. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  3392. mctx.pid, tttctx->thrdNum);
  3393. fflush (stdout);
  3394. }
  3395. }
  3396. else
  3397. {
  3398. int msgid = 0;
  3399. if ((mctx.mode & VERBOSE) &&
  3400. (tttctx->asyncHit == 1) &&
  3401. (!(mctx.mode & SUPER_QUIET)))
  3402. {
  3403. tttctx->asyncHit = 0;
  3404. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  3405. mctx.pid, tttctx->thrdNum);
  3406. fflush (stdout);
  3407. }
  3408. ret = ldap_search_ext (tttctx->ldapCtx, tttctx->bufBaseDN, mctx.scope,
  3409. tttctx->bufFilter, attrlist, /*JLS 15-03-01*/
  3410. mctx.attrsonly, ctrlsp, NULL, NULL, -1, &msgid); /*JLS 03-01-01*/
  3411. if (ret < 0)
  3412. {
  3413. if (ldap_get_option (tttctx->ldapCtx, LDAP_OPT_ERROR_NUMBER, &ret) < 0)
  3414. {
  3415. printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
  3416. mctx.pid, tttctx->thrdNum);
  3417. fflush (stdout);
  3418. goto bail;
  3419. }
  3420. else
  3421. {
  3422. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3423. {
  3424. printf ("ldclt[%d]: T%03d: Cannot ldap_search(), error=%d (%s)\n",
  3425. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  3426. fflush (stdout);
  3427. }
  3428. if (addErrorStat (ret) < 0)
  3429. return (-1);
  3430. if ((ret == LDAP_NO_SUCH_OBJECT) && /*JLS 15-12-00*/
  3431. (mctx.mode & COUNT_EACH)) /*JLS 15-12-00*/
  3432. { /*JLS 15-12-00*/
  3433. if (incrementNbOpers (tttctx) < 0) { /*JLS 15-12-00*/
  3434. goto bail;
  3435. }
  3436. } /*JLS 15-12-00*/
  3437. }
  3438. }
  3439. else
  3440. {
  3441. /*
  3442. * Memorize the operation
  3443. */
  3444. if (incrementNbOpers (tttctx) < 0) {
  3445. goto bail;
  3446. }
  3447. tttctx->pendingNb++;
  3448. }
  3449. }
  3450. if (mctx.mode & VERY_VERBOSE)
  3451. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  3452. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  3453. /*
  3454. * End of asynchronous operation... and also end of function.
  3455. */
  3456. if (dctrl)
  3457. {
  3458. ldap_control_free(dctrl);
  3459. }
  3460. return (0);
  3461. bail:
  3462. if (dctrl)
  3463. {
  3464. ldap_control_free(dctrl);
  3465. }
  3466. return (-1);
  3467. }
  3468. /* ****************************************************************************
  3469. FUNCTION : doAbandon
  3470. PURPOSE : Perform one abandon operation against an async search.
  3471. INPUT : tttctx = thread context
  3472. OUTPUT : None.
  3473. RETURN : -1 if error, 0 else.
  3474. DESCRIPTION :
  3475. *****************************************************************************/
  3476. int
  3477. doAbandon (thread_context *tttctx)
  3478. {
  3479. int ret; /* Return value */
  3480. LDAPMessage *res; /* LDAP results */
  3481. char **attrlist; /* Attribs list */
  3482. struct timeval mytimeout;
  3483. int msgid;
  3484. /*
  3485. * Connection to the server
  3486. * The function connectToServer() will take care of the various connection/
  3487. * disconnection, bind/unbind/close etc... requested by the user.
  3488. * The cost is one more function call in this application, but the
  3489. * resulting source code will be much more easiest to maintain.
  3490. */
  3491. if (connectToServer (tttctx) < 0)
  3492. return (-1);
  3493. if (!(tttctx->binded))
  3494. return (0);
  3495. /*
  3496. * Build the filter
  3497. */
  3498. if (buildRandomRdnOrFilter (tttctx) < 0)
  3499. return (-1);
  3500. attrlist = NULL;
  3501. /*
  3502. * We use asynchronous search to abandon...
  3503. *
  3504. * set (1, 2) to (acyncMin, acyncMax), which combination does not stop write.
  3505. */
  3506. mctx.asyncMin = 1;
  3507. mctx.asyncMax = 2;
  3508. if (tttctx->pendingNb >= mctx.asyncMin)
  3509. {
  3510. mytimeout.tv_sec = 1;
  3511. mytimeout.tv_usec = 0;
  3512. ret = ldap_result (tttctx->ldapCtx,
  3513. LDAP_RES_ANY, LDAP_MSG_ONE, &mytimeout, &res);
  3514. if (ret < 0)
  3515. {
  3516. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3517. (void) printErrorFromLdap (tttctx, res, ret, "Cannot ldap_result()");
  3518. if (addErrorStat (ret) < 0)
  3519. return (-1);
  3520. }
  3521. else
  3522. {
  3523. /* ret == 0 --> timeout; op abandoned and no result is returned */
  3524. tttctx->pendingNb--;
  3525. /*
  3526. * Don't forget to free the returned message !
  3527. */
  3528. if ((ret = ldap_msgfree (res)) < 0)
  3529. {
  3530. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3531. {
  3532. printf ("ldclt[%d]: T%03d: Cannot ldap_msgfree(), error=%d (%s)\n",
  3533. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  3534. fflush (stdout);
  3535. }
  3536. if (addErrorStat (ret) < 0)
  3537. return (-1);
  3538. }
  3539. }
  3540. }
  3541. /*
  3542. * Maybe we may send another request ?
  3543. * Well... there is no proper way to retrieve the error number for
  3544. * this, so I guess I may use direct access to the ldap context
  3545. * to read the field ld_errno.
  3546. */
  3547. if (tttctx->pendingNb > mctx.asyncMax)
  3548. {
  3549. if ((mctx.mode & VERBOSE) &&
  3550. (tttctx->asyncHit == 1) &&
  3551. (!(mctx.mode & SUPER_QUIET)))
  3552. {
  3553. tttctx->asyncHit = 1;
  3554. printf ("ldclt[%d]: T%03d: Max pending request hit.\n",
  3555. mctx.pid, tttctx->thrdNum);
  3556. fflush (stdout);
  3557. }
  3558. }
  3559. else
  3560. {
  3561. if ((mctx.mode & VERBOSE) &&
  3562. (tttctx->asyncHit == 1) &&
  3563. (!(mctx.mode & SUPER_QUIET)))
  3564. {
  3565. tttctx->asyncHit = 0;
  3566. printf ("ldclt[%d]: T%03d: Restart sending.\n",
  3567. mctx.pid, tttctx->thrdNum);
  3568. fflush (stdout);
  3569. }
  3570. msgid = -1;
  3571. /* for some reasons, it is an error to pass in a zero'd timeval */
  3572. mytimeout.tv_sec = mytimeout.tv_usec = -1;
  3573. ret = ldap_search_ext (tttctx->ldapCtx, tttctx->bufBaseDN, mctx.scope,
  3574. tttctx->bufFilter, attrlist, mctx.attrsonly,
  3575. NULL, NULL, &mytimeout, -1, &msgid);
  3576. if (mctx.mode & VERY_VERBOSE)
  3577. printf ("ldclt[%d]: T%03d: ldap_search(%s)=>%d\n",
  3578. mctx.pid, tttctx->thrdNum, tttctx->bufFilter, ret);
  3579. if (ret != 0)
  3580. {
  3581. if (ldap_get_option (tttctx->ldapCtx, LDAP_OPT_ERROR_NUMBER, &ret) < 0)
  3582. {
  3583. printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
  3584. mctx.pid, tttctx->thrdNum);
  3585. fflush (stdout);
  3586. return (-1);
  3587. }
  3588. else
  3589. {
  3590. if (!((mctx.mode & QUIET) && ignoreError (ret)))
  3591. {
  3592. printf ("ldclt[%d]: T%03d: Cannot ldap_search(), error=%d (%s)\n",
  3593. mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
  3594. fflush (stdout);
  3595. }
  3596. if (addErrorStat (ret) < 0)
  3597. return (-1);
  3598. }
  3599. }
  3600. else
  3601. {
  3602. if (msgid >= 0)
  3603. {
  3604. /* ABANDON the search request immediately */
  3605. (void) ldap_abandon_ext(tttctx->ldapCtx, msgid, NULL, NULL);
  3606. }
  3607. /*
  3608. * Memorize the operation
  3609. */
  3610. if (incrementNbOpers (tttctx) < 0)
  3611. return (-1);
  3612. tttctx->pendingNb++;
  3613. if (mctx.mode & VERY_VERBOSE)
  3614. printf ("ldclt[%d]: T%03d: ldap_abandon(%d)\n",
  3615. mctx.pid, tttctx->thrdNum, msgid);
  3616. }
  3617. }
  3618. if (mctx.mode & VERY_VERBOSE)
  3619. printf ("ldclt[%d]: T%03d: pendingNb=%d\n",
  3620. mctx.pid, tttctx->thrdNum, tttctx->pendingNb);
  3621. /*
  3622. * End of asynchronous operation... and also end of function.
  3623. */
  3624. return (0);
  3625. }
  3626. #define LDAP_CONTROL_X_DEREF "1.3.6.1.4.1.4203.666.5.16"
  3627. int
  3628. ldclt_create_deref_control(
  3629. LDAP *ld,
  3630. char *derefAttr,
  3631. char **attrs,
  3632. LDAPControl **ctrlp
  3633. )
  3634. {
  3635. BerElement *ber;
  3636. int rc;
  3637. #if defined(USE_OPENLDAP)
  3638. struct berval *bv = NULL;
  3639. #endif
  3640. if (ld == 0) {
  3641. return( LDAP_PARAM_ERROR );
  3642. }
  3643. if ( NULL == ctrlp || NULL == derefAttr ||
  3644. NULL == attrs || NULL == *attrs || 0 == strlen(*attrs) ) {
  3645. return ( LDAP_PARAM_ERROR );
  3646. }
  3647. if ( LDAP_SUCCESS != ldclt_alloc_ber( ld, &ber ) )
  3648. {
  3649. return( LDAP_NO_MEMORY );
  3650. }
  3651. if ( LBER_ERROR == ber_printf( ber, "{{s{v}}}", derefAttr, attrs ))
  3652. {
  3653. ber_free( ber, 1 );
  3654. return( LDAP_ENCODING_ERROR );
  3655. }
  3656. #if defined(USE_OPENLDAP)
  3657. if ( LBER_ERROR == ber_flatten(ber, &bv) ) {
  3658. ber_bvfree( bv );
  3659. ber_free( ber, 1 );
  3660. return( LDAP_ENCODING_ERROR );
  3661. }
  3662. if ( NULL == bv ) {
  3663. ber_free( ber, 1 );
  3664. return( LDAP_NO_MEMORY );
  3665. }
  3666. rc = ldap_control_create( LDAP_CONTROL_X_DEREF, 1, bv, 1, ctrlp );
  3667. ber_bvfree( bv );
  3668. ber_free( ber, 1 );
  3669. #else
  3670. rc = ldclt_build_control( LDAP_CONTROL_X_DEREF, ber, 1, 1, ctrlp );
  3671. ber_free( ber, 1 );
  3672. #endif
  3673. return( rc );
  3674. }
  3675. #if !defined(USE_OPENLDAP)
  3676. /*
  3677. * Duplicated nsldapi_build_control from
  3678. * mozilla/directory/c-sdk/ldap/libraries/libldap/control.c
  3679. *
  3680. * build an allocated LDAPv3 control. Returns an LDAP error code.
  3681. */
  3682. int
  3683. ldclt_build_control( char *oid, BerElement *ber, int freeber, char iscritical,
  3684. LDAPControl **ctrlp )
  3685. {
  3686. int rc;
  3687. struct berval *bvp;
  3688. if ( ber == NULL ) {
  3689. bvp = NULL;
  3690. } else {
  3691. /* allocate struct berval with contents of the BER encoding */
  3692. rc = ber_flatten( ber, &bvp );
  3693. if ( freeber ) {
  3694. ber_free( ber, 1 );
  3695. }
  3696. if ( rc == -1 ) {
  3697. return( LDAP_NO_MEMORY );
  3698. }
  3699. }
  3700. /* allocate the new control structure */
  3701. if (( *ctrlp = (LDAPControl *)malloc( sizeof(LDAPControl))) == NULL ) {
  3702. if ( bvp != NULL ) {
  3703. ber_bvfree( bvp );
  3704. }
  3705. return( LDAP_NO_MEMORY );
  3706. }
  3707. /* fill in the fields of this new control */
  3708. (*ctrlp)->ldctl_iscritical = iscritical;
  3709. if (( (*ctrlp)->ldctl_oid = strdup( oid )) == NULL ) {
  3710. free( *ctrlp );
  3711. if ( bvp != NULL ) {
  3712. ber_bvfree( bvp );
  3713. }
  3714. return( LDAP_NO_MEMORY );
  3715. }
  3716. if ( bvp == NULL ) {
  3717. (*ctrlp)->ldctl_value.bv_len = 0;
  3718. (*ctrlp)->ldctl_value.bv_val = NULL;
  3719. } else {
  3720. (*ctrlp)->ldctl_value = *bvp; /* struct copy */
  3721. free( bvp ); /* free container, not contents! */
  3722. }
  3723. return( LDAP_SUCCESS );
  3724. }
  3725. #endif
  3726. /*
  3727. * Duplicated nsldapi_build_control from
  3728. * mozilla/directory/c-sdk/ldap/libraries/libldap/request.c
  3729. *
  3730. * returns an LDAP error code and also sets error inside LDAP
  3731. */
  3732. int
  3733. ldclt_alloc_ber( LDAP *ld, BerElement **berp )
  3734. {
  3735. int err;
  3736. int beropt;
  3737. #if defined(USE_OPENLDAP)
  3738. beropt = LBER_USE_DER;
  3739. #else
  3740. beropt = LBER_OPT_USE_DER;
  3741. #endif
  3742. /* We use default lberoptions since the value is not public in mozldap. */
  3743. if (( *berp = ber_alloc_t( beropt )) == (BerElement *)NULL ) {
  3744. err = LDAP_NO_MEMORY;
  3745. } else {
  3746. err = LDAP_SUCCESS;
  3747. }
  3748. return( err );
  3749. }
  3750. /* End of file */