clientgen.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. SMB client generic functions
  5. Copyright (C) Andrew Tridgell 1994-1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #define NO_SYSLOG
  19. #include "includes.h"
  20. #include "trans2.h"
  21. extern int DEBUGLEVEL;
  22. extern pstring user_socket_options;
  23. /*
  24. * Change the port number used to call on
  25. */
  26. int cli_set_port(struct cli_state *cli, int port)
  27. {
  28. if (port > 0)
  29. cli -> port = port;
  30. return cli -> port;
  31. }
  32. /****************************************************************************
  33. recv an smb
  34. ****************************************************************************/
  35. static BOOL cli_receive_smb(struct cli_state *cli)
  36. {
  37. return client_receive_smb(cli->fd,cli->inbuf,cli->timeout);
  38. }
  39. /****************************************************************************
  40. send an smb to a fd and re-establish if necessary
  41. ****************************************************************************/
  42. static BOOL cli_send_smb(struct cli_state *cli)
  43. {
  44. size_t len;
  45. size_t nwritten=0;
  46. ssize_t ret;
  47. BOOL reestablished=False;
  48. len = smb_len(cli->outbuf) + 4;
  49. while (nwritten < len) {
  50. ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten);
  51. if (ret <= 0 && errno == EPIPE && !reestablished) {
  52. if (cli_reestablish_connection(cli)) {
  53. reestablished = True;
  54. nwritten=0;
  55. continue;
  56. }
  57. }
  58. if (ret <= 0) {
  59. DEBUG(0,("Error writing %d bytes to client. %d. Exiting\n",
  60. (int)len,(int)ret));
  61. close_sockets();
  62. exit(1);
  63. }
  64. nwritten += ret;
  65. }
  66. return True;
  67. }
  68. /*****************************************************
  69. RAP error codes - a small start but will be extended.
  70. *******************************************************/
  71. struct
  72. {
  73. int err;
  74. const char *message;
  75. } const rap_errmap[] =
  76. {
  77. {5, "User has insufficient privilege" },
  78. {86, "The specified password is invalid" },
  79. {2226, "Operation only permitted on a Primary Domain Controller" },
  80. {2242, "The password of this user has expired." },
  81. {2243, "The password of this user cannot change." },
  82. {2244, "This password cannot be used now (password history conflict)." },
  83. {2245, "The password is shorter than required." },
  84. {2246, "The password of this user is too recent to change."},
  85. {0, NULL}
  86. };
  87. /****************************************************************************
  88. return a description of an SMB error
  89. ****************************************************************************/
  90. static char *cli_smb_errstr(struct cli_state *cli)
  91. {
  92. return smb_errstr(cli->inbuf);
  93. }
  94. /******************************************************
  95. Return an error message - either an SMB error or a RAP
  96. error.
  97. *******************************************************/
  98. char *cli_errstr(struct cli_state *cli)
  99. {
  100. static fstring error_message;
  101. uint8 errclass;
  102. uint32 errnum;
  103. uint32 nt_rpc_error;
  104. int i;
  105. /*
  106. * Errors are of three kinds - smb errors,
  107. * dealt with by cli_smb_errstr, NT errors,
  108. * whose code is in cli.nt_error, and rap
  109. * errors, whose error code is in cli.rap_error.
  110. */
  111. cli_error(cli, &errclass, &errnum, &nt_rpc_error);
  112. if (errclass != 0)
  113. {
  114. return cli_smb_errstr(cli);
  115. }
  116. /*
  117. * Was it an NT error ?
  118. */
  119. if (nt_rpc_error)
  120. {
  121. const char *nt_msg = get_nt_error_msg(nt_rpc_error);
  122. if (nt_msg == NULL)
  123. {
  124. slprintf(error_message, sizeof(fstring) - 1, "NT code %d", nt_rpc_error);
  125. }
  126. else
  127. {
  128. fstrcpy(error_message, nt_msg);
  129. }
  130. return error_message;
  131. }
  132. /*
  133. * Must have been a rap error.
  134. */
  135. slprintf(error_message, sizeof(error_message) - 1, "code %d", cli->rap_error);
  136. for (i = 0; rap_errmap[i].message != NULL; i++)
  137. {
  138. if (rap_errmap[i].err == cli->rap_error)
  139. {
  140. fstrcpy( error_message, rap_errmap[i].message);
  141. break;
  142. }
  143. }
  144. return error_message;
  145. }
  146. /****************************************************************************
  147. setup basics in a outgoing packet
  148. ****************************************************************************/
  149. static void cli_setup_packet(struct cli_state *cli)
  150. {
  151. cli->rap_error = 0;
  152. cli->nt_error = 0;
  153. SSVAL(cli->outbuf,smb_pid,cli->pid);
  154. SSVAL(cli->outbuf,smb_uid,cli->vuid);
  155. SSVAL(cli->outbuf,smb_mid,cli->mid);
  156. if (cli->protocol > PROTOCOL_CORE) {
  157. SCVAL(cli->outbuf,smb_flg,0x8);
  158. SSVAL(cli->outbuf,smb_flg2,0x1);
  159. }
  160. }
  161. #if 0
  162. /*****************************************************************************
  163. Convert a character pointer in a cli_call_api() response to a form we can use.
  164. This function contains code to prevent core dumps if the server returns
  165. invalid data.
  166. *****************************************************************************/
  167. static char *fix_char_ptr(unsigned int datap, unsigned int converter,
  168. char *rdata, int rdrcnt)
  169. {
  170. if (datap == 0) { /* turn NULL pointers into zero length strings */
  171. return "";
  172. } else {
  173. unsigned int offset = datap - converter;
  174. if (offset >= rdrcnt) {
  175. DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
  176. datap, converter, rdrcnt));
  177. return "<ERROR>";
  178. } else {
  179. return &rdata[offset];
  180. }
  181. }
  182. }
  183. #endif /* 0 */
  184. /****************************************************************************
  185. send a SMB trans or trans2 request
  186. ****************************************************************************/
  187. static BOOL cli_send_trans(struct cli_state *cli, int trans,
  188. const char *name, int pipe_name_len,
  189. int fid, int flags,
  190. uint16 *setup, int lsetup, int msetup,
  191. char *param, int lparam, int mparam,
  192. char *data, int ldata, int mdata)
  193. {
  194. int i;
  195. int this_ldata,this_lparam;
  196. int tot_data=0,tot_param=0;
  197. char *outdata,*outparam;
  198. char *p;
  199. this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
  200. this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
  201. memset(cli->outbuf,'\0',smb_size);
  202. set_message(cli->outbuf,14+lsetup,0,True);
  203. CVAL(cli->outbuf,smb_com) = trans;
  204. SSVAL(cli->outbuf,smb_tid, cli->cnum);
  205. cli_setup_packet(cli);
  206. outparam = smb_buf(cli->outbuf)+(trans==SMBtrans ? pipe_name_len+1 : 3);
  207. outdata = outparam+this_lparam;
  208. /* primary request */
  209. SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
  210. SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
  211. SSVAL(cli->outbuf,smb_mprcnt,mparam); /* mprcnt */
  212. SSVAL(cli->outbuf,smb_mdrcnt,mdata); /* mdrcnt */
  213. SCVAL(cli->outbuf,smb_msrcnt,msetup); /* msrcnt */
  214. SSVAL(cli->outbuf,smb_flags,flags); /* flags */
  215. SIVAL(cli->outbuf,smb_timeout,0); /* timeout */
  216. SSVAL(cli->outbuf,smb_pscnt,this_lparam); /* pscnt */
  217. SSVAL(cli->outbuf,smb_psoff,smb_offset(outparam,cli->outbuf)); /* psoff */
  218. SSVAL(cli->outbuf,smb_dscnt,this_ldata); /* dscnt */
  219. SSVAL(cli->outbuf,smb_dsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
  220. SCVAL(cli->outbuf,smb_suwcnt,lsetup); /* suwcnt */
  221. for (i=0;i<lsetup;i++) /* setup[] */
  222. SSVAL(cli->outbuf,smb_setup+i*2,setup[i]);
  223. p = smb_buf(cli->outbuf);
  224. if (trans==SMBtrans) {
  225. memcpy(p,name, pipe_name_len + 1); /* name[] */
  226. } else {
  227. *p++ = 0; /* put in a null smb_name */
  228. *p++ = 'D'; *p++ = ' '; /* observed in OS/2 */
  229. }
  230. if (this_lparam) /* param[] */
  231. memcpy(outparam,param,this_lparam);
  232. if (this_ldata) /* data[] */
  233. memcpy(outdata,data,this_ldata);
  234. set_message(cli->outbuf,14+lsetup, /* wcnt, bcc */
  235. PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
  236. show_msg(cli->outbuf);
  237. cli_send_smb(cli);
  238. if (this_ldata < ldata || this_lparam < lparam) {
  239. /* receive interim response */
  240. if (!cli_receive_smb(cli) ||
  241. CVAL(cli->inbuf,smb_rcls) != 0) {
  242. return(False);
  243. }
  244. tot_data = this_ldata;
  245. tot_param = this_lparam;
  246. while (tot_data < ldata || tot_param < lparam) {
  247. this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
  248. this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
  249. set_message(cli->outbuf,trans==SMBtrans?8:9,0,True);
  250. CVAL(cli->outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
  251. outparam = smb_buf(cli->outbuf);
  252. outdata = outparam+this_lparam;
  253. /* secondary request */
  254. SSVAL(cli->outbuf,smb_tpscnt,lparam); /* tpscnt */
  255. SSVAL(cli->outbuf,smb_tdscnt,ldata); /* tdscnt */
  256. SSVAL(cli->outbuf,smb_spscnt,this_lparam); /* pscnt */
  257. SSVAL(cli->outbuf,smb_spsoff,smb_offset(outparam,cli->outbuf)); /* psoff */
  258. SSVAL(cli->outbuf,smb_spsdisp,tot_param); /* psdisp */
  259. SSVAL(cli->outbuf,smb_sdscnt,this_ldata); /* dscnt */
  260. SSVAL(cli->outbuf,smb_sdsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
  261. SSVAL(cli->outbuf,smb_sdsdisp,tot_data); /* dsdisp */
  262. if (trans==SMBtrans2)
  263. SSVALS(cli->outbuf,smb_sfid,fid); /* fid */
  264. if (this_lparam) /* param[] */
  265. memcpy(outparam,param,this_lparam);
  266. if (this_ldata) /* data[] */
  267. memcpy(outdata,data,this_ldata);
  268. set_message(cli->outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
  269. PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
  270. show_msg(cli->outbuf);
  271. cli_send_smb(cli);
  272. tot_data += this_ldata;
  273. tot_param += this_lparam;
  274. }
  275. }
  276. return(True);
  277. }
  278. /****************************************************************************
  279. receive a SMB trans or trans2 response allocating the necessary memory
  280. ****************************************************************************/
  281. static BOOL cli_receive_trans(struct cli_state *cli,int trans,
  282. char **param, int *param_len,
  283. char **data, int *data_len)
  284. {
  285. int total_data=0;
  286. int total_param=0;
  287. int this_data,this_param;
  288. uint8 eclass;
  289. uint32 ecode;
  290. *data_len = *param_len = 0;
  291. if (!cli_receive_smb(cli))
  292. return False;
  293. show_msg(cli->inbuf);
  294. /* sanity check */
  295. if (CVAL(cli->inbuf,smb_com) != trans) {
  296. DEBUG(0,("Expected %s response, got command 0x%02x\n",
  297. trans==SMBtrans?"SMBtrans":"SMBtrans2",
  298. CVAL(cli->inbuf,smb_com)));
  299. return(False);
  300. }
  301. /*
  302. * An NT RPC pipe call can return ERRDOS, ERRmoredata
  303. * to a trans call. This is not an error and should not
  304. * be treated as such.
  305. */
  306. if (cli_error(cli, &eclass, &ecode, NULL))
  307. {
  308. if(cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
  309. return(False);
  310. }
  311. /* parse out the lengths */
  312. total_data = SVAL(cli->inbuf,smb_tdrcnt);
  313. total_param = SVAL(cli->inbuf,smb_tprcnt);
  314. /* allocate it */
  315. *data = Realloc(*data,total_data);
  316. *param = Realloc(*param,total_param);
  317. while (1) {
  318. this_data = SVAL(cli->inbuf,smb_drcnt);
  319. this_param = SVAL(cli->inbuf,smb_prcnt);
  320. if (this_data + *data_len > total_data ||
  321. this_param + *param_len > total_param) {
  322. DEBUG(1,("Data overflow in cli_receive_trans\n"));
  323. return False;
  324. }
  325. if (this_data)
  326. memcpy(*data + SVAL(cli->inbuf,smb_drdisp),
  327. smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_droff),
  328. this_data);
  329. if (this_param)
  330. memcpy(*param + SVAL(cli->inbuf,smb_prdisp),
  331. smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_proff),
  332. this_param);
  333. *data_len += this_data;
  334. *param_len += this_param;
  335. /* parse out the total lengths again - they can shrink! */
  336. total_data = SVAL(cli->inbuf,smb_tdrcnt);
  337. total_param = SVAL(cli->inbuf,smb_tprcnt);
  338. if (total_data <= *data_len && total_param <= *param_len)
  339. break;
  340. if (!cli_receive_smb(cli))
  341. return False;
  342. show_msg(cli->inbuf);
  343. /* sanity check */
  344. if (CVAL(cli->inbuf,smb_com) != trans) {
  345. DEBUG(0,("Expected %s response, got command 0x%02x\n",
  346. trans==SMBtrans?"SMBtrans":"SMBtrans2",
  347. CVAL(cli->inbuf,smb_com)));
  348. return(False);
  349. }
  350. if (cli_error(cli, &eclass, &ecode, NULL))
  351. {
  352. if(cli->nt_pipe_fnum == 0 || !(eclass == ERRDOS && ecode == ERRmoredata))
  353. return(False);
  354. }
  355. }
  356. return(True);
  357. }
  358. #if 0
  359. /****************************************************************************
  360. Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
  361. ****************************************************************************/
  362. BOOL cli_api_pipe(struct cli_state *cli, char *pipe_name, int pipe_name_len,
  363. uint16 *setup, uint32 setup_count, uint32 max_setup_count,
  364. char *params, uint32 param_count, uint32 max_param_count,
  365. char *data, uint32 data_count, uint32 max_data_count,
  366. char **rparam, uint32 *rparam_count,
  367. char **rdata, uint32 *rdata_count)
  368. {
  369. if (pipe_name_len == 0)
  370. pipe_name_len = strlen(pipe_name);
  371. cli_send_trans(cli, SMBtrans,
  372. pipe_name, pipe_name_len,
  373. 0,0, /* fid, flags */
  374. setup, setup_count, max_setup_count,
  375. params, param_count, max_param_count,
  376. data, data_count, max_data_count);
  377. return (cli_receive_trans(cli, SMBtrans,
  378. rparam, (int *)rparam_count,
  379. rdata, (int *)rdata_count));
  380. }
  381. #endif /*0 */
  382. /****************************************************************************
  383. call a remote api
  384. ****************************************************************************/
  385. BOOL cli_api(struct cli_state *cli,
  386. char *param, int prcnt, int mprcnt,
  387. char *data, int drcnt, int mdrcnt,
  388. char **rparam, int *rprcnt,
  389. char **rdata, int *rdrcnt)
  390. {
  391. cli_send_trans(cli,SMBtrans,
  392. PIPE_LANMAN,strlen(PIPE_LANMAN), /* Name, length */
  393. 0,0, /* fid, flags */
  394. NULL,0,0, /* Setup, length, max */
  395. param, prcnt, mprcnt, /* Params, length, max */
  396. data, drcnt, mdrcnt /* Data, length, max */
  397. );
  398. return (cli_receive_trans(cli,SMBtrans,
  399. rparam, rprcnt,
  400. rdata, rdrcnt));
  401. }
  402. #if 0
  403. /****************************************************************************
  404. perform a NetWkstaUserLogon
  405. ****************************************************************************/
  406. BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
  407. {
  408. char *rparam = NULL;
  409. char *rdata = NULL;
  410. char *p;
  411. int rdrcnt,rprcnt;
  412. pstring param;
  413. memset(param, 0, sizeof(param));
  414. /* send a SMBtrans command with api NetWkstaUserLogon */
  415. p = param;
  416. SSVAL(p,0,132); /* api number */
  417. p += 2;
  418. pstrcpy(p,"OOWb54WrLh");
  419. p = skip_string(p,1);
  420. pstrcpy(p,"WB21BWDWWDDDDDDDzzzD");
  421. p = skip_string(p,1);
  422. SSVAL(p,0,1);
  423. p += 2;
  424. pstrcpy(p,user);
  425. strupper(p);
  426. p += 21;
  427. p++;
  428. p += 15;
  429. p++;
  430. pstrcpy(p, workstation);
  431. strupper(p);
  432. p += 16;
  433. SSVAL(p, 0, CLI_BUFFER_SIZE);
  434. p += 2;
  435. SSVAL(p, 0, CLI_BUFFER_SIZE);
  436. p += 2;
  437. if (cli_api(cli,
  438. param, PTR_DIFF(p,param),1024, /* param, length, max */
  439. NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
  440. &rparam, &rprcnt, /* return params, return size */
  441. &rdata, &rdrcnt /* return data, return size */
  442. )) {
  443. cli->rap_error = SVAL(rparam,0);
  444. p = rdata;
  445. if (cli->rap_error == 0) {
  446. DEBUG(4,("NetWkstaUserLogon success\n"));
  447. cli->privileges = SVAL(p, 24);
  448. fstrcpy(cli->eff_name,p+2);
  449. } else {
  450. DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
  451. }
  452. }
  453. if (rparam)
  454. free(rparam);
  455. if (rdata)
  456. free(rdata);
  457. return (cli->rap_error == 0);
  458. }
  459. #endif /*0 */
  460. /****************************************************************************
  461. call a NetShareEnum - try and browse available connections on a host
  462. ****************************************************************************/
  463. int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void * state)
  464. {
  465. char *rparam = NULL;
  466. char *rdata = NULL;
  467. char *p;
  468. int rdrcnt,rprcnt;
  469. pstring param;
  470. int count = -1;
  471. /* now send a SMBtrans command with api RNetShareEnum */
  472. p = param;
  473. SSVAL(p,0,0); /* api number */
  474. p += 2;
  475. pstrcpy(p,"WrLeh");
  476. p = skip_string(p,1);
  477. pstrcpy(p,"B13BWz");
  478. p = skip_string(p,1);
  479. SSVAL(p,0,1);
  480. /*
  481. * Win2k needs a *smaller* buffer than 0xFFFF here -
  482. * it returns "out of server memory" with 0xFFFF !!! JRA.
  483. */
  484. SSVAL(p,2,0xFFE0);
  485. p += 4;
  486. if (cli_api(cli,
  487. param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
  488. NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
  489. &rparam, &rprcnt, /* return params, length */
  490. &rdata, &rdrcnt)) /* return data, length */
  491. {
  492. int res = SVAL(rparam,0);
  493. int converter=SVAL(rparam,2);
  494. int i;
  495. if (res == 0 || res == ERRmoredata) {
  496. count=SVAL(rparam,4);
  497. p = rdata;
  498. for (i=0;i<count;i++,p+=20) {
  499. char *sname = p;
  500. int type = SVAL(p,14);
  501. int comment_offset = IVAL(p,16) & 0xFFFF;
  502. const char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
  503. fn(sname, type, cmnt, state);
  504. }
  505. } else {
  506. DEBUG(4,("NetShareEnum res=%d\n", res));
  507. }
  508. } else {
  509. DEBUG(4,("NetShareEnum failed\n"));
  510. }
  511. if (rparam)
  512. free(rparam);
  513. if (rdata)
  514. free(rdata);
  515. return count;
  516. }
  517. /****************************************************************************
  518. call a NetServerEnum for the specified workgroup and servertype mask.
  519. This function then calls the specified callback function for each name returned.
  520. The callback function takes 3 arguments: the machine name, the server type and
  521. the comment.
  522. ****************************************************************************/
  523. BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
  524. void (*fn)(const char *, uint32, const char *, void *), void *state)
  525. {
  526. char *rparam = NULL;
  527. char *rdata = NULL;
  528. int rdrcnt,rprcnt;
  529. char *p;
  530. pstring param;
  531. int uLevel = 1;
  532. int count = -1;
  533. /* send a SMBtrans command with api NetServerEnum */
  534. p = param;
  535. SSVAL(p,0,0x68); /* api number */
  536. p += 2;
  537. pstrcpy(p,"WrLehDz");
  538. p = skip_string(p,1);
  539. pstrcpy(p,"B16BBDz");
  540. p = skip_string(p,1);
  541. SSVAL(p,0,uLevel);
  542. SSVAL(p,2,CLI_BUFFER_SIZE);
  543. p += 4;
  544. SIVAL(p,0,stype);
  545. p += 4;
  546. pstrcpy(p, workgroup);
  547. p = skip_string(p,1);
  548. if (cli_api(cli,
  549. param, PTR_DIFF(p,param), 8, /* params, length, max */
  550. NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
  551. &rparam, &rprcnt, /* return params, return size */
  552. &rdata, &rdrcnt /* return data, return size */
  553. )) {
  554. int res = SVAL(rparam,0);
  555. int converter=SVAL(rparam,2);
  556. int i;
  557. if (res == 0 || res == ERRmoredata) {
  558. count=SVAL(rparam,4);
  559. p = rdata;
  560. for (i = 0;i < count;i++, p += 26) {
  561. char *sname = p;
  562. int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
  563. const char *cmnt = comment_offset?(rdata+comment_offset):"";
  564. if (comment_offset < 0 || comment_offset > rdrcnt) continue;
  565. stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
  566. fn(sname, stype, cmnt, state);
  567. }
  568. }
  569. }
  570. if (rparam)
  571. free(rparam);
  572. if (rdata)
  573. free(rdata);
  574. return(count > 0);
  575. }
  576. static struct {
  577. int prot;
  578. const char *name;
  579. }
  580. const prots[] =
  581. {
  582. {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
  583. {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
  584. {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
  585. {PROTOCOL_LANMAN1,"LANMAN1.0"},
  586. {PROTOCOL_LANMAN2,"LM1.2X002"},
  587. {PROTOCOL_LANMAN2,"Samba"},
  588. {PROTOCOL_NT1,"NT LANMAN 1.0"},
  589. {PROTOCOL_NT1,"NT LM 0.12"},
  590. {-1,NULL}
  591. };
  592. /****************************************************************************
  593. send a session setup
  594. ****************************************************************************/
  595. BOOL cli_session_setup(struct cli_state *cli,
  596. char *user,
  597. char *pass, int passlen,
  598. char *ntpass, int ntpasslen,
  599. char *workgroup)
  600. {
  601. char *p;
  602. fstring pword, ntpword;
  603. if (cli->protocol < PROTOCOL_LANMAN1)
  604. return True;
  605. if ((size_t) passlen > sizeof(pword)-1 || (size_t)ntpasslen > sizeof(ntpword)-1) {
  606. return False;
  607. }
  608. if (((passlen == 0) || (passlen == 1)) && (pass[0] == '\0')) {
  609. /* Null session connect. */
  610. pword[0] = '\0';
  611. ntpword[0] = '\0';
  612. } else {
  613. if ((cli->sec_mode & 2) && passlen != 24) {
  614. passlen = 24;
  615. ntpasslen = 24;
  616. SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
  617. SMBNTencrypt((uchar *)ntpass,(uchar *)cli->cryptkey,(uchar *)ntpword);
  618. } else {
  619. fstrcpy(pword, pass);
  620. fstrcpy(ntpword, "");
  621. ntpasslen = 0;
  622. }
  623. }
  624. /* if in share level security then don't send a password now */
  625. if (!(cli->sec_mode & 1)) {
  626. fstrcpy(pword, "");
  627. passlen=1;
  628. fstrcpy(ntpword, "");
  629. ntpasslen=1;
  630. }
  631. /* send a session setup command */
  632. memset(cli->outbuf,'\0',smb_size);
  633. if (cli->protocol < PROTOCOL_NT1)
  634. {
  635. set_message(cli->outbuf,10,1 + strlen(user) + passlen,True);
  636. CVAL(cli->outbuf,smb_com) = SMBsesssetupX;
  637. cli_setup_packet(cli);
  638. CVAL(cli->outbuf,smb_vwv0) = 0xFF;
  639. SSVAL(cli->outbuf,smb_vwv2,cli->max_xmit);
  640. SSVAL(cli->outbuf,smb_vwv3,2);
  641. SSVAL(cli->outbuf,smb_vwv4,1);
  642. SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
  643. SSVAL(cli->outbuf,smb_vwv7,passlen);
  644. p = smb_buf(cli->outbuf);
  645. memcpy(p,pword,passlen);
  646. p += passlen;
  647. pstrcpy(p,user);
  648. strupper(p);
  649. unix_to_dos (p, True);
  650. }
  651. else
  652. {
  653. set_message(cli->outbuf,13,0,True);
  654. CVAL(cli->outbuf,smb_com) = SMBsesssetupX;
  655. cli_setup_packet(cli);
  656. CVAL(cli->outbuf,smb_vwv0) = 0xFF;
  657. SSVAL(cli->outbuf,smb_vwv2,CLI_BUFFER_SIZE);
  658. SSVAL(cli->outbuf,smb_vwv3,2);
  659. SSVAL(cli->outbuf,smb_vwv4,cli->pid);
  660. SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
  661. SSVAL(cli->outbuf,smb_vwv7,passlen);
  662. SSVAL(cli->outbuf,smb_vwv8,ntpasslen);
  663. SSVAL(cli->outbuf,smb_vwv11,0);
  664. p = smb_buf(cli->outbuf);
  665. memcpy(p,pword,passlen);
  666. p += SVAL(cli->outbuf,smb_vwv7);
  667. memcpy(p,ntpword,ntpasslen);
  668. p += SVAL(cli->outbuf,smb_vwv8);
  669. pstrcpy(p,user);
  670. strupper(p);
  671. unix_to_dos (p, True);
  672. p = skip_string(p,1);
  673. pstrcpy(p,workgroup);
  674. strupper(p);
  675. p = skip_string(p,1);
  676. pstrcpy(p,"Unix");p = skip_string(p,1);
  677. pstrcpy(p,"Samba");p = skip_string(p,1);
  678. set_message(cli->outbuf,13,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
  679. }
  680. cli_send_smb(cli);
  681. if (!cli_receive_smb(cli))
  682. return False;
  683. show_msg(cli->inbuf);
  684. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  685. return False;
  686. }
  687. /* use the returned vuid from now on */
  688. cli->vuid = SVAL(cli->inbuf,smb_uid);
  689. if (cli->protocol >= PROTOCOL_NT1) {
  690. /*
  691. * Save off some of the connected server
  692. * info.
  693. */
  694. char *server_domain,*server_os,*server_type;
  695. server_os = smb_buf(cli->inbuf);
  696. server_type = skip_string(server_os,1);
  697. server_domain = skip_string(server_type,1);
  698. fstrcpy(cli->server_os, server_os);
  699. fstrcpy(cli->server_type, server_type);
  700. fstrcpy(cli->server_domain, server_domain);
  701. }
  702. fstrcpy(cli->user_name, user);
  703. return True;
  704. }
  705. /****************************************************************************
  706. Send a uloggoff.
  707. *****************************************************************************/
  708. #if 0
  709. BOOL cli_ulogoff(struct cli_state *cli)
  710. {
  711. memset(cli->outbuf,'\0',smb_size);
  712. set_message(cli->outbuf,2,0,True);
  713. CVAL(cli->outbuf,smb_com) = SMBulogoffX;
  714. cli_setup_packet(cli);
  715. SSVAL(cli->outbuf,smb_vwv0,0xFF);
  716. SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
  717. cli_send_smb(cli);
  718. if (!cli_receive_smb(cli))
  719. return False;
  720. return CVAL(cli->inbuf,smb_rcls) == 0;
  721. }
  722. #endif /*0 */
  723. /****************************************************************************
  724. send a tconX
  725. ****************************************************************************/
  726. BOOL cli_send_tconX(struct cli_state *cli,
  727. const char *share, const char *dev, const char *pass, int passlen)
  728. {
  729. fstring fullshare, pword;
  730. char *p;
  731. memset(cli->outbuf,'\0',smb_size);
  732. memset(cli->inbuf,'\0',smb_size);
  733. fstrcpy(cli->share, share);
  734. /* in user level security don't send a password now */
  735. if (cli->sec_mode & 1) {
  736. passlen = 1;
  737. pass = "";
  738. }
  739. if ((cli->sec_mode & 2) && *pass && passlen != 24) {
  740. passlen = 24;
  741. SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
  742. } else {
  743. memcpy(pword, pass, passlen);
  744. }
  745. slprintf(fullshare, sizeof(fullshare)-1,
  746. "\\\\%s\\%s", cli->desthost, share);
  747. unix_to_dos(fullshare, True);
  748. strupper(fullshare);
  749. set_message(cli->outbuf,4,
  750. 2 + strlen(fullshare) + passlen + strlen(dev),True);
  751. CVAL(cli->outbuf,smb_com) = SMBtconX;
  752. cli_setup_packet(cli);
  753. SSVAL(cli->outbuf,smb_vwv0,0xFF);
  754. SSVAL(cli->outbuf,smb_vwv3,passlen);
  755. p = smb_buf(cli->outbuf);
  756. memcpy(p,pword,passlen);
  757. p += passlen;
  758. fstrcpy(p,fullshare);
  759. p = skip_string(p,1);
  760. pstrcpy(p,dev);
  761. SCVAL(cli->inbuf,smb_rcls, 1);
  762. cli_send_smb(cli);
  763. if (!cli_receive_smb(cli))
  764. return False;
  765. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  766. return False;
  767. }
  768. fstrcpy(cli->dev, "A:");
  769. if (cli->protocol >= PROTOCOL_NT1) {
  770. fstrcpy(cli->dev, smb_buf(cli->inbuf));
  771. }
  772. if (strcasecmp(share,"IPC$")==0) {
  773. fstrcpy(cli->dev, "IPC");
  774. }
  775. /* only grab the device if we have a recent protocol level */
  776. if (cli->protocol >= PROTOCOL_NT1 &&
  777. smb_buflen(cli->inbuf) == 3) {
  778. /* almost certainly win95 - enable bug fixes */
  779. cli->win95 = True;
  780. }
  781. cli->cnum = SVAL(cli->inbuf,smb_tid);
  782. return True;
  783. }
  784. #if 0
  785. /****************************************************************************
  786. send a tree disconnect
  787. ****************************************************************************/
  788. BOOL cli_tdis(struct cli_state *cli)
  789. {
  790. memset(cli->outbuf,'\0',smb_size);
  791. set_message(cli->outbuf,0,0,True);
  792. CVAL(cli->outbuf,smb_com) = SMBtdis;
  793. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  794. cli_setup_packet(cli);
  795. cli_send_smb(cli);
  796. if (!cli_receive_smb(cli))
  797. return False;
  798. return CVAL(cli->inbuf,smb_rcls) == 0;
  799. }
  800. #endif /*0 */
  801. /****************************************************************************
  802. rename a file
  803. ****************************************************************************/
  804. BOOL cli_rename(struct cli_state *cli, char *fname_src, char *fname_dst)
  805. {
  806. char *p;
  807. memset(cli->outbuf,'\0',smb_size);
  808. memset(cli->inbuf,'\0',smb_size);
  809. set_message(cli->outbuf,1, 4 + strlen(fname_src) + strlen(fname_dst), True);
  810. CVAL(cli->outbuf,smb_com) = SMBmv;
  811. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  812. cli_setup_packet(cli);
  813. SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
  814. p = smb_buf(cli->outbuf);
  815. *p++ = 4;
  816. pstrcpy(p,fname_src);
  817. p = skip_string(p,1);
  818. *p++ = 4;
  819. pstrcpy(p,fname_dst);
  820. cli_send_smb(cli);
  821. if (!cli_receive_smb(cli)) {
  822. return False;
  823. }
  824. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  825. return False;
  826. }
  827. return True;
  828. }
  829. /****************************************************************************
  830. delete a file
  831. ****************************************************************************/
  832. BOOL cli_unlink(struct cli_state *cli, char *fname)
  833. {
  834. char *p;
  835. memset(cli->outbuf,'\0',smb_size);
  836. memset(cli->inbuf,'\0',smb_size);
  837. set_message(cli->outbuf,1, 2 + strlen(fname),True);
  838. CVAL(cli->outbuf,smb_com) = SMBunlink;
  839. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  840. cli_setup_packet(cli);
  841. SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
  842. p = smb_buf(cli->outbuf);
  843. *p++ = 4;
  844. pstrcpy(p,fname);
  845. cli_send_smb(cli);
  846. if (!cli_receive_smb(cli)) {
  847. return False;
  848. }
  849. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  850. return False;
  851. }
  852. return True;
  853. }
  854. /****************************************************************************
  855. create a directory
  856. ****************************************************************************/
  857. BOOL cli_mkdir(struct cli_state *cli, char *dname)
  858. {
  859. char *p;
  860. memset(cli->outbuf,'\0',smb_size);
  861. memset(cli->inbuf,'\0',smb_size);
  862. set_message(cli->outbuf,0, 2 + strlen(dname),True);
  863. CVAL(cli->outbuf,smb_com) = SMBmkdir;
  864. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  865. cli_setup_packet(cli);
  866. p = smb_buf(cli->outbuf);
  867. *p++ = 4;
  868. pstrcpy(p,dname);
  869. cli_send_smb(cli);
  870. if (!cli_receive_smb(cli)) {
  871. return False;
  872. }
  873. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  874. return False;
  875. }
  876. return True;
  877. }
  878. /****************************************************************************
  879. remove a directory
  880. ****************************************************************************/
  881. BOOL cli_rmdir(struct cli_state *cli, char *dname)
  882. {
  883. char *p;
  884. memset(cli->outbuf,'\0',smb_size);
  885. memset(cli->inbuf,'\0',smb_size);
  886. set_message(cli->outbuf,0, 2 + strlen(dname),True);
  887. CVAL(cli->outbuf,smb_com) = SMBrmdir;
  888. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  889. cli_setup_packet(cli);
  890. p = smb_buf(cli->outbuf);
  891. *p++ = 4;
  892. pstrcpy(p,dname);
  893. cli_send_smb(cli);
  894. if (!cli_receive_smb(cli)) {
  895. return False;
  896. }
  897. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  898. return False;
  899. }
  900. return True;
  901. }
  902. #if 0
  903. /****************************************************************************
  904. open a file
  905. ****************************************************************************/
  906. int cli_nt_create(struct cli_state *cli, char *fname)
  907. {
  908. char *p;
  909. memset(cli->outbuf,'\0',smb_size);
  910. memset(cli->inbuf,'\0',smb_size);
  911. set_message(cli->outbuf,24,1 + strlen(fname),True);
  912. CVAL(cli->outbuf,smb_com) = SMBntcreateX;
  913. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  914. cli_setup_packet(cli);
  915. SSVAL(cli->outbuf,smb_vwv0,0xFF);
  916. SIVAL(cli->outbuf,smb_ntcreate_Flags, 0x06);
  917. SIVAL(cli->outbuf,smb_ntcreate_RootDirectoryFid, 0x0);
  918. SIVAL(cli->outbuf,smb_ntcreate_DesiredAccess, 0x2019f);
  919. SIVAL(cli->outbuf,smb_ntcreate_FileAttributes, 0x0);
  920. SIVAL(cli->outbuf,smb_ntcreate_ShareAccess, 0x03);
  921. SIVAL(cli->outbuf,smb_ntcreate_CreateDisposition, 0x01);
  922. SIVAL(cli->outbuf,smb_ntcreate_CreateOptions, 0x0);
  923. SIVAL(cli->outbuf,smb_ntcreate_ImpersonationLevel, 0x02);
  924. SSVAL(cli->outbuf,smb_ntcreate_NameLength, strlen(fname));
  925. p = smb_buf(cli->outbuf);
  926. pstrcpy(p,fname);
  927. p = skip_string(p,1);
  928. cli_send_smb(cli);
  929. if (!cli_receive_smb(cli)) {
  930. return -1;
  931. }
  932. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  933. return -1;
  934. }
  935. return SVAL(cli->inbuf,smb_vwv2 + 1);
  936. }
  937. #endif /*0 */
  938. /****************************************************************************
  939. open a file
  940. ****************************************************************************/
  941. int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
  942. {
  943. char *p;
  944. unsigned openfn=0;
  945. unsigned accessmode=0;
  946. /* you must open for RW not just write - otherwise getattrE doesn't
  947. work! */
  948. if ((flags & O_ACCMODE) == O_WRONLY && strncmp(cli->dev, "LPT", 3)) {
  949. flags = (flags & ~O_ACCMODE) | O_RDWR;
  950. }
  951. if (flags & O_CREAT)
  952. openfn |= (1<<4);
  953. if (!(flags & O_EXCL)) {
  954. if (flags & O_TRUNC)
  955. openfn |= (1<<1);
  956. else
  957. openfn |= (1<<0);
  958. }
  959. accessmode = (share_mode<<4);
  960. if ((flags & O_ACCMODE) == O_RDWR) {
  961. accessmode |= 2;
  962. } else if ((flags & O_ACCMODE) == O_WRONLY) {
  963. accessmode |= 1;
  964. }
  965. #if defined(O_SYNC)
  966. if ((flags & O_SYNC) == O_SYNC) {
  967. accessmode |= (1<<14);
  968. }
  969. #endif /* O_SYNC */
  970. memset(cli->outbuf,'\0',smb_size);
  971. memset(cli->inbuf,'\0',smb_size);
  972. set_message(cli->outbuf,15,1 + strlen(fname),True);
  973. CVAL(cli->outbuf,smb_com) = SMBopenX;
  974. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  975. cli_setup_packet(cli);
  976. SSVAL(cli->outbuf,smb_vwv0,0xFF);
  977. SSVAL(cli->outbuf,smb_vwv2,0); /* no additional info */
  978. SSVAL(cli->outbuf,smb_vwv3,accessmode);
  979. SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
  980. SSVAL(cli->outbuf,smb_vwv5,0);
  981. SSVAL(cli->outbuf,smb_vwv8,openfn);
  982. p = smb_buf(cli->outbuf);
  983. pstrcpy(p,fname);
  984. p = skip_string(p,1);
  985. cli_send_smb(cli);
  986. if (!cli_receive_smb(cli)) {
  987. return -1;
  988. }
  989. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  990. return -1;
  991. }
  992. return SVAL(cli->inbuf,smb_vwv2);
  993. }
  994. /****************************************************************************
  995. close a file
  996. ****************************************************************************/
  997. BOOL cli_close(struct cli_state *cli, int fnum)
  998. {
  999. memset(cli->outbuf,'\0',smb_size);
  1000. memset(cli->inbuf,'\0',smb_size);
  1001. set_message(cli->outbuf,3,0,True);
  1002. CVAL(cli->outbuf,smb_com) = SMBclose;
  1003. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1004. cli_setup_packet(cli);
  1005. SSVAL(cli->outbuf,smb_vwv0,fnum);
  1006. SIVALS(cli->outbuf,smb_vwv1,-1);
  1007. cli_send_smb(cli);
  1008. if (!cli_receive_smb(cli)) {
  1009. return False;
  1010. }
  1011. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1012. return False;
  1013. }
  1014. return True;
  1015. }
  1016. #if 0
  1017. /****************************************************************************
  1018. lock a file
  1019. ****************************************************************************/
  1020. BOOL cli_lock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int timeout)
  1021. {
  1022. char *p;
  1023. int saved_timeout = cli->timeout;
  1024. memset(cli->outbuf,'\0',smb_size);
  1025. memset(cli->inbuf,'\0', smb_size);
  1026. set_message(cli->outbuf,8,10,True);
  1027. CVAL(cli->outbuf,smb_com) = SMBlockingX;
  1028. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1029. cli_setup_packet(cli);
  1030. CVAL(cli->outbuf,smb_vwv0) = 0xFF;
  1031. SSVAL(cli->outbuf,smb_vwv2,fnum);
  1032. CVAL(cli->outbuf,smb_vwv3) = 0;
  1033. SIVALS(cli->outbuf, smb_vwv4, timeout);
  1034. SSVAL(cli->outbuf,smb_vwv6,0);
  1035. SSVAL(cli->outbuf,smb_vwv7,1);
  1036. p = smb_buf(cli->outbuf);
  1037. SSVAL(p, 0, cli->pid);
  1038. SIVAL(p, 2, offset);
  1039. SIVAL(p, 6, len);
  1040. cli_send_smb(cli);
  1041. cli->timeout = (timeout == -1) ? 0x7FFFFFFF : timeout;
  1042. if (!cli_receive_smb(cli)) {
  1043. cli->timeout = saved_timeout;
  1044. return False;
  1045. }
  1046. cli->timeout = saved_timeout;
  1047. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1048. return False;
  1049. }
  1050. return True;
  1051. }
  1052. /****************************************************************************
  1053. unlock a file
  1054. ****************************************************************************/
  1055. BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int timeout)
  1056. {
  1057. char *p;
  1058. memset(cli->outbuf,'\0',smb_size);
  1059. memset(cli->inbuf,'\0',smb_size);
  1060. set_message(cli->outbuf,8,10,True);
  1061. CVAL(cli->outbuf,smb_com) = SMBlockingX;
  1062. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1063. cli_setup_packet(cli);
  1064. CVAL(cli->outbuf,smb_vwv0) = 0xFF;
  1065. SSVAL(cli->outbuf,smb_vwv2,fnum);
  1066. CVAL(cli->outbuf,smb_vwv3) = 0;
  1067. SIVALS(cli->outbuf, smb_vwv4, timeout);
  1068. SSVAL(cli->outbuf,smb_vwv6,1);
  1069. SSVAL(cli->outbuf,smb_vwv7,0);
  1070. p = smb_buf(cli->outbuf);
  1071. SSVAL(p, 0, cli->pid);
  1072. SIVAL(p, 2, offset);
  1073. SIVAL(p, 6, len);
  1074. cli_send_smb(cli);
  1075. if (!cli_receive_smb(cli)) {
  1076. return False;
  1077. }
  1078. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1079. return False;
  1080. }
  1081. return True;
  1082. }
  1083. #endif /*0 */
  1084. /****************************************************************************
  1085. issue a single SMBread and don't wait for a reply
  1086. ****************************************************************************/
  1087. static void cli_issue_read(struct cli_state *cli, int fnum, off_t offset,
  1088. size_t size, int i)
  1089. {
  1090. memset(cli->outbuf,'\0',smb_size);
  1091. memset(cli->inbuf,'\0',smb_size);
  1092. set_message(cli->outbuf,10,0,True);
  1093. CVAL(cli->outbuf,smb_com) = SMBreadX;
  1094. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1095. cli_setup_packet(cli);
  1096. CVAL(cli->outbuf,smb_vwv0) = 0xFF;
  1097. SSVAL(cli->outbuf,smb_vwv2,fnum);
  1098. SIVAL(cli->outbuf,smb_vwv3,offset);
  1099. SSVAL(cli->outbuf,smb_vwv5,size);
  1100. SSVAL(cli->outbuf,smb_vwv6,size);
  1101. SSVAL(cli->outbuf,smb_mid,cli->mid + i);
  1102. cli_send_smb(cli);
  1103. }
  1104. /****************************************************************************
  1105. read from a file
  1106. ****************************************************************************/
  1107. size_t cli_read(struct cli_state *cli, int fnum, char *buf, off_t offset, size_t size)
  1108. {
  1109. char *p;
  1110. int total = -1;
  1111. int issued=0;
  1112. int received=0;
  1113. int mpx = MAX(cli->max_mux-1, 1);
  1114. int block = (cli->max_xmit - (smb_size+32)) & ~1023;
  1115. int mid;
  1116. int blocks = (size + (block-1)) / block;
  1117. if (size == 0) return 0;
  1118. while (received < blocks) {
  1119. int size2;
  1120. while (issued - received < mpx && issued < blocks) {
  1121. int size1 = MIN(block, (int) size-issued*block);
  1122. cli_issue_read(cli, fnum, offset+issued*block, size1, issued);
  1123. issued++;
  1124. }
  1125. if (!cli_receive_smb(cli)) {
  1126. return total;
  1127. }
  1128. received++;
  1129. mid = SVAL(cli->inbuf, smb_mid) - cli->mid;
  1130. size2 = SVAL(cli->inbuf, smb_vwv5);
  1131. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1132. blocks = MIN(blocks, mid-1);
  1133. continue;
  1134. }
  1135. if (size2 <= 0) {
  1136. blocks = MIN(blocks, mid-1);
  1137. /* this distinguishes EOF from an error */
  1138. total = MAX(total, 0);
  1139. continue;
  1140. }
  1141. if (size2 > block) {
  1142. DEBUG(0,("server returned more than we wanted!\n"));
  1143. exit(1);
  1144. }
  1145. if (mid >= issued) {
  1146. DEBUG(0,("invalid mid from server!\n"));
  1147. exit(1);
  1148. }
  1149. p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
  1150. memcpy(buf+mid*block, p, size2);
  1151. total = MAX(total, mid*block + size2);
  1152. }
  1153. while (received < issued) {
  1154. cli_receive_smb(cli);
  1155. received++;
  1156. }
  1157. return total;
  1158. }
  1159. /****************************************************************************
  1160. issue a single SMBwrite and don't wait for a reply
  1161. ****************************************************************************/
  1162. static void cli_issue_write(struct cli_state *cli, int fnum, off_t offset, uint16 mode, const char *buf,
  1163. size_t size, int i)
  1164. {
  1165. char *p;
  1166. memset(cli->outbuf,'\0',smb_size);
  1167. memset(cli->inbuf,'\0',smb_size);
  1168. set_message(cli->outbuf,12,size,True);
  1169. CVAL(cli->outbuf,smb_com) = SMBwriteX;
  1170. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1171. cli_setup_packet(cli);
  1172. CVAL(cli->outbuf,smb_vwv0) = 0xFF;
  1173. SSVAL(cli->outbuf,smb_vwv2,fnum);
  1174. SIVAL(cli->outbuf,smb_vwv3,offset);
  1175. SIVAL(cli->outbuf,smb_vwv5,IS_BITS_SET_ALL(mode, 0x0008) ? 0xFFFFFFFF : 0);
  1176. SSVAL(cli->outbuf,smb_vwv7,mode);
  1177. SSVAL(cli->outbuf,smb_vwv8,IS_BITS_SET_ALL(mode, 0x0008) ? size : 0);
  1178. SSVAL(cli->outbuf,smb_vwv10,size);
  1179. SSVAL(cli->outbuf,smb_vwv11,
  1180. smb_buf(cli->outbuf) - smb_base(cli->outbuf));
  1181. p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
  1182. memcpy(p, buf, size);
  1183. SSVAL(cli->outbuf,smb_mid,cli->mid + i);
  1184. show_msg(cli->outbuf);
  1185. cli_send_smb(cli);
  1186. }
  1187. /****************************************************************************
  1188. write to a file
  1189. write_mode: 0x0001 disallow write cacheing
  1190. 0x0002 return bytes remaining
  1191. 0x0004 use raw named pipe protocol
  1192. 0x0008 start of message mode named pipe protocol
  1193. ****************************************************************************/
  1194. ssize_t cli_write(struct cli_state *cli,
  1195. int fnum, uint16 write_mode,
  1196. const char *buf, off_t offset, size_t size)
  1197. {
  1198. int bwritten = 0;
  1199. int issued = 0;
  1200. int received = 0;
  1201. int mpx = MAX(cli->max_mux-1, 1);
  1202. int block = (cli->max_xmit - (smb_size+32)) & ~1023;
  1203. int blocks = (size + (block-1)) / block;
  1204. while (received < blocks) {
  1205. while ((issued - received < mpx) && (issued < blocks))
  1206. {
  1207. int bsent = issued * block;
  1208. int size1 = MIN(block, (int) size - bsent);
  1209. cli_issue_write(cli, fnum, offset + bsent,
  1210. write_mode,
  1211. buf + bsent,
  1212. size1, issued);
  1213. issued++;
  1214. }
  1215. if (!cli_receive_smb(cli))
  1216. {
  1217. return bwritten;
  1218. }
  1219. received++;
  1220. if (CVAL(cli->inbuf,smb_rcls) != 0)
  1221. {
  1222. break;
  1223. }
  1224. bwritten += SVAL(cli->inbuf, smb_vwv2);
  1225. }
  1226. while (received < issued && cli_receive_smb(cli))
  1227. {
  1228. received++;
  1229. }
  1230. return bwritten;
  1231. }
  1232. #if 0
  1233. /****************************************************************************
  1234. write to a file using a SMBwrite and not bypassing 0 byte writes
  1235. ****************************************************************************/
  1236. ssize_t cli_smbwrite(struct cli_state *cli,
  1237. int fnum, const char *buf, off_t offset, size_t size)
  1238. {
  1239. char *p;
  1240. memset(cli->outbuf,'\0',smb_size);
  1241. memset(cli->inbuf,'\0',smb_size);
  1242. set_message(cli->outbuf,5, 3 + size,True);
  1243. CVAL(cli->outbuf,smb_com) = SMBwrite;
  1244. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1245. cli_setup_packet(cli);
  1246. SSVAL(cli->outbuf,smb_vwv0,fnum);
  1247. SSVAL(cli->outbuf,smb_vwv1,size);
  1248. SIVAL(cli->outbuf,smb_vwv2,offset);
  1249. SSVAL(cli->outbuf,smb_vwv4,0);
  1250. p = smb_buf(cli->outbuf);
  1251. *p++ = 1;
  1252. SSVAL(p, 0, size);
  1253. memcpy(p+2, buf, size);
  1254. cli_send_smb(cli);
  1255. if (!cli_receive_smb(cli)) {
  1256. return False;
  1257. }
  1258. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1259. return -1;
  1260. }
  1261. return SVAL(cli->inbuf,smb_vwv0);
  1262. }
  1263. #endif /*0 */
  1264. /****************************************************************************
  1265. do a SMBgetattrE call
  1266. ****************************************************************************/
  1267. BOOL cli_getattrE(struct cli_state *cli, int fd,
  1268. uint16 *attr, size_t *size,
  1269. time_t *c_time, time_t *a_time, time_t *m_time)
  1270. {
  1271. memset(cli->outbuf,'\0',smb_size);
  1272. memset(cli->inbuf,'\0',smb_size);
  1273. set_message(cli->outbuf,2,0,True);
  1274. CVAL(cli->outbuf,smb_com) = SMBgetattrE;
  1275. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1276. cli_setup_packet(cli);
  1277. SSVAL(cli->outbuf,smb_vwv0,fd);
  1278. cli_send_smb(cli);
  1279. if (!cli_receive_smb(cli)) {
  1280. return False;
  1281. }
  1282. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1283. return False;
  1284. }
  1285. if (size) {
  1286. *size = IVAL(cli->inbuf, smb_vwv6);
  1287. }
  1288. if (attr) {
  1289. *attr = SVAL(cli->inbuf,smb_vwv10);
  1290. }
  1291. if (c_time) {
  1292. *c_time = make_unix_date3(cli->inbuf+smb_vwv0);
  1293. }
  1294. if (a_time) {
  1295. *a_time = make_unix_date3(cli->inbuf+smb_vwv2);
  1296. }
  1297. if (m_time) {
  1298. *m_time = make_unix_date3(cli->inbuf+smb_vwv4);
  1299. }
  1300. return True;
  1301. }
  1302. /****************************************************************************
  1303. do a SMBgetatr call
  1304. ****************************************************************************/
  1305. BOOL cli_getatr(struct cli_state *cli, char *fname,
  1306. uint16 *attr, size_t *size, time_t *t)
  1307. {
  1308. char *p;
  1309. memset(cli->outbuf,'\0',smb_size);
  1310. memset(cli->inbuf,'\0',smb_size);
  1311. set_message(cli->outbuf,0,strlen(fname)+2,True);
  1312. CVAL(cli->outbuf,smb_com) = SMBgetatr;
  1313. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1314. cli_setup_packet(cli);
  1315. p = smb_buf(cli->outbuf);
  1316. *p = 4;
  1317. pstrcpy(p+1, fname);
  1318. cli_send_smb(cli);
  1319. if (!cli_receive_smb(cli)) {
  1320. return False;
  1321. }
  1322. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1323. return False;
  1324. }
  1325. if (size) {
  1326. *size = IVAL(cli->inbuf, smb_vwv3);
  1327. }
  1328. if (t) {
  1329. *t = make_unix_date3(cli->inbuf+smb_vwv1);
  1330. }
  1331. if (attr) {
  1332. *attr = SVAL(cli->inbuf,smb_vwv0);
  1333. }
  1334. return True;
  1335. }
  1336. /****************************************************************************
  1337. do a SMBsetatr call
  1338. ****************************************************************************/
  1339. BOOL cli_setatr(struct cli_state *cli, char *fname, uint16 attr, time_t t)
  1340. {
  1341. char *p;
  1342. memset(cli->outbuf,'\0',smb_size);
  1343. memset(cli->inbuf,'\0',smb_size);
  1344. set_message(cli->outbuf,8,strlen(fname)+4,True);
  1345. CVAL(cli->outbuf,smb_com) = SMBsetatr;
  1346. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  1347. cli_setup_packet(cli);
  1348. SSVAL(cli->outbuf,smb_vwv0, attr);
  1349. put_dos_date3(cli->outbuf,smb_vwv1, t);
  1350. p = smb_buf(cli->outbuf);
  1351. *p = 4;
  1352. pstrcpy(p+1, fname);
  1353. p = skip_string(p,1);
  1354. *p = 4;
  1355. cli_send_smb(cli);
  1356. if (!cli_receive_smb(cli)) {
  1357. return False;
  1358. }
  1359. if (CVAL(cli->inbuf,smb_rcls) != 0) {
  1360. return False;
  1361. }
  1362. return True;
  1363. }
  1364. #if 0
  1365. /****************************************************************************
  1366. send a qpathinfo call
  1367. ****************************************************************************/
  1368. BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
  1369. time_t *c_time, time_t *a_time, time_t *m_time,
  1370. size_t *size, uint16 *mode)
  1371. {
  1372. int data_len = 0;
  1373. int param_len = 0;
  1374. uint16 setup = TRANSACT2_QPATHINFO;
  1375. pstring param;
  1376. char *rparam=NULL, *rdata=NULL;
  1377. int count=8;
  1378. BOOL ret;
  1379. time_t (*date_fn)(void *);
  1380. param_len = strlen(fname) + 7;
  1381. memset(param, 0, param_len);
  1382. SSVAL(param, 0, SMB_INFO_STANDARD);
  1383. pstrcpy(&param[6], fname);
  1384. do {
  1385. ret = (cli_send_trans(cli, SMBtrans2,
  1386. NULL, 0, /* Name, length */
  1387. -1, 0, /* fid, flags */
  1388. &setup, 1, 0, /* setup, length, max */
  1389. param, param_len, 10, /* param, length, max */
  1390. NULL, data_len, cli->max_xmit /* data, length, max */
  1391. ) &&
  1392. cli_receive_trans(cli, SMBtrans2,
  1393. &rparam, &param_len,
  1394. &rdata, &data_len));
  1395. if (!ret) {
  1396. /* we need to work around a Win95 bug - sometimes
  1397. it gives ERRSRV/ERRerror temprarily */
  1398. uint8 eclass;
  1399. uint32 ecode;
  1400. cli_error(cli, &eclass, &ecode, NULL);
  1401. if (eclass != ERRSRV || ecode != ERRerror) break;
  1402. msleep(100);
  1403. }
  1404. } while (count-- && ret==False);
  1405. if (!ret || !rdata || data_len < 22) {
  1406. return False;
  1407. }
  1408. if (cli->win95) {
  1409. date_fn = make_unix_date;
  1410. } else {
  1411. date_fn = make_unix_date2;
  1412. }
  1413. if (c_time) {
  1414. *c_time = date_fn(rdata+0);
  1415. }
  1416. if (a_time) {
  1417. *a_time = date_fn(rdata+4);
  1418. }
  1419. if (m_time) {
  1420. *m_time = date_fn(rdata+8);
  1421. }
  1422. if (size) {
  1423. *size = IVAL(rdata, 12);
  1424. }
  1425. if (mode) {
  1426. *mode = SVAL(rdata,l1_attrFile);
  1427. }
  1428. if (rdata) free(rdata);
  1429. if (rparam) free(rparam);
  1430. return True;
  1431. }
  1432. /****************************************************************************
  1433. send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
  1434. ****************************************************************************/
  1435. BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
  1436. time_t *c_time, time_t *a_time, time_t *m_time,
  1437. time_t *w_time, size_t *size, uint16 *mode,
  1438. SMB_INO_T *ino)
  1439. {
  1440. int data_len = 0;
  1441. int param_len = 0;
  1442. uint16 setup = TRANSACT2_QPATHINFO;
  1443. pstring param;
  1444. char *rparam=NULL, *rdata=NULL;
  1445. param_len = strlen(fname) + 7;
  1446. memset(param, 0, param_len);
  1447. SSVAL(param, 0, SMB_QUERY_FILE_ALL_INFO);
  1448. pstrcpy(&param[6], fname);
  1449. if (!cli_send_trans(cli, SMBtrans2,
  1450. NULL, 0, /* name, length */
  1451. -1, 0, /* fid, flags */
  1452. &setup, 1, 0, /* setup, length, max */
  1453. param, param_len, 10, /* param, length, max */
  1454. NULL, data_len, cli->max_xmit /* data, length, max */
  1455. )) {
  1456. return False;
  1457. }
  1458. if (!cli_receive_trans(cli, SMBtrans2,
  1459. &rparam, &param_len,
  1460. &rdata, &data_len)) {
  1461. return False;
  1462. }
  1463. if (!rdata || data_len < 22) {
  1464. return False;
  1465. }
  1466. if (c_time) {
  1467. *c_time = interpret_long_date(rdata+0) - cli->serverzone;
  1468. }
  1469. if (a_time) {
  1470. *a_time = interpret_long_date(rdata+8) - cli->serverzone;
  1471. }
  1472. if (m_time) {
  1473. *m_time = interpret_long_date(rdata+16) - cli->serverzone;
  1474. }
  1475. if (w_time) {
  1476. *w_time = interpret_long_date(rdata+24) - cli->serverzone;
  1477. }
  1478. if (mode) {
  1479. *mode = SVAL(rdata, 32);
  1480. }
  1481. if (size) {
  1482. *size = IVAL(rdata, 48);
  1483. }
  1484. if (ino) {
  1485. *ino = IVAL(rdata, 64);
  1486. }
  1487. if (rdata) free(rdata);
  1488. if (rparam) free(rparam);
  1489. return True;
  1490. }
  1491. #endif /* 0 */
  1492. /****************************************************************************
  1493. send a qfileinfo call
  1494. ****************************************************************************/
  1495. BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
  1496. uint16 *mode, size_t *size,
  1497. time_t *c_time, time_t *a_time, time_t *m_time,
  1498. time_t *w_time, SMB_INO_T *ino)
  1499. {
  1500. int data_len = 0;
  1501. int param_len = 0;
  1502. uint16 setup = TRANSACT2_QFILEINFO;
  1503. pstring param;
  1504. char *rparam=NULL, *rdata=NULL;
  1505. /* if its a win95 server then fail this - win95 totally screws it
  1506. up */
  1507. if (cli->win95) return False;
  1508. param_len = 4;
  1509. memset(param, 0, param_len);
  1510. SSVAL(param, 0, fnum);
  1511. SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
  1512. if (!cli_send_trans(cli, SMBtrans2,
  1513. NULL, 0, /* name, length */
  1514. -1, 0, /* fid, flags */
  1515. &setup, 1, 0, /* setup, length, max */
  1516. param, param_len, 2, /* param, length, max */
  1517. NULL, data_len, cli->max_xmit /* data, length, max */
  1518. )) {
  1519. return False;
  1520. }
  1521. if (!cli_receive_trans(cli, SMBtrans2,
  1522. &rparam, &param_len,
  1523. &rdata, &data_len)) {
  1524. return False;
  1525. }
  1526. if (!rdata || data_len < 68) {
  1527. return False;
  1528. }
  1529. if (c_time) {
  1530. *c_time = interpret_long_date(rdata+0) - cli->serverzone;
  1531. }
  1532. if (a_time) {
  1533. *a_time = interpret_long_date(rdata+8) - cli->serverzone;
  1534. }
  1535. if (m_time) {
  1536. *m_time = interpret_long_date(rdata+16) - cli->serverzone;
  1537. }
  1538. if (w_time) {
  1539. *w_time = interpret_long_date(rdata+24) - cli->serverzone;
  1540. }
  1541. if (mode) {
  1542. *mode = SVAL(rdata, 32);
  1543. }
  1544. if (size) {
  1545. *size = IVAL(rdata, 48);
  1546. }
  1547. if (ino) {
  1548. *ino = IVAL(rdata, 64);
  1549. }
  1550. if (rdata) free(rdata);
  1551. if (rparam) free(rparam);
  1552. return True;
  1553. }
  1554. /****************************************************************************
  1555. interpret a long filename structure - this is mostly guesses at the moment
  1556. The length of the structure is returned
  1557. The structure of a long filename depends on the info level. 260 is used
  1558. by NT and 2 is used by OS/2
  1559. ****************************************************************************/
  1560. static int interpret_long_filename(int level,char *p,file_info *finfo)
  1561. {
  1562. extern file_info const def_finfo;
  1563. if (finfo)
  1564. memcpy(finfo,&def_finfo,sizeof(*finfo));
  1565. switch (level)
  1566. {
  1567. case 1: /* OS/2 understands this */
  1568. if (finfo) {
  1569. /* these dates are converted to GMT by make_unix_date */
  1570. finfo->ctime = make_unix_date2(p+4);
  1571. finfo->atime = make_unix_date2(p+8);
  1572. finfo->mtime = make_unix_date2(p+12);
  1573. finfo->size = IVAL(p,16);
  1574. finfo->mode = CVAL(p,24);
  1575. pstrcpy(finfo->name,p+27);
  1576. }
  1577. return(28 + CVAL(p,26));
  1578. case 2: /* this is what OS/2 uses mostly */
  1579. if (finfo) {
  1580. /* these dates are converted to GMT by make_unix_date */
  1581. finfo->ctime = make_unix_date2(p+4);
  1582. finfo->atime = make_unix_date2(p+8);
  1583. finfo->mtime = make_unix_date2(p+12);
  1584. finfo->size = IVAL(p,16);
  1585. finfo->mode = CVAL(p,24);
  1586. pstrcpy(finfo->name,p+31);
  1587. }
  1588. return(32 + CVAL(p,30));
  1589. /* levels 3 and 4 are untested */
  1590. case 3:
  1591. if (finfo) {
  1592. /* these dates are probably like the other ones */
  1593. finfo->ctime = make_unix_date2(p+8);
  1594. finfo->atime = make_unix_date2(p+12);
  1595. finfo->mtime = make_unix_date2(p+16);
  1596. finfo->size = IVAL(p,20);
  1597. finfo->mode = CVAL(p,28);
  1598. pstrcpy(finfo->name,p+33);
  1599. }
  1600. return(SVAL(p,4)+4);
  1601. case 4:
  1602. if (finfo) {
  1603. /* these dates are probably like the other ones */
  1604. finfo->ctime = make_unix_date2(p+8);
  1605. finfo->atime = make_unix_date2(p+12);
  1606. finfo->mtime = make_unix_date2(p+16);
  1607. finfo->size = IVAL(p,20);
  1608. finfo->mode = CVAL(p,28);
  1609. pstrcpy(finfo->name,p+37);
  1610. }
  1611. return(SVAL(p,4)+4);
  1612. case 260: /* NT uses this, but also accepts 2 */
  1613. if (finfo) {
  1614. int ret = SVAL(p,0);
  1615. int namelen;
  1616. p += 4; /* next entry offset */
  1617. p += 4; /* fileindex */
  1618. /* these dates appear to arrive in a
  1619. weird way. It seems to be localtime
  1620. plus the serverzone given in the
  1621. initial connect. This is GMT when
  1622. DST is not in effect and one hour
  1623. from GMT otherwise. Can this really
  1624. be right??
  1625. I suppose this could be called
  1626. kludge-GMT. Is is the GMT you get
  1627. by using the current DST setting on
  1628. a different localtime. It will be
  1629. cheap to calculate, I suppose, as
  1630. no DST tables will be needed */
  1631. finfo->ctime = interpret_long_date(p); p += 8;
  1632. finfo->atime = interpret_long_date(p); p += 8;
  1633. finfo->mtime = interpret_long_date(p); p += 8; p += 8;
  1634. finfo->size = IVAL(p,0); p += 8;
  1635. p += 8; /* alloc size */
  1636. finfo->mode = CVAL(p,0); p += 4;
  1637. namelen = IVAL(p,0); p += 4;
  1638. p += 4; /* EA size */
  1639. p += 2; /* short name len? */
  1640. p += 24; /* short name? */
  1641. StrnCpy(finfo->name,p,namelen);
  1642. return(ret);
  1643. }
  1644. return(SVAL(p,0));
  1645. }
  1646. DEBUG(1,("Unknown long filename format %d\n",level));
  1647. return(SVAL(p,0));
  1648. }
  1649. /****************************************************************************
  1650. do a directory listing, calling fn on each file found
  1651. ****************************************************************************/
  1652. int cli_list(struct cli_state *cli,const char *Mask,uint16 attribute,
  1653. void (*fn)(file_info *, const char *, void *), void *state)
  1654. {
  1655. int max_matches = 512;
  1656. /* NT uses 260, OS/2 uses 2. Both accept 1. */
  1657. int info_level = cli->protocol<PROTOCOL_NT1?1:260;
  1658. char *p, *p2;
  1659. pstring mask;
  1660. file_info finfo;
  1661. int i;
  1662. char *dirlist = NULL;
  1663. int dirlist_len = 0;
  1664. int total_received = -1;
  1665. BOOL First = True;
  1666. int ff_resume_key = 0;
  1667. int ff_searchcount=0;
  1668. int ff_eos=0;
  1669. int ff_lastname=0;
  1670. int ff_dir_handle=0;
  1671. int loop_count = 0;
  1672. char *rparam=NULL, *rdata=NULL;
  1673. int param_len, data_len;
  1674. uint16 setup;
  1675. pstring param;
  1676. pstrcpy(mask,Mask);
  1677. while (ff_eos == 0) {
  1678. loop_count++;
  1679. if (loop_count > 200) {
  1680. DEBUG(0,("Error: Looping in FIND_NEXT??\n"));
  1681. break;
  1682. }
  1683. param_len = 12+strlen(mask)+1;
  1684. if (First) {
  1685. setup = TRANSACT2_FINDFIRST;
  1686. SSVAL(param,0,attribute); /* attribute */
  1687. SSVAL(param,2,max_matches); /* max count */
  1688. SSVAL(param,4,8+4+2); /* resume required + close on end + continue */
  1689. SSVAL(param,6,info_level);
  1690. SIVAL(param,8,0);
  1691. pstrcpy(param+12,mask);
  1692. } else {
  1693. setup = TRANSACT2_FINDNEXT;
  1694. SSVAL(param,0,ff_dir_handle);
  1695. SSVAL(param,2,max_matches); /* max count */
  1696. SSVAL(param,4,info_level);
  1697. SIVAL(param,6,ff_resume_key); /* ff_resume_key */
  1698. SSVAL(param,10,8+4+2); /* resume required + close on end + continue */
  1699. pstrcpy(param+12,mask);
  1700. DEBUG(5,("hand=0x%X resume=%d ff_lastname=%d mask=%s\n",
  1701. ff_dir_handle,ff_resume_key,ff_lastname,mask));
  1702. }
  1703. if (!cli_send_trans(cli, SMBtrans2,
  1704. NULL, 0, /* Name, length */
  1705. -1, 0, /* fid, flags */
  1706. &setup, 1, 0, /* setup, length, max */
  1707. param, param_len, 10, /* param, length, max */
  1708. NULL, 0,
  1709. cli->max_xmit /* data, length, max */
  1710. )) {
  1711. break;
  1712. }
  1713. if (!cli_receive_trans(cli, SMBtrans2,
  1714. &rparam, &param_len,
  1715. &rdata, &data_len)) {
  1716. /* we need to work around a Win95 bug - sometimes
  1717. it gives ERRSRV/ERRerror temprarily */
  1718. uint8 eclass;
  1719. uint32 ecode;
  1720. cli_error(cli, &eclass, &ecode, NULL);
  1721. if (eclass != ERRSRV || ecode != ERRerror) break;
  1722. msleep(100);
  1723. continue;
  1724. }
  1725. if (total_received == -1) total_received = 0;
  1726. /* parse out some important return info */
  1727. p = rparam;
  1728. if (First) {
  1729. ff_dir_handle = SVAL(p,0);
  1730. ff_searchcount = SVAL(p,2);
  1731. ff_eos = SVAL(p,4);
  1732. ff_lastname = SVAL(p,8);
  1733. } else {
  1734. ff_searchcount = SVAL(p,0);
  1735. ff_eos = SVAL(p,2);
  1736. ff_lastname = SVAL(p,6);
  1737. }
  1738. if (ff_searchcount == 0)
  1739. break;
  1740. /* point to the data bytes */
  1741. p = rdata;
  1742. /* we might need the lastname for continuations */
  1743. if (ff_lastname > 0) {
  1744. switch(info_level)
  1745. {
  1746. case 260:
  1747. ff_resume_key =0;
  1748. StrnCpy(mask,p+ff_lastname,
  1749. data_len-ff_lastname);
  1750. break;
  1751. case 1:
  1752. pstrcpy(mask,p + ff_lastname + 1);
  1753. ff_resume_key = 0;
  1754. break;
  1755. }
  1756. } else {
  1757. pstrcpy(mask,"");
  1758. }
  1759. /* and add them to the dirlist pool */
  1760. dirlist = Realloc(dirlist,dirlist_len + data_len);
  1761. if (!dirlist) {
  1762. DEBUG(0,("Failed to expand dirlist\n"));
  1763. break;
  1764. }
  1765. /* put in a length for the last entry, to ensure we can chain entries
  1766. into the next packet */
  1767. for (p2=p,i=0;i<(ff_searchcount-1);i++)
  1768. p2 += interpret_long_filename(info_level,p2,NULL);
  1769. SSVAL(p2,0,data_len - PTR_DIFF(p2,p));
  1770. /* grab the data for later use */
  1771. memcpy(dirlist+dirlist_len,p,data_len);
  1772. dirlist_len += data_len;
  1773. total_received += ff_searchcount;
  1774. if (rdata) free(rdata); rdata = NULL;
  1775. if (rparam) free(rparam); rparam = NULL;
  1776. DEBUG(3,("received %d entries (eos=%d resume=%d)\n",
  1777. ff_searchcount,ff_eos,ff_resume_key));
  1778. First = False;
  1779. }
  1780. for (p=dirlist,i=0;i<total_received;i++) {
  1781. p += interpret_long_filename(info_level,p,&finfo);
  1782. fn(&finfo, Mask, state);
  1783. }
  1784. /* free up the dirlist buffer */
  1785. if (dirlist) free(dirlist);
  1786. return(total_received);
  1787. }
  1788. /****************************************************************************
  1789. send a negprot command
  1790. ****************************************************************************/
  1791. BOOL cli_negprot(struct cli_state *cli)
  1792. {
  1793. char *p;
  1794. int numprots;
  1795. int plength;
  1796. memset(cli->outbuf,'\0',smb_size);
  1797. /* setup the protocol strings */
  1798. for (plength=0,numprots=0;
  1799. prots[numprots].name && prots[numprots].prot<=cli->protocol;
  1800. numprots++)
  1801. plength += strlen(prots[numprots].name)+2;
  1802. set_message(cli->outbuf,0,plength,True);
  1803. p = smb_buf(cli->outbuf);
  1804. for (numprots=0;
  1805. prots[numprots].name && prots[numprots].prot<=cli->protocol;
  1806. numprots++) {
  1807. *p++ = 2;
  1808. pstrcpy(p,prots[numprots].name);
  1809. p += strlen(p) + 1;
  1810. }
  1811. CVAL(cli->outbuf,smb_com) = SMBnegprot;
  1812. cli_setup_packet(cli);
  1813. CVAL(smb_buf(cli->outbuf),0) = 2;
  1814. cli_send_smb(cli);
  1815. if (!cli_receive_smb(cli))
  1816. return False;
  1817. show_msg(cli->inbuf);
  1818. if (CVAL(cli->inbuf,smb_rcls) != 0 ||
  1819. ((int)SVAL(cli->inbuf,smb_vwv0) >= numprots)) {
  1820. return(False);
  1821. }
  1822. cli->protocol = prots[SVAL(cli->inbuf,smb_vwv0)].prot;
  1823. if (cli->protocol >= PROTOCOL_NT1) {
  1824. /* NT protocol */
  1825. cli->sec_mode = CVAL(cli->inbuf,smb_vwv1);
  1826. cli->max_mux = SVAL(cli->inbuf, smb_vwv1+1);
  1827. cli->max_xmit = IVAL(cli->inbuf,smb_vwv3+1);
  1828. cli->sesskey = IVAL(cli->inbuf,smb_vwv7+1);
  1829. cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1)*60;
  1830. /* this time arrives in real GMT */
  1831. cli->servertime = interpret_long_date(cli->inbuf+smb_vwv11+1);
  1832. memcpy(cli->cryptkey,smb_buf(cli->inbuf),8);
  1833. cli->capabilities = IVAL(cli->inbuf,smb_vwv9+1);
  1834. if (cli->capabilities & 1) {
  1835. cli->readbraw_supported = True;
  1836. cli->writebraw_supported = True;
  1837. }
  1838. } else if (cli->protocol >= PROTOCOL_LANMAN1) {
  1839. cli->sec_mode = SVAL(cli->inbuf,smb_vwv1);
  1840. cli->max_xmit = SVAL(cli->inbuf,smb_vwv2);
  1841. cli->sesskey = IVAL(cli->inbuf,smb_vwv6);
  1842. cli->serverzone = SVALS(cli->inbuf,smb_vwv10)*60;
  1843. /* this time is converted to GMT by make_unix_date */
  1844. cli->servertime = make_unix_date(cli->inbuf+smb_vwv8);
  1845. cli->readbraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x1) != 0);
  1846. cli->writebraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x2) != 0);
  1847. memcpy(cli->cryptkey,smb_buf(cli->inbuf),8);
  1848. } else {
  1849. /* the old core protocol */
  1850. cli->sec_mode = 0;
  1851. cli->serverzone = TimeDiff(time(NULL));
  1852. }
  1853. cli->max_xmit = MIN(cli->max_xmit, CLI_BUFFER_SIZE);
  1854. return True;
  1855. }
  1856. /****************************************************************************
  1857. send a session request. see rfc1002.txt 4.3 and 4.3.2
  1858. ****************************************************************************/
  1859. BOOL cli_session_request(struct cli_state *cli,
  1860. struct nmb_name *calling, struct nmb_name *called)
  1861. {
  1862. char *p;
  1863. int len = 4;
  1864. /* send a session request (RFC 1002) */
  1865. memcpy(&(cli->calling), calling, sizeof(*calling));
  1866. memcpy(&(cli->called ), called , sizeof(*called ));
  1867. /* put in the destination name */
  1868. p = cli->outbuf+len;
  1869. name_mangle(cli->called .name, p, cli->called .name_type);
  1870. len += name_len(p);
  1871. /* and my name */
  1872. p = cli->outbuf+len;
  1873. name_mangle(cli->calling.name, p, cli->calling.name_type);
  1874. len += name_len(p);
  1875. /* setup the packet length */
  1876. _smb_setlen(cli->outbuf,len);
  1877. CVAL(cli->outbuf,0) = 0x81;
  1878. #ifdef WITH_SSL
  1879. retry:
  1880. #endif /* WITH_SSL */
  1881. cli_send_smb(cli);
  1882. DEBUG(5,("Sent session request\n"));
  1883. if (!cli_receive_smb(cli))
  1884. return False;
  1885. if (CVAL(cli->inbuf,0) == 0x84) {
  1886. /* C. Hoch 9/14/95 Start */
  1887. /* For information, here is the response structure.
  1888. * We do the byte-twiddling to for portability.
  1889. struct RetargetResponse{
  1890. unsigned char type;
  1891. unsigned char flags;
  1892. int16 length;
  1893. int32 ip_addr;
  1894. int16 port;
  1895. };
  1896. */
  1897. int port = (CVAL(cli->inbuf,8)<<8)+CVAL(cli->inbuf,9);
  1898. /* SESSION RETARGET */
  1899. putip((char *)&cli->dest_ip,cli->inbuf+4);
  1900. close_sockets();
  1901. cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip, port, LONG_CONNECT_TIMEOUT);
  1902. if (cli->fd == -1)
  1903. return False;
  1904. DEBUG(3,("Retargeted\n"));
  1905. set_socket_options(cli->fd,user_socket_options);
  1906. /* Try again */
  1907. return cli_session_request(cli, calling, called);
  1908. } /* C. Hoch 9/14/95 End */
  1909. #ifdef WITH_SSL
  1910. if (CVAL(cli->inbuf,0) == 0x83 && CVAL(cli->inbuf,4) == 0x8e){ /* use ssl */
  1911. if (!sslutil_fd_is_ssl(cli->fd)){
  1912. if (sslutil_connect(cli->fd) == 0)
  1913. goto retry;
  1914. }
  1915. }
  1916. #endif /* WITH_SSL */
  1917. if (CVAL(cli->inbuf,0) != 0x82) {
  1918. /* This is the wrong place to put the error... JRA. */
  1919. cli->rap_error = CVAL(cli->inbuf,0);
  1920. return False;
  1921. }
  1922. return(True);
  1923. }
  1924. /****************************************************************************
  1925. open the client sockets
  1926. ****************************************************************************/
  1927. BOOL cli_connect(struct cli_state *cli, const char *host, struct in_addr *ip)
  1928. {
  1929. extern struct in_addr ipzero;
  1930. fstrcpy(cli->desthost, host);
  1931. if (!ip || ip_equal(*ip, ipzero)) {
  1932. if (!resolve_name( cli->desthost, &cli->dest_ip, 0x20)) {
  1933. return False;
  1934. }
  1935. if (ip) *ip = cli->dest_ip;
  1936. } else {
  1937. cli->dest_ip = *ip;
  1938. }
  1939. if (cli -> port == 0) cli -> port = 139; /* Set to default */
  1940. cli->fd = open_socket_out(SOCK_STREAM, &cli->dest_ip,
  1941. cli -> port, cli->timeout);
  1942. if (cli->fd == -1)
  1943. return False;
  1944. return True;
  1945. }
  1946. /****************************************************************************
  1947. initialise a client structure
  1948. ****************************************************************************/
  1949. struct cli_state *cli_initialise(struct cli_state *cli)
  1950. {
  1951. if (!cli) {
  1952. cli = (struct cli_state *)malloc(sizeof(*cli));
  1953. if (!cli)
  1954. return NULL;
  1955. ZERO_STRUCTP(cli);
  1956. }
  1957. if (cli->initialised) {
  1958. cli_shutdown(cli);
  1959. }
  1960. ZERO_STRUCTP(cli);
  1961. cli->port = 0;
  1962. cli->fd = -1;
  1963. cli->cnum = -1;
  1964. cli->pid = (uint16)getpid();
  1965. cli->mid = 1;
  1966. cli->vuid = UID_FIELD_INVALID;
  1967. cli->protocol = PROTOCOL_NT1;
  1968. cli->timeout = 20000; /* Timeout is in milliseconds. */
  1969. cli->bufsize = CLI_BUFFER_SIZE+4;
  1970. cli->max_xmit = cli->bufsize;
  1971. cli->outbuf = (char *)malloc(cli->bufsize);
  1972. cli->inbuf = (char *)malloc(cli->bufsize);
  1973. if (!cli->outbuf || !cli->inbuf)
  1974. {
  1975. return False;
  1976. }
  1977. cli->initialised = 1;
  1978. return cli;
  1979. }
  1980. /****************************************************************************
  1981. shutdown a client structure
  1982. ****************************************************************************/
  1983. void cli_shutdown(struct cli_state *cli)
  1984. {
  1985. if (cli->outbuf)
  1986. {
  1987. free(cli->outbuf);
  1988. }
  1989. if (cli->inbuf)
  1990. {
  1991. free(cli->inbuf);
  1992. }
  1993. #ifdef WITH_SSL
  1994. if (cli->fd != -1)
  1995. sslutil_disconnect(cli->fd);
  1996. #endif /* WITH_SSL */
  1997. if (cli->fd != -1)
  1998. close(cli->fd);
  1999. memset(cli, 0, sizeof(*cli));
  2000. }
  2001. /****************************************************************************
  2002. return error codes for the last packet
  2003. returns 0 if there was no error and the best approx of a unix errno
  2004. otherwise
  2005. for 32 bit "warnings", a return code of 0 is expected.
  2006. ****************************************************************************/
  2007. int cli_error(struct cli_state *cli, uint8 *eclass, uint32 *num, uint32 *nt_rpc_error)
  2008. {
  2009. int flgs2 = SVAL(cli->inbuf,smb_flg2);
  2010. char rcls;
  2011. int code;
  2012. if (eclass) *eclass = 0;
  2013. if (num ) *num = 0;
  2014. if (nt_rpc_error) *nt_rpc_error = cli->nt_error;
  2015. if (flgs2 & FLAGS2_32_BIT_ERROR_CODES) {
  2016. /* 32 bit error codes detected */
  2017. uint32 nt_err = IVAL(cli->inbuf,smb_rcls);
  2018. if (num) *num = nt_err;
  2019. DEBUG(10,("cli_error: 32 bit codes: code=%08x\n", nt_err));
  2020. if (!IS_BITS_SET_ALL(nt_err, 0xc0000000)) return 0;
  2021. switch (nt_err & 0xFFFFFF) {
  2022. case NT_STATUS_ACCESS_VIOLATION: return EACCES;
  2023. case NT_STATUS_NO_SUCH_FILE: return ENOENT;
  2024. case NT_STATUS_NO_SUCH_DEVICE: return ENODEV;
  2025. case NT_STATUS_INVALID_HANDLE: return EBADF;
  2026. case NT_STATUS_NO_MEMORY: return ENOMEM;
  2027. case NT_STATUS_ACCESS_DENIED: return EACCES;
  2028. case NT_STATUS_OBJECT_NAME_NOT_FOUND: return ENOENT;
  2029. case NT_STATUS_SHARING_VIOLATION: return EBUSY;
  2030. case NT_STATUS_OBJECT_PATH_INVALID: return ENOTDIR;
  2031. case NT_STATUS_OBJECT_NAME_COLLISION: return EEXIST;
  2032. }
  2033. /* for all other cases - a default code */
  2034. return EINVAL;
  2035. }
  2036. rcls = CVAL(cli->inbuf,smb_rcls);
  2037. code = SVAL(cli->inbuf,smb_err);
  2038. if (rcls == 0) return 0;
  2039. if (eclass) *eclass = rcls;
  2040. if (num ) *num = code;
  2041. if (rcls == ERRDOS) {
  2042. switch (code) {
  2043. case ERRbadfile: return ENOENT;
  2044. case ERRbadpath: return ENOTDIR;
  2045. case ERRnoaccess: return EACCES;
  2046. case ERRfilexists: return EEXIST;
  2047. case ERRrename: return EEXIST;
  2048. case ERRbadshare: return EBUSY;
  2049. case ERRlock: return EBUSY;
  2050. }
  2051. }
  2052. if (rcls == ERRSRV) {
  2053. switch (code) {
  2054. case ERRbadpw: return EPERM;
  2055. case ERRaccess: return EACCES;
  2056. case ERRnoresource: return ENOMEM;
  2057. case ERRinvdevice: return ENODEV;
  2058. case ERRinvnetname: return ENODEV;
  2059. }
  2060. }
  2061. /* for other cases */
  2062. return EINVAL;
  2063. }
  2064. /****************************************************************************
  2065. set socket options on a open connection
  2066. ****************************************************************************/
  2067. void cli_sockopt(struct cli_state *cli, char *options)
  2068. {
  2069. set_socket_options(cli->fd, options);
  2070. }
  2071. /****************************************************************************
  2072. set the PID to use for smb messages. Return the old pid.
  2073. ****************************************************************************/
  2074. uint16 cli_setpid(struct cli_state *cli, uint16 pid)
  2075. {
  2076. uint16 ret = cli->pid;
  2077. cli->pid = pid;
  2078. return ret;
  2079. }
  2080. /****************************************************************************
  2081. re-establishes a connection
  2082. ****************************************************************************/
  2083. BOOL cli_reestablish_connection(struct cli_state *cli)
  2084. {
  2085. struct nmb_name calling;
  2086. struct nmb_name called;
  2087. fstring dest_host;
  2088. fstring share;
  2089. fstring dev;
  2090. BOOL do_tcon = False;
  2091. int oldfd = cli->fd;
  2092. if (!cli->initialised || cli->fd == -1)
  2093. {
  2094. DEBUG(3,("cli_reestablish_connection: not connected\n"));
  2095. return False;
  2096. }
  2097. /* copy the parameters necessary to re-establish the connection */
  2098. if (cli->cnum != 0)
  2099. {
  2100. fstrcpy(share, cli->share);
  2101. fstrcpy(dev , cli->dev);
  2102. do_tcon = True;
  2103. }
  2104. memcpy(&called , &(cli->called ), sizeof(called ));
  2105. memcpy(&calling, &(cli->calling), sizeof(calling));
  2106. fstrcpy(dest_host, cli->full_dest_host_name);
  2107. DEBUG(5,("cli_reestablish_connection: %s connecting to %s (ip %s) - %s [%s]\n",
  2108. nmb_namestr(&calling), nmb_namestr(&called),
  2109. inet_ntoa(cli->dest_ip),
  2110. cli->user_name, cli->domain));
  2111. cli->fd = -1;
  2112. if (cli_establish_connection(cli,
  2113. dest_host, &cli->dest_ip,
  2114. &calling, &called,
  2115. share, dev, False, do_tcon)) {
  2116. if (cli->fd != oldfd) {
  2117. if (dup2(cli->fd, oldfd) == oldfd) {
  2118. close(cli->fd);
  2119. }
  2120. }
  2121. return True;
  2122. }
  2123. return False;
  2124. }
  2125. /****************************************************************************
  2126. establishes a connection right up to doing tconX, reading in a password.
  2127. ****************************************************************************/
  2128. BOOL cli_establish_connection(struct cli_state *cli,
  2129. char *dest_host, struct in_addr *dest_ip,
  2130. struct nmb_name *calling, struct nmb_name *called,
  2131. char *service, char *service_type,
  2132. BOOL do_shutdown, BOOL do_tcon)
  2133. {
  2134. DEBUG(5,("cli_establish_connection: %s connecting to %s (%s) - %s [%s]\n",
  2135. nmb_namestr(calling), nmb_namestr(called), inet_ntoa(*dest_ip),
  2136. cli->user_name, cli->domain));
  2137. /* establish connection */
  2138. if ((!cli->initialised))
  2139. {
  2140. return False;
  2141. }
  2142. if (cli->fd == -1)
  2143. {
  2144. if (!cli_connect(cli, dest_host, dest_ip))
  2145. {
  2146. DEBUG(1,("cli_establish_connection: failed to connect to %s (%s)\n",
  2147. nmb_namestr(calling), inet_ntoa(*dest_ip)));
  2148. return False;
  2149. }
  2150. }
  2151. if (!cli_session_request(cli, calling, called))
  2152. {
  2153. DEBUG(1,("failed session request\n"));
  2154. if (do_shutdown)
  2155. cli_shutdown(cli);
  2156. return False;
  2157. }
  2158. if (!cli_negprot(cli))
  2159. {
  2160. DEBUG(1,("failed negprot\n"));
  2161. if (do_shutdown)
  2162. cli_shutdown(cli);
  2163. return False;
  2164. }
  2165. if (cli->pwd.cleartext || cli->pwd.null_pwd)
  2166. {
  2167. fstring passwd;
  2168. int pass_len;
  2169. if (cli->pwd.null_pwd)
  2170. {
  2171. /* attempt null session */
  2172. passwd[0] = 0;
  2173. pass_len = 1;
  2174. }
  2175. else
  2176. {
  2177. /* attempt clear-text session */
  2178. pwd_get_cleartext(&(cli->pwd), passwd);
  2179. pass_len = strlen(passwd);
  2180. }
  2181. /* attempt clear-text session */
  2182. if (!cli_session_setup(cli, cli->user_name,
  2183. passwd, pass_len,
  2184. NULL, 0,
  2185. cli->domain))
  2186. {
  2187. DEBUG(1,("failed session setup\n"));
  2188. if (do_shutdown)
  2189. {
  2190. cli_shutdown(cli);
  2191. }
  2192. return False;
  2193. }
  2194. if (do_tcon)
  2195. {
  2196. if (!cli_send_tconX(cli, service, service_type,
  2197. (char*)passwd, strlen(passwd)))
  2198. {
  2199. DEBUG(1,("failed tcon_X\n"));
  2200. if (do_shutdown)
  2201. {
  2202. cli_shutdown(cli);
  2203. }
  2204. return False;
  2205. }
  2206. }
  2207. }
  2208. else
  2209. {
  2210. /* attempt encrypted session */
  2211. unsigned char nt_sess_pwd[24];
  2212. unsigned char lm_sess_pwd[24];
  2213. /* creates (storing a copy of) and then obtains a 24 byte password OWF */
  2214. pwd_make_lm_nt_owf(&(cli->pwd), cli->cryptkey);
  2215. pwd_get_lm_nt_owf(&(cli->pwd), lm_sess_pwd, nt_sess_pwd);
  2216. /* attempt encrypted session */
  2217. if (!cli_session_setup(cli, cli->user_name,
  2218. (char*)lm_sess_pwd, sizeof(lm_sess_pwd),
  2219. (char*)nt_sess_pwd, sizeof(nt_sess_pwd),
  2220. cli->domain))
  2221. {
  2222. DEBUG(1,("failed session setup\n"));
  2223. if (do_shutdown)
  2224. cli_shutdown(cli);
  2225. return False;
  2226. }
  2227. if (do_tcon)
  2228. {
  2229. if (!cli_send_tconX(cli, service, service_type,
  2230. (char*)nt_sess_pwd, sizeof(nt_sess_pwd)))
  2231. {
  2232. DEBUG(1,("failed tcon_X\n"));
  2233. if (do_shutdown)
  2234. cli_shutdown(cli);
  2235. return False;
  2236. }
  2237. }
  2238. }
  2239. if (do_shutdown)
  2240. cli_shutdown(cli);
  2241. return True;
  2242. }
  2243. /****************************************************************************
  2244. check for existance of a dir
  2245. ****************************************************************************/
  2246. BOOL cli_chkpath(struct cli_state *cli, char *path)
  2247. {
  2248. pstring path2;
  2249. char *p;
  2250. safe_strcpy(path2,path,sizeof(pstring)-1);
  2251. trim_string(path2,NULL,"\\");
  2252. if (!*path2) *path2 = '\\';
  2253. memset(cli->outbuf,'\0',smb_size);
  2254. set_message(cli->outbuf,0,4 + strlen(path2),True);
  2255. SCVAL(cli->outbuf,smb_com,SMBchkpth);
  2256. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  2257. cli_setup_packet(cli);
  2258. p = smb_buf(cli->outbuf);
  2259. *p++ = 4;
  2260. safe_strcpy(p,path2,strlen(path2));
  2261. cli_send_smb(cli);
  2262. if (!cli_receive_smb(cli)) {
  2263. return False;
  2264. }
  2265. if (cli_error(cli, NULL, NULL, NULL)) return False;
  2266. return True;
  2267. }
  2268. #if 0
  2269. /****************************************************************************
  2270. start a message sequence
  2271. ****************************************************************************/
  2272. BOOL cli_message_start(struct cli_state *cli, char *host, char *username,
  2273. int *grp)
  2274. {
  2275. char *p;
  2276. /* send a SMBsendstrt command */
  2277. memset(cli->outbuf,'\0',smb_size);
  2278. set_message(cli->outbuf,0,0,True);
  2279. CVAL(cli->outbuf,smb_com) = SMBsendstrt;
  2280. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  2281. cli_setup_packet(cli);
  2282. p = smb_buf(cli->outbuf);
  2283. *p++ = 4;
  2284. pstrcpy(p,username);
  2285. p = skip_string(p,1);
  2286. *p++ = 4;
  2287. pstrcpy(p,host);
  2288. p = skip_string(p,1);
  2289. set_message(cli->outbuf,0,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
  2290. cli_send_smb(cli);
  2291. if (!cli_receive_smb(cli)) {
  2292. return False;
  2293. }
  2294. if (cli_error(cli, NULL, NULL, NULL)) return False;
  2295. *grp = SVAL(cli->inbuf,smb_vwv0);
  2296. return True;
  2297. }
  2298. /****************************************************************************
  2299. send a message
  2300. ****************************************************************************/
  2301. BOOL cli_message_text(struct cli_state *cli, char *msg, int len, int grp)
  2302. {
  2303. char *p;
  2304. memset(cli->outbuf,'\0',smb_size);
  2305. set_message(cli->outbuf,1,len+3,True);
  2306. CVAL(cli->outbuf,smb_com) = SMBsendtxt;
  2307. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  2308. cli_setup_packet(cli);
  2309. SSVAL(cli->outbuf,smb_vwv0,grp);
  2310. p = smb_buf(cli->outbuf);
  2311. *p = 1;
  2312. SSVAL(p,1,len);
  2313. memcpy(p+3,msg,len);
  2314. cli_send_smb(cli);
  2315. if (!cli_receive_smb(cli)) {
  2316. return False;
  2317. }
  2318. if (cli_error(cli, NULL, NULL, NULL)) return False;
  2319. return True;
  2320. }
  2321. /****************************************************************************
  2322. end a message
  2323. ****************************************************************************/
  2324. BOOL cli_message_end(struct cli_state *cli, int grp)
  2325. {
  2326. memset(cli->outbuf,'\0',smb_size);
  2327. set_message(cli->outbuf,1,0,True);
  2328. CVAL(cli->outbuf,smb_com) = SMBsendend;
  2329. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  2330. SSVAL(cli->outbuf,smb_vwv0,grp);
  2331. cli_setup_packet(cli);
  2332. cli_send_smb(cli);
  2333. if (!cli_receive_smb(cli)) {
  2334. return False;
  2335. }
  2336. if (cli_error(cli, NULL, NULL, NULL)) return False;
  2337. return True;
  2338. }
  2339. #endif /*0 */
  2340. #if 0 /* May be useful one day */
  2341. /****************************************************************************
  2342. query disk space
  2343. ****************************************************************************/
  2344. BOOL cli_dskattr(struct cli_state *cli, int *bsize, int *total, int *avail)
  2345. {
  2346. memset(cli->outbuf,'\0',smb_size);
  2347. set_message(cli->outbuf,0,0,True);
  2348. CVAL(cli->outbuf,smb_com) = SMBdskattr;
  2349. SSVAL(cli->outbuf,smb_tid,cli->cnum);
  2350. cli_setup_packet(cli);
  2351. cli_send_smb(cli);
  2352. if (!cli_receive_smb(cli)) {
  2353. return False;
  2354. }
  2355. *bsize = SVAL(cli->inbuf,smb_vwv1)*SVAL(cli->inbuf,smb_vwv2);
  2356. *total = SVAL(cli->inbuf,smb_vwv0);
  2357. *avail = SVAL(cli->inbuf,smb_vwv3);
  2358. return True;
  2359. }
  2360. #endif /* 0 */