ldapfct.c 128 KB

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