url.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id$
  22. ***************************************************************************/
  23. /* -- WIN32 approved -- */
  24. #include "setup.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  34. #include <winsock.h>
  35. #include <time.h>
  36. #include <io.h>
  37. #else
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #include <netinet/in.h>
  42. #include <sys/time.h>
  43. #include <sys/resource.h>
  44. #ifdef HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #include <netdb.h>
  48. #ifdef HAVE_ARPA_INET_H
  49. #include <arpa/inet.h>
  50. #endif
  51. #ifdef HAVE_NET_IF_H
  52. #include <net/if.h>
  53. #endif
  54. #include <sys/ioctl.h>
  55. #include <signal.h>
  56. #ifdef HAVE_SYS_PARAM_H
  57. #include <sys/param.h>
  58. #endif
  59. #ifdef HAVE_SYS_SELECT_H
  60. #include <sys/select.h>
  61. #endif
  62. #ifdef VMS
  63. #include <in.h>
  64. #include <inet.h>
  65. #endif
  66. #ifdef HAVE_SETJMP_H
  67. #include <setjmp.h>
  68. #endif
  69. #ifndef HAVE_SELECT
  70. #error "We can't compile without select() support!"
  71. #endif
  72. #ifndef HAVE_SOCKET
  73. #error "We can't compile without socket() support!"
  74. #endif
  75. #endif
  76. #include "urldata.h"
  77. #include "netrc.h"
  78. #include "formdata.h"
  79. #include "base64.h"
  80. #include "ssluse.h"
  81. #include "hostip.h"
  82. #include "if2ip.h"
  83. #include "transfer.h"
  84. #include "sendf.h"
  85. #include "getpass.h"
  86. #include "progress.h"
  87. #include "cookie.h"
  88. #include "strequal.h"
  89. #include "escape.h"
  90. #include "strtok.h"
  91. #include "share.h"
  92. /* And now for the protocols */
  93. #include "ftp.h"
  94. #include "dict.h"
  95. #include "telnet.h"
  96. #include "http.h"
  97. #include "file.h"
  98. #include "ldap.h"
  99. #include "url.h"
  100. #include "connect.h"
  101. #include "ca-bundle.h"
  102. #include <curl/types.h>
  103. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  104. #include "inet_ntoa_r.h"
  105. #endif
  106. #define _MPRINTF_REPLACE /* use our functions only */
  107. #include <curl/mprintf.h>
  108. #ifdef KRB4
  109. #include "security.h"
  110. #endif
  111. /* The last #include file should be: */
  112. #ifdef MALLOCDEBUG
  113. #include "memdebug.h"
  114. #endif
  115. /* Local static prototypes */
  116. static int ConnectionKillOne(struct SessionHandle *data);
  117. static bool ConnectionExists(struct SessionHandle *data,
  118. struct connectdata *needle,
  119. struct connectdata **usethis);
  120. static unsigned int ConnectionStore(struct SessionHandle *data,
  121. struct connectdata *conn);
  122. #if !defined(WIN32)||defined(__CYGWIN32__)
  123. #ifndef RETSIGTYPE
  124. #define RETSIGTYPE void
  125. #endif
  126. #ifdef HAVE_SIGSETJMP
  127. extern sigjmp_buf curl_jmpenv;
  128. #endif
  129. static
  130. RETSIGTYPE alarmfunc(int signal)
  131. {
  132. /* this is for "-ansi -Wall -pedantic" to stop complaining! (rabe) */
  133. (void)signal;
  134. #ifdef HAVE_SIGSETJMP
  135. siglongjmp(curl_jmpenv, 1);
  136. #endif
  137. return;
  138. }
  139. #endif
  140. /*
  141. * This is the internal function curl_easy_cleanup() calls. This should
  142. * cleanup and free all resources associated with this sessionhandle.
  143. *
  144. * NOTE: if we ever add something that attempts to write to a socket or
  145. * similar here, we must ignore SIGPIPE first. It is currently only done
  146. * when curl_easy_perform() is invoked.
  147. */
  148. CURLcode Curl_close(struct SessionHandle *data)
  149. {
  150. /* Loop through all open connections and kill them one by one */
  151. while(-1 != ConnectionKillOne(data));
  152. #ifdef USE_SSLEAY
  153. /* Close down all open SSL info and sessions */
  154. Curl_SSL_Close_All(data);
  155. #endif
  156. /* No longer a dirty share, if it exists */
  157. if (data->share)
  158. data->share->dirty--;
  159. if(data->change.cookielist) /* clean up list if any */
  160. curl_slist_free_all(data->change.cookielist);
  161. if(data->state.auth_host)
  162. free(data->state.auth_host);
  163. if(data->change.proxy_alloc)
  164. free(data->change.proxy);
  165. if(data->change.referer_alloc)
  166. free(data->change.referer);
  167. if(data->change.url_alloc)
  168. free(data->change.url);
  169. if(data->state.headerbuff)
  170. free(data->state.headerbuff);
  171. #ifndef CURL_DISABLE_HTTP
  172. if(data->set.cookiejar)
  173. /* we have a "destination" for all the cookies to get dumped to */
  174. Curl_cookie_output(data->cookies, data->set.cookiejar);
  175. Curl_cookie_cleanup(data->cookies);
  176. #endif
  177. /* free the connection cache */
  178. free(data->state.connects);
  179. if(data->info.contenttype)
  180. free(data->info.contenttype);
  181. free(data);
  182. return CURLE_OK;
  183. }
  184. static
  185. int my_getpass(void *clientp, const char *prompt, char* buffer, int buflen )
  186. {
  187. char *retbuf;
  188. clientp=NULL; /* prevent compiler warning */
  189. retbuf = getpass_r(prompt, buffer, buflen);
  190. if(NULL == retbuf)
  191. return 1;
  192. else
  193. return 0; /* success */
  194. }
  195. CURLcode Curl_open(struct SessionHandle **curl)
  196. {
  197. /* We don't yet support specifying the URL at this point */
  198. struct SessionHandle *data;
  199. /* Very simple start-up: alloc the struct, init it with zeroes and return */
  200. data = (struct SessionHandle *)malloc(sizeof(struct SessionHandle));
  201. if(!data)
  202. /* this is a very serious error */
  203. return CURLE_OUT_OF_MEMORY;
  204. memset(data, 0, sizeof(struct SessionHandle));
  205. /* We do some initial setup here, all those fields that can't be just 0 */
  206. data->state.headerbuff=(char*)malloc(HEADERSIZE);
  207. if(!data->state.headerbuff) {
  208. free(data); /* free the memory again */
  209. return CURLE_OUT_OF_MEMORY;
  210. }
  211. data->state.headersize=HEADERSIZE;
  212. data->set.out = stdout; /* default output to stdout */
  213. data->set.in = stdin; /* default input from stdin */
  214. data->set.err = stderr; /* default stderr to stderr */
  215. /* use fwrite as default function to store output */
  216. data->set.fwrite = (curl_write_callback)fwrite;
  217. /* use fread as default function to read input */
  218. data->set.fread = (curl_read_callback)fread;
  219. /* set the default passwd function */
  220. data->set.fpasswd = my_getpass;
  221. data->set.infilesize = -1; /* we don't know any size */
  222. data->state.current_speed = -1; /* init to negative == impossible */
  223. data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
  224. data->set.ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  225. data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  226. /* make libcurl quiet by default: */
  227. data->set.hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  228. data->progress.flags |= PGRS_HIDE;
  229. /* Set the default size of the SSL session ID cache */
  230. data->set.ssl.numsessions = 5;
  231. data->set.proxyport = 1080;
  232. data->set.proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  233. /* create an array with connection data struct pointers */
  234. data->state.numconnects = 5; /* hard-coded right now */
  235. data->state.connects = (struct connectdata **)
  236. malloc(sizeof(struct connectdata *) * data->state.numconnects);
  237. if(!data->state.connects) {
  238. free(data);
  239. return CURLE_OUT_OF_MEMORY;
  240. }
  241. /*
  242. * libcurl 7.10 introduces SSL verification *by default*! This needs to be
  243. * switched off unless wanted.
  244. */
  245. data->set.ssl.verifypeer = TRUE;
  246. data->set.ssl.verifyhost = 2;
  247. #ifdef CURL_CA_BUNDLE
  248. /* This is our prefered CA cert bundle since install time */
  249. data->set.ssl.CAfile = (char *)CURL_CA_BUNDLE;
  250. #endif
  251. memset(data->state.connects, 0,
  252. sizeof(struct connectdata *)*data->state.numconnects);
  253. *curl = data;
  254. return CURLE_OK;
  255. }
  256. CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option, ...)
  257. {
  258. va_list param;
  259. char *cookiefile;
  260. va_start(param, option);
  261. switch(option) {
  262. case CURLOPT_DNS_CACHE_TIMEOUT:
  263. data->set.dns_cache_timeout = va_arg(param, int);
  264. break;
  265. case CURLOPT_DNS_USE_GLOBAL_CACHE:
  266. {
  267. int use_cache = va_arg(param, int);
  268. if (use_cache) {
  269. Curl_global_host_cache_init();
  270. }
  271. data->set.global_dns_cache = (bool)use_cache;
  272. }
  273. break;
  274. case CURLOPT_SSL_CIPHER_LIST:
  275. /* set a list of cipher we want to use in the SSL connection */
  276. data->set.ssl.cipher_list = va_arg(param, char *);
  277. break;
  278. case CURLOPT_RANDOM_FILE:
  279. /*
  280. * This is the path name to a file that contains random data to seed
  281. * the random SSL stuff with. The file is only used for reading.
  282. */
  283. data->set.ssl.random_file = va_arg(param, char *);
  284. break;
  285. case CURLOPT_EGDSOCKET:
  286. /*
  287. * The Entropy Gathering Daemon socket pathname
  288. */
  289. data->set.ssl.egdsocket = va_arg(param, char *);
  290. break;
  291. case CURLOPT_MAXCONNECTS:
  292. /*
  293. * Set the absolute number of maximum simultaneous alive connection that
  294. * libcurl is allowed to have.
  295. */
  296. {
  297. long newconnects= va_arg(param, long);
  298. struct connectdata **newptr;
  299. if(newconnects < data->state.numconnects) {
  300. /* Since this number is *decreased* from the existing number, we must
  301. close the possibly open connections that live on the indexes that
  302. are being removed! */
  303. int i;
  304. for(i=newconnects; i< data->state.numconnects; i++)
  305. Curl_disconnect(data->state.connects[i]);
  306. }
  307. if(newconnects) {
  308. newptr= (struct connectdata **)
  309. realloc(data->state.connects,
  310. sizeof(struct connectdata *) * newconnects);
  311. if(!newptr)
  312. /* we closed a few connections in vain, but so what? */
  313. return CURLE_OUT_OF_MEMORY;
  314. data->state.connects = newptr;
  315. data->state.numconnects = newconnects;
  316. }
  317. else {
  318. /* zero makes NO cache at all */
  319. if(data->state.connects)
  320. free(data->state.connects);
  321. data->state.connects=NULL;
  322. data->state.numconnects=0;
  323. }
  324. }
  325. break;
  326. case CURLOPT_FORBID_REUSE:
  327. /*
  328. * When this transfer is done, it must not be left to be reused by a
  329. * subsequent transfer but shall be closed immediately.
  330. */
  331. data->set.reuse_forbid = (bool)(va_arg(param, long)?TRUE:FALSE);
  332. break;
  333. case CURLOPT_FRESH_CONNECT:
  334. /*
  335. * This transfer shall not use a previously cached connection but
  336. * should be made with a fresh new connect!
  337. */
  338. data->set.reuse_fresh = (bool)(va_arg(param, long)?TRUE:FALSE);
  339. break;
  340. case CURLOPT_VERBOSE:
  341. /*
  342. * Verbose means infof() calls that give a lot of information about
  343. * the connection and transfer procedures as well as internal choices.
  344. */
  345. data->set.verbose = (bool)(va_arg(param, long)?TRUE:FALSE);
  346. break;
  347. case CURLOPT_HEADER:
  348. /*
  349. * Set to include the header in the general data output stream.
  350. */
  351. data->set.http_include_header = (bool)(va_arg(param, long)?TRUE:FALSE);
  352. break;
  353. case CURLOPT_NOPROGRESS:
  354. /*
  355. * Shut off the internal supported progress meter
  356. */
  357. data->set.hide_progress = (bool)(va_arg(param, long)?TRUE:FALSE);
  358. if(data->set.hide_progress)
  359. data->progress.flags |= PGRS_HIDE;
  360. else
  361. data->progress.flags &= ~PGRS_HIDE;
  362. break;
  363. case CURLOPT_NOBODY:
  364. /*
  365. * Do not include the body part in the output data stream.
  366. */
  367. data->set.no_body = (bool)(va_arg(param, long)?TRUE:FALSE);
  368. break;
  369. case CURLOPT_FAILONERROR:
  370. /*
  371. * Don't output the >=300 error code HTML-page, but instead only
  372. * return error.
  373. */
  374. data->set.http_fail_on_error = (bool)(va_arg(param, long)?TRUE:FALSE);
  375. break;
  376. case CURLOPT_UPLOAD:
  377. /*
  378. * We want to sent data to the remote host
  379. */
  380. data->set.upload = (bool)(va_arg(param, long)?TRUE:FALSE);
  381. if(data->set.upload)
  382. /* If this is HTTP, PUT is what's needed to "upload" */
  383. data->set.httpreq = HTTPREQ_PUT;
  384. break;
  385. case CURLOPT_FILETIME:
  386. /*
  387. * Try to get the file time of the remote document. The time will
  388. * later (possibly) become available using curl_easy_getinfo().
  389. */
  390. data->set.get_filetime = (bool)(va_arg(param, long)?TRUE:FALSE);
  391. break;
  392. case CURLOPT_FTPLISTONLY:
  393. /*
  394. * An FTP option that changes the command to one that asks for a list
  395. * only, no file info details.
  396. */
  397. data->set.ftp_list_only = (bool)(va_arg(param, long)?TRUE:FALSE);
  398. break;
  399. case CURLOPT_FTPAPPEND:
  400. /*
  401. * We want to upload and append to an existing (FTP) file.
  402. */
  403. data->set.ftp_append = (bool)(va_arg(param, long)?TRUE:FALSE);
  404. break;
  405. case CURLOPT_NETRC:
  406. /*
  407. * Parse the $HOME/.netrc file
  408. */
  409. data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
  410. break;
  411. case CURLOPT_FOLLOWLOCATION:
  412. /*
  413. * Follow Location: header hints on a HTTP-server.
  414. */
  415. data->set.http_follow_location = (bool)(va_arg(param, long)?TRUE:FALSE);
  416. break;
  417. case CURLOPT_HTTP_VERSION:
  418. /*
  419. * This sets a requested HTTP version to be used. The value is one of
  420. * the listed enums in curl/curl.h.
  421. */
  422. data->set.httpversion = va_arg(param, long);
  423. break;
  424. case CURLOPT_TRANSFERTEXT:
  425. /*
  426. * This option was previously named 'FTPASCII'. Renamed to work with
  427. * more protocols than merely FTP.
  428. *
  429. * Transfer using ASCII (instead of BINARY).
  430. */
  431. data->set.ftp_ascii = (bool)(va_arg(param, long)?TRUE:FALSE);
  432. break;
  433. case CURLOPT_PUT:
  434. /*
  435. * Use the HTTP PUT request to transfer data if this is TRUE. If this is
  436. * FALSE, don't set the httpreq. We can't know what to revert it to!
  437. */
  438. if(va_arg(param, long))
  439. data->set.httpreq = HTTPREQ_PUT;
  440. break;
  441. case CURLOPT_TIMECONDITION:
  442. /*
  443. * Set HTTP time condition. This must be one of the defines in the
  444. * curl/curl.h header file.
  445. */
  446. data->set.timecondition = (curl_TimeCond)va_arg(param, long);
  447. break;
  448. case CURLOPT_TIMEVALUE:
  449. /*
  450. * This is the value to compare with the remote document with the
  451. * method set with CURLOPT_TIMECONDITION
  452. */
  453. data->set.timevalue = va_arg(param, long);
  454. break;
  455. case CURLOPT_SSLVERSION:
  456. /*
  457. * Set explicit SSL version to try to connect with, as some SSL
  458. * implementations are lame.
  459. */
  460. data->set.ssl.version = va_arg(param, long);
  461. break;
  462. case CURLOPT_COOKIESESSION:
  463. /*
  464. * Set this option to TRUE to start a new "cookie session". It will
  465. * prevent the forthcoming read-cookies-from-file actions to accept
  466. * cookies that are marked as being session cookies, as they belong to a
  467. * previous session.
  468. *
  469. * In the original Netscape cookie spec, "session cookies" are cookies
  470. * with no expire date set. RFC2109 describes the same action if no
  471. * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
  472. * a 'Discard' action that can enforce the discard even for cookies that
  473. * have a Max-Age.
  474. *
  475. * We run mostly with the original cookie spec, as hardly anyone implements
  476. * anything else.
  477. */
  478. data->set.cookiesession = (bool)va_arg(param, long);
  479. break;
  480. #ifndef CURL_DISABLE_HTTP
  481. case CURLOPT_COOKIEFILE:
  482. /*
  483. * Set cookie file to read and parse. Can be used multiple times.
  484. */
  485. cookiefile = (char *)va_arg(param, void *);
  486. if(cookiefile)
  487. /* append the cookie file name to the list of file names, and deal with
  488. them later */
  489. data->change.cookielist =
  490. curl_slist_append(data->change.cookielist, cookiefile);
  491. break;
  492. case CURLOPT_COOKIEJAR:
  493. /*
  494. * Set cookie file name to dump all cookies to when we're done.
  495. */
  496. data->set.cookiejar = (char *)va_arg(param, void *);
  497. /*
  498. * Activate the cookie parser. This may or may not already
  499. * have been made.
  500. */
  501. data->cookies = Curl_cookie_init(NULL, data->cookies,
  502. data->set.cookiesession);
  503. break;
  504. #endif
  505. case CURLOPT_WRITEHEADER:
  506. /*
  507. * Custom pointer to pass the header write callback function
  508. */
  509. data->set.writeheader = (void *)va_arg(param, void *);
  510. break;
  511. case CURLOPT_COOKIE:
  512. /*
  513. * Cookie string to send to the remote server in the request.
  514. */
  515. data->set.cookie = va_arg(param, char *);
  516. break;
  517. case CURLOPT_ERRORBUFFER:
  518. /*
  519. * Error buffer provided by the caller to get the human readable
  520. * error string in.
  521. */
  522. data->set.errorbuffer = va_arg(param, char *);
  523. break;
  524. case CURLOPT_FILE:
  525. /*
  526. * FILE pointer to write to or include in the data write callback
  527. */
  528. data->set.out = va_arg(param, FILE *);
  529. break;
  530. case CURLOPT_FTPPORT:
  531. /*
  532. * Use FTP PORT, this also specifies which IP address to use
  533. */
  534. data->set.ftpport = va_arg(param, char *);
  535. data->set.ftp_use_port = (bool)(data->set.ftpport?TRUE:FALSE);
  536. break;
  537. case CURLOPT_FTP_USE_EPSV:
  538. data->set.ftp_use_epsv = (bool)(va_arg(param, long)?TRUE:FALSE);
  539. break;
  540. case CURLOPT_HTTPHEADER:
  541. /*
  542. * Set a list with HTTP headers to use (or replace internals with)
  543. */
  544. data->set.headers = va_arg(param, struct curl_slist *);
  545. break;
  546. case CURLOPT_CUSTOMREQUEST:
  547. /*
  548. * Set a custom string to use as request
  549. */
  550. data->set.customrequest = va_arg(param, char *);
  551. /* we don't set
  552. data->set.httpreq = HTTPREQ_CUSTOM;
  553. here, we continue as if we were using the already set type
  554. and this just changes the actual request keyword */
  555. break;
  556. case CURLOPT_HTTPPOST:
  557. /*
  558. * Set to make us do HTTP POST
  559. */
  560. data->set.httppost = va_arg(param, struct HttpPost *);
  561. if(data->set.httppost)
  562. data->set.httpreq = HTTPREQ_POST_FORM;
  563. break;
  564. case CURLOPT_HTTPGET:
  565. /*
  566. * Set to force us do HTTP GET
  567. */
  568. if(va_arg(param, long)) {
  569. data->set.httpreq = HTTPREQ_GET;
  570. data->set.upload = FALSE; /* switch off upload */
  571. }
  572. break;
  573. case CURLOPT_INFILE:
  574. /*
  575. * FILE pointer to read the file to be uploaded from. Or possibly
  576. * used as argument to the read callback.
  577. */
  578. data->set.in = va_arg(param, FILE *);
  579. break;
  580. case CURLOPT_INFILESIZE:
  581. /*
  582. * If known, this should inform curl about the file size of the
  583. * to-be-uploaded file.
  584. */
  585. data->set.infilesize = va_arg(param, long);
  586. break;
  587. case CURLOPT_LOW_SPEED_LIMIT:
  588. /*
  589. * The low speed limit that if transfers are below this for
  590. * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
  591. */
  592. data->set.low_speed_limit=va_arg(param, long);
  593. break;
  594. case CURLOPT_LOW_SPEED_TIME:
  595. /*
  596. * The low speed time that if transfers are below the set
  597. * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
  598. */
  599. data->set.low_speed_time=va_arg(param, long);
  600. break;
  601. case CURLOPT_URL:
  602. /*
  603. * The URL to fetch.
  604. */
  605. if(data->change.url_alloc) {
  606. /* the already set URL is allocated, free it first! */
  607. free(data->change.url);
  608. data->change.url_alloc=FALSE;
  609. }
  610. data->set.set_url = va_arg(param, char *);
  611. data->change.url = data->set.set_url;
  612. break;
  613. case CURLOPT_PORT:
  614. /*
  615. * The port number to use when getting the URL
  616. */
  617. data->set.use_port = va_arg(param, long);
  618. break;
  619. case CURLOPT_POST:
  620. /* Does this option serve a purpose anymore? Yes it does, when
  621. CURLOPT_POSTFIELDS isn't used and the POST data is read off the
  622. callback! */
  623. if(va_arg(param, long))
  624. data->set.httpreq = HTTPREQ_POST;
  625. break;
  626. case CURLOPT_POSTFIELDS:
  627. /*
  628. * A string with POST data. Makes curl HTTP POST.
  629. */
  630. data->set.postfields = va_arg(param, char *);
  631. if(data->set.postfields)
  632. data->set.httpreq = HTTPREQ_POST;
  633. break;
  634. case CURLOPT_POSTFIELDSIZE:
  635. /*
  636. * The size of the POSTFIELD data, if curl should now do a strlen
  637. * to find out. Enables binary posts.
  638. */
  639. data->set.postfieldsize = va_arg(param, long);
  640. break;
  641. case CURLOPT_REFERER:
  642. /*
  643. * String to set in the HTTP Referer: field.
  644. */
  645. if(data->change.referer_alloc) {
  646. free(data->change.referer);
  647. data->change.referer_alloc = FALSE;
  648. }
  649. data->set.set_referer = va_arg(param, char *);
  650. data->change.referer = data->set.set_referer;
  651. break;
  652. case CURLOPT_AUTOREFERER:
  653. /*
  654. * Switch on automatic referer that gets set if curl follows locations.
  655. */
  656. data->set.http_auto_referer = (bool)(va_arg(param, long)?TRUE:FALSE);
  657. break;
  658. case CURLOPT_PROXY:
  659. /*
  660. * Set proxy server:port to use as HTTP proxy.
  661. *
  662. * If the proxy is set to "" we explicitly say that we don't want to use a
  663. * proxy (even though there might be environment variables saying so).
  664. *
  665. * Setting it to NULL, means no proxy but allows the environment variables
  666. * to decide for us.
  667. */
  668. if(data->change.proxy_alloc) {
  669. /*
  670. * The already set string is allocated, free that first
  671. */
  672. data->change.proxy_alloc=FALSE;;
  673. free(data->change.proxy);
  674. }
  675. data->set.set_proxy = va_arg(param, char *);
  676. data->change.proxy = data->set.set_proxy;
  677. break;
  678. case CURLOPT_HTTPPROXYTUNNEL:
  679. /*
  680. * Tunnel operations through the proxy instead of normal proxy use
  681. */
  682. data->set.tunnel_thru_httpproxy = (bool)(va_arg(param, long)?TRUE:FALSE);
  683. break;
  684. case CURLOPT_PROXYPORT:
  685. /*
  686. * Explicitly set HTTP proxy port number.
  687. */
  688. data->set.proxyport = va_arg(param, long);
  689. break;
  690. case CURLOPT_TIMEOUT:
  691. /*
  692. * The maximum time you allow curl to use for a single transfer
  693. * operation.
  694. */
  695. data->set.timeout = va_arg(param, long);
  696. break;
  697. case CURLOPT_CONNECTTIMEOUT:
  698. /*
  699. * The maximum time you allow curl to use to connect.
  700. */
  701. data->set.connecttimeout = va_arg(param, long);
  702. break;
  703. case CURLOPT_MAXREDIRS:
  704. /*
  705. * The maximum amount of hops you allow curl to follow Location:
  706. * headers. This should mostly be used to detect never-ending loops.
  707. */
  708. data->set.maxredirs = va_arg(param, long);
  709. break;
  710. case CURLOPT_USERAGENT:
  711. /*
  712. * String to use in the HTTP User-Agent field
  713. */
  714. data->set.useragent = va_arg(param, char *);
  715. break;
  716. case CURLOPT_ENCODING:
  717. /*
  718. * String to use at the value of Accept-Encoding header. 08/28/02 jhrg
  719. */
  720. data->set.encoding = va_arg(param, char *);
  721. break;
  722. case CURLOPT_USERPWD:
  723. /*
  724. * user:password to use in the operation
  725. */
  726. data->set.userpwd = va_arg(param, char *);
  727. break;
  728. case CURLOPT_POSTQUOTE:
  729. /*
  730. * List of RAW FTP commands to use after a transfer
  731. */
  732. data->set.postquote = va_arg(param, struct curl_slist *);
  733. break;
  734. case CURLOPT_PREQUOTE:
  735. /*
  736. * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
  737. */
  738. data->set.prequote = va_arg(param, struct curl_slist *);
  739. break;
  740. case CURLOPT_QUOTE:
  741. /*
  742. * List of RAW FTP commands to use before a transfer
  743. */
  744. data->set.quote = va_arg(param, struct curl_slist *);
  745. break;
  746. case CURLOPT_PROGRESSFUNCTION:
  747. /*
  748. * Progress callback function
  749. */
  750. data->set.fprogress = va_arg(param, curl_progress_callback);
  751. if(data->set.fprogress)
  752. data->progress.callback = TRUE; /* no longer internal */
  753. else
  754. data->progress.callback = FALSE; /* NULL enforces internal */
  755. break;
  756. case CURLOPT_PROGRESSDATA:
  757. /*
  758. * Custom client data to pass to the progress callback
  759. */
  760. data->set.progress_client = va_arg(param, void *);
  761. break;
  762. case CURLOPT_PASSWDFUNCTION:
  763. /*
  764. * Password prompt callback
  765. */
  766. data->set.fpasswd = va_arg(param, curl_passwd_callback);
  767. /*
  768. * if the callback provided is null, reset the default callback
  769. */
  770. if(!data->set.fpasswd)
  771. {
  772. data->set.fpasswd = my_getpass;
  773. }
  774. break;
  775. case CURLOPT_PASSWDDATA:
  776. /*
  777. * Custom client data to pass to the password callback
  778. */
  779. data->set.passwd_client = va_arg(param, void *);
  780. break;
  781. case CURLOPT_PROXYUSERPWD:
  782. /*
  783. * user:password needed to use the proxy
  784. */
  785. data->set.proxyuserpwd = va_arg(param, char *);
  786. break;
  787. case CURLOPT_RANGE:
  788. /*
  789. * What range of the file you want to transfer
  790. */
  791. data->set.set_range = va_arg(param, char *);
  792. break;
  793. case CURLOPT_RESUME_FROM:
  794. /*
  795. * Resume transfer at the give file position
  796. */
  797. data->set.set_resume_from = va_arg(param, long);
  798. break;
  799. case CURLOPT_DEBUGFUNCTION:
  800. /*
  801. * stderr write callback.
  802. */
  803. data->set.fdebug = va_arg(param, curl_debug_callback);
  804. /*
  805. * if the callback provided is NULL, it'll use the default callback
  806. */
  807. break;
  808. case CURLOPT_DEBUGDATA:
  809. /*
  810. * Set to a void * that should receive all error writes. This
  811. * defaults to CURLOPT_STDERR for normal operations.
  812. */
  813. data->set.debugdata = va_arg(param, void *);
  814. break;
  815. case CURLOPT_STDERR:
  816. /*
  817. * Set to a FILE * that should receive all error writes. This
  818. * defaults to stderr for normal operations.
  819. */
  820. data->set.err = va_arg(param, FILE *);
  821. if(!data->set.err)
  822. data->set.err = stderr;
  823. break;
  824. case CURLOPT_HEADERFUNCTION:
  825. /*
  826. * Set header write callback
  827. */
  828. data->set.fwrite_header = va_arg(param, curl_write_callback);
  829. break;
  830. case CURLOPT_WRITEFUNCTION:
  831. /*
  832. * Set data write callback
  833. */
  834. data->set.fwrite = va_arg(param, curl_write_callback);
  835. break;
  836. case CURLOPT_READFUNCTION:
  837. /*
  838. * Read data callback
  839. */
  840. data->set.fread = va_arg(param, curl_read_callback);
  841. break;
  842. case CURLOPT_SSLCERT:
  843. /*
  844. * String that holds file name of the SSL certificate to use
  845. */
  846. data->set.cert = va_arg(param, char *);
  847. break;
  848. case CURLOPT_SSLCERTTYPE:
  849. /*
  850. * String that holds file type of the SSL certificate to use
  851. */
  852. data->set.cert_type = va_arg(param, char *);
  853. break;
  854. case CURLOPT_SSLKEY:
  855. /*
  856. * String that holds file name of the SSL certificate to use
  857. */
  858. data->set.key = va_arg(param, char *);
  859. break;
  860. case CURLOPT_SSLKEYTYPE:
  861. /*
  862. * String that holds file type of the SSL certificate to use
  863. */
  864. data->set.key_type = va_arg(param, char *);
  865. break;
  866. case CURLOPT_SSLKEYPASSWD:
  867. /*
  868. * String that holds the SSL private key password.
  869. */
  870. data->set.key_passwd = va_arg(param, char *);
  871. break;
  872. case CURLOPT_SSLENGINE:
  873. /*
  874. * String that holds the SSL crypto engine.
  875. */
  876. #ifdef HAVE_OPENSSL_ENGINE_H
  877. {
  878. const char *cpTemp = va_arg(param, char *);
  879. ENGINE *e;
  880. if (cpTemp && cpTemp[0]) {
  881. e = ENGINE_by_id(cpTemp);
  882. if (e) {
  883. if (data->engine) {
  884. ENGINE_free(data->engine);
  885. }
  886. data->engine = e;
  887. }
  888. else {
  889. failf(data, "SSL Engine '%s' not found", cpTemp);
  890. return CURLE_SSL_ENGINE_NOTFOUND;
  891. }
  892. }
  893. }
  894. break;
  895. #else
  896. return CURLE_SSL_ENGINE_NOTFOUND;
  897. #endif
  898. case CURLOPT_SSLENGINE_DEFAULT:
  899. /*
  900. * flag to set engine as default.
  901. */
  902. #ifdef HAVE_OPENSSL_ENGINE_H
  903. if (data->engine) {
  904. if (ENGINE_set_default(data->engine, ENGINE_METHOD_ALL) > 0) {
  905. #ifdef DEBUG
  906. fprintf(stderr,"set default crypto engine\n");
  907. #endif
  908. }
  909. else {
  910. #ifdef DEBUG
  911. failf(data, "set default crypto engine failed");
  912. #endif
  913. return CURLE_SSL_ENGINE_SETFAILED;
  914. }
  915. }
  916. #endif
  917. break;
  918. case CURLOPT_CRLF:
  919. /*
  920. * Kludgy option to enable CRLF convertions. Subject for removal.
  921. */
  922. data->set.crlf = (bool)(va_arg(param, long)?TRUE:FALSE);
  923. break;
  924. case CURLOPT_INTERFACE:
  925. /*
  926. * Set what interface to bind to when performing an operation and thus
  927. * what from-IP your connection will use.
  928. */
  929. data->set.device = va_arg(param, char *);
  930. break;
  931. case CURLOPT_KRB4LEVEL:
  932. /*
  933. * A string that defines the krb4 security level.
  934. */
  935. data->set.krb4_level = va_arg(param, char *);
  936. data->set.krb4=(bool)(data->set.krb4_level?TRUE:FALSE);
  937. break;
  938. case CURLOPT_SSL_VERIFYPEER:
  939. /*
  940. * Enable peer SSL verifying.
  941. */
  942. data->set.ssl.verifypeer = va_arg(param, long);
  943. break;
  944. case CURLOPT_SSL_VERIFYHOST:
  945. /*
  946. * Enable verification of the CN contained in the peer certificate
  947. */
  948. data->set.ssl.verifyhost = va_arg(param, long);
  949. break;
  950. case CURLOPT_CAINFO:
  951. /*
  952. * Set CA info for SSL connection. Specify file name of the CA certificate
  953. */
  954. data->set.ssl.CAfile = va_arg(param, char *);
  955. break;
  956. case CURLOPT_CAPATH:
  957. /*
  958. * Set CA path info for SSL connection. Specify directory name of the CA
  959. * certificates which have been prepared using openssl c_rehash utility.
  960. */
  961. /* This does not work on windows. */
  962. data->set.ssl.CApath = va_arg(param, char *);
  963. break;
  964. case CURLOPT_TELNETOPTIONS:
  965. /*
  966. * Set a linked list of telnet options
  967. */
  968. data->set.telnet_options = va_arg(param, struct curl_slist *);
  969. break;
  970. case CURLOPT_BUFFERSIZE:
  971. /*
  972. * The application kindly asks for a differently sized receive buffer.
  973. * If it seems reasonable, we'll use it.
  974. */
  975. data->set.buffer_size = va_arg(param, long);
  976. if(data->set.buffer_size> (BUFSIZE -1 ))
  977. data->set.buffer_size = 0; /* huge internal default */
  978. break;
  979. case CURLOPT_NOSIGNAL:
  980. /*
  981. * The application asks not to set any signal() or alarm() handlers,
  982. * even when using a timeout.
  983. */
  984. data->set.no_signal = (bool)(va_arg(param, long) ? TRUE : FALSE);
  985. break;
  986. case CURLOPT_SHARE:
  987. {
  988. struct Curl_share *set;
  989. set = va_arg(param, struct Curl_share *);
  990. if(data->share)
  991. data->share->dirty--;
  992. data->share = set;
  993. data->share->dirty++;
  994. }
  995. break;
  996. case CURLOPT_PROXYTYPE:
  997. /*
  998. * Set proxy type. HTTP/SOCKS4/SOCKS5
  999. */
  1000. data->set.proxytype = (curl_proxytype)va_arg(param, long);
  1001. break;
  1002. case CURLOPT_PRIVATE:
  1003. /*
  1004. * Set private data pointer.
  1005. */
  1006. data->set.private = va_arg(param, char *);
  1007. break;
  1008. case CURLOPT_HTTP200ALIASES:
  1009. /*
  1010. * Set a list of aliases for HTTP 200 in response header
  1011. */
  1012. data->set.http200aliases = va_arg(param, struct curl_slist *);
  1013. break;
  1014. default:
  1015. /* unknown tag and its companion, just ignore: */
  1016. return CURLE_FAILED_INIT; /* correct this */
  1017. }
  1018. return CURLE_OK;
  1019. }
  1020. CURLcode Curl_disconnect(struct connectdata *conn)
  1021. {
  1022. if(!conn)
  1023. return CURLE_OK; /* this is closed and fine already */
  1024. /*
  1025. * The range string is usually freed in curl_done(), but we might
  1026. * get here *instead* if we fail prematurely. Thus we need to be able
  1027. * to free this resource here as well.
  1028. */
  1029. if(conn->bits.rangestringalloc) {
  1030. free(conn->range);
  1031. conn->bits.rangestringalloc = FALSE;
  1032. }
  1033. if(-1 != conn->connectindex) {
  1034. /* unlink ourselves! */
  1035. infof(conn->data, "Closing connection #%d\n", conn->connectindex);
  1036. conn->data->state.connects[conn->connectindex] = NULL;
  1037. }
  1038. if(conn->curl_disconnect)
  1039. /* This is set if protocol-specific cleanups should be made */
  1040. conn->curl_disconnect(conn);
  1041. if(conn->proto.generic)
  1042. free(conn->proto.generic);
  1043. if(conn->newurl)
  1044. free(conn->newurl);
  1045. if(conn->path) /* the URL path part */
  1046. free(conn->path);
  1047. #ifdef USE_SSLEAY
  1048. Curl_SSL_Close(conn);
  1049. #endif /* USE_SSLEAY */
  1050. /* close possibly still open sockets */
  1051. if(-1 != conn->secondarysocket)
  1052. sclose(conn->secondarysocket);
  1053. if(-1 != conn->firstsocket)
  1054. sclose(conn->firstsocket);
  1055. if(conn->allocptr.proxyuserpwd)
  1056. free(conn->allocptr.proxyuserpwd);
  1057. if(conn->allocptr.uagent)
  1058. free(conn->allocptr.uagent);
  1059. if(conn->allocptr.userpwd)
  1060. free(conn->allocptr.userpwd);
  1061. if(conn->allocptr.accept_encoding)
  1062. free(conn->allocptr.accept_encoding);
  1063. if(conn->allocptr.rangeline)
  1064. free(conn->allocptr.rangeline);
  1065. if(conn->allocptr.ref)
  1066. free(conn->allocptr.ref);
  1067. if(conn->allocptr.cookie)
  1068. free(conn->allocptr.cookie);
  1069. if(conn->allocptr.host)
  1070. free(conn->allocptr.host);
  1071. if(conn->proxyhost)
  1072. free(conn->proxyhost);
  1073. free(conn); /* free all the connection oriented data */
  1074. return CURLE_OK;
  1075. }
  1076. /*
  1077. * This function should return TRUE if the socket is to be assumed to
  1078. * be dead. Most commonly this happens when the server has closed the
  1079. * connection due to inactivity.
  1080. */
  1081. static bool SocketIsDead(int sock)
  1082. {
  1083. int sval;
  1084. bool ret_val = TRUE;
  1085. fd_set check_set;
  1086. struct timeval to;
  1087. FD_ZERO(&check_set);
  1088. FD_SET(sock,&check_set);
  1089. to.tv_sec = 0;
  1090. to.tv_usec = 0;
  1091. sval = select(sock + 1, &check_set, 0, 0, &to);
  1092. if(sval == 0)
  1093. /* timeout */
  1094. ret_val = FALSE;
  1095. return ret_val;
  1096. }
  1097. /*
  1098. * Given one filled in connection struct (named needle), this function should
  1099. * detect if there already is one that have all the significant details
  1100. * exactly the same and thus should be used instead.
  1101. */
  1102. static bool
  1103. ConnectionExists(struct SessionHandle *data,
  1104. struct connectdata *needle,
  1105. struct connectdata **usethis)
  1106. {
  1107. long i;
  1108. struct connectdata *check;
  1109. for(i=0; i< data->state.numconnects; i++) {
  1110. bool match = FALSE;
  1111. /*
  1112. * Note that if we use a HTTP proxy, we check connections to that
  1113. * proxy and not to the actual remote server.
  1114. */
  1115. check = data->state.connects[i];
  1116. if(!check)
  1117. /* NULL pointer means not filled-in entry */
  1118. continue;
  1119. if(!needle->bits.httpproxy || needle->protocol&PROT_SSL) {
  1120. /* The requested connection does not use a HTTP proxy or it
  1121. uses SSL. */
  1122. if(!(needle->protocol&PROT_SSL) && check->bits.httpproxy)
  1123. /* we don't do SSL but the cached connection has a proxy,
  1124. then don't match this */
  1125. continue;
  1126. if(strequal(needle->protostr, check->protostr) &&
  1127. strequal(needle->name, check->name) &&
  1128. (needle->remote_port == check->remote_port) ) {
  1129. if(strequal(needle->protostr, "FTP")) {
  1130. /* This is FTP, verify that we're using the same name and
  1131. password as well */
  1132. if(!strequal(needle->data->state.user, check->proto.ftp->user) ||
  1133. !strequal(needle->data->state.passwd, check->proto.ftp->passwd)) {
  1134. /* one of them was different */
  1135. continue;
  1136. }
  1137. }
  1138. match = TRUE;
  1139. }
  1140. }
  1141. else { /* The requested needle connection is using a proxy,
  1142. is the checked one using the same? */
  1143. if(check->bits.httpproxy &&
  1144. strequal(needle->proxyhost, check->proxyhost) &&
  1145. needle->port == check->port) {
  1146. /* This is the same proxy connection, use it! */
  1147. match = TRUE;
  1148. }
  1149. }
  1150. if(match) {
  1151. bool dead = SocketIsDead(check->firstsocket);
  1152. if(dead) {
  1153. /*
  1154. */
  1155. infof(data, "Connection %d seems to be dead!\n", i);
  1156. Curl_disconnect(check); /* disconnect resources */
  1157. data->state.connects[i]=NULL; /* nothing here */
  1158. /* There's no need to continue searching, because we only store
  1159. one connection for each unique set of identifiers */
  1160. return FALSE;
  1161. }
  1162. *usethis = check;
  1163. return TRUE; /* yes, we found one to use! */
  1164. }
  1165. }
  1166. return FALSE; /* no matching connecting exists */
  1167. }
  1168. /*
  1169. * This function frees/closes a connection in the connection cache. This
  1170. * should take the previously set policy into account when deciding which
  1171. * of the connections to kill.
  1172. */
  1173. static int
  1174. ConnectionKillOne(struct SessionHandle *data)
  1175. {
  1176. long i;
  1177. struct connectdata *conn;
  1178. int highscore=-1;
  1179. int connindex=-1;
  1180. int score;
  1181. CURLcode result;
  1182. struct timeval now;
  1183. now = Curl_tvnow();
  1184. for(i=0; i< data->state.numconnects; i++) {
  1185. conn = data->state.connects[i];
  1186. if(!conn)
  1187. continue;
  1188. /*
  1189. * By using the set policy, we score each connection.
  1190. */
  1191. switch(data->set.closepolicy) {
  1192. case CURLCLOSEPOLICY_LEAST_RECENTLY_USED:
  1193. default:
  1194. /*
  1195. * Set higher score for the age passed since the connection
  1196. * was used.
  1197. */
  1198. score = Curl_tvdiff(now, conn->now);
  1199. break;
  1200. case CURLCLOSEPOLICY_OLDEST:
  1201. /*
  1202. * Set higher score for the age passed since the connection
  1203. * was created.
  1204. */
  1205. score = Curl_tvdiff(now, conn->created);
  1206. break;
  1207. }
  1208. if(score > highscore) {
  1209. highscore = score;
  1210. connindex = i;
  1211. }
  1212. }
  1213. if(connindex >= 0) {
  1214. /* the winner gets the honour of being disconnected */
  1215. result = Curl_disconnect(data->state.connects[connindex]);
  1216. (void)result;
  1217. /* clean the array entry */
  1218. data->state.connects[connindex] = NULL;
  1219. }
  1220. return connindex; /* return the available index or -1 */
  1221. }
  1222. /*
  1223. * The given input connection struct pointer is to be stored. If the "cache"
  1224. * is already full, we must clean out the most suitable using the previously
  1225. * set policy.
  1226. *
  1227. * The given connection should be unique. That must've been checked prior to
  1228. * this call.
  1229. */
  1230. static unsigned int
  1231. ConnectionStore(struct SessionHandle *data,
  1232. struct connectdata *conn)
  1233. {
  1234. long i;
  1235. for(i=0; i< data->state.numconnects; i++) {
  1236. if(!data->state.connects[i])
  1237. break;
  1238. }
  1239. if(i == data->state.numconnects) {
  1240. /* there was no room available, kill one */
  1241. i = ConnectionKillOne(data);
  1242. infof(data, "Connection (#%d) was killed to make room\n", i);
  1243. }
  1244. if(-1 != i) {
  1245. /* only do this if a true index was returned, if -1 was returned there
  1246. is no room in the cache for an unknown reason and we cannot store
  1247. this there. */
  1248. data->state.connects[i] = conn; /* fill in this */
  1249. conn->connectindex = i; /* make the child know where the pointer to this
  1250. particular data is stored */
  1251. }
  1252. return i;
  1253. }
  1254. /*
  1255. * This function logs in to a SOCKS5 proxy and sends the specifies the final
  1256. * desitination server.
  1257. */
  1258. static int handleSock5Proxy(
  1259. const char *proxy_name,
  1260. const char *proxy_password,
  1261. struct connectdata *conn,
  1262. int sock)
  1263. {
  1264. unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
  1265. ssize_t actualread;
  1266. ssize_t written;
  1267. CURLcode result;
  1268. Curl_nonblock(sock, FALSE);
  1269. socksreq[0] = 5; /* version */
  1270. socksreq[1] = (char)(proxy_name[0] ? 2 : 1); /* number of methods (below) */
  1271. socksreq[2] = 0; /* no authentication */
  1272. socksreq[3] = 2; /* username/password */
  1273. result = Curl_write(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
  1274. &written);
  1275. if ((result != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
  1276. failf(conn->data, "Unable to send initial SOCKS5 request.");
  1277. return 1;
  1278. }
  1279. result=(CURLcode)Curl_read(conn, sock, (char *)socksreq, 2, &actualread);
  1280. if ((result != CURLE_OK) || (actualread != 2)) {
  1281. failf(conn->data, "Unable to receive initial SOCKS5 response.");
  1282. return 1;
  1283. }
  1284. if (socksreq[0] != 5) {
  1285. failf(conn->data, "Received invalid version in initial SOCKS5 response.");
  1286. return 1;
  1287. }
  1288. if (socksreq[1] == 0) {
  1289. /* Nothing to do, no authentication needed */
  1290. ;
  1291. }
  1292. else if (socksreq[1] == 2) {
  1293. /* Needs user name and password */
  1294. int userlen, pwlen, len;
  1295. userlen = (int)strlen(proxy_name);
  1296. pwlen = (int)strlen(proxy_password);
  1297. /* username/password request looks like
  1298. * +----+------+----------+------+----------+
  1299. * |VER | ULEN | UNAME | PLEN | PASSWD |
  1300. * +----+------+----------+------+----------+
  1301. * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  1302. * +----+------+----------+------+----------+
  1303. */
  1304. len = 0;
  1305. socksreq[len++] = 1; /* username/pw subnegotiation version */
  1306. socksreq[len++] = (char) userlen;
  1307. memcpy(socksreq + len, proxy_name, (int) userlen);
  1308. len += userlen;
  1309. socksreq[len++] = (char) pwlen;
  1310. memcpy(socksreq + len, proxy_password, (int) pwlen);
  1311. len += pwlen;
  1312. result = Curl_write(conn, sock, (char *)socksreq, len, &written);
  1313. if ((result != CURLE_OK) || (len != written)) {
  1314. failf(conn->data, "Failed to send SOCKS5 sub-negotiation request.");
  1315. return 1;
  1316. }
  1317. result=(CURLcode)Curl_read(conn, sock, (char *)socksreq, 2, &actualread);
  1318. if ((result != CURLE_OK) || (actualread != 2)) {
  1319. failf(conn->data, "Unable to receive SOCKS5 sub-negotiation response.");
  1320. return 1;
  1321. }
  1322. if ((socksreq[0] != 5) || /* version */
  1323. (socksreq[1] != 0)) { /* status */
  1324. failf(conn->data, "User was rejected by the SOCKS5 server (%d %d).",
  1325. socksreq[0], socksreq[1]);
  1326. return 1;
  1327. }
  1328. /* Everything is good so far, user was authenticated! */
  1329. }
  1330. else {
  1331. /* error */
  1332. if (socksreq[1] == 1) {
  1333. failf(conn->data,
  1334. "SOCKS5 GSSAPI per-message authentication is not supported.");
  1335. return 1;
  1336. }
  1337. else if (socksreq[1] == 255) {
  1338. if (proxy_name[0] == 0) {
  1339. failf(conn->data,
  1340. "No authentication method was acceptable. (It is quite likely"
  1341. " that the SOCKS5 server wanted a username/password, since none"
  1342. " was supplied to the server on this connection.)");
  1343. }
  1344. else {
  1345. failf(conn->data, "No authentication method was acceptable.");
  1346. }
  1347. return 1;
  1348. }
  1349. else {
  1350. failf(conn->data,
  1351. "Undocumented SOCKS5 mode attempted to be used by server.");
  1352. return 1;
  1353. }
  1354. }
  1355. /* Authentication is complete, now specify destination to the proxy */
  1356. socksreq[0] = 5; /* version (SOCKS5) */
  1357. socksreq[1] = 1; /* connect */
  1358. socksreq[2] = 0; /* must be zero */
  1359. socksreq[3] = 1; /* IPv4 = 1 */
  1360. {
  1361. #ifndef ENABLE_IPV6
  1362. struct Curl_dns_entry *dns;
  1363. Curl_addrinfo *hp=NULL;
  1364. dns = Curl_resolv(conn->data, conn->hostname, conn->remote_port);
  1365. /*
  1366. * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
  1367. * returns a Curl_addrinfo pointer that may not always look the same.
  1368. */
  1369. if(dns)
  1370. hp=dns->addr;
  1371. if (hp && hp->h_addr_list[0]) {
  1372. socksreq[4] = ((char*)hp->h_addr_list[0])[0];
  1373. socksreq[5] = ((char*)hp->h_addr_list[0])[1];
  1374. socksreq[6] = ((char*)hp->h_addr_list[0])[2];
  1375. socksreq[7] = ((char*)hp->h_addr_list[0])[3];
  1376. Curl_resolv_unlock(dns); /* not used anymore from now on */
  1377. }
  1378. else {
  1379. failf(conn->data, "Failed to resolve \"%s\" for SOCKS5 connect.",
  1380. conn->hostname);
  1381. return 1;
  1382. }
  1383. #else
  1384. failf(conn->data,
  1385. "%s:%d has an internal error an needs to be fixed to work",
  1386. __FILE__, __LINE__);
  1387. return 1;
  1388. #endif
  1389. }
  1390. {
  1391. unsigned short htons_res = htons(conn->remote_port);
  1392. memcpy(&socksreq[8], &htons_res, sizeof(unsigned short*));
  1393. }
  1394. {
  1395. const int packetsize = 10;
  1396. result = Curl_write(conn, sock, (char *)socksreq, packetsize, &written);
  1397. if ((result != CURLE_OK) || (written != packetsize)) {
  1398. failf(conn->data, "Failed to send SOCKS5 connect request.");
  1399. return 1;
  1400. }
  1401. result = (CURLcode)Curl_read(conn, sock, (char *)socksreq, packetsize, &actualread);
  1402. if ((result != CURLE_OK) || (actualread != packetsize)) {
  1403. failf(conn->data, "Failed to receive SOCKS5 connect request ack.");
  1404. return 1;
  1405. }
  1406. if (socksreq[0] != 5) { /* version */
  1407. failf(conn->data,
  1408. "SOCKS5 reply has wrong version, version should be 5.");
  1409. return 1;
  1410. }
  1411. if (socksreq[1] != 0) { /* Anything besides 0 is an error */
  1412. unsigned short ntohs_arg;
  1413. memcpy(&ntohs_arg, &socksreq[8], sizeof(unsigned short*));
  1414. failf(conn->data,
  1415. "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
  1416. (unsigned char)socksreq[4], (unsigned char)socksreq[5],
  1417. (unsigned char)socksreq[6], (unsigned char)socksreq[7],
  1418. (unsigned int)ntohs(ntohs_arg),
  1419. socksreq[1]);
  1420. return 1;
  1421. }
  1422. }
  1423. Curl_nonblock(sock, TRUE);
  1424. return 0; /* Proxy was successful! */
  1425. }
  1426. static CURLcode ConnectPlease(struct connectdata *conn,
  1427. struct Curl_dns_entry *hostaddr,
  1428. bool *connected)
  1429. {
  1430. CURLcode result;
  1431. Curl_ipconnect *addr;
  1432. /*************************************************************
  1433. * Connect to server/proxy
  1434. *************************************************************/
  1435. result= Curl_connecthost(conn,
  1436. hostaddr,
  1437. conn->port,
  1438. &conn->firstsocket,
  1439. &addr,
  1440. connected);
  1441. if(CURLE_OK == result) {
  1442. /* All is cool, then we store the current information from the hostaddr
  1443. struct to the serv_addr, as it might be needed later. The address
  1444. returned from the function above is crucial here. */
  1445. conn->connect_addr = hostaddr;
  1446. #ifdef ENABLE_IPV6
  1447. conn->serv_addr = addr;
  1448. #else
  1449. memset((char *) &conn->serv_addr, '\0', sizeof(conn->serv_addr));
  1450. memcpy((char *)&(conn->serv_addr.sin_addr),
  1451. (struct in_addr *)addr, sizeof(struct in_addr));
  1452. conn->serv_addr.sin_family = hostaddr->addr->h_addrtype;
  1453. conn->serv_addr.sin_port = htons((unsigned short)conn->port);
  1454. #endif
  1455. if (conn->data->set.proxytype == CURLPROXY_SOCKS5) {
  1456. return handleSock5Proxy(conn->data->state.proxyuser,
  1457. conn->data->state.proxypasswd,
  1458. conn,
  1459. conn->firstsocket) ?
  1460. CURLE_COULDNT_CONNECT : CURLE_OK;
  1461. }
  1462. else if (conn->data->set.proxytype == CURLPROXY_HTTP) {
  1463. /* do nothing here. handled later. */
  1464. }
  1465. else {
  1466. failf(conn->data, "unknown proxytype option given");
  1467. return CURLE_COULDNT_CONNECT;
  1468. }
  1469. }
  1470. return result;
  1471. }
  1472. /*
  1473. * ALERT! The 'dns' pointer being passed in here might be NULL at times.
  1474. */
  1475. static void verboseconnect(struct connectdata *conn,
  1476. struct Curl_dns_entry *dns)
  1477. {
  1478. #ifdef HAVE_INET_NTOA_R
  1479. char ntoa_buf[64];
  1480. #endif
  1481. struct SessionHandle *data = conn->data;
  1482. /* Figure out the ip-number and display the first host name it shows: */
  1483. #ifdef ENABLE_IPV6
  1484. (void)dns; /* not used in the IPv6 enabled version */
  1485. {
  1486. char hbuf[NI_MAXHOST];
  1487. #ifdef NI_WITHSCOPEID
  1488. const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
  1489. #else
  1490. const int niflags = NI_NUMERICHOST;
  1491. #endif
  1492. struct addrinfo *ai = conn->serv_addr;
  1493. if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), NULL, 0,
  1494. niflags)) {
  1495. snprintf(hbuf, sizeof(hbuf), "?");
  1496. }
  1497. if (ai->ai_canonname) {
  1498. infof(data, "Connected to %s (%s) port %d\n", ai->ai_canonname, hbuf,
  1499. conn->port);
  1500. } else {
  1501. infof(data, "Connected to %s port %d\n", hbuf, conn->port);
  1502. }
  1503. }
  1504. #else
  1505. {
  1506. Curl_addrinfo *hostaddr=dns?dns->addr:NULL;
  1507. struct in_addr in;
  1508. (void) memcpy(&in.s_addr, &conn->serv_addr.sin_addr, sizeof (in.s_addr));
  1509. infof(data, "Connected to %s (%s) port %d\n",
  1510. hostaddr?hostaddr->h_name:"",
  1511. #if defined(HAVE_INET_NTOA_R)
  1512. inet_ntoa_r(in, ntoa_buf, sizeof(ntoa_buf)),
  1513. #else
  1514. inet_ntoa(in),
  1515. #endif
  1516. conn->port);
  1517. }
  1518. #endif
  1519. }
  1520. /*
  1521. * We have discovered that the TCP connection has been successful, we can now
  1522. * proceed with some action.
  1523. *
  1524. * If we're using the multi interface, this host address pointer is most
  1525. * likely NULL at this point as we can't keep the resolved info around. This
  1526. * may call for some reworking, like a reference counter in the struct or
  1527. * something. The hostaddr is not used for very much though, we have the
  1528. * 'serv_addr' field in the connectdata struct for most of it.
  1529. */
  1530. CURLcode Curl_protocol_connect(struct connectdata *conn,
  1531. struct Curl_dns_entry *hostaddr)
  1532. {
  1533. struct SessionHandle *data = conn->data;
  1534. CURLcode result=CURLE_OK;
  1535. if(conn->bits.tcpconnect)
  1536. /* We already are connected, get back. This may happen when the connect
  1537. worked fine in the first call, like when we connect to a local server
  1538. or proxy. */
  1539. return CURLE_OK;
  1540. Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */
  1541. if(data->set.verbose)
  1542. verboseconnect(conn, hostaddr);
  1543. if(conn->curl_connect) {
  1544. /* is there a protocol-specific connect() procedure? */
  1545. /* set start time here for timeout purposes in the
  1546. * connect procedure, it is later set again for the
  1547. * progress meter purpose */
  1548. conn->now = Curl_tvnow();
  1549. /* Call the protocol-specific connect function */
  1550. result = conn->curl_connect(conn);
  1551. }
  1552. return result; /* pass back status */
  1553. }
  1554. static CURLcode CreateConnection(struct SessionHandle *data,
  1555. struct connectdata **in_connect)
  1556. {
  1557. char *tmp;
  1558. char *buf;
  1559. CURLcode result=CURLE_OK;
  1560. char resumerange[40]="";
  1561. struct connectdata *conn;
  1562. struct connectdata *conn_temp;
  1563. int urllen;
  1564. struct Curl_dns_entry *hostaddr;
  1565. #ifdef HAVE_ALARM
  1566. unsigned int prev_alarm=0;
  1567. #endif
  1568. char endbracket;
  1569. #ifdef HAVE_SIGACTION
  1570. struct sigaction keep_sigact; /* store the old struct here */
  1571. bool keep_copysig=FALSE; /* did copy it? */
  1572. #else
  1573. #ifdef HAVE_SIGNAL
  1574. void *keep_sigact; /* store the old handler here */
  1575. #endif
  1576. #endif
  1577. /*************************************************************
  1578. * Check input data
  1579. *************************************************************/
  1580. if(!data->change.url)
  1581. return CURLE_URL_MALFORMAT;
  1582. /* First, split up the current URL in parts so that we can use the
  1583. parts for checking against the already present connections. In order
  1584. to not have to modify everything at once, we allocate a temporary
  1585. connection data struct and fill in for comparison purposes. */
  1586. conn = (struct connectdata *)malloc(sizeof(struct connectdata));
  1587. if(!conn) {
  1588. *in_connect = NULL; /* clear the pointer */
  1589. return CURLE_OUT_OF_MEMORY;
  1590. }
  1591. /* We must set the return variable as soon as possible, so that our
  1592. parent can cleanup any possible allocs we may have done before
  1593. any failure */
  1594. *in_connect = conn;
  1595. /* we have to init the struct */
  1596. memset(conn, 0, sizeof(struct connectdata));
  1597. /* and we setup a few fields in case we end up actually using this struct */
  1598. conn->data = data; /* remember our daddy */
  1599. conn->firstsocket = -1; /* no file descriptor */
  1600. conn->secondarysocket = -1; /* no file descriptor */
  1601. conn->connectindex = -1; /* no index */
  1602. conn->bits.httpproxy = (bool)((data->change.proxy && *data->change.proxy &&
  1603. (data->set.proxytype == CURLPROXY_HTTP))?
  1604. TRUE:FALSE); /* http proxy or not */
  1605. conn->bits.use_range = (bool)(data->set.set_range?TRUE:FALSE);
  1606. /* range status */
  1607. conn->range = data->set.set_range; /* clone the range setting */
  1608. conn->resume_from = data->set.set_resume_from; /* inherite resume_from */
  1609. /* Default protocol-independent behavior doesn't support persistant
  1610. connections, so we set this to force-close. Protocols that support
  1611. this need to set this to FALSE in their "curl_do" functions. */
  1612. conn->bits.close = (bool)TRUE;
  1613. /* inherite initial knowledge from the data struct */
  1614. conn->bits.user_passwd = (bool)(data->set.userpwd?TRUE:FALSE);
  1615. conn->bits.proxy_user_passwd = (bool)(data->set.proxyuserpwd?TRUE:FALSE);
  1616. /* maxdownload must be -1 on init, as 0 is a valid value! */
  1617. conn->maxdownload = -1; /* might have been used previously! */
  1618. /* Store creation time to help future close decision making */
  1619. conn->created = Curl_tvnow();
  1620. /* Set the start time temporary to this creation time to allow easier
  1621. timeout checks before the transfer has started for real. The start time
  1622. is later set "for real" using Curl_pgrsStartNow(). */
  1623. conn->data->progress.start = conn->created;
  1624. conn->bits.upload_chunky = (bool)(
  1625. ((conn->protocol&PROT_HTTP) &&
  1626. data->set.upload &&
  1627. (data->set.infilesize == -1) &&
  1628. (data->set.httpversion != CURL_HTTP_VERSION_1_0))?
  1629. /* HTTP, upload, unknown file size and not HTTP 1.0 */
  1630. TRUE:
  1631. /* else, no chunky upload */
  1632. FALSE);
  1633. conn->fread = data->set.fread;
  1634. conn->fread_in = data->set.in;
  1635. /***********************************************************
  1636. * We need to allocate memory to store the path in. We get the size of the
  1637. * full URL to be sure, and we need to make it at least 256 bytes since
  1638. * other parts of the code will rely on this fact
  1639. ***********************************************************/
  1640. #define LEAST_PATH_ALLOC 256
  1641. urllen=(int)strlen(data->change.url);
  1642. if(urllen < LEAST_PATH_ALLOC)
  1643. urllen=LEAST_PATH_ALLOC;
  1644. conn->path=(char *)malloc(urllen);
  1645. if(NULL == conn->path)
  1646. return CURLE_OUT_OF_MEMORY; /* really bad error */
  1647. /*************************************************************
  1648. * Parse the URL.
  1649. *
  1650. * We need to parse the url even when using the proxy, because we will need
  1651. * the hostname and port in case we are trying to SSL connect through the
  1652. * proxy -- and we don't know if we will need to use SSL until we parse the
  1653. * url ...
  1654. ************************************************************/
  1655. if((2 == sscanf(data->change.url, "%64[^:]://%[^\n]",
  1656. conn->protostr,
  1657. conn->path)) && strequal(conn->protostr, "file")) {
  1658. /*
  1659. * we deal with file://<host>/<path> differently since it supports no
  1660. * hostname other than "localhost" and "127.0.0.1", which is unique among
  1661. * the URL protocols specified in RFC 1738
  1662. */
  1663. if(conn->path[0] != '/') {
  1664. /* the URL included a host name, we ignore host names in file:// URLs
  1665. as the standards don't define what to do with them */
  1666. char *ptr=strchr(conn->path, '/');
  1667. if(ptr) {
  1668. /* there was a slash present
  1669. RFC1738 (section 3.1, page 5) says:
  1670. The rest of the locator consists of data specific to the scheme,
  1671. and is known as the "url-path". It supplies the details of how the
  1672. specified resource can be accessed. Note that the "/" between the
  1673. host (or port) and the url-path is NOT part of the url-path.
  1674. As most agents use file://localhost/foo to get '/foo' although the
  1675. slash preceeding foo is a separator and not a slash for the path,
  1676. a URL as file://localhost//foo must be valid as well, to refer to
  1677. the same file with an absolute path.
  1678. */
  1679. if(ptr[1] && ('/' == ptr[1]))
  1680. /* if there was two slashes, we skip the first one as that is then
  1681. used truly as a separator */
  1682. ptr++;
  1683. strcpy(conn->path, ptr);
  1684. }
  1685. }
  1686. strcpy(conn->protostr, "file"); /* store protocol string lowercase */
  1687. }
  1688. else {
  1689. /* Set default host and default path */
  1690. strcpy(conn->gname, "curl.haxx.se");
  1691. strcpy(conn->path, "/");
  1692. if (2 > sscanf(data->change.url,
  1693. "%64[^\n:]://%512[^\n/]%[^\n]",
  1694. conn->protostr, conn->gname, conn->path)) {
  1695. /*
  1696. * The URL was badly formatted, let's try the browser-style _without_
  1697. * protocol specified like 'http://'.
  1698. */
  1699. if((1 > sscanf(data->change.url, "%512[^\n/]%[^\n]",
  1700. conn->gname, conn->path)) ) {
  1701. /*
  1702. * We couldn't even get this format.
  1703. */
  1704. failf(data, "<url> malformed");
  1705. return CURLE_URL_MALFORMAT;
  1706. }
  1707. /*
  1708. * Since there was no protocol part specified, we guess what protocol it
  1709. * is based on the first letters of the server name.
  1710. */
  1711. /* Note: if you add a new protocol, please update the list in
  1712. * lib/version.c too! */
  1713. if(checkprefix("FTP", conn->gname)) {
  1714. strcpy(conn->protostr, "ftp");
  1715. }
  1716. else if(checkprefix("GOPHER", conn->gname))
  1717. strcpy(conn->protostr, "gopher");
  1718. #ifdef USE_SSLEAY
  1719. else if(checkprefix("HTTPS", conn->gname))
  1720. strcpy(conn->protostr, "https");
  1721. else if(checkprefix("FTPS", conn->gname))
  1722. strcpy(conn->protostr, "ftps");
  1723. #endif /* USE_SSLEAY */
  1724. else if(checkprefix("TELNET", conn->gname))
  1725. strcpy(conn->protostr, "telnet");
  1726. else if (checkprefix("DICT", conn->gname))
  1727. strcpy(conn->protostr, "DICT");
  1728. else if (checkprefix("LDAP", conn->gname))
  1729. strcpy(conn->protostr, "LDAP");
  1730. else {
  1731. strcpy(conn->protostr, "http");
  1732. }
  1733. conn->protocol |= PROT_MISSING; /* not given in URL */
  1734. }
  1735. }
  1736. buf = data->state.buffer; /* this is our buffer */
  1737. (void)buf;
  1738. /*
  1739. * So if the URL was A://B/C,
  1740. * conn->protostr is A
  1741. * conn->gname is B
  1742. * conn->path is /C
  1743. */
  1744. /*************************************************************
  1745. * Take care of proxy authentication stuff
  1746. *************************************************************/
  1747. if(conn->bits.proxy_user_passwd) {
  1748. data->state.proxyuser[0] =0;
  1749. data->state.proxypasswd[0]=0;
  1750. if(*data->set.proxyuserpwd != ':') {
  1751. /* the name is given, get user+password */
  1752. sscanf(data->set.proxyuserpwd, "%127[^:]:%127[^\n]",
  1753. data->state.proxyuser, data->state.proxypasswd);
  1754. }
  1755. else
  1756. /* no name given, get the password only */
  1757. sscanf(data->set.proxyuserpwd+1, "%127[^\n]", data->state.proxypasswd);
  1758. /* check for password, if no ask for one */
  1759. if( !data->state.proxypasswd[0] ) {
  1760. if(data->set.fpasswd( data->set.passwd_client,
  1761. "proxy password:",
  1762. data->state.proxypasswd,
  1763. sizeof(data->state.proxypasswd))) {
  1764. failf(data, "Bad password from password callback");
  1765. return CURLE_BAD_PASSWORD_ENTERED;
  1766. }
  1767. }
  1768. }
  1769. /*************************************************************
  1770. * Set a few convenience pointers
  1771. *************************************************************/
  1772. conn->name = conn->gname;
  1773. conn->ppath = conn->path;
  1774. conn->hostname = conn->name;
  1775. /*************************************************************
  1776. * Detect what (if any) proxy to use
  1777. *************************************************************/
  1778. if(!data->change.proxy) {
  1779. /* If proxy was not specified, we check for default proxy environment
  1780. * variables, to enable i.e Lynx compliance:
  1781. *
  1782. * http_proxy=http://some.server.dom:port/
  1783. * https_proxy=http://some.server.dom:port/
  1784. * ftp_proxy=http://some.server.dom:port/
  1785. * gopher_proxy=http://some.server.dom:port/
  1786. * no_proxy=domain1.dom,host.domain2.dom
  1787. * (a comma-separated list of hosts which should
  1788. * not be proxied, or an asterisk to override
  1789. * all proxy variables)
  1790. * all_proxy=http://some.server.dom:port/
  1791. * (seems to exist for the CERN www lib. Probably
  1792. * the first to check for.)
  1793. *
  1794. * For compatibility, the all-uppercase versions of these variables are
  1795. * checked if the lowercase versions don't exist.
  1796. */
  1797. char *no_proxy=NULL;
  1798. char *no_proxy_tok_buf;
  1799. char *proxy=NULL;
  1800. char proxy_env[128];
  1801. no_proxy=curl_getenv("no_proxy");
  1802. if(!no_proxy)
  1803. no_proxy=curl_getenv("NO_PROXY");
  1804. if(!no_proxy || !strequal("*", no_proxy)) {
  1805. /* NO_PROXY wasn't specified or it wasn't just an asterisk */
  1806. char *nope;
  1807. nope=no_proxy?strtok_r(no_proxy, ", ", &no_proxy_tok_buf):NULL;
  1808. while(nope) {
  1809. unsigned int namelen;
  1810. char *endptr = strchr(conn->name, ':');
  1811. if(endptr)
  1812. namelen=(unsigned int)(endptr-conn->name);
  1813. else
  1814. namelen=(unsigned int)strlen(conn->name);
  1815. if(strlen(nope) <= namelen) {
  1816. char *checkn=
  1817. conn->name + namelen - strlen(nope);
  1818. if(checkprefix(nope, checkn)) {
  1819. /* no proxy for this host! */
  1820. break;
  1821. }
  1822. }
  1823. nope=strtok_r(NULL, ", ", &no_proxy_tok_buf);
  1824. }
  1825. if(!nope) {
  1826. /* It was not listed as without proxy */
  1827. char *protop = conn->protostr;
  1828. char *envp = proxy_env;
  1829. char *prox;
  1830. /* Now, build <protocol>_proxy and check for such a one to use */
  1831. while(*protop)
  1832. *envp++ = (char)tolower((int)*protop++);
  1833. /* append _proxy */
  1834. strcpy(envp, "_proxy");
  1835. /* read the protocol proxy: */
  1836. prox=curl_getenv(proxy_env);
  1837. /*
  1838. * We don't try the uppercase version of HTTP_PROXY because of
  1839. * security reasons:
  1840. *
  1841. * When curl is used in a webserver application
  1842. * environment (cgi or php), this environment variable can
  1843. * be controlled by the web server user by setting the
  1844. * http header 'Proxy:' to some value.
  1845. *
  1846. * This can cause 'internal' http/ftp requests to be
  1847. * arbitrarily redirected by any external attacker.
  1848. */
  1849. if(!prox && !strequal("http_proxy", proxy_env)) {
  1850. /* There was no lowercase variable, try the uppercase version: */
  1851. for(envp = proxy_env; *envp; envp++)
  1852. *envp = (char)toupper((int)*envp);
  1853. prox=curl_getenv(proxy_env);
  1854. }
  1855. if(prox && *prox) { /* don't count "" strings */
  1856. proxy = prox; /* use this */
  1857. }
  1858. else {
  1859. proxy = curl_getenv("all_proxy"); /* default proxy to use */
  1860. if(!proxy)
  1861. proxy=curl_getenv("ALL_PROXY");
  1862. }
  1863. if(proxy && *proxy) {
  1864. /* we have a proxy here to set */
  1865. data->change.proxy = proxy;
  1866. data->change.proxy_alloc=TRUE; /* this needs to be freed later */
  1867. conn->bits.httpproxy = TRUE;
  1868. }
  1869. } /* if (!nope) - it wasn't specified non-proxy */
  1870. } /* NO_PROXY wasn't specified or '*' */
  1871. if(no_proxy)
  1872. free(no_proxy);
  1873. } /* if not using proxy */
  1874. /*************************************************************
  1875. * No protocol part in URL was used, add it!
  1876. *************************************************************/
  1877. if(conn->protocol&PROT_MISSING) {
  1878. /* We're guessing prefixes here and if we're told to use a proxy or if
  1879. we're gonna follow a Location: later or... then we need the protocol
  1880. part added so that we have a valid URL. */
  1881. char *reurl;
  1882. reurl = aprintf("%s://%s", conn->protostr, data->change.url);
  1883. if(!reurl)
  1884. return CURLE_OUT_OF_MEMORY;
  1885. data->change.url = reurl;
  1886. data->change.url_alloc = TRUE; /* free this later */
  1887. conn->protocol &= ~PROT_MISSING; /* switch that one off again */
  1888. }
  1889. #ifndef CURL_DISABLE_HTTP
  1890. /************************************************************
  1891. * RESUME on a HTTP page is a tricky business. First, let's just check that
  1892. * 'range' isn't used, then set the range parameter and leave the resume as
  1893. * it is to inform about this situation for later use. We will then
  1894. * "attempt" to resume, and if we're talking to a HTTP/1.1 (or later)
  1895. * server, we will get the document resumed. If we talk to a HTTP/1.0
  1896. * server, we just fail since we can't rewind the file writing from within
  1897. * this function.
  1898. ***********************************************************/
  1899. if(conn->resume_from) {
  1900. if(!conn->bits.use_range) {
  1901. /* if it already was in use, we just skip this */
  1902. snprintf(resumerange, sizeof(resumerange), "%d-", conn->resume_from);
  1903. conn->range=strdup(resumerange); /* tell ourselves to fetch this range */
  1904. conn->bits.rangestringalloc = TRUE; /* mark as allocated */
  1905. conn->bits.use_range = 1; /* switch on range usage */
  1906. }
  1907. }
  1908. #endif
  1909. /*************************************************************
  1910. * Setup internals depending on protocol
  1911. *************************************************************/
  1912. if (strequal(conn->protostr, "HTTP")) {
  1913. #ifndef CURL_DISABLE_HTTP
  1914. conn->port = (data->set.use_port && data->state.allow_port)?
  1915. data->set.use_port:PORT_HTTP;
  1916. conn->remote_port = PORT_HTTP;
  1917. conn->protocol |= PROT_HTTP;
  1918. conn->curl_do = Curl_http;
  1919. conn->curl_do_more = NULL;
  1920. conn->curl_done = Curl_http_done;
  1921. conn->curl_connect = Curl_http_connect;
  1922. #else
  1923. failf(data, LIBCURL_NAME
  1924. " was built with HTTP disabled, http: not supported!");
  1925. return CURLE_UNSUPPORTED_PROTOCOL;
  1926. #endif
  1927. }
  1928. else if (strequal(conn->protostr, "HTTPS")) {
  1929. #if defined(USE_SSLEAY) && !defined(CURL_DISABLE_HTTP)
  1930. conn->port = (data->set.use_port && data->state.allow_port)?
  1931. data->set.use_port:PORT_HTTPS;
  1932. conn->remote_port = PORT_HTTPS;
  1933. conn->protocol |= PROT_HTTP|PROT_HTTPS|PROT_SSL;
  1934. conn->curl_do = Curl_http;
  1935. conn->curl_do_more = NULL;
  1936. conn->curl_done = Curl_http_done;
  1937. conn->curl_connect = Curl_http_connect;
  1938. #else /* USE_SSLEAY */
  1939. failf(data, LIBCURL_NAME
  1940. " was built with SSL disabled, https: not supported!");
  1941. return CURLE_UNSUPPORTED_PROTOCOL;
  1942. #endif /* !USE_SSLEAY */
  1943. }
  1944. else if (strequal(conn->protostr, "GOPHER")) {
  1945. #ifndef CURL_DISABLE_GOPHER
  1946. conn->port = (data->set.use_port && data->state.allow_port)?
  1947. data->set.use_port:PORT_GOPHER;
  1948. conn->remote_port = PORT_GOPHER;
  1949. /* Skip /<item-type>/ in path if present */
  1950. if (isdigit((int)conn->path[1])) {
  1951. conn->ppath = strchr(&conn->path[1], '/');
  1952. if (conn->ppath == NULL)
  1953. conn->ppath = conn->path;
  1954. }
  1955. conn->protocol |= PROT_GOPHER;
  1956. conn->curl_do = Curl_http;
  1957. conn->curl_do_more = NULL;
  1958. conn->curl_done = Curl_http_done;
  1959. #else
  1960. failf(data, LIBCURL_NAME
  1961. " was built with GOPHER disabled, gopher: not supported!");
  1962. #endif
  1963. }
  1964. else if(strequal(conn->protostr, "FTP") ||
  1965. strequal(conn->protostr, "FTPS")) {
  1966. /* MN 06/07/02 */
  1967. #ifndef CURL_DISABLE_FTP
  1968. char *type;
  1969. if(strequal(conn->protostr, "FTPS")) {
  1970. #ifdef USE_SSLEAY
  1971. conn->protocol |= PROT_FTPS|PROT_SSL;
  1972. #else
  1973. failf(data, LIBCURL_NAME
  1974. " was built with SSL disabled, ftps: not supported!");
  1975. return CURLE_UNSUPPORTED_PROTOCOL;
  1976. #endif /* !USE_SSLEAY */
  1977. }
  1978. conn->port = (data->set.use_port && data->state.allow_port)?
  1979. data->set.use_port:PORT_FTP;
  1980. conn->remote_port = PORT_FTP;
  1981. conn->protocol |= PROT_FTP;
  1982. if(data->change.proxy &&
  1983. *data->change.proxy &&
  1984. !data->set.tunnel_thru_httpproxy) {
  1985. /* Unless we have asked to tunnel ftp operations through the proxy, we
  1986. switch and use HTTP operations only */
  1987. if(conn->protocol & PROT_FTPS) {
  1988. /* FTPS is a hacked protocol and does not work through your
  1989. ordinary http proxy! */
  1990. failf(data, "ftps does not work through http proxy!");
  1991. return CURLE_UNSUPPORTED_PROTOCOL;
  1992. }
  1993. #ifndef CURL_DISABLE_HTTP
  1994. conn->curl_do = Curl_http;
  1995. conn->curl_done = Curl_http_done;
  1996. #else
  1997. failf(data, "FTP over http proxy requires HTTP support built-in!");
  1998. return CURLE_UNSUPPORTED_PROTOCOL;
  1999. #endif
  2000. }
  2001. else {
  2002. conn->curl_do = Curl_ftp;
  2003. conn->curl_do_more = Curl_ftp_nextconnect;
  2004. conn->curl_done = Curl_ftp_done;
  2005. conn->curl_connect = Curl_ftp_connect;
  2006. conn->curl_disconnect = Curl_ftp_disconnect;
  2007. }
  2008. conn->ppath++; /* don't include the initial slash */
  2009. /* FTP URLs support an extension like ";type=<typecode>" that
  2010. * we'll try to get now! */
  2011. type=strstr(conn->ppath, ";type=");
  2012. if(!type) {
  2013. type=strstr(conn->gname, ";type=");
  2014. }
  2015. if(type) {
  2016. char command;
  2017. *type=0; /* it was in the middle of the hostname */
  2018. command = (char)toupper((int)type[6]);
  2019. switch(command) {
  2020. case 'A': /* ASCII mode */
  2021. data->set.ftp_ascii = 1;
  2022. break;
  2023. case 'D': /* directory mode */
  2024. data->set.ftp_list_only = 1;
  2025. break;
  2026. case 'I': /* binary mode */
  2027. default:
  2028. /* switch off ASCII */
  2029. data->set.ftp_ascii = 0;
  2030. break;
  2031. }
  2032. }
  2033. /* MN 06/07/02 */
  2034. #else /* CURL_DISABLE_FTP */
  2035. failf(data, LIBCURL_NAME
  2036. " was built with FTP disabled, ftp/ftps: not supported!");
  2037. return CURLE_UNSUPPORTED_PROTOCOL;
  2038. #endif
  2039. }
  2040. else if(strequal(conn->protostr, "TELNET")) {
  2041. #ifndef CURL_DISABLE_TELNET
  2042. /* telnet testing factory */
  2043. conn->protocol |= PROT_TELNET;
  2044. conn->port = (data->set.use_port && data->state.allow_port)?
  2045. data->set.use_port: PORT_TELNET;
  2046. conn->remote_port = PORT_TELNET;
  2047. conn->curl_do = Curl_telnet;
  2048. conn->curl_done = Curl_telnet_done;
  2049. #else
  2050. failf(data, LIBCURL_NAME
  2051. " was built with TELNET disabled!");
  2052. #endif
  2053. }
  2054. else if (strequal(conn->protostr, "DICT")) {
  2055. #ifndef CURL_DISABLE_DICT
  2056. conn->protocol |= PROT_DICT;
  2057. conn->port = (data->set.use_port && data->state.allow_port)?
  2058. data->set.use_port:PORT_DICT;
  2059. conn->remote_port = PORT_DICT;
  2060. conn->curl_do = Curl_dict;
  2061. conn->curl_done = NULL; /* no DICT-specific done */
  2062. #else
  2063. failf(data, LIBCURL_NAME
  2064. " was built with DICT disabled!");
  2065. #endif
  2066. }
  2067. else if (strequal(conn->protostr, "LDAP")) {
  2068. #ifndef CURL_DISABLE_LDAP
  2069. conn->protocol |= PROT_LDAP;
  2070. conn->port = (data->set.use_port && data->state.allow_port)?
  2071. data->set.use_port:PORT_LDAP;
  2072. conn->remote_port = PORT_LDAP;
  2073. conn->curl_do = Curl_ldap;
  2074. conn->curl_done = NULL; /* no LDAP-specific done */
  2075. #else
  2076. failf(data, LIBCURL_NAME
  2077. " was built with LDAP disabled!");
  2078. #endif
  2079. }
  2080. else if (strequal(conn->protostr, "FILE")) {
  2081. #ifndef CURL_DISABLE_FILE
  2082. conn->protocol |= PROT_FILE;
  2083. conn->curl_do = Curl_file;
  2084. /* no done() function */
  2085. /* anyway, this is supposed to be the connect function so we better
  2086. at least check that the file is present here! */
  2087. result = Curl_file_connect(conn);
  2088. /* Setup a "faked" transfer that'll do nothing */
  2089. if(CURLE_OK == result) {
  2090. conn->bits.tcpconnect = TRUE; /* we are "connected */
  2091. result = Curl_Transfer(conn, -1, -1, FALSE, NULL, /* no download */
  2092. -1, NULL); /* no upload */
  2093. }
  2094. return result;
  2095. #else
  2096. failf(data, LIBCURL_NAME
  2097. " was built with FILE disabled!");
  2098. #endif
  2099. }
  2100. else {
  2101. /* We fell through all checks and thus we don't support the specified
  2102. protocol */
  2103. failf(data, "Unsupported protocol: %s", conn->protostr);
  2104. return CURLE_UNSUPPORTED_PROTOCOL;
  2105. }
  2106. /*************************************************************
  2107. * Figure out the remote port number
  2108. *
  2109. * No matter if we use a proxy or not, we have to figure out the remote
  2110. * port number of various reasons.
  2111. *
  2112. * To be able to detect port number flawlessly, we must not confuse them
  2113. * IPv6-specified addresses in the [0::1] style. (RFC2732)
  2114. *
  2115. * The conn->name is currently [user:passwd@]host[:port] where host could
  2116. * be a hostname, IPv4 address or IPv6 address.
  2117. *************************************************************/
  2118. if((1 == sscanf(conn->name, "[%*39[0-9a-fA-F:.]%c", &endbracket)) &&
  2119. (']' == endbracket)) {
  2120. /* this is a RFC2732-style specified IP-address */
  2121. conn->bits.ipv6_ip = TRUE;
  2122. conn->name++; /* pass the starting bracket */
  2123. conn->hostname++;
  2124. tmp = strchr(conn->name, ']');
  2125. *tmp = 0; /* zero terminate */
  2126. tmp++; /* pass the ending bracket */
  2127. if(':' != *tmp)
  2128. tmp = NULL; /* no port number available */
  2129. }
  2130. else
  2131. tmp = strrchr(conn->name, ':');
  2132. if (tmp) {
  2133. char *rest;
  2134. unsigned long port;
  2135. port=strtoul(tmp+1, &rest, 10); /* Port number must be decimal */
  2136. if (rest != (tmp+1) && *rest == '\0') {
  2137. /* The colon really did have only digits after it,
  2138. * so it is either a port number or a mistake */
  2139. if (port > 0xffff) { /* Single unix standard says port numbers are
  2140. * 16 bits long */
  2141. failf(data, "Port number too large: %lu", port);
  2142. return CURLE_URL_MALFORMAT;
  2143. }
  2144. *tmp = '\0'; /* cut off the name there */
  2145. conn->remote_port = (unsigned short)port;
  2146. }
  2147. }
  2148. if(data->change.proxy && *data->change.proxy) {
  2149. /* If this is supposed to use a proxy, we need to figure out the proxy
  2150. host name name, so that we can re-use an existing connection
  2151. that may exist registered to the same proxy host. */
  2152. char *prox_portno;
  2153. char *endofprot;
  2154. /* We need to make a duplicate of the proxy so that we can modify the
  2155. string safely. */
  2156. char *proxydup=strdup(data->change.proxy);
  2157. /* We use 'proxyptr' to point to the proxy name from now on... */
  2158. char *proxyptr=proxydup;
  2159. if(NULL == proxydup) {
  2160. failf(data, "memory shortage");
  2161. return CURLE_OUT_OF_MEMORY;
  2162. }
  2163. /* Daniel Dec 10, 1998:
  2164. We do the proxy host string parsing here. We want the host name and the
  2165. port name. Accept a protocol:// prefix, even though it should just be
  2166. ignored. */
  2167. /* 1. skip the protocol part if present */
  2168. endofprot=strstr(proxyptr, "://");
  2169. if(endofprot) {
  2170. proxyptr = endofprot+3;
  2171. }
  2172. /* allow user to specify proxy.server.com:1080 if desired */
  2173. prox_portno = strchr (proxyptr, ':');
  2174. if (prox_portno) {
  2175. *prox_portno = 0x0; /* cut off number from host name */
  2176. prox_portno ++;
  2177. /* now set the local port number */
  2178. conn->port = atoi(prox_portno);
  2179. }
  2180. else if(data->set.proxyport) {
  2181. /* None given in the proxy string, then get the default one if it is
  2182. given */
  2183. conn->port = data->set.proxyport;
  2184. }
  2185. /* now, clone the cleaned proxy host name */
  2186. conn->proxyhost = strdup(proxyptr);
  2187. free(proxydup); /* free the duplicate pointer and not the modified */
  2188. }
  2189. /*************************************************************
  2190. * Take care of user and password authentication stuff
  2191. *************************************************************/
  2192. /*
  2193. * Inputs: data->set.userpwd (CURLOPT_USERPWD)
  2194. * data->set.fpasswd (CURLOPT_PASSWDFUNCTION)
  2195. * data->set.use_netrc (CURLOPT_NETRC)
  2196. * conn->hostname
  2197. * netrc file
  2198. * hard-coded defaults
  2199. *
  2200. * Outputs: (almost :- all currently undefined)
  2201. * conn->bits.user_passwd - non-zero if non-default passwords exist
  2202. * conn->state.user - non-zero length if defined
  2203. * conn->state.passwd - ditto
  2204. * conn->hostname - remove user name and password
  2205. */
  2206. /* At this point, we're hoping all the other special cases have
  2207. * been taken care of, so conn->hostname is at most
  2208. * [user[:password]]@]hostname
  2209. *
  2210. * We need somewhere to put the embedded details, so do that first.
  2211. */
  2212. data->state.user[0] =0; /* to make everything well-defined */
  2213. data->state.passwd[0]=0;
  2214. if (conn->protocol & (PROT_FTP|PROT_HTTP)) {
  2215. /* This is a FTP or HTTP URL, we will now try to extract the possible
  2216. * user+password pair in a string like:
  2217. * ftp://user:[email protected]:8021/README */
  2218. char *ptr=strchr(conn->name, '@');
  2219. char *userpass = conn->name;
  2220. if(ptr != NULL) {
  2221. /* there's a user+password given here, to the left of the @ */
  2222. conn->name = conn->hostname = ++ptr;
  2223. /* So the hostname is sane. Only bother interpreting the
  2224. * results if we could care. It could still be wasted
  2225. * work because it might be overtaken by the programmatically
  2226. * set user/passwd, but doing that first adds more cases here :-(
  2227. */
  2228. if (data->set.use_netrc != CURL_NETRC_REQUIRED) {
  2229. /* We could use the one in the URL */
  2230. conn->bits.user_passwd = 1; /* enable user+password */
  2231. if(*userpass != ':') {
  2232. /* the name is given, get user+password */
  2233. sscanf(userpass, "%127[^:@]:%127[^@]",
  2234. data->state.user, data->state.passwd);
  2235. }
  2236. else
  2237. /* no name given, get the password only */
  2238. sscanf(userpass, ":%127[^@]", data->state.passwd);
  2239. /* we have set the password */
  2240. data->state.passwdgiven = TRUE;
  2241. if(data->state.user[0]) {
  2242. char *newname=curl_unescape(data->state.user, 0);
  2243. if(strlen(newname) < sizeof(data->state.user)) {
  2244. strcpy(data->state.user, newname);
  2245. }
  2246. /* if the new name is longer than accepted, then just use
  2247. the unconverted name, it'll be wrong but what the heck */
  2248. free(newname);
  2249. }
  2250. if (data->state.passwd[0]) {
  2251. /* we have a password found in the URL, decode it! */
  2252. char *newpasswd=curl_unescape(data->state.passwd, 0);
  2253. if(strlen(newpasswd) < sizeof(data->state.passwd)) {
  2254. strcpy(data->state.passwd, newpasswd);
  2255. }
  2256. free(newpasswd);
  2257. }
  2258. }
  2259. }
  2260. }
  2261. /* Programmatically set password:
  2262. * - always applies, if available
  2263. * - takes precedence over the values we just set above
  2264. * so scribble it over the top.
  2265. * User-supplied passwords are assumed not to need unescaping.
  2266. *
  2267. * user_password is set in "inherite initial knowledge' above,
  2268. * so it doesn't have to be set in this block
  2269. */
  2270. if (data->set.userpwd != NULL) {
  2271. if(*data->set.userpwd != ':') {
  2272. /* the name is given, get user+password */
  2273. sscanf(data->set.userpwd, "%127[^:]:%127[^\n]",
  2274. data->state.user, data->state.passwd);
  2275. if(strchr(data->set.userpwd, ':'))
  2276. /* a colon means the password was given, even if blank */
  2277. data->state.passwdgiven = TRUE;
  2278. }
  2279. else
  2280. /* no name given, starts with a colon, get the password only */
  2281. sscanf(data->set.userpwd+1, "%127[^\n]", data->state.passwd);
  2282. }
  2283. if (data->set.use_netrc != CURL_NETRC_IGNORED &&
  2284. !data->state.passwdgiven) { /* need passwd */
  2285. if(Curl_parsenetrc(conn->hostname,
  2286. data->state.user,
  2287. data->state.passwd)) {
  2288. infof(data, "Couldn't find host %s in the .netrc file, using defaults",
  2289. conn->hostname);
  2290. } else
  2291. conn->bits.user_passwd = 1; /* enable user+password */
  2292. }
  2293. /* if we have a user but no password, ask for one */
  2294. if(conn->bits.user_passwd && !data->state.passwdgiven ) {
  2295. if(data->set.fpasswd(data->set.passwd_client,
  2296. "password:", data->state.passwd,
  2297. sizeof(data->state.passwd)))
  2298. return CURLE_BAD_PASSWORD_ENTERED;
  2299. }
  2300. /* So we could have a password but no user; that's just too bad. */
  2301. /* If our protocol needs a password and we have none, use the defaults */
  2302. if ( (conn->protocol & (PROT_FTP|PROT_HTTP)) &&
  2303. !conn->bits.user_passwd &&
  2304. !data->state.passwdgiven) {
  2305. strcpy(data->state.user, CURL_DEFAULT_USER);
  2306. strcpy(data->state.passwd, CURL_DEFAULT_PASSWORD);
  2307. /* This is the default password, so DON'T set conn->bits.user_passwd */
  2308. }
  2309. /*************************************************************
  2310. * Check the current list of connections to see if we can
  2311. * re-use an already existing one or if we have to create a
  2312. * new one.
  2313. *************************************************************/
  2314. /* reuse_fresh is set TRUE if we are told to use a fresh connection
  2315. by force */
  2316. if(!data->set.reuse_fresh &&
  2317. ConnectionExists(data, conn, &conn_temp)) {
  2318. /*
  2319. * We already have a connection for this, we got the former connection
  2320. * in the conn_temp variable and thus we need to cleanup the one we
  2321. * just allocated before we can move along and use the previously
  2322. * existing one.
  2323. */
  2324. struct connectdata *old_conn = conn;
  2325. char *path = old_conn->path; /* setup the current path pointer properly */
  2326. char *ppath = old_conn->ppath; /* this is the modified path pointer */
  2327. if(old_conn->proxyhost)
  2328. free(old_conn->proxyhost);
  2329. conn = conn_temp; /* use this connection from now on */
  2330. /* If we speak over a proxy, we need to copy the host name too, as it
  2331. might be another remote host even when re-using a connection */
  2332. strcpy(conn->gname, old_conn->gname); /* safe strcpy() */
  2333. /* we need these pointers if we speak over a proxy */
  2334. conn->hostname = conn->gname;
  2335. conn->name = &conn->gname[old_conn->name - old_conn->gname];
  2336. free(conn->path); /* free the previously allocated path pointer */
  2337. /* 'path' points to the allocated data, 'ppath' may have been advanced
  2338. to point somewhere within the 'path' area. */
  2339. conn->path = path;
  2340. conn->ppath = ppath;
  2341. /* re-use init */
  2342. conn->bits.reuse = TRUE; /* yes, we're re-using here */
  2343. conn->bits.chunk = FALSE; /* always assume not chunked unless told
  2344. otherwise */
  2345. conn->maxdownload = -1; /* might have been used previously! */
  2346. free(old_conn); /* we don't need this anymore */
  2347. /*
  2348. * If we're doing a resumed transfer, we need to setup our stuff
  2349. * properly.
  2350. */
  2351. conn->resume_from = data->set.set_resume_from;
  2352. if (conn->resume_from) {
  2353. snprintf(resumerange, sizeof(resumerange), "%d-", conn->resume_from);
  2354. if (conn->bits.rangestringalloc == TRUE)
  2355. free(conn->range);
  2356. /* tell ourselves to fetch this range */
  2357. conn->range = strdup(resumerange);
  2358. conn->bits.use_range = TRUE; /* enable range download */
  2359. conn->bits.rangestringalloc = TRUE; /* mark range string allocated */
  2360. }
  2361. else if (data->set.set_range) {
  2362. /* There is a range, but is not a resume, useful for random ftp access */
  2363. conn->range = strdup(data->set.set_range);
  2364. conn->bits.rangestringalloc = TRUE; /* mark range string allocated */
  2365. conn->bits.use_range = TRUE; /* enable range download */
  2366. }
  2367. *in_connect = conn; /* return this instead! */
  2368. infof(data, "Re-using existing connection! (#%d)\n", conn->connectindex);
  2369. }
  2370. else {
  2371. /*
  2372. * This is a brand new connection, so let's store it in the connection
  2373. * cache of ours!
  2374. */
  2375. ConnectionStore(data, conn);
  2376. }
  2377. /*************************************************************
  2378. * Set timeout if that is being used
  2379. *************************************************************/
  2380. if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
  2381. /*************************************************************
  2382. * Set signal handler to catch SIGALRM
  2383. * Store the old value to be able to set it back later!
  2384. *************************************************************/
  2385. #ifdef HAVE_SIGACTION
  2386. struct sigaction sigact;
  2387. sigaction(SIGALRM, NULL, &sigact);
  2388. keep_sigact = sigact;
  2389. keep_copysig = TRUE; /* yes, we have a copy */
  2390. sigact.sa_handler = alarmfunc;
  2391. #ifdef SA_RESTART
  2392. /* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
  2393. sigact.sa_flags &= ~SA_RESTART;
  2394. #endif
  2395. /* now set the new struct */
  2396. sigaction(SIGALRM, &sigact, NULL);
  2397. #else
  2398. /* no sigaction(), revert to the much lamer signal() */
  2399. #ifdef HAVE_SIGNAL
  2400. keep_sigact = signal(SIGALRM, alarmfunc);
  2401. #endif
  2402. #endif
  2403. /* We set the timeout on the name resolving phase first, separately from
  2404. * the download/upload part to allow a maximum time on everything. This is
  2405. * a signal-based timeout, why it won't work and shouldn't be used in
  2406. * multi-threaded environments. */
  2407. #ifdef HAVE_ALARM
  2408. /* alarm() makes a signal get sent when the timeout fires off, and that
  2409. will abort system calls */
  2410. prev_alarm = alarm(data->set.connecttimeout?
  2411. data->set.connecttimeout:
  2412. data->set.timeout);
  2413. /* We can expect the conn->created time to be "now", as that was just
  2414. recently set in the beginning of this function and nothing slow
  2415. has been done since then until now. */
  2416. #endif
  2417. }
  2418. /*************************************************************
  2419. * Resolve the name of the server or proxy
  2420. *************************************************************/
  2421. if(conn->bits.reuse) {
  2422. /* re-used connection, no resolving is necessary */
  2423. hostaddr = NULL;
  2424. }
  2425. else if(!data->change.proxy || !*data->change.proxy) {
  2426. /* If not connecting via a proxy, extract the port from the URL, if it is
  2427. * there, thus overriding any defaults that might have been set above. */
  2428. conn->port = conn->remote_port; /* it is the same port */
  2429. /* Resolve target host right on */
  2430. hostaddr = Curl_resolv(data, conn->name, conn->port);
  2431. if(!hostaddr) {
  2432. failf(data, "Couldn't resolve host '%s'", conn->name);
  2433. result = CURLE_COULDNT_RESOLVE_HOST;
  2434. /* don't return yet, we need to clean up the timeout first */
  2435. }
  2436. }
  2437. else {
  2438. /* This is a proxy that hasn't been resolved yet. */
  2439. /* resolve proxy */
  2440. hostaddr = Curl_resolv(data, conn->proxyhost, conn->port);
  2441. if(!hostaddr) {
  2442. failf(data, "Couldn't resolve proxy '%s'", conn->proxyhost);
  2443. result = CURLE_COULDNT_RESOLVE_PROXY;
  2444. /* don't return yet, we need to clean up the timeout first */
  2445. }
  2446. }
  2447. Curl_pgrsTime(data, TIMER_NAMELOOKUP);
  2448. #ifdef HAVE_ALARM
  2449. if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
  2450. #ifdef HAVE_SIGACTION
  2451. if(keep_copysig) {
  2452. /* we got a struct as it looked before, now put that one back nice
  2453. and clean */
  2454. sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
  2455. }
  2456. #else
  2457. #ifdef HAVE_SIGNAL
  2458. /* restore the previous SIGALRM handler */
  2459. signal(SIGALRM, keep_sigact);
  2460. #endif
  2461. #endif
  2462. /* switch back the alarm() to either zero or to what it was before minus
  2463. the time we spent until now! */
  2464. if(prev_alarm) {
  2465. /* there was an alarm() set before us, now put it back */
  2466. long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
  2467. long alarm_set;
  2468. /* the alarm period is counted in even number of seconds */
  2469. alarm_set = prev_alarm - elapsed_ms/1000;
  2470. if(alarm_set<=0) {
  2471. /* if it turned negative, we should fire off a SIGALRM here, but we
  2472. won't, and zero would be to switch it off so we never set it to
  2473. less than 1! */
  2474. alarm(1);
  2475. result = CURLE_OPERATION_TIMEOUTED;
  2476. failf(data, "Previous alarm fired off!");
  2477. }
  2478. else
  2479. alarm(alarm_set);
  2480. }
  2481. else
  2482. alarm(0); /* just shut it off */
  2483. }
  2484. #endif
  2485. if(result)
  2486. return result;
  2487. /*************************************************************
  2488. * Proxy authentication
  2489. *************************************************************/
  2490. if(conn->bits.proxy_user_passwd) {
  2491. char *authorization;
  2492. snprintf(data->state.buffer, BUFSIZE, "%s:%s",
  2493. data->state.proxyuser, data->state.proxypasswd);
  2494. if(Curl_base64_encode(data->state.buffer, (int)strlen(data->state.buffer),
  2495. &authorization) >= 0) {
  2496. if(conn->allocptr.proxyuserpwd)
  2497. free(conn->allocptr.proxyuserpwd);
  2498. conn->allocptr.proxyuserpwd =
  2499. aprintf("Proxy-authorization: Basic %s\015\012", authorization);
  2500. free(authorization);
  2501. }
  2502. }
  2503. /*************************************************************
  2504. * Send user-agent to HTTP proxies even if the target protocol
  2505. * isn't HTTP.
  2506. *************************************************************/
  2507. if((conn->protocol&PROT_HTTP) ||
  2508. (data->change.proxy && *data->change.proxy)) {
  2509. if(data->set.useragent) {
  2510. if(conn->allocptr.uagent)
  2511. free(conn->allocptr.uagent);
  2512. conn->allocptr.uagent =
  2513. aprintf("User-Agent: %s\015\012", data->set.useragent);
  2514. }
  2515. }
  2516. if(data->set.encoding) {
  2517. if(conn->allocptr.accept_encoding)
  2518. free(conn->allocptr.accept_encoding);
  2519. conn->allocptr.accept_encoding =
  2520. aprintf("Accept-Encoding: %s\015\012", data->set.encoding);
  2521. }
  2522. conn->bytecount = 0;
  2523. conn->headerbytecount = 0;
  2524. if(-1 == conn->firstsocket) {
  2525. bool connected;
  2526. /* Connect only if not already connected! */
  2527. result = ConnectPlease(conn, hostaddr, &connected);
  2528. if(connected) {
  2529. result = Curl_protocol_connect(conn, hostaddr);
  2530. if(CURLE_OK == result)
  2531. conn->bits.tcpconnect = TRUE;
  2532. }
  2533. else
  2534. conn->bits.tcpconnect = FALSE;
  2535. if(CURLE_OK != result)
  2536. return result;
  2537. }
  2538. else {
  2539. Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
  2540. conn->bits.tcpconnect = TRUE;
  2541. if(data->set.verbose)
  2542. verboseconnect(conn, hostaddr);
  2543. }
  2544. conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
  2545. set this here perhaps a second time */
  2546. #ifdef __EMX__
  2547. /* 20000330 mgs
  2548. * the check is quite a hack...
  2549. * we're calling _fsetmode to fix the problem with fwrite converting newline
  2550. * characters (you get mangled text files, and corrupted binary files when
  2551. * you download to stdout and redirect it to a file). */
  2552. if ((data->set.out)->_handle == NULL) {
  2553. _fsetmode(stdout, "b");
  2554. }
  2555. #endif
  2556. return CURLE_OK;
  2557. }
  2558. CURLcode Curl_connect(struct SessionHandle *data,
  2559. struct connectdata **in_connect)
  2560. {
  2561. CURLcode code;
  2562. struct connectdata *conn;
  2563. /* call the stuff that needs to be called */
  2564. code = CreateConnection(data, in_connect);
  2565. if(CURLE_OK != code) {
  2566. /* We're not allowed to return failure with memory left allocated
  2567. in the connectdata struct, free those here */
  2568. conn = (struct connectdata *)*in_connect;
  2569. if(conn) {
  2570. Curl_disconnect(conn); /* close the connection */
  2571. *in_connect = NULL; /* return a NULL */
  2572. }
  2573. }
  2574. return code;
  2575. }
  2576. CURLcode Curl_done(struct connectdata *conn)
  2577. {
  2578. struct SessionHandle *data=conn->data;
  2579. CURLcode result;
  2580. /* cleanups done even if the connection is re-used */
  2581. if(conn->bits.rangestringalloc) {
  2582. free(conn->range);
  2583. conn->bits.rangestringalloc = FALSE;
  2584. }
  2585. /* Cleanup possible redirect junk */
  2586. if(conn->newurl) {
  2587. free(conn->newurl);
  2588. conn->newurl = NULL;
  2589. }
  2590. if(conn->connect_addr)
  2591. Curl_resolv_unlock(conn->connect_addr); /* done with this */
  2592. #if defined(MALLOCDEBUG) && defined(AGGRESIVE_TEST)
  2593. /* scan for DNS cache entries still marked as in use */
  2594. Curl_hash_apply(data->hostcache,
  2595. NULL, Curl_scan_cache_used);
  2596. #endif
  2597. /* this calls the protocol-specific function pointer previously set */
  2598. if(conn->curl_done)
  2599. result = conn->curl_done(conn);
  2600. else
  2601. result = CURLE_OK;
  2602. Curl_pgrsDone(conn); /* done with the operation */
  2603. /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
  2604. forced us to close this no matter what we think.
  2605. if conn->bits.close is TRUE, it means that the connection should be
  2606. closed in spite of all our efforts to be nice, due to protocol
  2607. restrictions in our or the server's end */
  2608. if(data->set.reuse_forbid ||
  2609. ((CURLE_OK == result) && conn->bits.close))
  2610. result = Curl_disconnect(conn); /* close the connection */
  2611. else
  2612. infof(data, "Connection #%d left intact\n", conn->connectindex);
  2613. return result;
  2614. }
  2615. CURLcode Curl_do(struct connectdata **connp)
  2616. {
  2617. CURLcode result=CURLE_OK;
  2618. struct connectdata *conn = *connp;
  2619. struct SessionHandle *data=conn->data;
  2620. conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
  2621. if(conn->curl_do) {
  2622. /* generic protocol-specific function pointer set in curl_connect() */
  2623. result = conn->curl_do(conn);
  2624. /* This was formerly done in transfer.c, but we better do it here */
  2625. if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
  2626. /* This was a re-use of a connection and we got a write error in the
  2627. * DO-phase. Then we DISCONNECT this connection and have another attempt
  2628. * to CONNECT and then DO again! The retry cannot possibly find another
  2629. * connection to re-use, since we only keep one possible connection for
  2630. * each. */
  2631. infof(data, "Re-used connection seems dead, get a new one\n");
  2632. conn->bits.close = TRUE; /* enforce close of this connetion */
  2633. result = Curl_done(conn); /* we are so done with this */
  2634. if(CURLE_OK == result) {
  2635. /* Now, redo the connect and get a new connection */
  2636. result = Curl_connect(data, connp);
  2637. if(CURLE_OK == result)
  2638. /* ... finally back to actually retry the DO phase */
  2639. result = conn->curl_do(*connp);
  2640. }
  2641. }
  2642. }
  2643. return result;
  2644. }
  2645. CURLcode Curl_do_more(struct connectdata *conn)
  2646. {
  2647. CURLcode result=CURLE_OK;
  2648. if(conn->curl_do_more)
  2649. result = conn->curl_do_more(conn);
  2650. return result;
  2651. }
  2652. /*
  2653. * local variables:
  2654. * eval: (load-file "../curl-mode.el")
  2655. * end:
  2656. * vim600: fdm=marker
  2657. * vim: et sw=2 ts=2 sts=2 tw=78
  2658. */