ldapfct.c 126 KB

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