2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1994-1998
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.
27 /****************************************************************************
28 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
29 ****************************************************************************/
30 BOOL cli_api_pipe(struct cli_state *cli, char *pipe_name,
31 uint16 *setup, uint32 setup_count, uint32 max_setup_count,
32 char *params, uint32 param_count, uint32 max_param_count,
33 char *data, uint32 data_count, uint32 max_data_count,
34 char **rparam, uint32 *rparam_count,
35 char **rdata, uint32 *rdata_count)
37 cli_send_trans(cli, SMBtrans,
40 setup, setup_count, max_setup_count,
41 params, param_count, max_param_count,
42 data, data_count, max_data_count);
44 return (cli_receive_trans(cli, SMBtrans,
45 rparam, (int *)rparam_count,
46 rdata, (int *)rdata_count));
49 /****************************************************************************
51 ****************************************************************************/
52 BOOL cli_api(struct cli_state *cli,
53 char *param, int prcnt, int mprcnt,
54 char *data, int drcnt, int mdrcnt,
55 char **rparam, int *rprcnt,
56 char **rdata, int *rdrcnt)
58 cli_send_trans(cli,SMBtrans,
59 PIPE_LANMAN, /* Name */
61 NULL,0,0, /* Setup, length, max */
62 param, prcnt, mprcnt, /* Params, length, max */
63 data, drcnt, mdrcnt /* Data, length, max */
66 return (cli_receive_trans(cli,SMBtrans,
72 /****************************************************************************
73 perform a NetWkstaUserLogon
74 ****************************************************************************/
75 BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
83 memset(param, 0, sizeof(param));
85 /* send a SMBtrans command with api NetWkstaUserLogon */
87 SSVAL(p,0,132); /* api number */
89 pstrcpy(p,"OOWb54WrLh");
91 pstrcpy(p,"WB21BWDWWDDDDDDDzzzD");
101 pstrcpy(p, workstation);
104 SSVAL(p, 0, CLI_BUFFER_SIZE);
106 SSVAL(p, 0, CLI_BUFFER_SIZE);
110 param, PTR_DIFF(p,param),1024, /* param, length, max */
111 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
112 &rparam, &rprcnt, /* return params, return size */
113 &rdata, &rdrcnt /* return data, return size */
115 cli->rap_error = rparam? SVAL(rparam,0) : -1;
118 if (cli->rap_error == 0) {
119 DEBUG(4,("NetWkstaUserLogon success\n"));
120 cli->privileges = SVAL(p, 24);
121 fstrcpy(cli->eff_name,p+2);
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 ****************************************************************************/
135 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
144 /* now send a SMBtrans command with api RNetShareEnum */
146 SSVAL(p,0,0); /* api number */
149 p = skip_string(p,1);
151 p = skip_string(p,1);
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 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"));
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 ****************************************************************************/
208 BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
209 void (*fn)(const char *, uint32, const char *, void *),
220 /* send a SMBtrans command with api NetServerEnum */
222 SSVAL(p,0,0x68); /* api number */
224 pstrcpy(p,"WrLehDz");
225 p = skip_string(p,1);
227 pstrcpy(p,"B16BBDz");
229 p = skip_string(p,1);
231 SSVAL(p,2,CLI_BUFFER_SIZE);
236 p += push_pstring(p, workgroup);
239 param, PTR_DIFF(p,param), 8, /* params, length, max */
240 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
241 &rparam, &rprcnt, /* return params, return size */
242 &rdata, &rdrcnt /* return data, return size */
244 int res = rparam? SVAL(rparam,0) : -1;
246 if (res == 0 || res == ERRmoredata) {
248 int converter=SVAL(rparam,2);
250 count=SVAL(rparam,4);
253 for (i = 0;i < count;i++, p += 26) {
255 int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
256 char *cmnt = comment_offset?(rdata+comment_offset):"";
259 if (comment_offset < 0 || comment_offset > rdrcnt) continue;
261 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
263 pull_ascii_pstring(s1, sname);
264 pull_ascii_pstring(s2, cmnt);
265 fn(s1, stype, s2, state);
278 /****************************************************************************
279 Send a SamOEMChangePassword command
280 ****************************************************************************/
281 BOOL cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
282 const char *old_password)
284 char param[16+sizeof(fstring)];
287 fstring upper_case_old_pw;
288 fstring upper_case_new_pw;
289 unsigned char old_pw_hash[16];
290 unsigned char new_pw_hash[16];
296 pstring dos_new_password;
298 if (strlen(user) >= sizeof(fstring)-1) {
299 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
303 SSVAL(p,0,214); /* SamOEMChangePassword command. */
306 p = skip_string(p,1);
307 pstrcpy(p, "B516B16");
308 p = skip_string(p,1);
310 p = skip_string(p,1);
314 param_len = PTR_DIFF(p,param);
317 * Get the Lanman hash of the old password, we
318 * use this as the key to make_oem_passwd_hash().
320 memset(upper_case_old_pw, '\0', sizeof(upper_case_old_pw));
321 clistr_push(cli, upper_case_old_pw, old_password, -1,STR_TERMINATE|STR_UPPER|STR_ASCII);
322 E_P16((uchar *)upper_case_old_pw, old_pw_hash);
324 clistr_push(cli, dos_new_password, new_password, -1, STR_TERMINATE|STR_ASCII);
326 if (!make_oem_passwd_hash( data, dos_new_password, old_pw_hash, False))
330 * Now place the old password hash in the data.
332 memset(upper_case_new_pw, '\0', sizeof(upper_case_new_pw));
333 clistr_push(cli, upper_case_new_pw, new_password, -1, STR_TERMINATE|STR_UPPER|STR_ASCII);
335 E_P16((uchar *)upper_case_new_pw, new_pw_hash);
337 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
341 if (cli_send_trans(cli,SMBtrans,
342 PIPE_LANMAN, /* name */
343 0,0, /* fid, flags */
344 NULL,0,0, /* setup, length, max */
345 param,param_len,2, /* param, length, max */
346 data,data_len,0 /* data, length, max */
348 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
353 if (cli_receive_trans(cli,SMBtrans,
357 cli->rap_error = SVAL(rparam,0);
363 return (cli->rap_error == 0);
367 /****************************************************************************
368 send a qpathinfo call
369 ****************************************************************************/
370 BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
371 time_t *c_time, time_t *a_time, time_t *m_time,
372 size_t *size, uint16 *mode)
376 uint16 setup = TRANSACT2_QPATHINFO;
378 char *rparam=NULL, *rdata=NULL;
381 time_t (*date_fn)(void *);
386 SSVAL(p, 0, SMB_INFO_STANDARD);
388 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
390 param_len = PTR_DIFF(p, param);
393 ret = (cli_send_trans(cli, SMBtrans2,
395 -1, 0, /* fid, flags */
396 &setup, 1, 0, /* setup, length, max */
397 param, param_len, 10, /* param, length, max */
398 NULL, data_len, cli->max_xmit /* data, length, max */
400 cli_receive_trans(cli, SMBtrans2,
403 if (!ret && cli_is_dos_error(cli)) {
404 /* we need to work around a Win95 bug - sometimes
405 it gives ERRSRV/ERRerror temprarily */
408 cli_dos_error(cli, &eclass, &ecode);
409 if (eclass != ERRSRV || ecode != ERRerror) break;
412 } while (count-- && ret==False);
414 if (!ret || !rdata || data_len < 22) {
419 date_fn = make_unix_date;
421 date_fn = make_unix_date2;
425 *c_time = date_fn(rdata+0);
428 *a_time = date_fn(rdata+4);
431 *m_time = date_fn(rdata+8);
434 *size = IVAL(rdata, 12);
437 *mode = SVAL(rdata,l1_attrFile);
445 /****************************************************************************
446 send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
447 ****************************************************************************/
448 BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
449 time_t *c_time, time_t *a_time, time_t *m_time,
450 time_t *w_time, size_t *size, uint16 *mode,
455 uint16 setup = TRANSACT2_QPATHINFO;
457 char *rparam=NULL, *rdata=NULL;
462 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
464 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
466 param_len = PTR_DIFF(p, param);
468 if (!cli_send_trans(cli, SMBtrans2,
470 -1, 0, /* fid, flags */
471 &setup, 1, 0, /* setup, length, max */
472 param, param_len, 10, /* param, length, max */
473 NULL, data_len, cli->max_xmit /* data, length, max */
478 if (!cli_receive_trans(cli, SMBtrans2,
480 &rdata, &data_len)) {
484 if (!rdata || data_len < 22) {
489 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
492 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
495 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
498 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
501 *mode = SVAL(rdata, 32);
504 *size = IVAL(rdata, 48);
507 *ino = IVAL(rdata, 64);
516 /****************************************************************************
517 send a qfileinfo call
518 ****************************************************************************/
519 BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
520 uint16 *mode, size_t *size,
521 time_t *c_time, time_t *a_time, time_t *m_time,
522 time_t *w_time, SMB_INO_T *ino)
526 uint16 setup = TRANSACT2_QFILEINFO;
528 char *rparam=NULL, *rdata=NULL;
530 /* if its a win95 server then fail this - win95 totally screws it
532 if (cli->win95) return False;
536 memset(param, 0, param_len);
537 SSVAL(param, 0, fnum);
538 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
540 if (!cli_send_trans(cli, SMBtrans2,
542 -1, 0, /* fid, flags */
543 &setup, 1, 0, /* setup, length, max */
544 param, param_len, 2, /* param, length, max */
545 NULL, data_len, cli->max_xmit /* data, length, max */
550 if (!cli_receive_trans(cli, SMBtrans2,
552 &rdata, &data_len)) {
556 if (!rdata || data_len < 68) {
561 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
564 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
567 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
570 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
573 *mode = SVAL(rdata, 32);
576 *size = IVAL(rdata, 48);
579 *ino = IVAL(rdata, 64);
587 /****************************************************************************
588 send a qfileinfo call
589 ****************************************************************************/
590 BOOL cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char *outdata)
594 uint16 setup = TRANSACT2_QFILEINFO;
596 char *rparam=NULL, *rdata=NULL;
598 /* if its a win95 server then fail this - win95 totally screws it
600 if (cli->win95) return False;
604 memset(param, 0, param_len);
605 SSVAL(param, 0, fnum);
606 SSVAL(param, 2, level);
608 if (!cli_send_trans(cli, SMBtrans2,
610 -1, 0, /* fid, flags */
611 &setup, 1, 0, /* setup, length, max */
612 param, param_len, 2, /* param, length, max */
613 NULL, data_len, cli->max_xmit /* data, length, max */
618 if (!cli_receive_trans(cli, SMBtrans2,
620 &rdata, &data_len)) {
624 memcpy(outdata, rdata, data_len);
633 /****************************************************************************
634 send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call
635 ****************************************************************************/
636 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
640 uint16 setup = TRANSACT2_QPATHINFO;
642 char *rparam=NULL, *rdata=NULL;
650 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
652 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
654 param_len = PTR_DIFF(p, param);
657 ret = (cli_send_trans(cli, SMBtrans2,
659 -1, 0, /* fid, flags */
660 &setup, 1, 0, /* setup, length, max */
661 param, param_len, 10, /* param, length, max */
662 NULL, data_len, cli->max_xmit /* data, length, max */
664 cli_receive_trans(cli, SMBtrans2,
667 if (!ret && cli_is_dos_error(cli)) {
668 /* we need to work around a Win95 bug - sometimes
669 it gives ERRSRV/ERRerror temprarily */
672 cli_dos_error(cli, &eclass, &ecode);
673 if (eclass != ERRSRV || ecode != ERRerror) break;
676 } while (count-- && ret==False);
678 if (!ret || !rdata || data_len < 4) {
679 return NT_STATUS_UNSUCCESSFUL;
682 len = IVAL(rdata, 0);
684 if (len > data_len - 4) {
685 return NT_STATUS_INVALID_NETWORK_RESPONSE;
688 clistr_pull(cli, alt_name, rdata+4, sizeof(fstring), len, 0);