ldapfct.c 127 KB

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