2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Gerald (Jerry) Carter 2004
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.
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.
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.
24 /****************************************************************************
25 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
26 ****************************************************************************/
28 BOOL
cli_api_pipe(struct cli_state
*cli
, const char *pipe_name
,
29 uint16
*setup
, uint32 setup_count
, uint32 max_setup_count
,
30 char *params
, uint32 param_count
, uint32 max_param_count
,
31 char *data
, uint32 data_count
, uint32 max_data_count
,
32 char **rparam
, uint32
*rparam_count
,
33 char **rdata
, uint32
*rdata_count
)
35 cli_send_trans(cli
, SMBtrans
,
38 setup
, setup_count
, max_setup_count
,
39 params
, param_count
, max_param_count
,
40 data
, data_count
, max_data_count
);
42 return (cli_receive_trans(cli
, SMBtrans
,
43 rparam
, (unsigned int *)rparam_count
,
44 rdata
, (unsigned int *)rdata_count
));
47 /****************************************************************************
49 ****************************************************************************/
51 BOOL
cli_api(struct cli_state
*cli
,
52 char *param
, int prcnt
, int mprcnt
,
53 char *data
, int drcnt
, int mdrcnt
,
54 char **rparam
, unsigned int *rprcnt
,
55 char **rdata
, unsigned int *rdrcnt
)
57 cli_send_trans(cli
,SMBtrans
,
58 PIPE_LANMAN
, /* Name */
60 NULL
,0,0, /* Setup, length, max */
61 param
, prcnt
, mprcnt
, /* Params, length, max */
62 data
, drcnt
, mdrcnt
/* Data, length, max */
65 return (cli_receive_trans(cli
,SMBtrans
,
70 /****************************************************************************
71 Perform a NetWkstaUserLogon.
72 ****************************************************************************/
74 BOOL
cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
79 unsigned int rdrcnt
,rprcnt
;
82 memset(param
, 0, sizeof(param
));
84 /* send a SMBtrans command with api NetWkstaUserLogon */
86 SSVAL(p
,0,132); /* api number */
88 pstrcpy_base(p
,"OOWb54WrLh",param
);
89 p
= skip_string(param
,sizeof(param
),p
);
90 pstrcpy_base(p
,"WB21BWDWWDDDDDDDzzzD",param
);
91 p
= skip_string(param
,sizeof(param
),p
);
94 pstrcpy_base(p
,user
,param
);
100 pstrcpy_base(p
, workstation
, param
);
103 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
105 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
109 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
110 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
111 &rparam
, &rprcnt
, /* return params, return size */
112 &rdata
, &rdrcnt
/* return data, return size */
114 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
117 if (cli
->rap_error
== 0) {
118 DEBUG(4,("NetWkstaUserLogon success\n"));
119 cli
->privileges
= SVAL(p
, 24);
120 /* The cli->eff_name field used to be set here
121 but it wasn't used anywhere else. */
123 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
129 return (cli
->rap_error
== 0);
132 /****************************************************************************
133 Call a NetShareEnum - try and browse available connections on a host.
134 ****************************************************************************/
136 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *, void *), void *state
)
141 unsigned int rdrcnt
,rprcnt
;
145 /* now send a SMBtrans command with api RNetShareEnum */
147 SSVAL(p
,0,0); /* api number */
149 pstrcpy_base(p
,"WrLeh",param
);
150 p
= skip_string(param
,sizeof(param
),p
);
151 pstrcpy_base(p
,"B13BWz",param
);
152 p
= skip_string(param
,sizeof(param
),p
);
155 * Win2k needs a *smaller* buffer than 0xFFFF here -
156 * it returns "out of server memory" with 0xFFFF !!! JRA.
162 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
163 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
164 &rparam
, &rprcnt
, /* return params, length */
165 &rdata
, &rdrcnt
)) /* return data, length */
167 int res
= rparam
? SVAL(rparam
,0) : -1;
169 if (res
== 0 || res
== ERRmoredata
) {
170 int converter
=SVAL(rparam
,2);
173 count
=SVAL(rparam
,4);
176 for (i
=0;i
<count
;i
++,p
+=20) {
178 int type
= SVAL(p
,14);
179 int comment_offset
= IVAL(p
,16) & 0xFFFF;
180 const char *cmnt
= comment_offset
?(rdata
+comment_offset
-converter
):"";
183 pull_ascii_pstring(s1
, sname
);
184 pull_ascii_pstring(s2
, cmnt
);
186 fn(s1
, type
, s2
, state
);
189 DEBUG(4,("NetShareEnum res=%d\n", res
));
192 DEBUG(4,("NetShareEnum failed\n"));
201 /****************************************************************************
202 Call a NetServerEnum for the specified workgroup and servertype mask. This
203 function then calls the specified callback function for each name returned.
205 The callback function takes 4 arguments: the machine name, the server type,
206 the comment and a state pointer.
207 ****************************************************************************/
209 BOOL
cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
210 void (*fn
)(const char *, uint32
, const char *, void *),
215 unsigned int rdrcnt
,rprcnt
;
222 errno
= 0; /* reset */
224 /* send a SMBtrans command with api NetServerEnum */
226 SSVAL(p
,0,0x68); /* api number */
228 pstrcpy_base(p
,"WrLehDz", param
);
229 p
= skip_string(param
,sizeof(param
),p
);
231 pstrcpy_base(p
,"B16BBDz", param
);
233 p
= skip_string(param
,sizeof(param
),p
);
235 SSVAL(p
,2,CLI_BUFFER_SIZE
);
240 len
= push_ascii(p
, workgroup
, sizeof(pstring
)-PTR_DIFF(p
,param
)-1, STR_TERMINATE
|STR_UPPER
);
241 if (len
== (size_t)-1) {
247 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
248 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
249 &rparam
, &rprcnt
, /* return params, return size */
250 &rdata
, &rdrcnt
/* return data, return size */
252 int res
= rparam
? SVAL(rparam
,0) : -1;
254 if (res
== 0 || res
== ERRmoredata
||
255 (res
!= -1 && cli_errno(cli
) == 0)) {
257 int converter
=SVAL(rparam
,2);
259 count
=SVAL(rparam
,4);
262 for (i
= 0;i
< count
;i
++, p
+= 26) {
264 int comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
265 const char *cmnt
= comment_offset
?(rdata
+comment_offset
):"";
268 if (comment_offset
< 0 || comment_offset
> (int)rdrcnt
) continue;
270 stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
272 pull_ascii_pstring(s1
, sname
);
273 pull_ascii_pstring(s2
, cmnt
);
274 fn(s1
, stype
, s2
, state
);
283 errno
= cli_errno(cli
);
286 /* this is a very special case, when the domain master for the
287 work group isn't part of the work group itself, there is something
296 /****************************************************************************
297 Send a SamOEMChangePassword command.
298 ****************************************************************************/
300 BOOL
cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
301 const char *old_password
)
304 unsigned char data
[532];
306 unsigned char old_pw_hash
[16];
307 unsigned char new_pw_hash
[16];
308 unsigned int data_len
;
309 unsigned int param_len
= 0;
312 unsigned int rprcnt
, rdrcnt
;
314 if (strlen(user
) >= sizeof(fstring
)-1) {
315 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
319 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
321 pstrcpy_base(p
, "zsT", param
);
322 p
= skip_string(param
,sizeof(param
),p
);
323 pstrcpy_base(p
, "B516B16", param
);
324 p
= skip_string(param
,sizeof(param
),p
);
325 pstrcpy_base(p
,user
, param
);
326 p
= skip_string(param
,sizeof(param
),p
);
330 param_len
= PTR_DIFF(p
,param
);
333 * Get the Lanman hash of the old password, we
334 * use this as the key to make_oem_passwd_hash().
336 E_deshash(old_password
, old_pw_hash
);
338 encode_pw_buffer(data
, new_password
, STR_ASCII
);
340 #ifdef DEBUG_PASSWORD
341 DEBUG(100,("make_oem_passwd_hash\n"));
342 dump_data(100, (char *)data
, 516);
344 SamOEMhash( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
347 * Now place the old password hash in the data.
349 E_deshash(new_password
, new_pw_hash
);
351 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
355 if (cli_send_trans(cli
,SMBtrans
,
356 PIPE_LANMAN
, /* name */
357 0,0, /* fid, flags */
358 NULL
,0,0, /* setup, length, max */
359 param
,param_len
,2, /* param, length, max */
360 (char *)data
,data_len
,0 /* data, length, max */
362 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
367 if (!cli_receive_trans(cli
,SMBtrans
,
370 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
376 cli
->rap_error
= SVAL(rparam
,0);
382 return (cli
->rap_error
== 0);
385 /****************************************************************************
386 Send a qpathinfo call.
387 ****************************************************************************/
389 BOOL
cli_qpathinfo(struct cli_state
*cli
, const char *fname
,
393 SMB_OFF_T
*size
, uint16
*mode
)
395 unsigned int data_len
= 0;
396 unsigned int param_len
= 0;
397 unsigned int rparam_len
, rdata_len
;
398 uint16 setup
= TRANSACT2_QPATHINFO
;
400 char *rparam
=NULL
, *rdata
=NULL
;
403 time_t (*date_fn
)(struct cli_state
*, void *);
408 SSVAL(p
, 0, SMB_INFO_STANDARD
);
410 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
412 param_len
= PTR_DIFF(p
, param
);
415 ret
= (cli_send_trans(cli
, SMBtrans2
,
417 -1, 0, /* fid, flags */
418 &setup
, 1, 0, /* setup, length, max */
419 param
, param_len
, 10, /* param, length, max */
420 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
422 cli_receive_trans(cli
, SMBtrans2
,
423 &rparam
, &rparam_len
,
424 &rdata
, &rdata_len
));
425 if (!cli_is_dos_error(cli
)) break;
427 /* we need to work around a Win95 bug - sometimes
428 it gives ERRSRV/ERRerror temprarily */
431 cli_dos_error(cli
, &eclass
, &ecode
);
432 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
435 } while (count
-- && ret
==False
);
437 if (!ret
|| !rdata
|| rdata_len
< 22) {
442 date_fn
= cli_make_unix_date
;
444 date_fn
= cli_make_unix_date2
;
448 *change_time
= date_fn(cli
, rdata
+0);
451 *access_time
= date_fn(cli
, rdata
+4);
454 *write_time
= date_fn(cli
, rdata
+8);
457 *size
= IVAL(rdata
, 12);
460 *mode
= SVAL(rdata
,l1_attrFile
);
468 /****************************************************************************
469 Send a setpathinfo call.
470 ****************************************************************************/
472 BOOL
cli_setpathinfo(struct cli_state
*cli
, const char *fname
,
479 unsigned int data_len
= 0;
480 unsigned int param_len
= 0;
481 unsigned int rparam_len
, rdata_len
;
482 uint16 setup
= TRANSACT2_SETPATHINFO
;
485 char *rparam
=NULL
, *rdata
=NULL
;
490 memset(param
, 0, sizeof(param
));
491 memset(data
, 0, sizeof(data
));
495 /* Add the information level */
496 SSVAL(p
, 0, SMB_FILE_BASIC_INFORMATION
);
501 /* Add the file name */
502 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
504 param_len
= PTR_DIFF(p
, param
);
509 * Add the create, last access, modification, and status change times
512 put_long_date(p
, create_time
);
515 put_long_date(p
, access_time
);
518 put_long_date(p
, write_time
);
521 put_long_date(p
, change_time
);
532 data_len
= PTR_DIFF(p
, data
);
535 ret
= (cli_send_trans(cli
, SMBtrans2
,
537 -1, 0, /* fid, flags */
538 &setup
, 1, 0, /* setup, length, max */
539 param
, param_len
, 10, /* param, length, max */
540 data
, data_len
, cli
->max_xmit
/* data, length, max */
542 cli_receive_trans(cli
, SMBtrans2
,
543 &rparam
, &rparam_len
,
544 &rdata
, &rdata_len
));
545 if (!cli_is_dos_error(cli
)) break;
547 /* we need to work around a Win95 bug - sometimes
548 it gives ERRSRV/ERRerror temprarily */
551 cli_dos_error(cli
, &eclass
, &ecode
);
552 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
555 } while (count
-- && ret
==False
);
566 /****************************************************************************
567 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
568 ****************************************************************************/
570 BOOL
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
571 struct timespec
*create_time
,
572 struct timespec
*access_time
,
573 struct timespec
*write_time
,
574 struct timespec
*change_time
,
575 SMB_OFF_T
*size
, uint16
*mode
,
578 unsigned int data_len
= 0;
579 unsigned int param_len
= 0;
580 uint16 setup
= TRANSACT2_QPATHINFO
;
582 char *rparam
=NULL
, *rdata
=NULL
;
587 SSVAL(p
, 0, SMB_QUERY_FILE_ALL_INFO
);
589 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
591 param_len
= PTR_DIFF(p
, param
);
593 if (!cli_send_trans(cli
, SMBtrans2
,
595 -1, 0, /* fid, flags */
596 &setup
, 1, 0, /* setup, length, max */
597 param
, param_len
, 10, /* param, length, max */
598 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
603 if (!cli_receive_trans(cli
, SMBtrans2
,
605 &rdata
, &data_len
)) {
609 if (!rdata
|| data_len
< 22) {
614 *create_time
= interpret_long_date(rdata
+0);
617 *access_time
= interpret_long_date(rdata
+8);
620 *write_time
= interpret_long_date(rdata
+16);
623 *change_time
= interpret_long_date(rdata
+24);
626 *mode
= SVAL(rdata
, 32);
629 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
632 *ino
= IVAL(rdata
, 64);
640 /****************************************************************************
641 Send a qfileinfo QUERY_FILE_NAME_INFO call.
642 ****************************************************************************/
644 BOOL
cli_qfilename(struct cli_state
*cli
, int fnum
,
647 unsigned int data_len
= 0;
648 unsigned int param_len
= 0;
649 uint16 setup
= TRANSACT2_QFILEINFO
;
651 char *rparam
=NULL
, *rdata
=NULL
;
654 memset(param
, 0, param_len
);
655 SSVAL(param
, 0, fnum
);
656 SSVAL(param
, 2, SMB_QUERY_FILE_NAME_INFO
);
658 if (!cli_send_trans(cli
, SMBtrans2
,
660 -1, 0, /* fid, flags */
661 &setup
, 1, 0, /* setup, length, max */
662 param
, param_len
, 2, /* param, length, max */
663 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
668 if (!cli_receive_trans(cli
, SMBtrans2
,
670 &rdata
, &data_len
)) {
674 if (!rdata
|| data_len
< 4) {
678 clistr_pull(cli
, name
, rdata
+4, sizeof(pstring
), IVAL(rdata
, 0), STR_UNICODE
);
683 /****************************************************************************
684 Send a qfileinfo call.
685 ****************************************************************************/
687 BOOL
cli_qfileinfo(struct cli_state
*cli
, int fnum
,
688 uint16
*mode
, SMB_OFF_T
*size
,
689 struct timespec
*create_time
,
690 struct timespec
*access_time
,
691 struct timespec
*write_time
,
692 struct timespec
*change_time
,
695 unsigned int data_len
= 0;
696 unsigned int param_len
= 0;
697 uint16 setup
= TRANSACT2_QFILEINFO
;
699 char *rparam
=NULL
, *rdata
=NULL
;
701 /* if its a win95 server then fail this - win95 totally screws it
703 if (cli
->win95
) return False
;
707 memset(param
, 0, param_len
);
708 SSVAL(param
, 0, fnum
);
709 SSVAL(param
, 2, SMB_QUERY_FILE_ALL_INFO
);
711 if (!cli_send_trans(cli
, SMBtrans2
,
713 -1, 0, /* fid, flags */
714 &setup
, 1, 0, /* setup, length, max */
715 param
, param_len
, 2, /* param, length, max */
716 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
721 if (!cli_receive_trans(cli
, SMBtrans2
,
723 &rdata
, &data_len
)) {
727 if (!rdata
|| data_len
< 68) {
732 *create_time
= interpret_long_date(rdata
+0);
735 *access_time
= interpret_long_date(rdata
+8);
738 *write_time
= interpret_long_date(rdata
+16);
741 *change_time
= interpret_long_date(rdata
+24);
744 *mode
= SVAL(rdata
, 32);
747 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
750 *ino
= IVAL(rdata
, 64);
758 /****************************************************************************
759 Send a qpathinfo BASIC_INFO call.
760 ****************************************************************************/
762 BOOL
cli_qpathinfo_basic( struct cli_state
*cli
, const char *name
,
763 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
765 unsigned int param_len
= 0;
766 unsigned int data_len
= 0;
767 uint16 setup
= TRANSACT2_QPATHINFO
;
768 char param
[sizeof(pstring
)+6];
769 char *rparam
=NULL
, *rdata
=NULL
;
774 pstrcpy( path
, name
);
777 len
= strlen( path
);
778 if ( path
[len
-1] == '\\' || path
[len
-1] == '/')
783 SSVAL(p
, 0, SMB_QUERY_FILE_BASIC_INFO
);
785 p
+= clistr_push(cli
, p
, path
, sizeof(pstring
)-6, STR_TERMINATE
);
786 param_len
= PTR_DIFF(p
, param
);
788 if (!cli_send_trans(cli
, SMBtrans2
,
790 -1, 0, /* fid, flags */
791 &setup
, 1, 0, /* setup, length, max */
792 param
, param_len
, 2, /* param, length, max */
793 NULL
, 0, cli
->max_xmit
/* data, length, max */
798 if (!cli_receive_trans(cli
, SMBtrans2
,
800 &rdata
, &data_len
)) {
810 set_atimespec(sbuf
, interpret_long_date( rdata
+8 )); /* Access time. */
811 set_mtimespec(sbuf
, interpret_long_date( rdata
+16 )); /* Write time. */
812 set_ctimespec(sbuf
, interpret_long_date( rdata
+24 )); /* Change time. */
814 *attributes
= IVAL( rdata
, 32 );
822 /****************************************************************************
823 Send a qfileinfo call.
824 ****************************************************************************/
826 BOOL
cli_qfileinfo_test(struct cli_state
*cli
, int fnum
, int level
, char **poutdata
, uint32
*poutlen
)
828 unsigned int data_len
= 0;
829 unsigned int param_len
= 0;
830 uint16 setup
= TRANSACT2_QFILEINFO
;
832 char *rparam
=NULL
, *rdata
=NULL
;
837 /* if its a win95 server then fail this - win95 totally screws it
844 memset(param
, 0, param_len
);
845 SSVAL(param
, 0, fnum
);
846 SSVAL(param
, 2, level
);
848 if (!cli_send_trans(cli
, SMBtrans2
,
850 -1, 0, /* fid, flags */
851 &setup
, 1, 0, /* setup, length, max */
852 param
, param_len
, 2, /* param, length, max */
853 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
858 if (!cli_receive_trans(cli
, SMBtrans2
,
860 &rdata
, &data_len
)) {
864 *poutdata
= (char *)memdup(rdata
, data_len
);
878 /****************************************************************************
879 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
880 ****************************************************************************/
882 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
884 unsigned int data_len
= 0;
885 unsigned int param_len
= 0;
886 uint16 setup
= TRANSACT2_QPATHINFO
;
888 char *rparam
=NULL
, *rdata
=NULL
;
896 SSVAL(p
, 0, SMB_QUERY_FILE_ALT_NAME_INFO
);
898 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
900 param_len
= PTR_DIFF(p
, param
);
903 ret
= (cli_send_trans(cli
, SMBtrans2
,
905 -1, 0, /* fid, flags */
906 &setup
, 1, 0, /* setup, length, max */
907 param
, param_len
, 10, /* param, length, max */
908 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
910 cli_receive_trans(cli
, SMBtrans2
,
913 if (!ret
&& cli_is_dos_error(cli
)) {
914 /* we need to work around a Win95 bug - sometimes
915 it gives ERRSRV/ERRerror temprarily */
918 cli_dos_error(cli
, &eclass
, &ecode
);
919 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
922 } while (count
-- && ret
==False
);
924 if (!ret
|| !rdata
|| data_len
< 4) {
925 return NT_STATUS_UNSUCCESSFUL
;
928 len
= IVAL(rdata
, 0);
930 if (len
> data_len
- 4) {
931 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
934 clistr_pull(cli
, alt_name
, rdata
+4, sizeof(fstring
), len
, STR_UNICODE
);