tridge the destroyer returns!
[samba.git] / source3 / libsmb / clientgen.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client generic functions
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25 #include "trans2.h"
26
27
28 extern int DEBUGLEVEL;
29
30 /*****************************************************
31  RAP error codes - a small start but will be extended.
32 *******************************************************/
33
34 struct
35 {
36   int err;
37   char *message;
38 } rap_errmap[] =
39 {
40   {5,    "User has insufficient privilege" },
41   {86,   "The specified password is invalid" },
42   {2226, "Operation only permitted on a Primary Domain Controller"  },
43   {2242, "The password of this user has expired." },
44   {2243, "The password of this user cannot change." },
45   {2244, "This password cannot be used now (password history conflict)." },
46   {2245, "The password is shorter than required." },
47   {2246, "The password of this user is too recent to change."},
48   {0, NULL}
49 };  
50
51 /****************************************************************************
52   return a description of an SMB error
53 ****************************************************************************/
54 static char *cli_smb_errstr(struct cli_state *cli)
55 {
56         return smb_errstr(cli->inbuf);
57 }
58
59 /******************************************************
60  Return an error message - either an SMB error or a RAP
61  error.
62 *******************************************************/
63     
64 char *cli_errstr(struct cli_state *cli)
65 {   
66   static fstring error_message;
67   int errclass;
68   int errnum;
69   int i;      
70       
71   /*  
72    * Errors are of three kinds - smb errors,
73    * dealt with by cli_smb_errstr, NT errors,
74    * whose code is in cli.nt_error, and rap
75    * errors, whose error code is in cli.rap_error.
76    */ 
77
78   cli_error(cli, &errclass, &errnum);
79   if(errclass != 0)
80     return cli_smb_errstr(cli);
81
82   /*
83    * Was it an NT error ?
84    */
85
86   if(cli->nt_error) {
87     char *nt_msg = get_nt_error_msg(cli->nt_error);
88
89     if(nt_msg == NULL)
90       slprintf(error_message, sizeof(fstring) - 1, "NT code %d", cli->nt_error);
91     else
92       fstrcpy(error_message, nt_msg);
93
94     return error_message;
95   }
96
97   /*
98    * Must have been a rap error.
99    */
100
101   slprintf(error_message, sizeof(error_message) - 1, "code %d", cli->rap_error);
102     
103   for(i = 0; rap_errmap[i].message != NULL; i++) {
104     if (rap_errmap[i].err == cli->rap_error) {
105       fstrcpy( error_message, rap_errmap[i].message);
106       break;
107     }
108   } 
109   
110   return error_message;
111 }
112
113 /****************************************************************************
114 setup basics in a outgoing packet
115 ****************************************************************************/
116 static void cli_setup_packet(struct cli_state *cli)
117 {
118         cli->rap_error = 0;
119         cli->nt_error = 0;
120         SSVAL(cli->outbuf,smb_pid,cli->pid);
121         SSVAL(cli->outbuf,smb_uid,cli->uid);
122         SSVAL(cli->outbuf,smb_mid,cli->mid);
123         if (cli->protocol > PROTOCOL_CORE) {
124                 SCVAL(cli->outbuf,smb_flg,0x8);
125                 SSVAL(cli->outbuf,smb_flg2,0x1);
126         }
127 }
128
129
130 /****************************************************************************
131   send a SMB trans or trans2 request
132   ****************************************************************************/
133 static BOOL cli_send_trans(struct cli_state *cli, int trans, 
134                            char *name, int pipe_name_len, 
135                            int fid, int flags,
136                            uint16 *setup, int lsetup, int msetup,
137                            char *param, int lparam, int mparam,
138                            char *data, int ldata, int mdata)
139 {
140         int i;
141         int this_ldata,this_lparam;
142         int tot_data=0,tot_param=0;
143         char *outdata,*outparam;
144         char *p;
145
146         this_lparam = MIN(lparam,cli->max_xmit - (500+lsetup*2)); /* hack */
147         this_ldata = MIN(ldata,cli->max_xmit - (500+lsetup*2+this_lparam));
148
149         bzero(cli->outbuf,smb_size);
150         set_message(cli->outbuf,14+lsetup,0,True);
151         CVAL(cli->outbuf,smb_com) = trans;
152         SSVAL(cli->outbuf,smb_tid, cli->cnum);
153         cli_setup_packet(cli);
154
155         outparam = smb_buf(cli->outbuf)+(trans==SMBtrans ? pipe_name_len+1 : 3);
156         outdata = outparam+this_lparam;
157
158         /* primary request */
159         SSVAL(cli->outbuf,smb_tpscnt,lparam);   /* tpscnt */
160         SSVAL(cli->outbuf,smb_tdscnt,ldata);    /* tdscnt */
161         SSVAL(cli->outbuf,smb_mprcnt,mparam);   /* mprcnt */
162         SSVAL(cli->outbuf,smb_mdrcnt,mdata);    /* mdrcnt */
163         SCVAL(cli->outbuf,smb_msrcnt,msetup);   /* msrcnt */
164         SSVAL(cli->outbuf,smb_flags,flags);     /* flags */
165         SIVAL(cli->outbuf,smb_timeout,0);               /* timeout */
166         SSVAL(cli->outbuf,smb_pscnt,this_lparam);       /* pscnt */
167         SSVAL(cli->outbuf,smb_psoff,smb_offset(outparam,cli->outbuf)); /* psoff */
168         SSVAL(cli->outbuf,smb_dscnt,this_ldata);        /* dscnt */
169         SSVAL(cli->outbuf,smb_dsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
170         SCVAL(cli->outbuf,smb_suwcnt,lsetup);   /* suwcnt */
171         for (i=0;i<lsetup;i++)          /* setup[] */
172                 SSVAL(cli->outbuf,smb_setup+i*2,setup[i]);
173         p = smb_buf(cli->outbuf);
174         if (trans==SMBtrans) {
175                 memcpy(p,name, pipe_name_len + 1);  /* name[] */
176         } else {
177                 *p++ = 0;  /* put in a null smb_name */
178                 *p++ = 'D'; *p++ = ' '; /* observed in OS/2 */
179         }
180         if (this_lparam)                        /* param[] */
181                 memcpy(outparam,param,this_lparam);
182         if (this_ldata)                 /* data[] */
183                 memcpy(outdata,data,this_ldata);
184         set_message(cli->outbuf,14+lsetup,              /* wcnt, bcc */
185                     PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
186
187         show_msg(cli->outbuf);
188         send_smb(cli->fd,cli->outbuf);
189
190         if (this_ldata < ldata || this_lparam < lparam) {
191                 /* receive interim response */
192                 if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout) || 
193                     CVAL(cli->inbuf,smb_rcls) != 0) {
194                         return(False);
195                 }      
196
197                 tot_data = this_ldata;
198                 tot_param = this_lparam;
199                 
200                 while (tot_data < ldata || tot_param < lparam)  {
201                         this_lparam = MIN(lparam-tot_param,cli->max_xmit - 500); /* hack */
202                         this_ldata = MIN(ldata-tot_data,cli->max_xmit - (500+this_lparam));
203
204                         set_message(cli->outbuf,trans==SMBtrans?8:9,0,True);
205                         CVAL(cli->outbuf,smb_com) = trans==SMBtrans ? SMBtranss : SMBtranss2;
206                         
207                         outparam = smb_buf(cli->outbuf);
208                         outdata = outparam+this_lparam;
209                         
210                         /* secondary request */
211                         SSVAL(cli->outbuf,smb_tpscnt,lparam);   /* tpscnt */
212                         SSVAL(cli->outbuf,smb_tdscnt,ldata);    /* tdscnt */
213                         SSVAL(cli->outbuf,smb_spscnt,this_lparam);      /* pscnt */
214                         SSVAL(cli->outbuf,smb_spsoff,smb_offset(outparam,cli->outbuf)); /* psoff */
215                         SSVAL(cli->outbuf,smb_spsdisp,tot_param);       /* psdisp */
216                         SSVAL(cli->outbuf,smb_sdscnt,this_ldata);       /* dscnt */
217                         SSVAL(cli->outbuf,smb_sdsoff,smb_offset(outdata,cli->outbuf)); /* dsoff */
218                         SSVAL(cli->outbuf,smb_sdsdisp,tot_data);        /* dsdisp */
219                         if (trans==SMBtrans2)
220                                 SSVALS(cli->outbuf,smb_sfid,fid);               /* fid */
221                         if (this_lparam)                        /* param[] */
222                                 memcpy(outparam,param,this_lparam);
223                         if (this_ldata)                 /* data[] */
224                                 memcpy(outdata,data,this_ldata);
225                         set_message(cli->outbuf,trans==SMBtrans?8:9, /* wcnt, bcc */
226                                     PTR_DIFF(outdata+this_ldata,smb_buf(cli->outbuf)),False);
227                         
228                         show_msg(cli->outbuf);
229                         send_smb(cli->fd,cli->outbuf);
230                         
231                         tot_data += this_ldata;
232                         tot_param += this_lparam;
233                 }
234         }
235
236         return(True);
237 }
238
239
240 /****************************************************************************
241   receive a SMB trans or trans2 response allocating the necessary memory
242   ****************************************************************************/
243 static BOOL cli_receive_trans(struct cli_state *cli,int trans,
244                               char **param, int *param_len,
245                               char **data, int *data_len)
246 {
247         int total_data=0;
248         int total_param=0;
249         int this_data,this_param;
250         
251         *data_len = *param_len = 0;
252         
253         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
254                 return False;
255
256         show_msg(cli->inbuf);
257         
258         /* sanity check */
259         if (CVAL(cli->inbuf,smb_com) != trans) {
260                 DEBUG(0,("Expected %s response, got command 0x%02x\n",
261                          trans==SMBtrans?"SMBtrans":"SMBtrans2", 
262                          CVAL(cli->inbuf,smb_com)));
263                 return(False);
264         }
265         if (CVAL(cli->inbuf,smb_rcls) != 0)
266                 return(False);
267
268         /* parse out the lengths */
269         total_data = SVAL(cli->inbuf,smb_tdrcnt);
270         total_param = SVAL(cli->inbuf,smb_tprcnt);
271
272         /* allocate it */
273         *data = Realloc(*data,total_data);
274         *param = Realloc(*param,total_param);
275
276         while (1)  {
277                 this_data = SVAL(cli->inbuf,smb_drcnt);
278                 this_param = SVAL(cli->inbuf,smb_prcnt);
279
280                 if (this_data + *data_len > total_data ||
281                     this_param + *param_len > total_param) {
282                         DEBUG(1,("Data overflow in cli_receive_trans\n"));
283                         return False;
284                 }
285
286                 if (this_data)
287                         memcpy(*data + SVAL(cli->inbuf,smb_drdisp),
288                                smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_droff),
289                                this_data);
290                 if (this_param)
291                         memcpy(*param + SVAL(cli->inbuf,smb_prdisp),
292                                smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_proff),
293                                this_param);
294                 *data_len += this_data;
295                 *param_len += this_param;
296
297                 /* parse out the total lengths again - they can shrink! */
298                 total_data = SVAL(cli->inbuf,smb_tdrcnt);
299                 total_param = SVAL(cli->inbuf,smb_tprcnt);
300                 
301                 if (total_data <= *data_len && total_param <= *param_len)
302                         break;
303                 
304                 if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
305                         return False;
306
307                 show_msg(cli->inbuf);
308                 
309                 /* sanity check */
310                 if (CVAL(cli->inbuf,smb_com) != trans) {
311                         DEBUG(0,("Expected %s response, got command 0x%02x\n",
312                                  trans==SMBtrans?"SMBtrans":"SMBtrans2", 
313                                  CVAL(cli->inbuf,smb_com)));
314                         return(False);
315                 }
316                 if (CVAL(cli->inbuf,smb_rcls) != 0)
317                         return(False);
318         }
319         
320         return(True);
321 }
322
323 /****************************************************************************
324 Call a remote api on an arbitrary pipe.  takes param, data and setup buffers.
325 ****************************************************************************/
326 BOOL cli_api_pipe(struct cli_state *cli, char *pipe_name, int pipe_name_len,
327                   uint16 *setup, uint32 setup_count, uint32 max_setup_count,
328                   char *params, uint32 param_count, uint32 max_param_count,
329                   char *data, uint32 data_count, uint32 max_data_count,
330                   char **rparam, uint32 *rparam_count,
331                   char **rdata, uint32 *rdata_count)
332 {
333   if(pipe_name_len == 0)
334     pipe_name_len = strlen(pipe_name);
335
336   cli_send_trans(cli, SMBtrans, 
337                  pipe_name, pipe_name_len,
338                  0,0,                         /* fid, flags */
339                  setup, setup_count, max_setup_count,
340                  params, param_count, max_param_count,
341                  data, data_count, max_data_count);
342
343   return (cli_receive_trans(cli, SMBtrans, 
344                             rparam, (int *)rparam_count,
345                             rdata, (int *)rdata_count));
346 }
347
348 /****************************************************************************
349 call a remote api
350 ****************************************************************************/
351 BOOL cli_api(struct cli_state *cli,
352              char *param, int prcnt, int mprcnt,
353              char *data, int drcnt, int mdrcnt,
354              char **rparam, int *rprcnt,
355              char **rdata, int *rdrcnt)
356 {
357   cli_send_trans(cli,SMBtrans,
358                  PIPE_LANMAN,strlen(PIPE_LANMAN), /* Name, length */
359                  0,0,                             /* fid, flags */
360                  NULL,0,0,                /* Setup, length, max */
361                  param, prcnt, mprcnt,    /* Params, length, max */
362                  data, drcnt, mdrcnt      /* Data, length, max */ 
363                 );
364
365   return (cli_receive_trans(cli,SMBtrans,
366                             rparam, rprcnt,
367                             rdata, rdrcnt));
368 }
369
370
371 /****************************************************************************
372 perform a NetWkstaUserLogon
373 ****************************************************************************/
374 BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
375 {
376         char *rparam = NULL;
377         char *rdata = NULL;
378         char *p;
379         int rdrcnt,rprcnt;
380         pstring param;
381
382         memset(param, 0, sizeof(param));
383         
384         /* send a SMBtrans command with api NetWkstaUserLogon */
385         p = param;
386         SSVAL(p,0,132); /* api number */
387         p += 2;
388         pstrcpy(p,"OOWb54WrLh");
389         p = skip_string(p,1);
390         pstrcpy(p,"WB21BWDWWDDDDDDDzzzD");
391         p = skip_string(p,1);
392         SSVAL(p,0,1);
393         p += 2;
394         pstrcpy(p,user);
395         strupper(p);
396         p += 21; p++; p += 15; p++; 
397         pstrcpy(p, workstation); 
398         strupper(p);
399         p += 16;
400         SSVAL(p, 0, BUFFER_SIZE);
401         p += 2;
402         SSVAL(p, 0, BUFFER_SIZE);
403         p += 2;
404         
405         if (cli_api(cli, 
406                     param, PTR_DIFF(p,param),1024,  /* param, length, max */
407                     NULL, 0, BUFFER_SIZE,           /* data, length, max */
408                     &rparam, &rprcnt,               /* return params, return size */
409                     &rdata, &rdrcnt                 /* return data, return size */
410                    )) {
411                 cli->rap_error = SVAL(rparam,0);
412                 p = rdata;
413                 
414                 if (cli->rap_error == 0) {
415                         DEBUG(4,("NetWkstaUserLogon success\n"));
416                         cli->privilages = SVAL(p, 24);
417                         fstrcpy(cli->eff_name,p+2);
418                 } else {
419                         DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
420                 }
421         }
422         
423         if (rparam) free(rparam);
424         if (rdata) free(rdata);
425         return (cli->rap_error == 0);
426 }
427
428 #if UNUSED_CODE
429 /****************************************************************************
430 call a NetShareEnum - try and browse available connections on a host
431 ****************************************************************************/
432 BOOL cli_RNetShareEnum(struct cli_state *cli, void (*fn)(char *, uint32, char *))
433 {
434   char *rparam = NULL;
435   char *rdata = NULL;
436   char *p;
437   int rdrcnt,rprcnt;
438   pstring param;
439   int count = -1;
440
441   /* now send a SMBtrans command with api RNetShareEnum */
442   p = param;
443   SSVAL(p,0,0); /* api number */
444   p += 2;
445   pstrcpy(p,"WrLeh");
446   p = skip_string(p,1);
447   pstrcpy(p,"B13BWz");
448   p = skip_string(p,1);
449   SSVAL(p,0,1);
450   SSVAL(p,2,BUFFER_SIZE);
451   p += 4;
452
453   if (cli_api(cli, 
454               param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
455               NULL, 0, BUFFER_SIZE,            /* data, length, maxlen */
456               &rparam, &rprcnt,                /* return params, length */
457               &rdata, &rdrcnt))                /* return data, length */
458     {
459       int res = SVAL(rparam,0);
460       int converter=SVAL(rparam,2);
461       int i;
462       
463       if (res == 0)
464         {
465           count=SVAL(rparam,4);
466           p = rdata;
467
468           for (i=0;i<count;i++,p+=20)
469             {
470               char *sname = p;
471               int type = SVAL(p,14);
472               int comment_offset = IVAL(p,16) & 0xFFFF;
473               char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
474               fn(sname, type, cmnt);
475             }
476         }
477     }
478   
479   if (rparam) free(rparam);
480   if (rdata) free(rdata);
481
482   return(count>0);
483 }
484 #endif
485
486 /****************************************************************************
487 call a NetServerEnum for the specified workgroup and servertype mask.
488 This function then calls the specified callback function for each name returned.
489
490 The callback function takes 3 arguments: the machine name, the server type and
491 the comment.
492 ****************************************************************************/
493 BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
494                        void (*fn)(char *, uint32, char *))
495 {
496         char *rparam = NULL;
497         char *rdata = NULL;
498         int rdrcnt,rprcnt;
499         char *p;
500         pstring param;
501         int uLevel = 1;
502         int count = -1;
503   
504         /* send a SMBtrans command with api NetServerEnum */
505         p = param;
506         SSVAL(p,0,0x68); /* api number */
507         p += 2;
508         pstrcpy(p,"WrLehDz");
509         p = skip_string(p,1);
510   
511         pstrcpy(p,"B16BBDz");
512   
513         p = skip_string(p,1);
514         SSVAL(p,0,uLevel);
515         SSVAL(p,2,BUFFER_SIZE);
516         p += 4;
517         SIVAL(p,0,stype);
518         p += 4;
519         
520         pstrcpy(p, workgroup);
521         p = skip_string(p,1);
522         
523         if (cli_api(cli, 
524                     param, PTR_DIFF(p,param), 8,        /* params, length, max */
525                     NULL, 0, BUFFER_SIZE,               /* data, length, max */
526                     &rparam, &rprcnt,                   /* return params, return size */
527                     &rdata, &rdrcnt                     /* return data, return size */
528                    )) {
529                 int res = SVAL(rparam,0);
530                 int converter=SVAL(rparam,2);
531                 int i;
532                         
533                 if (res == 0) {
534                         count=SVAL(rparam,4);
535                         p = rdata;
536                                         
537                         for (i = 0;i < count;i++, p += 26) {
538                                 char *sname = p;
539                                 int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
540                                 char *cmnt = comment_offset?(rdata+comment_offset):"";
541                                 if (comment_offset < 0 || comment_offset > rdrcnt) continue;
542
543                                 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
544
545                                 fn(sname, stype, cmnt);
546                         }
547                 }
548         }
549   
550         if (rparam) free(rparam);
551         if (rdata) free(rdata);
552         
553         return(count > 0);
554 }
555
556
557
558
559 static  struct {
560     int prot;
561     char *name;
562   }
563 prots[] = 
564     {
565       {PROTOCOL_CORE,"PC NETWORK PROGRAM 1.0"},
566       {PROTOCOL_COREPLUS,"MICROSOFT NETWORKS 1.03"},
567       {PROTOCOL_LANMAN1,"MICROSOFT NETWORKS 3.0"},
568       {PROTOCOL_LANMAN1,"LANMAN1.0"},
569       {PROTOCOL_LANMAN2,"LM1.2X002"},
570       {PROTOCOL_LANMAN2,"Samba"},
571       {PROTOCOL_NT1,"NT LM 0.12"},
572       {PROTOCOL_NT1,"NT LANMAN 1.0"},
573       {-1,NULL}
574     };
575
576
577 /****************************************************************************
578 send a session setup
579 ****************************************************************************/
580 BOOL cli_session_setup(struct cli_state *cli, 
581                        char *user, 
582                        char *pass, int passlen,
583                        char *ntpass, int ntpasslen,
584                        char *workgroup)
585 {
586         char *p;
587         fstring pword;
588
589         if (cli->protocol < PROTOCOL_LANMAN1)
590                 return True;
591
592         if (passlen > sizeof(pword)-1) {
593                 return False;
594         }
595
596         if(((passlen == 0) || (passlen == 1)) && (pass[0] == '\0')) {
597           /* Null session connect. */
598           pword[0] = '\0';
599         } else {
600           if ((cli->sec_mode & 2) && passlen != 24) {
601             passlen = 24;
602             SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
603           } else {
604             memcpy(pword, pass, passlen);
605           }
606         }
607
608         /* if in share level security then don't send a password now */
609         if (!(cli->sec_mode & 1)) {fstrcpy(pword, "");passlen=1;} 
610
611         /* send a session setup command */
612         bzero(cli->outbuf,smb_size);
613
614         if (cli->protocol < PROTOCOL_NT1) {
615                 set_message(cli->outbuf,10,1 + strlen(user) + passlen,True);
616                 CVAL(cli->outbuf,smb_com) = SMBsesssetupX;
617                 cli_setup_packet(cli);
618
619                 CVAL(cli->outbuf,smb_vwv0) = 0xFF;
620                 SSVAL(cli->outbuf,smb_vwv2,cli->max_xmit);
621                 SSVAL(cli->outbuf,smb_vwv3,2);
622                 SSVAL(cli->outbuf,smb_vwv4,1);
623                 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
624                 SSVAL(cli->outbuf,smb_vwv7,passlen);
625                 p = smb_buf(cli->outbuf);
626                 memcpy(p,pword,passlen);
627                 p += passlen;
628                 pstrcpy(p,user);
629                 strupper(p);
630         } else {
631                 set_message(cli->outbuf,13,0,True);
632                 CVAL(cli->outbuf,smb_com) = SMBsesssetupX;
633                 cli_setup_packet(cli);
634                 
635                 CVAL(cli->outbuf,smb_vwv0) = 0xFF;
636                 SSVAL(cli->outbuf,smb_vwv2,BUFFER_SIZE);
637                 SSVAL(cli->outbuf,smb_vwv3,2);
638                 SSVAL(cli->outbuf,smb_vwv4,cli->pid);
639                 SIVAL(cli->outbuf,smb_vwv5,cli->sesskey);
640                 SSVAL(cli->outbuf,smb_vwv7,passlen);
641                 SSVAL(cli->outbuf,smb_vwv8,ntpasslen);
642                 p = smb_buf(cli->outbuf);
643                 memcpy(p,pword,passlen); 
644                 p += SVAL(cli->outbuf,smb_vwv7);
645                 memcpy(p,ntpass,ntpasslen); 
646                 p += SVAL(cli->outbuf,smb_vwv8);
647                 pstrcpy(p,user);
648                 strupper(p);
649                 p = skip_string(p,1);
650                 pstrcpy(p,workgroup);
651                 strupper(p);
652                 p = skip_string(p,1);
653                 pstrcpy(p,"Unix");p = skip_string(p,1);
654                 pstrcpy(p,"Samba");p = skip_string(p,1);
655                 set_message(cli->outbuf,13,PTR_DIFF(p,smb_buf(cli->outbuf)),False);
656         }
657
658       send_smb(cli->fd,cli->outbuf);
659       if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
660               return False;
661
662       show_msg(cli->inbuf);
663
664       if (CVAL(cli->inbuf,smb_rcls) != 0) {
665               return False;
666       }
667
668       /* use the returned uid from now on */
669       cli->uid = SVAL(cli->inbuf,smb_uid);
670
671       return True;
672 }
673
674 /****************************************************************************
675  Send a uloggoff.
676 *****************************************************************************/
677
678 BOOL cli_ulogoff(struct cli_state *cli)
679 {
680         bzero(cli->outbuf,smb_size);
681         set_message(cli->outbuf,2,0,True);
682         CVAL(cli->outbuf,smb_com) = SMBulogoffX;
683         cli_setup_packet(cli);
684         SSVAL(cli->outbuf,smb_vwv0,0xFF);
685         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
686
687         send_smb(cli->fd,cli->outbuf);
688         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
689                 return False;
690
691         return CVAL(cli->inbuf,smb_rcls) == 0;
692 }
693
694 /****************************************************************************
695 send a tconX
696 ****************************************************************************/
697 BOOL cli_send_tconX(struct cli_state *cli, 
698                     char *share, char *dev, char *pass, int passlen)
699 {
700         fstring fullshare, pword;
701         char *p;
702         bzero(cli->outbuf,smb_size);
703         bzero(cli->inbuf,smb_size);
704
705         if (cli->sec_mode & 1) {
706                 passlen = 1;
707                 pass = "";
708         }
709
710         if ((cli->sec_mode & 2) && *pass && passlen != 24) {
711                 passlen = 24;
712                 SMBencrypt((uchar *)pass,(uchar *)cli->cryptkey,(uchar *)pword);
713         } else {
714                 memcpy(pword, pass, passlen);
715         }
716
717         slprintf(fullshare, sizeof(fullshare)-1,
718                  "\\\\%s\\%s", cli->desthost, share);
719
720         set_message(cli->outbuf,4,
721                     2 + strlen(fullshare) + passlen + strlen(dev),True);
722         CVAL(cli->outbuf,smb_com) = SMBtconX;
723         cli_setup_packet(cli);
724
725         SSVAL(cli->outbuf,smb_vwv0,0xFF);
726         SSVAL(cli->outbuf,smb_vwv3,passlen);
727
728         p = smb_buf(cli->outbuf);
729         memcpy(p,pword,passlen);
730         p += passlen;
731         fstrcpy(p,fullshare);
732         p = skip_string(p,1);
733         pstrcpy(p,dev);
734
735         SCVAL(cli->inbuf,smb_rcls, 1);
736
737         send_smb(cli->fd,cli->outbuf);
738         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
739                 return False;
740
741         if (CVAL(cli->inbuf,smb_rcls) != 0) {
742                 return False;
743         }
744
745         cli->cnum = SVAL(cli->inbuf,smb_tid);
746         return True;
747 }
748
749
750 /****************************************************************************
751 send a tree disconnect
752 ****************************************************************************/
753 BOOL cli_tdis(struct cli_state *cli)
754 {
755         bzero(cli->outbuf,smb_size);
756         set_message(cli->outbuf,0,0,True);
757         CVAL(cli->outbuf,smb_com) = SMBtdis;
758         SSVAL(cli->outbuf,smb_tid,cli->cnum);
759         cli_setup_packet(cli);
760         
761         send_smb(cli->fd,cli->outbuf);
762         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
763                 return False;
764         
765         return CVAL(cli->inbuf,smb_rcls) == 0;
766 }
767
768 #if UNUSED_CODE
769 /****************************************************************************
770 rename a file
771 ****************************************************************************/
772 BOOL cli_mv(struct cli_state *cli, char *fname_src, char *fname_dst)
773 {
774         char *p;
775
776         bzero(cli->outbuf,smb_size);
777         bzero(cli->inbuf,smb_size);
778
779         set_message(cli->outbuf,1, 4 + strlen(fname_src) + strlen(fname_dst), True);
780
781         CVAL(cli->outbuf,smb_com) = SMBmv;
782         SSVAL(cli->outbuf,smb_tid,cli->cnum);
783         cli_setup_packet(cli);
784
785         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
786
787         p = smb_buf(cli->outbuf);
788         *p++ = 4;
789         pstrcpy(p,fname_src);
790         p = skip_string(p,1);
791         *p++ = 4;
792         pstrcpy(p,fname_dst);
793
794         send_smb(cli->fd,cli->outbuf);
795         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
796                 return False;
797         }
798
799         if (CVAL(cli->inbuf,smb_rcls) != 0) {
800                 return False;
801         }
802
803         return True;
804 }
805 #endif
806
807 /****************************************************************************
808 delete a file
809 ****************************************************************************/
810 BOOL cli_unlink(struct cli_state *cli, char *fname)
811 {
812         char *p;
813
814         bzero(cli->outbuf,smb_size);
815         bzero(cli->inbuf,smb_size);
816
817         set_message(cli->outbuf,1, 2 + strlen(fname),True);
818
819         CVAL(cli->outbuf,smb_com) = SMBunlink;
820         SSVAL(cli->outbuf,smb_tid,cli->cnum);
821         cli_setup_packet(cli);
822
823         SSVAL(cli->outbuf,smb_vwv0,aSYSTEM | aHIDDEN);
824   
825         p = smb_buf(cli->outbuf);
826         *p++ = 4;      
827         pstrcpy(p,fname);
828
829         send_smb(cli->fd,cli->outbuf);
830         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
831                 return False;
832         }
833
834         if (CVAL(cli->inbuf,smb_rcls) != 0) {
835                 return False;
836         }
837
838         return True;
839 }
840
841
842 /****************************************************************************
843 create a directory
844 ****************************************************************************/
845 BOOL cli_mkdir(struct cli_state *cli, char *dname)
846 {
847         char *p;
848
849         bzero(cli->outbuf,smb_size);
850         bzero(cli->inbuf,smb_size);
851
852         set_message(cli->outbuf,0, 2 + strlen(dname),True);
853
854         CVAL(cli->outbuf,smb_com) = SMBmkdir;
855         SSVAL(cli->outbuf,smb_tid,cli->cnum);
856         cli_setup_packet(cli);
857
858         p = smb_buf(cli->outbuf);
859         *p++ = 4;      
860         pstrcpy(p,dname);
861
862         send_smb(cli->fd,cli->outbuf);
863         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
864                 return False;
865         }
866
867         if (CVAL(cli->inbuf,smb_rcls) != 0) {
868                 return False;
869         }
870
871         return True;
872 }
873
874 /****************************************************************************
875 remove a directory
876 ****************************************************************************/
877 BOOL cli_rmdir(struct cli_state *cli, char *dname)
878 {
879         char *p;
880
881         bzero(cli->outbuf,smb_size);
882         bzero(cli->inbuf,smb_size);
883
884         set_message(cli->outbuf,0, 2 + strlen(dname),True);
885
886         CVAL(cli->outbuf,smb_com) = SMBrmdir;
887         SSVAL(cli->outbuf,smb_tid,cli->cnum);
888         cli_setup_packet(cli);
889
890         p = smb_buf(cli->outbuf);
891         *p++ = 4;      
892         pstrcpy(p,dname);
893
894         send_smb(cli->fd,cli->outbuf);
895         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
896                 return False;
897         }
898
899         if (CVAL(cli->inbuf,smb_rcls) != 0) {
900                 return False;
901         }
902
903         return True;
904 }
905
906
907
908 /****************************************************************************
909 open a file
910 ****************************************************************************/
911 int cli_open(struct cli_state *cli, char *fname, int flags, int share_mode)
912 {
913         char *p;
914         unsigned openfn=0;
915         unsigned accessmode=0;
916
917         if (flags & O_CREAT)
918                 openfn |= (1<<4);
919         if (!(flags & O_EXCL)) {
920                 if (flags & O_TRUNC)
921                         openfn |= (1<<1);
922                 else
923                         openfn |= (1<<0);
924         }
925
926         accessmode = (share_mode<<4);
927
928         if ((flags & O_RDWR) == O_RDWR) {
929                 accessmode |= 2;
930         } else if ((flags & O_WRONLY) == O_WRONLY) {
931                 accessmode |= 1;
932         } 
933
934 #if defined(O_SYNC)
935         if ((flags & O_SYNC) == O_SYNC) {
936                 accessmode |= (1<<14);
937         }
938 #endif /* O_SYNC */
939
940         bzero(cli->outbuf,smb_size);
941         bzero(cli->inbuf,smb_size);
942
943         set_message(cli->outbuf,15,1 + strlen(fname),True);
944
945         CVAL(cli->outbuf,smb_com) = SMBopenX;
946         SSVAL(cli->outbuf,smb_tid,cli->cnum);
947         cli_setup_packet(cli);
948
949         SSVAL(cli->outbuf,smb_vwv0,0xFF);
950         SSVAL(cli->outbuf,smb_vwv2,0);  /* no additional info */
951         SSVAL(cli->outbuf,smb_vwv3,accessmode);
952         SSVAL(cli->outbuf,smb_vwv4,aSYSTEM | aHIDDEN);
953         SSVAL(cli->outbuf,smb_vwv5,0);
954         SSVAL(cli->outbuf,smb_vwv8,openfn);
955   
956         p = smb_buf(cli->outbuf);
957         pstrcpy(p,fname);
958         p = skip_string(p,1);
959
960         send_smb(cli->fd,cli->outbuf);
961         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
962                 return -1;
963         }
964
965         if (CVAL(cli->inbuf,smb_rcls) != 0) {
966                 return -1;
967         }
968
969         return SVAL(cli->inbuf,smb_vwv2);
970 }
971
972
973
974
975 /****************************************************************************
976   close a file
977 ****************************************************************************/
978 BOOL cli_close(struct cli_state *cli, int fnum)
979 {
980         bzero(cli->outbuf,smb_size);
981         bzero(cli->inbuf,smb_size);
982
983         set_message(cli->outbuf,3,0,True);
984
985         CVAL(cli->outbuf,smb_com) = SMBclose;
986         SSVAL(cli->outbuf,smb_tid,cli->cnum);
987         cli_setup_packet(cli);
988
989         SSVAL(cli->outbuf,smb_vwv0,fnum);
990         SIVALS(cli->outbuf,smb_vwv1,-1);
991
992         send_smb(cli->fd,cli->outbuf);
993         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
994                 return False;
995         }
996
997         if (CVAL(cli->inbuf,smb_rcls) != 0) {
998                 return False;
999         }
1000
1001         return True;
1002 }
1003
1004
1005 /****************************************************************************
1006   lock a file
1007 ****************************************************************************/
1008 BOOL cli_lock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int timeout)
1009 {
1010         char *p;
1011
1012         bzero(cli->outbuf,smb_size);
1013         bzero(cli->inbuf,smb_size);
1014
1015         set_message(cli->outbuf,8,10,True);
1016
1017         CVAL(cli->outbuf,smb_com) = SMBlockingX;
1018         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1019         cli_setup_packet(cli);
1020
1021         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
1022         SSVAL(cli->outbuf,smb_vwv2,fnum);
1023         CVAL(cli->outbuf,smb_vwv3) = 0;
1024         SIVALS(cli->outbuf, smb_vwv4, timeout);
1025         SSVAL(cli->outbuf,smb_vwv6,0);
1026         SSVAL(cli->outbuf,smb_vwv7,1);
1027
1028         p = smb_buf(cli->outbuf);
1029         SSVAL(p, 0, cli->pid);
1030         SIVAL(p, 2, offset);
1031         SIVAL(p, 6, len);
1032
1033         send_smb(cli->fd,cli->outbuf);
1034         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
1035                 return False;
1036         }
1037
1038         if (CVAL(cli->inbuf,smb_rcls) != 0) {
1039                 return False;
1040         }
1041
1042         return True;
1043 }
1044
1045 /****************************************************************************
1046   unlock a file
1047 ****************************************************************************/
1048 BOOL cli_unlock(struct cli_state *cli, int fnum, uint32 offset, uint32 len, int timeout)
1049 {
1050         char *p;
1051
1052         bzero(cli->outbuf,smb_size);
1053         bzero(cli->inbuf,smb_size);
1054
1055         set_message(cli->outbuf,8,10,True);
1056
1057         CVAL(cli->outbuf,smb_com) = SMBlockingX;
1058         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1059         cli_setup_packet(cli);
1060
1061         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
1062         SSVAL(cli->outbuf,smb_vwv2,fnum);
1063         CVAL(cli->outbuf,smb_vwv3) = 0;
1064         SIVALS(cli->outbuf, smb_vwv4, timeout);
1065         SSVAL(cli->outbuf,smb_vwv6,1);
1066         SSVAL(cli->outbuf,smb_vwv7,0);
1067
1068         p = smb_buf(cli->outbuf);
1069         SSVAL(p, 0, cli->pid);
1070         SIVAL(p, 2, offset);
1071         SIVAL(p, 6, len);
1072
1073         send_smb(cli->fd,cli->outbuf);
1074         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
1075                 return False;
1076         }
1077
1078         if (CVAL(cli->inbuf,smb_rcls) != 0) {
1079                 return False;
1080         }
1081
1082         return True;
1083 }
1084
1085
1086 /****************************************************************************
1087   read from a file
1088 ****************************************************************************/
1089 int cli_read(struct cli_state *cli, int fnum, char *buf, uint32 offset, uint16 size)
1090 {
1091         char *p;
1092
1093         bzero(cli->outbuf,smb_size);
1094         bzero(cli->inbuf,smb_size);
1095
1096         set_message(cli->outbuf,10,0,True);
1097
1098         CVAL(cli->outbuf,smb_com) = SMBreadX;
1099         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1100         cli_setup_packet(cli);
1101
1102         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
1103         SSVAL(cli->outbuf,smb_vwv2,fnum);
1104         SIVAL(cli->outbuf,smb_vwv3,offset);
1105         SSVAL(cli->outbuf,smb_vwv5,size);
1106         SSVAL(cli->outbuf,smb_vwv6,size);
1107
1108         send_smb(cli->fd,cli->outbuf);
1109         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
1110                 return -1;
1111         }
1112
1113         if (CVAL(cli->inbuf,smb_rcls) != 0) {
1114                 return -1;
1115         }
1116
1117         size = SVAL(cli->inbuf, smb_vwv5);
1118         p = smb_base(cli->inbuf) + SVAL(cli->inbuf,smb_vwv6);
1119
1120         memcpy(buf, p, size);
1121
1122         return size;
1123 }
1124
1125
1126 /****************************************************************************
1127   write to a file
1128 ****************************************************************************/
1129 int cli_write(struct cli_state *cli, int fnum, char *buf, uint32 offset, uint16 size)
1130 {
1131         char *p;
1132
1133         bzero(cli->outbuf,smb_size);
1134         bzero(cli->inbuf,smb_size);
1135
1136         set_message(cli->outbuf,12,size,True);
1137
1138         CVAL(cli->outbuf,smb_com) = SMBwriteX;
1139         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1140         cli_setup_packet(cli);
1141
1142         CVAL(cli->outbuf,smb_vwv0) = 0xFF;
1143         SSVAL(cli->outbuf,smb_vwv2,fnum);
1144         SIVAL(cli->outbuf,smb_vwv3,offset);
1145
1146         SSVAL(cli->outbuf,smb_vwv10,size);
1147         SSVAL(cli->outbuf,smb_vwv11,smb_buf(cli->outbuf) - smb_base(cli->outbuf));
1148
1149         p = smb_base(cli->outbuf) + SVAL(cli->outbuf,smb_vwv11);
1150         memcpy(p, buf, size);
1151
1152         send_smb(cli->fd,cli->outbuf);
1153         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
1154                 return -1;
1155         }
1156
1157         if (CVAL(cli->inbuf,smb_rcls) != 0) {
1158                 return -1;
1159         }
1160
1161         return SVAL(cli->inbuf, smb_vwv2);
1162 }
1163
1164
1165 /****************************************************************************
1166 do a SMBgetatr call
1167 ****************************************************************************/
1168 BOOL cli_getatr(struct cli_state *cli, char *fname, 
1169                 int *attr, uint32 *size, time_t *t)
1170 {
1171         char *p;
1172
1173         bzero(cli->outbuf,smb_size);
1174         bzero(cli->inbuf,smb_size);
1175
1176         set_message(cli->outbuf,0,strlen(fname)+2,True);
1177
1178         CVAL(cli->outbuf,smb_com) = SMBgetatr;
1179         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1180         cli_setup_packet(cli);
1181
1182         p = smb_buf(cli->outbuf);
1183         *p = 4;
1184         pstrcpy(p+1, fname);
1185
1186         send_smb(cli->fd,cli->outbuf);
1187         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
1188                 return False;
1189         }
1190         
1191         if (CVAL(cli->inbuf,smb_rcls) != 0) {
1192                 return False;
1193         }
1194
1195         if (size) {
1196                 *size = IVAL(cli->inbuf, smb_vwv3);
1197         }
1198
1199         if (t) {
1200                 *t = make_unix_date3(cli->inbuf+smb_vwv1);
1201         }
1202
1203         if (attr) {
1204                 *attr = SVAL(cli->inbuf,smb_vwv0);
1205         }
1206
1207
1208         return True;
1209 }
1210
1211
1212 /****************************************************************************
1213 do a SMBsetatr call
1214 ****************************************************************************/
1215 BOOL cli_setatr(struct cli_state *cli, char *fname, int attr, time_t t)
1216 {
1217         char *p;
1218
1219         bzero(cli->outbuf,smb_size);
1220         bzero(cli->inbuf,smb_size);
1221
1222         set_message(cli->outbuf,8,strlen(fname)+4,True);
1223
1224         CVAL(cli->outbuf,smb_com) = SMBsetatr;
1225         SSVAL(cli->outbuf,smb_tid,cli->cnum);
1226         cli_setup_packet(cli);
1227
1228         SSVAL(cli->outbuf,smb_vwv0, attr);
1229         put_dos_date3(cli->outbuf,smb_vwv1, t);
1230
1231         p = smb_buf(cli->outbuf);
1232         *p = 4;
1233         pstrcpy(p+1, fname);
1234         p = skip_string(p,1);
1235         *p = 4;
1236
1237         send_smb(cli->fd,cli->outbuf);
1238         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout)) {
1239                 return False;
1240         }
1241         
1242         if (CVAL(cli->inbuf,smb_rcls) != 0) {
1243                 return False;
1244         }
1245
1246         return True;
1247 }
1248
1249 /****************************************************************************
1250 send a qpathinfo call
1251 ****************************************************************************/
1252 BOOL cli_qpathinfo(struct cli_state *cli, char *fname, 
1253                    time_t *c_time, time_t *a_time, time_t *m_time, uint32 *size)
1254 {
1255         int data_len = 0;
1256         int param_len = 0;
1257         uint16 setup = TRANSACT2_QPATHINFO;
1258         pstring param;
1259         char *rparam=NULL, *rdata=NULL;
1260
1261         param_len = strlen(fname) + 7;
1262
1263         memset(param, 0, param_len);
1264         SSVAL(param, 0, SMB_INFO_STANDARD);
1265         pstrcpy(&param[6], fname);
1266
1267         if (!cli_send_trans(cli, SMBtrans2, 
1268                             NULL, 0,                      /* Name, length */
1269                             -1, 0,                        /* fid, flags */
1270                             &setup, 1, 0,                 /* setup, length, max */
1271                             param, param_len, 10,         /* param, length, max */
1272                             NULL, data_len, cli->max_xmit /* data, length, max */
1273                            )) {
1274                 return False;
1275         }
1276
1277         if (!cli_receive_trans(cli, SMBtrans2, 
1278                                &rparam, &param_len,
1279                                &rdata, &data_len)) {
1280                 return False;
1281         }
1282
1283         if (!rdata || data_len < 22) {
1284                 return False;
1285         }
1286
1287         if (c_time) {
1288                 *c_time = make_unix_date2(rdata+0);
1289         }
1290         if (a_time) {
1291                 *a_time = make_unix_date2(rdata+4);
1292         }
1293         if (m_time) {
1294                 *m_time = make_unix_date2(rdata+8);
1295         }
1296         if (size) {
1297                 *size = IVAL(rdata, 12);
1298         }
1299
1300         if (rdata) free(rdata);
1301         if (rparam) free(rparam);
1302         return True;
1303 }
1304
1305 /****************************************************************************
1306 send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
1307 ****************************************************************************/
1308 BOOL cli_qpathinfo2(struct cli_state *cli, char *fname, 
1309                     time_t *c_time, time_t *a_time, time_t *m_time, 
1310                     time_t *w_time, uint32 *size)
1311 {
1312         int data_len = 0;
1313         int param_len = 0;
1314         uint16 setup = TRANSACT2_QPATHINFO;
1315         pstring param;
1316         char *rparam=NULL, *rdata=NULL;
1317
1318         param_len = strlen(fname) + 7;
1319
1320         memset(param, 0, param_len);
1321         SSVAL(param, 0, SMB_QUERY_FILE_ALL_INFO);
1322         pstrcpy(&param[6], fname);
1323
1324         if (!cli_send_trans(cli, SMBtrans2, 
1325                             NULL, 0,                      /* name, length */
1326                             -1, 0,                        /* fid, flags */
1327                             &setup, 1, 0,                 /* setup, length, max */
1328                             param, param_len, 10,         /* param, length, max */
1329                             NULL, data_len, cli->max_xmit /* data, length, max */
1330                            )) {
1331                 return False;
1332         }
1333
1334         if (!cli_receive_trans(cli, SMBtrans2,
1335                                &rparam, &param_len,
1336                                &rdata, &data_len)) {
1337                 return False;
1338         }
1339
1340         if (!rdata || data_len < 22) {
1341                 return False;
1342         }
1343
1344         if (c_time) {
1345                 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
1346         }
1347         if (a_time) {
1348                 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
1349         }
1350         if (m_time) {
1351                 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
1352         }
1353         if (w_time) {
1354                 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
1355         }
1356         if (size) {
1357                 *size = IVAL(rdata, 40);
1358         }
1359
1360         if (rdata) free(rdata);
1361         if (rparam) free(rparam);
1362         return True;
1363 }
1364
1365
1366 /****************************************************************************
1367 send a qfileinfo call
1368 ****************************************************************************/
1369 BOOL cli_qfileinfo(struct cli_state *cli, int fnum, 
1370                    time_t *c_time, time_t *a_time, time_t *m_time, uint32 *size)
1371 {
1372         int data_len = 0;
1373         int param_len = 0;
1374         uint16 setup = TRANSACT2_QFILEINFO;
1375         pstring param;
1376         char *rparam=NULL, *rdata=NULL;
1377
1378         param_len = 4;
1379
1380         memset(param, 0, param_len);
1381         SSVAL(param, 0, fnum);
1382         SSVAL(param, 2, SMB_INFO_STANDARD);
1383
1384         if (!cli_send_trans(cli, SMBtrans2, 
1385                             NULL, 0,                        /* name, length */
1386                             -1, 0,                          /* fid, flags */
1387                             &setup, 1, 0,                   /* setup, length, max */
1388                             param, param_len, 2,            /* param, length, max */
1389                             NULL, data_len, cli->max_xmit   /* data, length, max */
1390                            )) {
1391                 return False;
1392         }
1393
1394         if (!cli_receive_trans(cli, SMBtrans2,
1395                                &rparam, &param_len,
1396                                &rdata, &data_len)) {
1397                 return False;
1398         }
1399
1400         if (!rdata || data_len < 22) {
1401                 return False;
1402         }
1403
1404         if (c_time) {
1405                 *c_time = make_unix_date2(rdata+0);
1406         }
1407         if (a_time) {
1408                 *a_time = make_unix_date2(rdata+4);
1409         }
1410         if (m_time) {
1411                 *m_time = make_unix_date2(rdata+8);
1412         }
1413         if (size) {
1414                 *size = IVAL(rdata, 12);
1415         }
1416
1417         if (rdata) free(rdata);
1418         if (rparam) free(rparam);
1419         return True;
1420 }
1421
1422 /****************************************************************************
1423 Send a SamOEMChangePassword command
1424 ****************************************************************************/
1425
1426 BOOL cli_oem_change_password(struct cli_state *cli, char *user, char *new_password,
1427                              char *old_password)
1428 {
1429   char param[16+sizeof(fstring)];
1430   char data[532];
1431   char *p = param;
1432   fstring upper_case_old_pw;
1433   fstring upper_case_new_pw;
1434   unsigned char old_pw_hash[16];
1435   unsigned char new_pw_hash[16];
1436   int data_len;
1437   int param_len = 0;
1438   int new_pw_len = strlen(new_password);
1439   char *rparam = NULL;
1440   char *rdata = NULL;
1441   int rprcnt, rdrcnt;
1442
1443   if(strlen(user) >= sizeof(fstring)-1) {
1444     DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
1445     return False;
1446   }
1447
1448   if(new_pw_len > 512) {
1449     DEBUG(0,("cli_oem_change_password: new password for user %s is too long.\n", user));
1450     return False;
1451   }
1452
1453   SSVAL(p,0,214); /* SamOEMChangePassword command. */
1454   p += 2;
1455   pstrcpy(p, "zsT");
1456   p = skip_string(p,1);
1457   pstrcpy(p, "B516B16");
1458   p = skip_string(p,1);
1459   pstrcpy(p,user);
1460   p = skip_string(p,1);
1461   SSVAL(p,0,532);
1462   p += 2;
1463
1464   param_len = PTR_DIFF(p,param);
1465
1466   /*
1467    * Now setup the data area.
1468    * We need to generate a random fill
1469    * for this area to make it harder to
1470    * decrypt. JRA.
1471    */
1472   generate_random_buffer((unsigned char *)data, sizeof(data), False);
1473   fstrcpy( &data[512 - new_pw_len], new_password);
1474   SIVAL(data, 512, new_pw_len);
1475
1476   /*
1477    * Get the Lanman hash of the old password, we
1478    * use this as the key to SamOEMHash().
1479    */
1480   memset(upper_case_old_pw, '\0', sizeof(upper_case_old_pw));
1481   fstrcpy(upper_case_old_pw, old_password);
1482   strupper(upper_case_old_pw);
1483   E_P16((uchar *)upper_case_old_pw, old_pw_hash);
1484
1485   SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, True);
1486
1487   /* 
1488    * Now place the old password hash in the data.
1489    */
1490   memset(upper_case_new_pw, '\0', sizeof(upper_case_new_pw));
1491   fstrcpy(upper_case_new_pw, new_password);
1492   strupper(upper_case_new_pw);
1493
1494   E_P16((uchar *)upper_case_new_pw, new_pw_hash);
1495
1496   E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
1497
1498   data_len = 532;
1499     
1500   if(cli_send_trans(cli,SMBtrans,
1501                     PIPE_LANMAN,strlen(PIPE_LANMAN),      /* name, length */
1502                     0,0,                                  /* fid, flags */
1503                     NULL,0,0,                             /* setup, length, max */
1504                     param,param_len,2,                    /* param, length, max */
1505                     data,data_len,0                       /* data, length, max */
1506                    ) == False) {
1507     DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
1508               user ));
1509     return False;
1510   }
1511
1512   if(cli_receive_trans(cli,SMBtrans,
1513                        &rparam, &rprcnt,
1514                        &rdata, &rdrcnt)) {
1515     if(rparam)
1516       cli->rap_error = SVAL(rparam,0);
1517   }
1518
1519   if (rparam)
1520     free(rparam);
1521   if (rdata)
1522     free(rdata);
1523
1524   return (cli->rap_error == 0);
1525 }
1526
1527 /****************************************************************************
1528 send a negprot command
1529 ****************************************************************************/
1530 BOOL cli_negprot(struct cli_state *cli)
1531 {
1532         char *p;
1533         int numprots;
1534         int plength;
1535
1536         bzero(cli->outbuf,smb_size);
1537
1538         /* setup the protocol strings */
1539         for (plength=0,numprots=0;
1540              prots[numprots].name && prots[numprots].prot<=cli->protocol;
1541              numprots++)
1542                 plength += strlen(prots[numprots].name)+2;
1543     
1544         set_message(cli->outbuf,0,plength,True);
1545
1546         p = smb_buf(cli->outbuf);
1547         for (numprots=0;
1548              prots[numprots].name && prots[numprots].prot<=cli->protocol;
1549              numprots++) {
1550                 *p++ = 2;
1551                 pstrcpy(p,prots[numprots].name);
1552                 p += strlen(p) + 1;
1553         }
1554
1555         CVAL(cli->outbuf,smb_com) = SMBnegprot;
1556         cli_setup_packet(cli);
1557
1558         CVAL(smb_buf(cli->outbuf),0) = 2;
1559
1560         send_smb(cli->fd,cli->outbuf);
1561         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
1562                 return False;
1563
1564         show_msg(cli->inbuf);
1565
1566         if (CVAL(cli->inbuf,smb_rcls) != 0 || 
1567             ((int)SVAL(cli->inbuf,smb_vwv0) >= numprots)) {
1568                 return(False);
1569         }
1570
1571         cli->protocol = prots[SVAL(cli->inbuf,smb_vwv0)].prot;
1572
1573
1574         if (cli->protocol >= PROTOCOL_NT1) {    
1575                 /* NT protocol */
1576                 cli->sec_mode = CVAL(cli->inbuf,smb_vwv1);
1577                 cli->max_xmit = IVAL(cli->inbuf,smb_vwv3+1);
1578                 cli->sesskey = IVAL(cli->inbuf,smb_vwv7+1);
1579                 cli->serverzone = SVALS(cli->inbuf,smb_vwv15+1)*60;
1580                 /* this time arrives in real GMT */
1581                 cli->servertime = interpret_long_date(cli->inbuf+smb_vwv11+1);
1582                 memcpy(cli->cryptkey,smb_buf(cli->inbuf),8);
1583                 if (IVAL(cli->inbuf,smb_vwv9+1) & 1)
1584                         cli->readbraw_supported = 
1585                                 cli->writebraw_supported = True;      
1586         } else if (cli->protocol >= PROTOCOL_LANMAN1) {
1587                 cli->sec_mode = SVAL(cli->inbuf,smb_vwv1);
1588                 cli->max_xmit = SVAL(cli->inbuf,smb_vwv2);
1589                 cli->sesskey = IVAL(cli->inbuf,smb_vwv6);
1590                 cli->serverzone = SVALS(cli->inbuf,smb_vwv10)*60;
1591                 /* this time is converted to GMT by make_unix_date */
1592                 cli->servertime = make_unix_date(cli->inbuf+smb_vwv8);
1593                 cli->readbraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x1) != 0);
1594                 cli->writebraw_supported = ((SVAL(cli->inbuf,smb_vwv5) & 0x2) != 0);
1595                 memcpy(cli->cryptkey,smb_buf(cli->inbuf),8);
1596         } else {
1597                 /* the old core protocol */
1598                 cli->sec_mode = 0;
1599                 cli->serverzone = TimeDiff(time(NULL));
1600         }
1601
1602         return True;
1603 }
1604
1605
1606 /****************************************************************************
1607   send a session request
1608 ****************************************************************************/
1609 BOOL cli_session_request(struct cli_state *cli, char *host, int name_type,
1610                          char *myname)
1611 {
1612         fstring dest;
1613         char *p;
1614         int len = 4;
1615         /* send a session request (RFC 1002) */
1616
1617         fstrcpy(dest,host);
1618   
1619         p = strchr(dest,'.');
1620         if (p) *p = 0;
1621
1622         fstrcpy(cli->desthost, dest);
1623
1624         /* put in the destination name */
1625         p = cli->outbuf+len;
1626         name_mangle(dest,p,name_type);
1627         len += name_len(p);
1628
1629         /* and my name */
1630         p = cli->outbuf+len;
1631         name_mangle(myname,p,0);
1632         len += name_len(p);
1633
1634         /* setup the packet length */
1635         _smb_setlen(cli->outbuf,len);
1636         CVAL(cli->outbuf,0) = 0x81;
1637
1638 #ifdef WITH_SSL
1639 retry:
1640 #endif /* WITH_SSL */
1641
1642         send_smb(cli->fd,cli->outbuf);
1643         DEBUG(5,("Sent session request\n"));
1644
1645         if (!client_receive_smb(cli->fd,cli->inbuf,cli->timeout))
1646                 return False;
1647
1648 #ifdef WITH_SSL
1649     if(CVAL(cli->inbuf,0) == 0x83 && CVAL(cli->inbuf,4) == 0x8e){ /* use ssl */
1650         if(!sslutil_fd_is_ssl(cli->fd)){
1651             if(sslutil_connect(cli->fd) == 0)
1652                 goto retry;
1653         }
1654     }
1655 #endif /* WITH_SSL */
1656
1657         if (CVAL(cli->inbuf,0) != 0x82) {
1658                 /* This is the wrong place to put the error... JRA. */
1659                 cli->rap_error = CVAL(cli->inbuf,0);
1660                 return False;
1661         }
1662         return(True);
1663 }
1664
1665
1666 /****************************************************************************
1667 open the client sockets
1668 ****************************************************************************/
1669 BOOL cli_connect(struct cli_state *cli, char *host, struct in_addr *ip)
1670 {
1671         struct in_addr dest_ip;
1672         extern struct in_addr ipzero;
1673
1674         fstrcpy(cli->desthost, host);
1675         
1676         if (!ip || ip_equal(*ip, ipzero)) {
1677                 if(!resolve_name( cli->desthost, &dest_ip)) {
1678                         return False;
1679                 }
1680         } else {
1681                 dest_ip = *ip;
1682         }
1683
1684
1685         cli->fd = open_socket_out(SOCK_STREAM, &dest_ip, 139, cli->timeout);
1686         if (cli->fd == -1)
1687                 return False;
1688
1689         return True;
1690 }
1691
1692
1693 /****************************************************************************
1694 initialise a client structure
1695 ****************************************************************************/
1696 BOOL cli_initialise(struct cli_state *cli)
1697 {
1698         if (cli->initialised) cli_shutdown(cli);
1699
1700         memset(cli, 0, sizeof(*cli));
1701         cli->fd = -1;
1702         cli->cnum = -1;
1703         cli->pid = getpid();
1704         cli->mid = 1;
1705         cli->uid = getuid();
1706         cli->protocol = PROTOCOL_NT1;
1707         cli->timeout = 20000;
1708         cli->bufsize = 0x10000;
1709         cli->max_xmit = cli->bufsize - 4;
1710         cli->outbuf = (char *)malloc(cli->bufsize);
1711         cli->inbuf = (char *)malloc(cli->bufsize);
1712         if (!cli->outbuf || !cli->inbuf) return False;
1713         cli->initialised = 1;
1714         return True;
1715 }
1716
1717 /****************************************************************************
1718 shutdown a client structure
1719 ****************************************************************************/
1720 void cli_shutdown(struct cli_state *cli)
1721 {
1722         if (cli->outbuf) free(cli->outbuf);
1723         if (cli->inbuf) free(cli->inbuf);
1724 #ifdef WITH_SSL
1725     if (cli->fd != -1) sslutil_disconnect(cli->fd);
1726 #endif /* WITH_SSL */
1727         if (cli->fd != -1) close(cli->fd);
1728         memset(cli, 0, sizeof(*cli));
1729 }
1730
1731 /****************************************************************************
1732   return error codes for the last packet
1733 ****************************************************************************/
1734 void cli_error(struct cli_state *cli, int *eclass, int *num)
1735 {
1736         *eclass = CVAL(cli->inbuf,smb_rcls);
1737         *num = SVAL(cli->inbuf,smb_err);
1738 }
1739
1740 /****************************************************************************
1741 set socket options on a open connection
1742 ****************************************************************************/
1743 void cli_sockopt(struct cli_state *cli, char *options)
1744 {
1745         set_socket_options(cli->fd, options);
1746 }
1747
1748 /****************************************************************************
1749 set the PID to use for smb messages. Return the old pid.
1750 ****************************************************************************/
1751 int cli_setpid(struct cli_state *cli, int pid)
1752 {
1753         int ret = cli->pid;
1754         cli->pid = pid;
1755         return ret;
1756 }