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
, int pipe_name_len
,
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 if (pipe_name_len
== 0)
38 pipe_name_len
= strlen(pipe_name
);
40 cli_send_trans(cli
, SMBtrans
,
41 pipe_name
, pipe_name_len
,
43 setup
, setup_count
, max_setup_count
,
44 params
, param_count
, max_param_count
,
45 data
, data_count
, max_data_count
);
47 return (cli_receive_trans(cli
, SMBtrans
,
48 rparam
, (int *)rparam_count
,
49 rdata
, (int *)rdata_count
));
52 /****************************************************************************
54 ****************************************************************************/
55 BOOL
cli_api(struct cli_state
*cli
,
56 char *param
, int prcnt
, int mprcnt
,
57 char *data
, int drcnt
, int mdrcnt
,
58 char **rparam
, int *rprcnt
,
59 char **rdata
, int *rdrcnt
)
61 cli_send_trans(cli
,SMBtrans
,
62 PIPE_LANMAN
,strlen(PIPE_LANMAN
), /* Name, length */
64 NULL
,0,0, /* Setup, length, max */
65 param
, prcnt
, mprcnt
, /* Params, length, max */
66 data
, drcnt
, mdrcnt
/* Data, length, max */
69 return (cli_receive_trans(cli
,SMBtrans
,
75 /****************************************************************************
76 perform a NetWkstaUserLogon
77 ****************************************************************************/
78 BOOL
cli_NetWkstaUserLogon(struct cli_state
*cli
,char *user
, char *workstation
)
86 memset(param
, 0, sizeof(param
));
88 /* send a SMBtrans command with api NetWkstaUserLogon */
90 SSVAL(p
,0,132); /* api number */
92 pstrcpy(p
,"OOWb54WrLh");
94 pstrcpy(p
,"WB21BWDWWDDDDDDDzzzD");
104 pstrcpy(p
, workstation
);
107 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
109 SSVAL(p
, 0, CLI_BUFFER_SIZE
);
113 param
, PTR_DIFF(p
,param
),1024, /* param, length, max */
114 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
115 &rparam
, &rprcnt
, /* return params, return size */
116 &rdata
, &rdrcnt
/* return data, return size */
118 cli
->rap_error
= rparam
? SVAL(rparam
,0) : -1;
121 if (cli
->rap_error
== 0) {
122 DEBUG(4,("NetWkstaUserLogon success\n"));
123 cli
->privileges
= SVAL(p
, 24);
124 fstrcpy(cli
->eff_name
,p
+2);
126 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli
->rap_error
));
134 return (cli
->rap_error
== 0);
137 /****************************************************************************
138 call a NetShareEnum - try and browse available connections on a host
139 ****************************************************************************/
140 int cli_RNetShareEnum(struct cli_state
*cli
, void (*fn
)(const char *, uint32
, const char *))
149 /* now send a SMBtrans command with api RNetShareEnum */
151 SSVAL(p
,0,0); /* api number */
154 p
= skip_string(p
,1);
156 p
= skip_string(p
,1);
159 * Win2k needs a *smaller* buffer than 0xFFFF here -
160 * it returns "out of server memory" with 0xFFFF !!! JRA.
166 param
, PTR_DIFF(p
,param
), 1024, /* Param, length, maxlen */
167 NULL
, 0, 0xFFE0, /* data, length, maxlen - Win2k needs a small buffer here too ! */
168 &rparam
, &rprcnt
, /* return params, length */
169 &rdata
, &rdrcnt
)) /* return data, length */
171 int res
= rparam
? SVAL(rparam
,0) : -1;
173 if (res
== 0 || res
== ERRmoredata
) {
174 int converter
=SVAL(rparam
,2);
177 count
=SVAL(rparam
,4);
180 for (i
=0;i
<count
;i
++,p
+=20) {
182 int type
= SVAL(p
,14);
183 int comment_offset
= IVAL(p
,16) & 0xFFFF;
184 char *cmnt
= comment_offset
?(rdata
+comment_offset
-converter
):"";
185 dos_to_unix(sname
,True
);
186 dos_to_unix(cmnt
,True
);
187 fn(sname
, type
, cmnt
);
190 DEBUG(4,("NetShareEnum res=%d\n", res
));
193 DEBUG(4,("NetShareEnum failed\n"));
205 /****************************************************************************
206 call a NetServerEnum for the specified workgroup and servertype mask.
207 This function then calls the specified callback function for each name returned.
209 The callback function takes 3 arguments: the machine name, the server type and
211 ****************************************************************************/
212 BOOL
cli_NetServerEnum(struct cli_state
*cli
, char *workgroup
, uint32 stype
,
213 void (*fn
)(const char *, uint32
, const char *))
223 /* send a SMBtrans command with api NetServerEnum */
225 SSVAL(p
,0,0x68); /* api number */
227 pstrcpy(p
,"WrLehDz");
228 p
= skip_string(p
,1);
230 pstrcpy(p
,"B16BBDz");
232 p
= skip_string(p
,1);
234 SSVAL(p
,2,CLI_BUFFER_SIZE
);
239 pstrcpy(p
, workgroup
);
240 p
= skip_string(p
,1);
243 param
, PTR_DIFF(p
,param
), 8, /* params, length, max */
244 NULL
, 0, CLI_BUFFER_SIZE
, /* data, length, max */
245 &rparam
, &rprcnt
, /* return params, return size */
246 &rdata
, &rdrcnt
/* return data, return size */
248 int res
= rparam
? SVAL(rparam
,0) : -1;
250 if (res
== 0 || res
== ERRmoredata
) {
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 char *cmnt
= comment_offset
?(rdata
+comment_offset
):"";
261 if (comment_offset
< 0 || comment_offset
> rdrcnt
) continue;
263 stype
= IVAL(p
,18) & ~SV_TYPE_LOCAL_LIST_ONLY
;
265 dos_to_unix(sname
, True
);
266 dos_to_unix(cmnt
, True
);
267 fn(sname
, stype
, cmnt
);
282 /****************************************************************************
283 Send a SamOEMChangePassword command
284 ****************************************************************************/
285 BOOL
cli_oem_change_password(struct cli_state
*cli
, const char *user
, const char *new_password
,
286 const char *old_password
)
288 char param
[16+sizeof(fstring
)];
291 fstring upper_case_old_pw
;
292 fstring upper_case_new_pw
;
293 unsigned char old_pw_hash
[16];
294 unsigned char new_pw_hash
[16];
300 pstring dos_new_password
;
302 if (strlen(user
) >= sizeof(fstring
)-1) {
303 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user
));
307 SSVAL(p
,0,214); /* SamOEMChangePassword command. */
310 p
= skip_string(p
,1);
311 pstrcpy(p
, "B516B16");
312 p
= skip_string(p
,1);
314 p
= skip_string(p
,1);
318 param_len
= PTR_DIFF(p
,param
);
321 * Get the Lanman hash of the old password, we
322 * use this as the key to make_oem_passwd_hash().
324 memset(upper_case_old_pw
, '\0', sizeof(upper_case_old_pw
));
325 fstrcpy(upper_case_old_pw
, old_password
);
326 unix_to_dos(upper_case_old_pw
,True
);
327 strupper(upper_case_old_pw
);
328 E_P16((uchar
*)upper_case_old_pw
, old_pw_hash
);
330 pstrcpy(dos_new_password
, new_password
);
331 unix_to_dos(dos_new_password
, True
);
333 if (!make_oem_passwd_hash( data
, dos_new_password
, old_pw_hash
, False
))
337 * Now place the old password hash in the data.
339 memset(upper_case_new_pw
, '\0', sizeof(upper_case_new_pw
));
340 fstrcpy(upper_case_new_pw
, new_password
);
341 unix_to_dos(upper_case_new_pw
,True
);
342 strupper(upper_case_new_pw
);
344 E_P16((uchar
*)upper_case_new_pw
, 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
,strlen(PIPE_LANMAN
), /* name, length */
352 0,0, /* fid, flags */
353 NULL
,0,0, /* setup, length, max */
354 param
,param_len
,2, /* param, length, max */
355 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
,
366 cli
->rap_error
= SVAL(rparam
,0);
374 return (cli
->rap_error
== 0);
378 /****************************************************************************
379 send a qpathinfo call
380 ****************************************************************************/
381 BOOL
cli_qpathinfo(struct cli_state
*cli
, const char *fname
,
382 time_t *c_time
, time_t *a_time
, time_t *m_time
,
383 size_t *size
, uint16
*mode
)
387 uint16 setup
= TRANSACT2_QPATHINFO
;
389 char *rparam
=NULL
, *rdata
=NULL
;
392 time_t (*date_fn
)(void *);
394 param_len
= strlen(fname
) + 7;
396 memset(param
, 0, param_len
);
397 SSVAL(param
, 0, SMB_INFO_STANDARD
);
398 pstrcpy(¶m
[6], fname
);
399 unix_to_dos(¶m
[6],True
);
402 ret
= (cli_send_trans(cli
, SMBtrans2
,
403 NULL
, 0, /* Name, length */
404 -1, 0, /* fid, flags */
405 &setup
, 1, 0, /* setup, length, max */
406 param
, param_len
, 10, /* param, length, max */
407 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
409 cli_receive_trans(cli
, SMBtrans2
,
413 /* we need to work around a Win95 bug - sometimes
414 it gives ERRSRV/ERRerror temprarily */
417 cli_error(cli
, &eclass
, &ecode
, NULL
);
418 if (eclass
!= ERRSRV
|| ecode
!= ERRerror
) break;
421 } while (count
-- && ret
==False
);
423 if (!ret
|| !rdata
|| data_len
< 22) {
428 date_fn
= make_unix_date
;
430 date_fn
= make_unix_date2
;
434 *c_time
= date_fn(rdata
+0);
437 *a_time
= date_fn(rdata
+4);
440 *m_time
= date_fn(rdata
+8);
443 *size
= IVAL(rdata
, 12);
446 *mode
= SVAL(rdata
,l1_attrFile
);
449 if (rdata
) free(rdata
);
450 if (rparam
) free(rparam
);
454 /****************************************************************************
455 send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
456 ****************************************************************************/
457 BOOL
cli_qpathinfo2(struct cli_state
*cli
, const char *fname
,
458 time_t *c_time
, time_t *a_time
, time_t *m_time
,
459 time_t *w_time
, size_t *size
, uint16
*mode
,
464 uint16 setup
= TRANSACT2_QPATHINFO
;
466 char *rparam
=NULL
, *rdata
=NULL
;
468 param_len
= strlen(fname
) + 7;
470 memset(param
, 0, param_len
);
471 SSVAL(param
, 0, SMB_QUERY_FILE_ALL_INFO
);
472 pstrcpy(¶m
[6], fname
);
473 unix_to_dos(¶m
[6],True
);
475 if (!cli_send_trans(cli
, SMBtrans2
,
476 NULL
, 0, /* name, length */
477 -1, 0, /* fid, flags */
478 &setup
, 1, 0, /* setup, length, max */
479 param
, param_len
, 10, /* param, length, max */
480 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
485 if (!cli_receive_trans(cli
, SMBtrans2
,
487 &rdata
, &data_len
)) {
491 if (!rdata
|| data_len
< 22) {
496 *c_time
= interpret_long_date(rdata
+0) - cli
->serverzone
;
499 *a_time
= interpret_long_date(rdata
+8) - cli
->serverzone
;
502 *m_time
= interpret_long_date(rdata
+16) - cli
->serverzone
;
505 *w_time
= interpret_long_date(rdata
+24) - cli
->serverzone
;
508 *mode
= SVAL(rdata
, 32);
511 *size
= IVAL(rdata
, 48);
514 *ino
= IVAL(rdata
, 64);
517 if (rdata
) free(rdata
);
518 if (rparam
) free(rparam
);
523 /****************************************************************************
524 send a qfileinfo call
525 ****************************************************************************/
526 BOOL
cli_qfileinfo(struct cli_state
*cli
, int fnum
,
527 uint16
*mode
, size_t *size
,
528 time_t *c_time
, time_t *a_time
, time_t *m_time
,
529 time_t *w_time
, SMB_INO_T
*ino
)
533 uint16 setup
= TRANSACT2_QFILEINFO
;
535 char *rparam
=NULL
, *rdata
=NULL
;
537 /* if its a win95 server then fail this - win95 totally screws it
539 if (cli
->win95
) return False
;
543 memset(param
, 0, param_len
);
544 SSVAL(param
, 0, fnum
);
545 SSVAL(param
, 2, SMB_QUERY_FILE_ALL_INFO
);
547 if (!cli_send_trans(cli
, SMBtrans2
,
548 NULL
, 0, /* name, length */
549 -1, 0, /* fid, flags */
550 &setup
, 1, 0, /* setup, length, max */
551 param
, param_len
, 2, /* param, length, max */
552 NULL
, data_len
, cli
->max_xmit
/* data, length, max */
557 if (!cli_receive_trans(cli
, SMBtrans2
,
559 &rdata
, &data_len
)) {
563 if (!rdata
|| data_len
< 68) {
568 *c_time
= interpret_long_date(rdata
+0) - cli
->serverzone
;
571 *a_time
= interpret_long_date(rdata
+8) - cli
->serverzone
;
574 *m_time
= interpret_long_date(rdata
+16) - cli
->serverzone
;
577 *w_time
= interpret_long_date(rdata
+24) - cli
->serverzone
;
580 *mode
= SVAL(rdata
, 32);
583 *size
= IVAL(rdata
, 48);
586 *ino
= IVAL(rdata
, 64);
589 if (rdata
) free(rdata
);
590 if (rparam
) free(rparam
);