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
, const 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
, (unsigned int *)rparam_count
,
46 rdata
, (unsigned 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
, unsigned int *rprcnt
,
56 char **rdata
, unsigned 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 const char *cmnt
= comment_offset
?(rdata
+comment_offset
-converter
):"";
182 pstrcpy(s1
, dos_to_unix_static(sname
));
183 pstrcpy(s2
, dos_to_unix_static(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
+= clistr_push(cli
, p
, workgroup
, -1,
237 STR_TERMINATE
| STR_CONVERT
| STR_ASCII
);
240 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
241 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
242 &rparam
, &rprcnt
, /* return params, return size */
243 &rdata
, &rdrcnt
/* return data, return size */
245 int res
= rparam
? SVAL(rparam
,0) : -1;
247 if (res
== 0 || res
== ERRmoredata
) {
249 int converter
=SVAL(rparam
,2);
251 count
=SVAL(rparam
,4);
254 for (i
= 0;i
< count
;i
++, p
+= 26) {
256 int comment_offset
= (IVAL(p
,22) & 0xFFFF)-converter
;
257 const char *cmnt
= comment_offset
?(rdata
+comment_offset
):"";
260 if (comment_offset
< 0 || comment_offset
> rdrcnt
) continue;
262 stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
264 pstrcpy(s1
, dos_to_unix_static(sname
));
265 pstrcpy(s2
, dos_to_unix_static(cmnt
));
266 fn(s1
, stype
, s2
, state
);
279 /****************************************************************************
280 Send a SamOEMChangePassword command
281 ****************************************************************************/
282 BOOL
cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
283 const char *old_password
)
285 char param
[16+sizeof(fstring
)];
288 fstring upper_case_old_pw
;
289 fstring upper_case_new_pw
;
290 unsigned char old_pw_hash
[16];
291 unsigned char new_pw_hash
[16];
292 unsigned int data_len
;
293 unsigned int param_len
= 0;
297 pstring dos_new_password
;
299 if (strlen(user
) >= sizeof(fstring
)-1) {
300 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
304 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
307 p
= skip_string(p
,1);
308 pstrcpy(p
, "B516B16");
309 p
= skip_string(p
,1);
311 p
= skip_string(p
,1);
315 param_len
= PTR_DIFF(p
,param
);
318 * Get the Lanman hash of the old password, we
319 * use this as the key to make_oem_passwd_hash().
321 memset(upper_case_old_pw
, '\0', sizeof(upper_case_old_pw
));
322 clistr_push(cli
, upper_case_old_pw
, old_password
, -1,STR_CONVERT
|STR_TERMINATE
|STR_UPPER
|STR_ASCII
);
323 E_P16((uchar
*)upper_case_old_pw
, old_pw_hash
);
325 clistr_push(cli
, dos_new_password
, new_password
, -1, STR_CONVERT
|STR_TERMINATE
|STR_ASCII
);
327 if (!make_oem_passwd_hash( data
, dos_new_password
, old_pw_hash
, False
))
331 * Now place the old password hash in the data.
333 memset(upper_case_new_pw
, '\0', sizeof(upper_case_new_pw
));
334 clistr_push(cli
, upper_case_new_pw
, new_password
, -1, STR_CONVERT
|STR_TERMINATE
|STR_UPPER
|STR_ASCII
);
336 E_P16((uchar
*)upper_case_new_pw
, new_pw_hash
);
338 E_old_pw_hash( new_pw_hash
, old_pw_hash
, (uchar
*)&data
[516]);
342 if (cli_send_trans(cli
,SMBtrans
,
343 PIPE_LANMAN
, /* name */
344 0,0, /* fid, flags */
345 NULL
,0,0, /* setup, length, max */
346 param
,param_len
,2, /* param, length, max */
347 data
,data_len
,0 /* data, length, max */
349 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
354 if (cli_receive_trans(cli
,SMBtrans
,
358 cli
->rap_error
= SVAL(rparam
,0);
364 return (cli
->rap_error
== 0);
368 /****************************************************************************
369 send a qpathinfo call
370 ****************************************************************************/
371 BOOL
cli_qpathinfo(struct cli_state
*cli
, const char *fname
,
372 time_t *c_time
, time_t *a_time
, time_t *m_time
,
373 size_t *size
, uint16
*mode
)
375 unsigned int data_len
= 0;
376 unsigned int param_len
= 0;
377 uint16 setup
= TRANSACT2_QPATHINFO
;
379 char *rparam
=NULL
, *rdata
=NULL
;
382 time_t (*date_fn
)(void *);
387 SSVAL(p
, 0, SMB_INFO_STANDARD
);
389 p
+= clistr_push(cli
, p
, fname
, sizeof(pstring
)-6, STR_TERMINATE
|STR_CONVERT
);
391 param_len
= PTR_DIFF(p
, param
);
394 ret
= (cli_send_trans(cli
, SMBtrans2
,
396 -1, 0, /* fid, flags */
397 &setup
, 1, 0, /* setup, length, max */
398 param
, param_len
, 10, /* param, length, max */
399 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
401 cli_receive_trans(cli
, SMBtrans2
,
404 if (!ret
&& cli_is_dos_error(cli
)) {
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
|| data_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
,
454 unsigned int data_len
= 0;
455 unsigned int param_len
= 0;
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
|STR_CONVERT
);
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
)
525 unsigned int data_len
= 0;
526 unsigned int param_len
= 0;
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
)
593 unsigned int data_len
= 0;
594 unsigned int param_len
= 0;
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
);
632 /****************************************************************************
633 Send a qpathinfo SMB_QUERY_FILE_ALT_NAME_INFO call.
634 ****************************************************************************/
636 NTSTATUS
cli_qpathinfo_alt_name(struct cli_state
*cli
, const char *fname
, fstring alt_name
)
638 unsigned int data_len
= 0;
639 unsigned int param_len
= 0;
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
|STR_CONVERT
);
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);