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
;
221 errno
= 0; /* reset */
223 /* send a SMBtrans command with api NetServerEnum */
225 SSVAL(p
,0,0x68); /* api number */
227 pstrcpy_base(p
,"WrLehDz", param
);
228 p
= skip_string(param
,sizeof(param
),p
);
230 pstrcpy_base(p
,"B16BBDz", param
);
232 p
= skip_string(param
,sizeof(param
),p
);
234 SSVAL(p
,2,CLI_BUFFER_SIZE
);
239 p
+= push_ascii(p
, workgroup
, sizeof(pstring
)-PTR_DIFF(p
,param
)-1, STR_TERMINATE
|STR_UPPER
);
242 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
243 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
244 &rparam
, &rprcnt
, /* return params, return size */
245 &rdata
, &rdrcnt
/* return data, return size */
247 int res
= rparam
? SVAL(rparam
,0) : -1;
249 if (res
== 0 || res
== ERRmoredata
||
250 (res
!= -1 && cli_errno(cli
) == 0)) {
252 int converter
=SVAL(rparam
,2);
254 count
=SVAL(rparam
,4);
257 for (i
= 0;i
< count
;i
++, p
+= 26) {
259 int comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
260 const char *cmnt
= comment_offset
?(rdata
+comment_offset
):"";
263 if (comment_offset
< 0 || comment_offset
> (int)rdrcnt
) continue;
265 stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
267 pull_ascii_pstring(s1
, sname
);
268 pull_ascii_pstring(s2
, cmnt
);
269 fn(s1
, stype
, s2
, state
);
278 errno
= cli_errno(cli
);
281 /* this is a very special case, when the domain master for the
282 work group isn't part of the work group itself, there is something
291 /****************************************************************************
292 Send a SamOEMChangePassword command.
293 ****************************************************************************/
295 BOOL
cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
296 const char *old_password
)
299 unsigned char data
[532];
301 unsigned char old_pw_hash
[16];
302 unsigned char new_pw_hash
[16];
303 unsigned int data_len
;
304 unsigned int param_len
= 0;
307 unsigned int rprcnt
, rdrcnt
;
309 if (strlen(user
) >= sizeof(fstring
)-1) {
310 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
314 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
316 pstrcpy_base(p
, "zsT", param
);
317 p
= skip_string(param
,sizeof(param
),p
);
318 pstrcpy_base(p
, "B516B16", param
);
319 p
= skip_string(param
,sizeof(param
),p
);
320 pstrcpy_base(p
,user
, param
);
321 p
= skip_string(param
,sizeof(param
),p
);
325 param_len
= PTR_DIFF(p
,param
);
328 * Get the Lanman hash of the old password, we
329 * use this as the key to make_oem_passwd_hash().
331 E_deshash(old_password
, old_pw_hash
);
333 encode_pw_buffer(data
, new_password
, STR_ASCII
);
335 #ifdef DEBUG_PASSWORD
336 DEBUG(100,("make_oem_passwd_hash\n"));
337 dump_data(100, data
, 516);
339 SamOEMhash( (unsigned char *)data
, (unsigned char *)old_pw_hash
, 516);
342 * Now place the old password hash in the data.
344 E_deshash(new_password
, new_pw_hash
);
346 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
350 if (cli_send_trans(cli
,SMBtrans
,
351 PIPE_LANMAN
, /* name */
352 0,0, /* fid, flags */
353 NULL
,0,0, /* setup, length, max */
354 param
,param_len
,2, /* param, length, max */
355 (char *)data
,data_len
,0 /* data, length, max */
357 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
362 if (!cli_receive_trans(cli
,SMBtrans
,
365 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
371 cli
->rap_error
= SVAL(rparam
,0);
377 return (cli
->rap_error
== 0);
380 /****************************************************************************
381 Send a qpathinfo call.
382 ****************************************************************************/
384 BOOL
cli_qpathinfo(struct cli_state
*cli
, const char *fname
,
388 SMB_OFF_T
*size
, uint16
*mode
)
390 unsigned int data_len
= 0;
391 unsigned int param_len
= 0;
392 unsigned int rparam_len
, rdata_len
;
393 uint16 setup
= TRANSACT2_QPATHINFO
;
395 char *rparam
=NULL
, *rdata
=NULL
;
398 time_t (*date_fn
)(struct cli_state
*, void *);
403 SSVAL(p
, 0, SMB_INFO_STANDARD
);
405 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
407 param_len
= PTR_DIFF(p
, param
);
410 ret
= (cli_send_trans(cli
, SMBtrans2
,
412 -1, 0, /* fid, flags */
413 &setup
, 1, 0, /* setup, length, max */
414 param
, param_len
, 10, /* param, length, max */
415 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
417 cli_receive_trans(cli
, SMBtrans2
,
418 &rparam
, &rparam_len
,
419 &rdata
, &rdata_len
));
420 if (!cli_is_dos_error(cli
)) break;
422 /* we need to work around a Win95 bug - sometimes
423 it gives ERRSRV/ERRerror temprarily */
426 cli_dos_error(cli
, &eclass
, &ecode
);
427 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
430 } while (count
-- && ret
==False
);
432 if (!ret
|| !rdata
|| rdata_len
< 22) {
437 date_fn
= cli_make_unix_date
;
439 date_fn
= cli_make_unix_date2
;
443 *change_time
= date_fn(cli
, rdata
+0);
446 *access_time
= date_fn(cli
, rdata
+4);
449 *write_time
= date_fn(cli
, rdata
+8);
452 *size
= IVAL(rdata
, 12);
455 *mode
= SVAL(rdata
,l1_attrFile
);
463 /****************************************************************************
464 Send a setpathinfo call.
465 ****************************************************************************/
467 BOOL
cli_setpathinfo(struct cli_state
*cli
, const char *fname
,
474 unsigned int data_len
= 0;
475 unsigned int param_len
= 0;
476 unsigned int rparam_len
, rdata_len
;
477 uint16 setup
= TRANSACT2_SETPATHINFO
;
480 char *rparam
=NULL
, *rdata
=NULL
;
485 memset(param
, 0, sizeof(param
));
486 memset(data
, 0, sizeof(data
));
490 /* Add the information level */
491 SSVAL(p
, 0, SMB_FILE_BASIC_INFORMATION
);
496 /* Add the file name */
497 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
499 param_len
= PTR_DIFF(p
, param
);
504 * Add the create, last access, modification, and status change times
507 put_long_date(p
, create_time
);
510 put_long_date(p
, access_time
);
513 put_long_date(p
, write_time
);
516 put_long_date(p
, change_time
);
527 data_len
= PTR_DIFF(p
, data
);
530 ret
= (cli_send_trans(cli
, SMBtrans2
,
532 -1, 0, /* fid, flags */
533 &setup
, 1, 0, /* setup, length, max */
534 param
, param_len
, 10, /* param, length, max */
535 data
, data_len
, cli
->max_xmit
/* data, length, max */
537 cli_receive_trans(cli
, SMBtrans2
,
538 &rparam
, &rparam_len
,
539 &rdata
, &rdata_len
));
540 if (!cli_is_dos_error(cli
)) break;
542 /* we need to work around a Win95 bug - sometimes
543 it gives ERRSRV/ERRerror temprarily */
546 cli_dos_error(cli
, &eclass
, &ecode
);
547 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
550 } while (count
-- && ret
==False
);
561 /****************************************************************************
562 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
563 ****************************************************************************/
565 BOOL
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
566 struct timespec
*create_time
,
567 struct timespec
*access_time
,
568 struct timespec
*write_time
,
569 struct timespec
*change_time
,
570 SMB_OFF_T
*size
, uint16
*mode
,
573 unsigned int data_len
= 0;
574 unsigned int param_len
= 0;
575 uint16 setup
= TRANSACT2_QPATHINFO
;
577 char *rparam
=NULL
, *rdata
=NULL
;
582 SSVAL(p
, 0, SMB_QUERY_FILE_ALL_INFO
);
584 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
586 param_len
= PTR_DIFF(p
, param
);
588 if (!cli_send_trans(cli
, SMBtrans2
,
590 -1, 0, /* fid, flags */
591 &setup
, 1, 0, /* setup, length, max */
592 param
, param_len
, 10, /* param, length, max */
593 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
598 if (!cli_receive_trans(cli
, SMBtrans2
,
600 &rdata
, &data_len
)) {
604 if (!rdata
|| data_len
< 22) {
609 *create_time
= interpret_long_date(rdata
+0);
612 *access_time
= interpret_long_date(rdata
+8);
615 *write_time
= interpret_long_date(rdata
+16);
618 *change_time
= interpret_long_date(rdata
+24);
621 *mode
= SVAL(rdata
, 32);
624 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
627 *ino
= IVAL(rdata
, 64);
635 /****************************************************************************
636 Send a qfileinfo QUERY_FILE_NAME_INFO call.
637 ****************************************************************************/
639 BOOL
cli_qfilename(struct cli_state
*cli
, int fnum
,
642 unsigned int data_len
= 0;
643 unsigned int param_len
= 0;
644 uint16 setup
= TRANSACT2_QFILEINFO
;
646 char *rparam
=NULL
, *rdata
=NULL
;
649 memset(param
, 0, param_len
);
650 SSVAL(param
, 0, fnum
);
651 SSVAL(param
, 2, SMB_QUERY_FILE_NAME_INFO
);
653 if (!cli_send_trans(cli
, SMBtrans2
,
655 -1, 0, /* fid, flags */
656 &setup
, 1, 0, /* setup, length, max */
657 param
, param_len
, 2, /* param, length, max */
658 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
663 if (!cli_receive_trans(cli
, SMBtrans2
,
665 &rdata
, &data_len
)) {
669 if (!rdata
|| data_len
< 4) {
673 clistr_pull(cli
, name
, rdata
+4, sizeof(pstring
), IVAL(rdata
, 0), STR_UNICODE
);
678 /****************************************************************************
679 Send a qfileinfo call.
680 ****************************************************************************/
682 BOOL
cli_qfileinfo(struct cli_state
*cli
, int fnum
,
683 uint16
*mode
, SMB_OFF_T
*size
,
684 struct timespec
*create_time
,
685 struct timespec
*access_time
,
686 struct timespec
*write_time
,
687 struct timespec
*change_time
,
690 unsigned int data_len
= 0;
691 unsigned int param_len
= 0;
692 uint16 setup
= TRANSACT2_QFILEINFO
;
694 char *rparam
=NULL
, *rdata
=NULL
;
696 /* if its a win95 server then fail this - win95 totally screws it
698 if (cli
->win95
) return False
;
702 memset(param
, 0, param_len
);
703 SSVAL(param
, 0, fnum
);
704 SSVAL(param
, 2, SMB_QUERY_FILE_ALL_INFO
);
706 if (!cli_send_trans(cli
, SMBtrans2
,
708 -1, 0, /* fid, flags */
709 &setup
, 1, 0, /* setup, length, max */
710 param
, param_len
, 2, /* param, length, max */
711 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
716 if (!cli_receive_trans(cli
, SMBtrans2
,
718 &rdata
, &data_len
)) {
722 if (!rdata
|| data_len
< 68) {
727 *create_time
= interpret_long_date(rdata
+0);
730 *access_time
= interpret_long_date(rdata
+8);
733 *write_time
= interpret_long_date(rdata
+16);
736 *change_time
= interpret_long_date(rdata
+24);
739 *mode
= SVAL(rdata
, 32);
742 *size
= IVAL2_TO_SMB_BIG_UINT(rdata
,48);
745 *ino
= IVAL(rdata
, 64);
753 /****************************************************************************
754 Send a qpathinfo BASIC_INFO call.
755 ****************************************************************************/
757 BOOL
cli_qpathinfo_basic( struct cli_state
*cli
, const char *name
,
758 SMB_STRUCT_STAT
*sbuf
, uint32
*attributes
)
760 unsigned int param_len
= 0;
761 unsigned int data_len
= 0;
762 uint16 setup
= TRANSACT2_QPATHINFO
;
763 char param
[sizeof(pstring
)+6];
764 char *rparam
=NULL
, *rdata
=NULL
;
769 pstrcpy( path
, name
);
772 len
= strlen( path
);
773 if ( path
[len
-1] == '\\' || path
[len
-1] == '/')
778 SSVAL(p
, 0, SMB_QUERY_FILE_BASIC_INFO
);
780 p
+= clistr_push(cli
, p
, path
, sizeof(pstring
)-6, STR_TERMINATE
);
781 param_len
= PTR_DIFF(p
, param
);
783 if (!cli_send_trans(cli
, SMBtrans2
,
785 -1, 0, /* fid, flags */
786 &setup
, 1, 0, /* setup, length, max */
787 param
, param_len
, 2, /* param, length, max */
788 NULL
, 0, cli
->max_xmit
/* data, length, max */
793 if (!cli_receive_trans(cli
, SMBtrans2
,
795 &rdata
, &data_len
)) {
805 set_atimespec(sbuf
, interpret_long_date( rdata
+8 )); /* Access time. */
806 set_mtimespec(sbuf
, interpret_long_date( rdata
+16 )); /* Write time. */
807 set_ctimespec(sbuf
, interpret_long_date( rdata
+24 )); /* Change time. */
809 *attributes
= IVAL( rdata
, 32 );
817 /****************************************************************************
818 Send a qfileinfo call.
819 ****************************************************************************/
821 BOOL
cli_qfileinfo_test(struct cli_state
*cli
, int fnum
, int level
, char **poutdata
, uint32
*poutlen
)
823 unsigned int data_len
= 0;
824 unsigned int param_len
= 0;
825 uint16 setup
= TRANSACT2_QFILEINFO
;
827 char *rparam
=NULL
, *rdata
=NULL
;
832 /* if its a win95 server then fail this - win95 totally screws it
839 memset(param
, 0, param_len
);
840 SSVAL(param
, 0, fnum
);
841 SSVAL(param
, 2, level
);
843 if (!cli_send_trans(cli
, SMBtrans2
,
845 -1, 0, /* fid, flags */
846 &setup
, 1, 0, /* setup, length, max */
847 param
, param_len
, 2, /* param, length, max */
848 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
853 if (!cli_receive_trans(cli
, SMBtrans2
,
855 &rdata
, &data_len
)) {
859 *poutdata
= (char *)memdup(rdata
, data_len
);
873 /****************************************************************************
874 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
875 ****************************************************************************/
877 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
879 unsigned int data_len
= 0;
880 unsigned int param_len
= 0;
881 uint16 setup
= TRANSACT2_QPATHINFO
;
883 char *rparam
=NULL
, *rdata
=NULL
;
891 SSVAL(p
, 0, SMB_QUERY_FILE_ALT_NAME_INFO
);
893 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
);
895 param_len
= PTR_DIFF(p
, param
);
898 ret
= (cli_send_trans(cli
, SMBtrans2
,
900 -1, 0, /* fid, flags */
901 &setup
, 1, 0, /* setup, length, max */
902 param
, param_len
, 10, /* param, length, max */
903 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
905 cli_receive_trans(cli
, SMBtrans2
,
908 if (!ret
&& cli_is_dos_error(cli
)) {
909 /* we need to work around a Win95 bug - sometimes
910 it gives ERRSRV/ERRerror temprarily */
913 cli_dos_error(cli
, &eclass
, &ecode
);
914 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
917 } while (count
-- && ret
==False
);
919 if (!ret
|| !rdata
|| data_len
< 4) {
920 return NT_STATUS_UNSUCCESSFUL
;
923 len
= IVAL(rdata
, 0);
925 if (len
> data_len
- 4) {
926 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
929 clistr_pull(cli
, alt_name
, rdata
+4, sizeof(fstring
), len
, STR_UNICODE
);