2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1994-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /****************************************************************************
27 Call a remote api on an arbitrary pipe. takes param, data and setup buffers.
28 ****************************************************************************/
29 BOOL cli_api_pipe(struct cli_state *cli, char *pipe_name,
30 uint16 *setup, uint32 setup_count, uint32 max_setup_count,
31 char *params, uint32 param_count, uint32 max_param_count,
32 char *data, uint32 data_count, uint32 max_data_count,
33 char **rparam, uint32 *rparam_count,
34 char **rdata, uint32 *rdata_count)
36 cli_send_trans(cli, SMBtrans,
39 setup, setup_count, max_setup_count,
40 params, param_count, max_param_count,
41 data, data_count, max_data_count);
43 return (cli_receive_trans(cli, SMBtrans,
44 rparam, (int *)rparam_count,
45 rdata, (int *)rdata_count));
48 /****************************************************************************
50 ****************************************************************************/
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, int *rprcnt,
55 char **rdata, 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,
71 /****************************************************************************
72 perform a NetWkstaUserLogon
73 ****************************************************************************/
74 BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
82 memset(param, 0, sizeof(param));
84 /* send a SMBtrans command with api NetWkstaUserLogon */
86 SSVAL(p,0,132); /* api number */
88 pstrcpy(p,"OOWb54WrLh");
90 pstrcpy(p,"WB21BWDWWDDDDDDDzzzD");
100 pstrcpy(p, workstation);
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 fstrcpy(cli->eff_name,p+2);
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 ****************************************************************************/
134 int cli_RNetShareEnum(struct cli_state *cli, void (*fn)(const char *, uint32, const char *, void *), void *state)
143 /* now send a SMBtrans command with api RNetShareEnum */
145 SSVAL(p,0,0); /* api number */
148 p = skip_string(p,1);
150 p = skip_string(p,1);
153 * Win2k needs a *smaller* buffer than 0xFFFF here -
154 * it returns "out of server memory" with 0xFFFF !!! JRA.
160 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
161 NULL, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
162 &rparam, &rprcnt, /* return params, length */
163 &rdata, &rdrcnt)) /* return data, length */
165 int res = rparam? SVAL(rparam,0) : -1;
167 if (res == 0 || res == ERRmoredata) {
168 int converter=SVAL(rparam,2);
171 count=SVAL(rparam,4);
174 for (i=0;i<count;i++,p+=20) {
176 int type = SVAL(p,14);
177 int comment_offset = IVAL(p,16) & 0xFFFF;
178 char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
181 pull_ascii_pstring(s1, sname);
182 pull_ascii_pstring(s2, cmnt);
184 fn(s1, type, s2, state);
187 DEBUG(4,("NetShareEnum res=%d\n", res));
190 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 ****************************************************************************/
207 BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
208 void (*fn)(const char *, uint32, const char *, void *),
219 /* send a SMBtrans command with api NetServerEnum */
221 SSVAL(p,0,0x68); /* api number */
223 pstrcpy(p,"WrLehDz");
224 p = skip_string(p,1);
226 pstrcpy(p,"B16BBDz");
228 p = skip_string(p,1);
230 SSVAL(p,2,CLI_BUFFER_SIZE);
235 p += push_pstring(p, workgroup);
238 param, PTR_DIFF(p,param), 8, /* params, length, max */
239 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
240 &rparam, &rprcnt, /* return params, return size */
241 &rdata, &rdrcnt /* return data, return size */
243 int res = rparam? SVAL(rparam,0) : -1;
245 if (res == 0 || res == ERRmoredata) {
247 int converter=SVAL(rparam,2);
249 count=SVAL(rparam,4);
252 for (i = 0;i < count;i++, p += 26) {
254 int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
255 char *cmnt = comment_offset?(rdata+comment_offset):"";
258 if (comment_offset < 0 || comment_offset > rdrcnt) continue;
260 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
262 pull_ascii_pstring(s1, sname);
263 pull_ascii_pstring(s2, cmnt);
264 fn(s1, stype, s2, state);
277 /****************************************************************************
278 Send a SamOEMChangePassword command
279 ****************************************************************************/
280 BOOL cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
281 const char *old_password)
283 char param[16+sizeof(fstring)];
286 fstring upper_case_old_pw;
287 fstring upper_case_new_pw;
288 unsigned char old_pw_hash[16];
289 unsigned char new_pw_hash[16];
295 pstring dos_new_password;
297 if (strlen(user) >= sizeof(fstring)-1) {
298 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
302 SSVAL(p,0,214); /* SamOEMChangePassword command. */
305 p = skip_string(p,1);
306 pstrcpy(p, "B516B16");
307 p = skip_string(p,1);
309 p = skip_string(p,1);
313 param_len = PTR_DIFF(p,param);
316 * Get the Lanman hash of the old password, we
317 * use this as the key to make_oem_passwd_hash().
319 memset(upper_case_old_pw, '\0', sizeof(upper_case_old_pw));
320 clistr_push(cli, upper_case_old_pw, old_password, -1,STR_TERMINATE|STR_UPPER|STR_ASCII);
321 E_P16((uchar *)upper_case_old_pw, old_pw_hash);
323 clistr_push(cli, dos_new_password, new_password, -1, STR_TERMINATE|STR_ASCII);
325 if (!make_oem_passwd_hash( data, dos_new_password, old_pw_hash, False))
329 * Now place the old password hash in the data.
331 memset(upper_case_new_pw, '\0', sizeof(upper_case_new_pw));
332 clistr_push(cli, upper_case_new_pw, new_password, -1, STR_TERMINATE|STR_UPPER|STR_ASCII);
334 E_P16((uchar *)upper_case_new_pw, new_pw_hash);
336 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
340 if (cli_send_trans(cli,SMBtrans,
341 PIPE_LANMAN, /* name */
342 0,0, /* fid, flags */
343 NULL,0,0, /* setup, length, max */
344 param,param_len,2, /* param, length, max */
345 data,data_len,0 /* data, length, max */
347 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
352 if (cli_receive_trans(cli,SMBtrans,
356 cli->rap_error = SVAL(rparam,0);
362 return (cli->rap_error == 0);
366 /****************************************************************************
367 send a qpathinfo call
368 ****************************************************************************/
369 BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
370 time_t *c_time, time_t *a_time, time_t *m_time,
371 size_t *size, uint16 *mode)
375 int rparam_len, rdata_len;
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,
401 &rparam, &rparam_len,
402 &rdata, &rdata_len));
403 if (!cli_is_dos_error(cli)) break;
405 /* we need to work around a Win95 bug - sometimes
406 it gives ERRSRV/ERRerror temprarily */
409 cli_dos_error(cli, &eclass, &ecode);
410 if (eclass != ERRSRV || ecode != ERRerror) break;
413 } while (count-- && ret==False);
415 if (!ret || !rdata || rdata_len < 22) {
420 date_fn = make_unix_date;
422 date_fn = make_unix_date2;
426 *c_time = date_fn(rdata+0);
429 *a_time = date_fn(rdata+4);
432 *m_time = date_fn(rdata+8);
435 *size = IVAL(rdata, 12);
438 *mode = SVAL(rdata,l1_attrFile);
446 /****************************************************************************
447 send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
448 ****************************************************************************/
449 BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
450 time_t *c_time, time_t *a_time, time_t *m_time,
451 time_t *w_time, size_t *size, uint16 *mode,
456 uint16 setup = TRANSACT2_QPATHINFO;
458 char *rparam=NULL, *rdata=NULL;
463 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
465 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
467 param_len = PTR_DIFF(p, param);
469 if (!cli_send_trans(cli, SMBtrans2,
471 -1, 0, /* fid, flags */
472 &setup, 1, 0, /* setup, length, max */
473 param, param_len, 10, /* param, length, max */
474 NULL, data_len, cli->max_xmit /* data, length, max */
479 if (!cli_receive_trans(cli, SMBtrans2,
481 &rdata, &data_len)) {
485 if (!rdata || data_len < 22) {
490 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
493 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
496 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
499 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
502 *mode = SVAL(rdata, 32);
505 *size = IVAL(rdata, 48);
508 *ino = IVAL(rdata, 64);
517 /****************************************************************************
518 send a qfileinfo call
519 ****************************************************************************/
520 BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
521 uint16 *mode, size_t *size,
522 time_t *c_time, time_t *a_time, time_t *m_time,
523 time_t *w_time, SMB_INO_T *ino)
527 uint16 setup = TRANSACT2_QFILEINFO;
529 char *rparam=NULL, *rdata=NULL;
531 /* if its a win95 server then fail this - win95 totally screws it
533 if (cli->win95) return False;
537 memset(param, 0, param_len);
538 SSVAL(param, 0, fnum);
539 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
541 if (!cli_send_trans(cli, SMBtrans2,
543 -1, 0, /* fid, flags */
544 &setup, 1, 0, /* setup, length, max */
545 param, param_len, 2, /* param, length, max */
546 NULL, data_len, cli->max_xmit /* data, length, max */
551 if (!cli_receive_trans(cli, SMBtrans2,
553 &rdata, &data_len)) {
557 if (!rdata || data_len < 68) {
562 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
565 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
568 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
571 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
574 *mode = SVAL(rdata, 32);
577 *size = IVAL(rdata, 48);
580 *ino = IVAL(rdata, 64);
588 /****************************************************************************
589 send a qfileinfo call
590 ****************************************************************************/
591 BOOL cli_qfileinfo_test(struct cli_state *cli, int fnum, int level, char *outdata)
595 uint16 setup = TRANSACT2_QFILEINFO;
597 char *rparam=NULL, *rdata=NULL;
599 /* if its a win95 server then fail this - win95 totally screws it
601 if (cli->win95) return False;
605 memset(param, 0, param_len);
606 SSVAL(param, 0, fnum);
607 SSVAL(param, 2, level);
609 if (!cli_send_trans(cli, SMBtrans2,
611 -1, 0, /* fid, flags */
612 &setup, 1, 0, /* setup, length, max */
613 param, param_len, 2, /* param, length, max */
614 NULL, data_len, cli->max_xmit /* data, length, max */
619 if (!cli_receive_trans(cli, SMBtrans2,
621 &rdata, &data_len)) {
625 memcpy(outdata, rdata, data_len);
634 /****************************************************************************
635 send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call
636 ****************************************************************************/
637 NTSTATUS cli_qpathinfo_alt_name(struct cli_state *cli, const char *fname, fstring alt_name)
641 uint16 setup = TRANSACT2_QPATHINFO;
643 char *rparam=NULL, *rdata=NULL;
651 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
653 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE);
655 param_len = PTR_DIFF(p, param);
658 ret = (cli_send_trans(cli, SMBtrans2,
660 -1, 0, /* fid, flags */
661 &setup, 1, 0, /* setup, length, max */
662 param, param_len, 10, /* param, length, max */
663 NULL, data_len, cli->max_xmit /* data, length, max */
665 cli_receive_trans(cli, SMBtrans2,
668 if (!ret && cli_is_dos_error(cli)) {
669 /* we need to work around a Win95 bug - sometimes
670 it gives ERRSRV/ERRerror temprarily */
673 cli_dos_error(cli, &eclass, &ecode);
674 if (eclass != ERRSRV || ecode != ERRerror) break;
677 } while (count-- && ret==False);
679 if (!ret || !rdata || data_len < 4) {
680 return NT_STATUS_UNSUCCESSFUL;
683 len = IVAL(rdata, 0);
685 if (len > data_len - 4) {
686 return NT_STATUS_INVALID_NETWORK_RESPONSE;
689 clistr_pull(cli, alt_name, rdata+4, sizeof(fstring), len, 0);