r585: merge of fix for KB828741 -- pw chg -- from 3.0
[Samba.git] / source / libsmb / clirap.c
blobbea3939565f118daf1e071c053cd48bdb2e59317
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 client RAP calls
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.
22 #define NO_SYSLOG
24 #include "includes.h"
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,
38 pipe_name,
39 0,0, /* fid, flags */
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 /****************************************************************************
50 call a remote api
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 */
60 0,0, /* fid, flags */
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,
67 rparam, rprcnt,
68 rdata, rdrcnt));
72 /****************************************************************************
73 perform a NetWkstaUserLogon
74 ****************************************************************************/
75 BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
77 char *rparam = NULL;
78 char *rdata = NULL;
79 char *p;
80 int rdrcnt,rprcnt;
81 pstring param;
83 memset(param, 0, sizeof(param));
85 /* send a SMBtrans command with api NetWkstaUserLogon */
86 p = param;
87 SSVAL(p,0,132); /* api number */
88 p += 2;
89 pstrcpy(p,"OOWb54WrLh");
90 p = skip_string(p,1);
91 pstrcpy(p,"WB21BWDWWDDDDDDDzzzD");
92 p = skip_string(p,1);
93 SSVAL(p,0,1);
94 p += 2;
95 pstrcpy(p,user);
96 strupper(p);
97 p += 21;
98 p++;
99 p += 15;
100 p++;
101 pstrcpy(p, workstation);
102 strupper(p);
103 p += 16;
104 SSVAL(p, 0, CLI_BUFFER_SIZE);
105 p += 2;
106 SSVAL(p, 0, CLI_BUFFER_SIZE);
107 p += 2;
109 if (cli_api(cli,
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 */
114 )) {
115 cli->rap_error = rparam? SVAL(rparam,0) : -1;
116 p = rdata;
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);
122 } else {
123 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
127 SAFE_FREE(rparam);
128 SAFE_FREE(rdata);
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)
137 char *rparam = NULL;
138 char *rdata = NULL;
139 char *p;
140 int rdrcnt,rprcnt;
141 pstring param;
142 int count = -1;
144 /* now send a SMBtrans command with api RNetShareEnum */
145 p = param;
146 SSVAL(p,0,0); /* api number */
147 p += 2;
148 pstrcpy(p,"WrLeh");
149 p = skip_string(p,1);
150 pstrcpy(p,"B13BWz");
151 p = skip_string(p,1);
152 SSVAL(p,0,1);
154 * Win2k needs a *smaller* buffer than 0xFFFF here -
155 * it returns "out of server memory" with 0xFFFF !!! JRA.
157 SSVAL(p,2,0xFFE0);
158 p += 4;
160 if (cli_api(cli,
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);
170 int i;
172 count=SVAL(rparam,4);
173 p = rdata;
175 for (i=0;i<count;i++,p+=20) {
176 char *sname = p;
177 int type = SVAL(p,14);
178 int comment_offset = IVAL(p,16) & 0xFFFF;
179 const char *cmnt = comment_offset?(rdata+comment_offset-converter):"";
180 pstring s1, s2;
182 pstrcpy(s1, dos_to_unix_static(sname));
183 pstrcpy(s2, dos_to_unix_static(cmnt));
185 fn(s1, type, s2, state);
187 } else {
188 DEBUG(4,("NetShareEnum res=%d\n", res));
190 } else {
191 DEBUG(4,("NetShareEnum failed\n"));
194 SAFE_FREE(rparam);
195 SAFE_FREE(rdata);
197 return count;
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 *),
210 void *state)
212 char *rparam = NULL;
213 char *rdata = NULL;
214 int rdrcnt,rprcnt;
215 char *p;
216 pstring param;
217 int uLevel = 1;
218 int count = -1;
220 /* send a SMBtrans command with api NetServerEnum */
221 p = param;
222 SSVAL(p,0,0x68); /* api number */
223 p += 2;
224 pstrcpy(p,"WrLehDz");
225 p = skip_string(p,1);
227 pstrcpy(p,"B16BBDz");
229 p = skip_string(p,1);
230 SSVAL(p,0,uLevel);
231 SSVAL(p,2,CLI_BUFFER_SIZE);
232 p += 4;
233 SIVAL(p,0,stype);
234 p += 4;
236 p += clistr_push(cli, p, workgroup, -1,
237 STR_TERMINATE | STR_CONVERT | STR_ASCII);
239 if (cli_api(cli,
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 */
244 )) {
245 int res = rparam? SVAL(rparam,0) : -1;
247 if (res == 0 || res == ERRmoredata) {
248 int i;
249 int converter=SVAL(rparam,2);
251 count=SVAL(rparam,4);
252 p = rdata;
254 for (i = 0;i < count;i++, p += 26) {
255 char *sname = p;
256 int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
257 const char *cmnt = comment_offset?(rdata+comment_offset):"";
258 pstring s1, s2;
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);
271 SAFE_FREE(rparam);
272 SAFE_FREE(rdata);
274 return(count > 0);
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)];
286 char data[532];
287 char *p = param;
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;
294 char *rparam = NULL;
295 char *rdata = NULL;
296 int rprcnt, rdrcnt;
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));
301 return False;
304 SSVAL(p,0,214); /* SamOEMChangePassword command. */
305 p += 2;
306 pstrcpy(p, "zsT");
307 p = skip_string(p,1);
308 pstrcpy(p, "B516B16");
309 p = skip_string(p,1);
310 pstrcpy(p,user);
311 p = skip_string(p,1);
312 SSVAL(p,0,532);
313 p += 2;
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))
328 return 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]);
340 data_len = 532;
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 */
348 ) == False) {
349 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
350 user ));
351 return False;
354 if (cli_receive_trans(cli,SMBtrans,
355 &rparam, &rprcnt,
356 &rdata, &rdrcnt)) {
357 if (rparam)
358 cli->rap_error = SVAL(rparam,0);
361 SAFE_FREE(rparam);
362 SAFE_FREE(rdata);
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;
378 pstring param;
379 char *rparam=NULL, *rdata=NULL;
380 int count=8;
381 BOOL ret;
382 time_t (*date_fn)(void *);
383 char *p;
385 p = param;
386 memset(p, 0, 6);
387 SSVAL(p, 0, SMB_INFO_STANDARD);
388 p += 6;
389 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE|STR_CONVERT );
391 param_len = PTR_DIFF(p, param);
393 do {
394 ret = (cli_send_trans(cli, SMBtrans2,
395 NULL, /* Name */
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 */
400 ) &&
401 cli_receive_trans(cli, SMBtrans2,
402 &rparam, &param_len,
403 &rdata, &data_len));
404 if (!ret && cli_is_dos_error(cli)) {
405 /* we need to work around a Win95 bug - sometimes
406 it gives ERRSRV/ERRerror temprarily */
407 uint8 eclass;
408 uint32 ecode;
409 cli_dos_error(cli, &eclass, &ecode);
410 if (eclass != ERRSRV || ecode != ERRerror) break;
411 msleep(100);
413 } while (count-- && ret==False);
415 if (!ret || !rdata || data_len < 22) {
416 return False;
419 if (cli->win95) {
420 date_fn = make_unix_date;
421 } else {
422 date_fn = make_unix_date2;
425 if (c_time) {
426 *c_time = date_fn(rdata+0);
428 if (a_time) {
429 *a_time = date_fn(rdata+4);
431 if (m_time) {
432 *m_time = date_fn(rdata+8);
434 if (size) {
435 *size = IVAL(rdata, 12);
437 if (mode) {
438 *mode = SVAL(rdata,l1_attrFile);
441 SAFE_FREE(rdata);
442 SAFE_FREE(rparam);
443 return True;
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,
452 SMB_INO_T *ino)
454 unsigned int data_len = 0;
455 unsigned int param_len = 0;
456 uint16 setup = TRANSACT2_QPATHINFO;
457 pstring param;
458 char *rparam=NULL, *rdata=NULL;
459 char *p;
461 p = param;
462 memset(p, 0, 6);
463 SSVAL(p, 0, SMB_QUERY_FILE_ALL_INFO);
464 p += 6;
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,
470 NULL, /* name */
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 */
475 )) {
476 return False;
479 if (!cli_receive_trans(cli, SMBtrans2,
480 &rparam, &param_len,
481 &rdata, &data_len)) {
482 return False;
485 if (!rdata || data_len < 22) {
486 return False;
489 if (c_time) {
490 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
492 if (a_time) {
493 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
495 if (m_time) {
496 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
498 if (w_time) {
499 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
501 if (mode) {
502 *mode = SVAL(rdata, 32);
504 if (size) {
505 *size = IVAL(rdata, 48);
507 if (ino) {
508 *ino = IVAL(rdata, 64);
511 SAFE_FREE(rdata);
512 SAFE_FREE(rparam);
513 return True;
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;
528 pstring param;
529 char *rparam=NULL, *rdata=NULL;
531 /* if its a win95 server then fail this - win95 totally screws it
532 up */
533 if (cli->win95) return False;
535 param_len = 4;
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,
542 NULL, /* name */
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 */
547 )) {
548 return False;
551 if (!cli_receive_trans(cli, SMBtrans2,
552 &rparam, &param_len,
553 &rdata, &data_len)) {
554 return False;
557 if (!rdata || data_len < 68) {
558 return False;
561 if (c_time) {
562 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
564 if (a_time) {
565 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
567 if (m_time) {
568 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
570 if (w_time) {
571 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
573 if (mode) {
574 *mode = SVAL(rdata, 32);
576 if (size) {
577 *size = IVAL(rdata, 48);
579 if (ino) {
580 *ino = IVAL(rdata, 64);
583 SAFE_FREE(rdata);
584 SAFE_FREE(rparam);
585 return True;
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;
596 pstring param;
597 char *rparam=NULL, *rdata=NULL;
599 /* if its a win95 server then fail this - win95 totally screws it
600 up */
601 if (cli->win95) return False;
603 param_len = 4;
605 memset(param, 0, param_len);
606 SSVAL(param, 0, fnum);
607 SSVAL(param, 2, level);
609 if (!cli_send_trans(cli, SMBtrans2,
610 NULL, /* name */
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 */
615 )) {
616 return False;
619 if (!cli_receive_trans(cli, SMBtrans2,
620 &rparam, &param_len,
621 &rdata, &data_len)) {
622 return False;
625 memcpy(outdata, rdata, data_len);
627 SAFE_FREE(rdata);
628 SAFE_FREE(rparam);
629 return True;
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;
641 pstring param;
642 char *rparam=NULL, *rdata=NULL;
643 int count=8;
644 char *p;
645 BOOL ret;
646 int len;
648 p = param;
649 memset(p, 0, 6);
650 SSVAL(p, 0, SMB_QUERY_FILE_ALT_NAME_INFO);
651 p += 6;
652 p += clistr_push(cli, p, fname, sizeof(pstring)-6, STR_TERMINATE|STR_CONVERT);
654 param_len = PTR_DIFF(p, param);
656 do {
657 ret = (cli_send_trans(cli, SMBtrans2,
658 NULL, /* Name */
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 */
663 ) &&
664 cli_receive_trans(cli, SMBtrans2,
665 &rparam, &param_len,
666 &rdata, &data_len));
667 if (!ret && cli_is_dos_error(cli)) {
668 /* we need to work around a Win95 bug - sometimes
669 it gives ERRSRV/ERRerror temprarily */
670 uint8 eclass;
671 uint32 ecode;
672 cli_dos_error(cli, &eclass, &ecode);
673 if (eclass != ERRSRV || ecode != ERRerror) break;
674 msleep(100);
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);
690 SAFE_FREE(rdata);
691 SAFE_FREE(rparam);
693 return NT_STATUS_OK;