ldapfct.c 113 KB

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