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