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 3 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, see <http://www.gnu.org/licenses/>.
23 /****************************************************************************
24 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
25 ****************************************************************************/
27 BOOL cli_api_pipe(struct cli_state *cli, const char *pipe_name,
28 uint16 *setup, uint32 setup_count, uint32 max_setup_count,
29 char *params, uint32 param_count, uint32 max_param_count,
30 char *data, uint32 data_count, uint32 max_data_count,
31 char **rparam, uint32 *rparam_count,
32 char **rdata, uint32 *rdata_count)
34 cli_send_trans(cli, SMBtrans,
37 setup, setup_count, max_setup_count,
38 params, param_count, max_param_count,
39 data, data_count, max_data_count);
41 return (cli_receive_trans(cli, SMBtrans,
42 rparam, (unsigned int *)rparam_count,
43 rdata, (unsigned int *)rdata_count));
46 /****************************************************************************
48 ****************************************************************************/
50 BOOL cli_api(struct cli_state *cli,
51 char *param, int prcnt, int mprcnt,
52 char *data, int drcnt, int mdrcnt,
53 char **rparam, unsigned int *rprcnt,
54 char **rdata, unsigned int *rdrcnt)
56 cli_send_trans(cli,SMBtrans,
57 PIPE_LANMAN, /* Name */
59 NULL,0,0, /* Setup, length, max */
60 param, prcnt, mprcnt, /* Params, length, max */
61 data, drcnt, mdrcnt /* Data, length, max */
64 return (cli_receive_trans(cli,SMBtrans,
69 /****************************************************************************
70 Perform a NetWkstaUserLogon.
71 ****************************************************************************/
73 BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
78 unsigned int rdrcnt,rprcnt;
81 memset(param, 0, sizeof(param));
83 /* send a SMBtrans command with api NetWkstaUserLogon */
85 SSVAL(p,0,132); /* api number */
87 pstrcpy_base(p,"OOWb54WrLh",param);
88 p = skip_string(param,sizeof(param),p);
89 pstrcpy_base(p,"WB21BWDWWDDDDDDDzzzD",param);
90 p = skip_string(param,sizeof(param),p);
93 pstrcpy_base(p,user,param);
99 pstrcpy_base(p, workstation, param);
102 SSVAL(p, 0, CLI_BUFFER_SIZE);
104 SSVAL(p, 0, CLI_BUFFER_SIZE);
108 param, PTR_DIFF(p,param),1024, /* param, length, max */
109 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
110 &rparam, &rprcnt, /* return params, return size */
111 &rdata, &rdrcnt /* return data, return size */
113 cli->rap_error = rparam? SVAL(rparam,0) : -1;
116 if (cli->rap_error == 0) {
117 DEBUG(4,("NetWkstaUserLogon success\n"));
118 cli->privileges = SVAL(p, 24);
119 /* The cli->eff_name field used to be set here
120 but it wasn't used anywhere else. */
122 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
128 return (cli->rap_error == 0);
131 /****************************************************************************
132 Call a NetShareEnum - try and browse available connections on a host.
133 ****************************************************************************/
135 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
140 unsigned int rdrcnt,rprcnt;
144 /* now send a SMBtrans command with api RNetShareEnum */
146 SSVAL(p,0,0); /* api number */
148 pstrcpy_base(p,"WrLeh",param);
149 p = skip_string(param,sizeof(param),p);
150 pstrcpy_base(p,"B13BWz",param);
151 p = skip_string(param,sizeof(param),p);
154 * Win2k needs a *smaller* buffer than 0xFFFF here -
155 * it returns "out of server memory" with 0xFFFF !!! JRA.
161 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
162 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
163 &rparam, &rprcnt, /* return params, length */
164 &rdata, &rdrcnt)) /* return data, length */
166 int res = rparam? SVAL(rparam,0) : -1;
168 if (res == 0 || res == ERRmoredata) {
169 int converter=SVAL(rparam,2);
172 count=SVAL(rparam,4);
175 for (i=0;i<count;i++,p+=20) {
177 int type = SVAL(p,14);
178 int comment_offset = IVAL(p,16) & 0xFFFF;
179 const char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
182 pull_ascii_pstring(s1, sname);
183 pull_ascii_pstring(s2, cmnt);
185 fn(s1, type, s2, state);
188 DEBUG(4,("NetShareEnum res=%d\n", res));
191 DEBUG(4,("NetShareEnum failed\n"));
200 /****************************************************************************
201 Call a NetServerEnum for the specified workgroup and servertype mask. This
202 function then calls the specified callback function for each name returned.
204 The callback function takes 4 arguments: the machine name, the server type,
205 the comment and a state pointer.
206 ****************************************************************************/
208 BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
209 void (*fn)(const char *, uint32, const char *, void *),
214 unsigned int rdrcnt,rprcnt;
220 errno = 0; /* reset */
222 /* send a SMBtrans command with api NetServerEnum */
224 SSVAL(p,0,0x68); /* api number */
226 pstrcpy_base(p,"WrLehDz", param);
227 p = skip_string(param,sizeof(param),p);
229 pstrcpy_base(p,"B16BBDz", param);
231 p = skip_string(param,sizeof(param),p);
233 SSVAL(p,2,CLI_BUFFER_SIZE);
238 p += push_ascii(p, workgroup, sizeof(pstring)-PTR_DIFF(p,param)-1, STR_TERMINATE|STR_UPPER);
241 param, PTR_DIFF(p,param), 8, /* params, length, max */
242 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
243 &rparam, &rprcnt, /* return params, return size */
244 &rdata, &rdrcnt /* return data, return size */
246 int res = rparam? SVAL(rparam,0) : -1;
248 if (res == 0 || res == ERRmoredata ||
249 (res != -1 && cli_errno(cli) == 0)) {
251 int converter=SVAL(rparam,2);
253 count=SVAL(rparam,4);
256 for (i = 0;i < count;i++, p += 26) {
258 int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
259 const char *cmnt = comment_offset?(rdata+comment_offset):"";
262 if (comment_offset < 0 || comment_offset > (int)rdrcnt) continue;
264 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
266 pull_ascii_pstring(s1, sname);
267 pull_ascii_pstring(s2, cmnt);
268 fn(s1, stype, s2, state);
277 errno = cli_errno(cli);
280 /* this is a very special case, when the domain master for the
281 work group isn't part of the work group itself, there is something
290 /****************************************************************************
291 Send a SamOEMChangePassword command.
292 ****************************************************************************/
294 BOOL cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
295 const char *old_password)
298 unsigned char data[532];
300 unsigned char old_pw_hash[16];
301 unsigned char new_pw_hash[16];
302 unsigned int data_len;
303 unsigned int param_len = 0;
306 unsigned int rprcnt, rdrcnt;
308 if (strlen(user) >= sizeof(fstring)-1) {
309 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
313 SSVAL(p,0,214); /* SamOEMChangePassword command. */
315 pstrcpy_base(p, "zsT", param);
316 p = skip_string(param,sizeof(param),p);
317 pstrcpy_base(p, "B516B16", param);
318 p = skip_string(param,sizeof(param),p);
319 pstrcpy_base(p,user, param);
320 p = skip_string(param,sizeof(param),p);
324 param_len = PTR_DIFF(p,param);
327 * Get the Lanman hash of the old password, we
328 * use this as the key to make_oem_passwd_hash().
330 E_deshash(old_password, old_pw_hash);
332 encode_pw_buffer(data, new_password, STR_ASCII);
334 #ifdef DEBUG_PASSWORD
335 DEBUG(100,("make_oem_passwd_hash\n"));
336 dump_data(100, data, 516);
338 SamOEMhash( (unsigned char *)data, (unsigned char *)old_pw_hash, 516);
341 * Now place the old password hash in the data.
343 E_deshash(new_password, new_pw_hash);
345 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
349 if (cli_send_trans(cli,SMBtrans,
350 PIPE_LANMAN, /* name */
351 0,0, /* fid, flags */
352 NULL,0,0, /* setup, length, max */
353 param,param_len,2, /* param, length, max */
354 (char *)data,data_len,0 /* data, length, max */
356 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
361 if (!cli_receive_trans(cli,SMBtrans,
364 DEBUG(0,("cli_oem_change_password: Failed to recieve reply to password change for user %s\n",
370 cli->rap_error = SVAL(rparam,0);
376 return (cli->rap_error == 0);
379 /****************************************************************************
380 Send a qpathinfo call.
381 ****************************************************************************/
383 BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
387 SMB_OFF_T *size, uint16 *mode)
389 unsigned int data_len = 0;
390 unsigned int param_len = 0;
391 unsigned int rparam_len, rdata_len;
392 uint16 setup = TRANSACT2_QPATHINFO;
394 char *rparam=NULL, *rdata=NULL;
397 time_t (*date_fn)(struct cli_state *, void *);
402 SSVAL(p, 0, SMB_INFO_STANDARD);
404 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
406 param_len = PTR_DIFF(p, param);
409 ret = (cli_send_trans(cli, SMBtrans2,
411 -1, 0, /* fid, flags */
412 &setup, 1, 0, /* setup, length, max */
413 param, param_len, 10, /* param, length, max */
414 NULL, data_len, cli->max_xmit /* data, length, max */
416 cli_receive_trans(cli, SMBtrans2,
417 &rparam, &rparam_len,
418 &rdata, &rdata_len));
419 if (!cli_is_dos_error(cli)) break;
421 /* we need to work around a Win95 bug - sometimes
422 it gives ERRSRV/ERRerror temprarily */
425 cli_dos_error(cli, &eclass, &ecode);
426 if (eclass != ERRSRV || ecode != ERRerror) break;
429 } while (count-- && ret==False);
431 if (!ret || !rdata || rdata_len < 22) {
436 date_fn = cli_make_unix_date;
438 date_fn = cli_make_unix_date2;
442 *change_time = date_fn(cli, rdata+0);
445 *access_time = date_fn(cli, rdata+4);
448 *write_time = date_fn(cli, rdata+8);
451 *size = IVAL(rdata, 12);
454 *mode = SVAL(rdata,l1_attrFile);
462 /****************************************************************************
463 Send a setpathinfo call.
464 ****************************************************************************/
466 BOOL cli_setpathinfo(struct cli_state *cli, const char *fname,
473 unsigned int data_len = 0;
474 unsigned int param_len = 0;
475 unsigned int rparam_len, rdata_len;
476 uint16 setup = TRANSACT2_SETPATHINFO;
479 char *rparam=NULL, *rdata=NULL;
484 memset(param, 0, sizeof(param));
485 memset(data, 0, sizeof(data));
489 /* Add the information level */
490 SSVAL(p, 0, SMB_FILE_BASIC_INFORMATION);
495 /* Add the file name */
496 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
498 param_len = PTR_DIFF(p, param);
503 * Add the create, last access, modification, and status change times
506 put_long_date(p, create_time);
509 put_long_date(p, access_time);
512 put_long_date(p, write_time);
515 put_long_date(p, change_time);
526 data_len = PTR_DIFF(p, data);
529 ret = (cli_send_trans(cli, SMBtrans2,
531 -1, 0, /* fid, flags */
532 &setup, 1, 0, /* setup, length, max */
533 param, param_len, 10, /* param, length, max */
534 data, data_len, cli->max_xmit /* data, length, max */
536 cli_receive_trans(cli, SMBtrans2,
537 &rparam, &rparam_len,
538 &rdata, &rdata_len));
539 if (!cli_is_dos_error(cli)) break;
541 /* we need to work around a Win95 bug - sometimes
542 it gives ERRSRV/ERRerror temprarily */
545 cli_dos_error(cli, &eclass, &ecode);
546 if (eclass != ERRSRV || ecode != ERRerror) break;
549 } while (count-- && ret==False);
560 /****************************************************************************
561 Send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level.
562 ****************************************************************************/
564 BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
565 struct timespec *create_time,
566 struct timespec *access_time,
567 struct timespec *write_time,
568 struct timespec *change_time,
569 SMB_OFF_T *size, uint16 *mode,
572 unsigned int data_len = 0;
573 unsigned int param_len = 0;
574 uint16 setup = TRANSACT2_QPATHINFO;
576 char *rparam=NULL, *rdata=NULL;
581 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
583 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
585 param_len = PTR_DIFF(p, param);
587 if (!cli_send_trans(cli, SMBtrans2,
589 -1, 0, /* fid, flags */
590 &setup, 1, 0, /* setup, length, max */
591 param, param_len, 10, /* param, length, max */
592 NULL, data_len, cli->max_xmit /* data, length, max */
597 if (!cli_receive_trans(cli, SMBtrans2,
599 &rdata, &data_len)) {
603 if (!rdata || data_len < 22) {
608 *create_time = interpret_long_date(rdata+0);
611 *access_time = interpret_long_date(rdata+8);
614 *write_time = interpret_long_date(rdata+16);
617 *change_time = interpret_long_date(rdata+24);
620 *mode = SVAL(rdata, 32);
623 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
626 *ino = IVAL(rdata, 64);
634 /****************************************************************************
635 Send a qfileinfo QUERY_FILE_NAME_INFO call.
636 ****************************************************************************/
638 BOOL cli_qfilename(struct cli_state *cli, int fnum,
641 unsigned int data_len = 0;
642 unsigned int param_len = 0;
643 uint16 setup = TRANSACT2_QFILEINFO;
645 char *rparam=NULL, *rdata=NULL;
648 memset(param, 0, param_len);
649 SSVAL(param, 0, fnum);
650 SSVAL(param, 2, SMB_QUERY_FILE_NAME_INFO);
652 if (!cli_send_trans(cli, SMBtrans2,
654 -1, 0, /* fid, flags */
655 &setup, 1, 0, /* setup, length, max */
656 param, param_len, 2, /* param, length, max */
657 NULL, data_len, cli->max_xmit /* data, length, max */
662 if (!cli_receive_trans(cli, SMBtrans2,
664 &rdata, &data_len)) {
668 if (!rdata || data_len < 4) {
672 clistr_pull(cli, name, rdata+4, sizeof(pstring), IVAL(rdata, 0), STR_UNICODE);
677 /****************************************************************************
678 Send a qfileinfo call.
679 ****************************************************************************/
681 BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
682 uint16 *mode, SMB_OFF_T *size,
683 struct timespec *create_time,
684 struct timespec *access_time,
685 struct timespec *write_time,
686 struct timespec *change_time,
689 unsigned int data_len = 0;
690 unsigned int param_len = 0;
691 uint16 setup = TRANSACT2_QFILEINFO;
693 char *rparam=NULL, *rdata=NULL;
695 /* if its a win95 server then fail this - win95 totally screws it
697 if (cli->win95) return False;
701 memset(param, 0, param_len);
702 SSVAL(param, 0, fnum);
703 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
705 if (!cli_send_trans(cli, SMBtrans2,
707 -1, 0, /* fid, flags */
708 &setup, 1, 0, /* setup, length, max */
709 param, param_len, 2, /* param, length, max */
710 NULL, data_len, cli->max_xmit /* data, length, max */
715 if (!cli_receive_trans(cli, SMBtrans2,
717 &rdata, &data_len)) {
721 if (!rdata || data_len < 68) {
726 *create_time = interpret_long_date(rdata+0);
729 *access_time = interpret_long_date(rdata+8);
732 *write_time = interpret_long_date(rdata+16);
735 *change_time = interpret_long_date(rdata+24);
738 *mode = SVAL(rdata, 32);
741 *size = IVAL2_TO_SMB_BIG_UINT(rdata,48);
744 *ino = IVAL(rdata, 64);
752 /****************************************************************************
753 Send a qpathinfo BASIC_INFO call.
754 ****************************************************************************/
756 BOOL cli_qpathinfo_basic( struct cli_state *cli, const char *name,
757 SMB_STRUCT_STAT *sbuf, uint32 *attributes )
759 unsigned int param_len = 0;
760 unsigned int data_len = 0;
761 uint16 setup = TRANSACT2_QPATHINFO;
762 char param[sizeof(pstring)+6];
763 char *rparam=NULL, *rdata=NULL;
768 pstrcpy( path, name );
771 len = strlen( path );
772 if ( path[len-1] == '\\' || path[len-1] == '/')
777 SSVAL(p, 0, SMB_QUERY_FILE_BASIC_INFO);
779 p += clistr_push(cli, p, path, sizeof(pstring)-6, STR_TERMINATE);
780 param_len = PTR_DIFF(p, param);
782 if (!cli_send_trans(cli, SMBtrans2,
784 -1, 0, /* fid, flags */
785 &setup, 1, 0, /* setup, length, max */
786 param, param_len, 2, /* param, length, max */
787 NULL, 0, cli->max_xmit /* data, length, max */
792 if (!cli_receive_trans(cli, SMBtrans2,
794 &rdata, &data_len)) {
804 set_atimespec(sbuf, interpret_long_date( rdata+8 )); /* Access time. */
805 set_mtimespec(sbuf, interpret_long_date( rdata+16 )); /* Write time. */
806 set_ctimespec(sbuf, interpret_long_date( rdata+24 )); /* Change time. */
808 *attributes = IVAL( rdata, 32 );
816 /****************************************************************************
817 Send a qfileinfo call.
818 ****************************************************************************/
820 BOOL cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char **poutdata, uint32 *poutlen)
822 unsigned int data_len = 0;
823 unsigned int param_len = 0;
824 uint16 setup = TRANSACT2_QFILEINFO;
826 char *rparam=NULL, *rdata=NULL;
831 /* if its a win95 server then fail this - win95 totally screws it
838 memset(param, 0, param_len);
839 SSVAL(param, 0, fnum);
840 SSVAL(param, 2, level);
842 if (!cli_send_trans(cli, SMBtrans2,
844 -1, 0, /* fid, flags */
845 &setup, 1, 0, /* setup, length, max */
846 param, param_len, 2, /* param, length, max */
847 NULL, data_len, cli->max_xmit /* data, length, max */
852 if (!cli_receive_trans(cli, SMBtrans2,
854 &rdata, &data_len)) {
858 *poutdata = (char *)memdup(rdata, data_len);
872 /****************************************************************************
873 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
874 ****************************************************************************/
876 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
878 unsigned int data_len = 0;
879 unsigned int param_len = 0;
880 uint16 setup = TRANSACT2_QPATHINFO;
882 char *rparam=NULL, *rdata=NULL;
890 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
892 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
894 param_len = PTR_DIFF(p, param);
897 ret = (cli_send_trans(cli, SMBtrans2,
899 -1, 0, /* fid, flags */
900 &setup, 1, 0, /* setup, length, max */
901 param, param_len, 10, /* param, length, max */
902 NULL, data_len, cli->max_xmit /* data, length, max */
904 cli_receive_trans(cli, SMBtrans2,
907 if (!ret && cli_is_dos_error(cli)) {
908 /* we need to work around a Win95 bug - sometimes
909 it gives ERRSRV/ERRerror temprarily */
912 cli_dos_error(cli, &eclass, &ecode);
913 if (eclass != ERRSRV || ecode != ERRerror) break;
916 } while (count-- && ret==False);
918 if (!ret || !rdata || data_len < 4) {
919 return NT_STATUS_UNSUCCESSFUL;
922 len = IVAL(rdata, 0);
924 if (len > data_len - 4) {
925 return NT_STATUS_INVALID_NETWORK_RESPONSE;
928 clistr_pull(cli, alt_name, rdata+4, sizeof(fstring), len, STR_UNICODE);