Fix calls to smbc_init in testsmbc.c and tree.c
[Samba/gbeck.git] / source3 / libsmb / clirap.c
blobcd261eac595745b2de59cec57283486d31bc9965
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, 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,
42 0,0, /* fid, flags */
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 /****************************************************************************
53 call a remote api
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 */
63 0,0, /* fid, flags */
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,
70 rparam, rprcnt,
71 rdata, rdrcnt));
75 /****************************************************************************
76 perform a NetWkstaUserLogon
77 ****************************************************************************/
78 BOOL cli_NetWkstaUserLogon(struct cli_state *cli,char *user, char *workstation)
80 char *rparam = NULL;
81 char *rdata = NULL;
82 char *p;
83 int rdrcnt,rprcnt;
84 pstring param;
86 memset(param, 0, sizeof(param));
88 /* send a SMBtrans command with api NetWkstaUserLogon */
89 p = param;
90 SSVAL(p,0,132); /* api number */
91 p += 2;
92 pstrcpy(p,"OOWb54WrLh");
93 p = skip_string(p,1);
94 pstrcpy(p,"WB21BWDWWDDDDDDDzzzD");
95 p = skip_string(p,1);
96 SSVAL(p,0,1);
97 p += 2;
98 pstrcpy(p,user);
99 strupper(p);
100 p += 21;
101 p++;
102 p += 15;
103 p++;
104 pstrcpy(p, workstation);
105 strupper(p);
106 p += 16;
107 SSVAL(p, 0, CLI_BUFFER_SIZE);
108 p += 2;
109 SSVAL(p, 0, CLI_BUFFER_SIZE);
110 p += 2;
112 if (cli_api(cli,
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 */
117 )) {
118 cli->rap_error = rparam? SVAL(rparam,0) : -1;
119 p = rdata;
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);
125 } else {
126 DEBUG(1,("NetwkstaUserLogon gave error %d\n", cli->rap_error));
130 if (rparam)
131 free(rparam);
132 if (rdata)
133 free(rdata);
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 *, void *), void *state)
142 char *rparam = NULL;
143 char *rdata = NULL;
144 char *p;
145 int rdrcnt,rprcnt;
146 pstring param;
147 int count = -1;
149 /* now send a SMBtrans command with api RNetShareEnum */
150 p = param;
151 SSVAL(p,0,0); /* api number */
152 p += 2;
153 pstrcpy(p,"WrLeh");
154 p = skip_string(p,1);
155 pstrcpy(p,"B13BWz");
156 p = skip_string(p,1);
157 SSVAL(p,0,1);
159 * Win2k needs a *smaller* buffer than 0xFFFF here -
160 * it returns "out of server memory" with 0xFFFF !!! JRA.
162 SSVAL(p,2,0xFFE0);
163 p += 4;
165 if (cli_api(cli,
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);
175 int i;
177 count=SVAL(rparam,4);
178 p = rdata;
180 for (i=0;i<count;i++,p+=20) {
181 char *sname = p;
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, state);
189 } else {
190 DEBUG(4,("NetShareEnum res=%d\n", res));
192 } else {
193 DEBUG(4,("NetShareEnum failed\n"));
196 if (rparam)
197 free(rparam);
198 if (rdata)
199 free(rdata);
201 return count;
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
210 the comment.
211 ****************************************************************************/
212 BOOL cli_NetServerEnum(struct cli_state *cli, char *workgroup, uint32 stype,
213 void (*fn)(const char *, uint32, const char *, void *),
214 void *state)
216 char *rparam = NULL;
217 char *rdata = NULL;
218 int rdrcnt,rprcnt;
219 char *p;
220 pstring param;
221 int uLevel = 1;
222 int count = -1;
225 * First, check that the stype is reasonable ...
228 if (stype&0x80000000 && stype&0x7FFFFFFF) {
230 /* Set an error here ... */
232 return False;
236 /* send a SMBtrans command with api NetServerEnum */
237 p = param;
238 SSVAL(p,0,0x68); /* api number */
239 p += 2;
240 if (!(stype&0x80000000))
241 pstrcpy(p,"WrLehDz");
242 else
243 pstrcpy(p,"WrLehDO");
244 p = skip_string(p,1);
246 if (!(stype&0x80000000)) {
247 pstrcpy(p,"B16BBDz");
248 uLevel = 1;
250 else {
251 pstrcpy(p,"B16");
252 uLevel = 0;
255 p = skip_string(p,1);
256 SSVAL(p,0,uLevel);
257 SSVAL(p,2,CLI_BUFFER_SIZE);
258 p += 4;
259 SIVAL(p,0,stype);
260 p += 4;
262 pstrcpy(p, workgroup);
263 unix_to_dos(p, True);
264 p = skip_string(p,1);
266 if (cli_api(cli,
267 param, PTR_DIFF(p,param), 8, /* params, length, max */
268 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
269 &rparam, &rprcnt, /* return params, return size */
270 &rdata, &rdrcnt /* return data, return size */
271 )) {
272 int res = rparam? SVAL(rparam,0) : -1;
274 if (res == 0 || res == ERRmoredata) {
275 int i;
276 int converter=SVAL(rparam,2);
278 count=SVAL(rparam,4);
279 p = rdata;
282 if (!(stype&0x80000000)) {
283 for (i = 0;i < count;i++, p += 26) {
284 char *sname = p;
285 int comment_offset = (IVAL(p,22) & 0xFFFF)-converter;
286 char *cmnt = comment_offset?(rdata+comment_offset):"";
287 if (comment_offset < 0 || comment_offset > rdrcnt) continue;
289 stype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
291 dos_to_unix(sname, True);
292 dos_to_unix(cmnt, True);
293 fn(sname, stype, cmnt, state);
296 else {
297 for (i = 0; i < count; i++, p+= 16) {
298 char *sname = p;
300 dos_to_unix(sname, True);
302 fn(sname, stype, NULL, state);
309 if (rparam)
310 free(rparam);
311 if (rdata)
312 free(rdata);
314 return(count > 0);
319 /****************************************************************************
320 Send a SamOEMChangePassword command
321 ****************************************************************************/
322 BOOL cli_oem_change_password(struct cli_state *cli, const char *user, const char *new_password,
323 const char *old_password)
325 char param[16+sizeof(fstring)];
326 char data[532];
327 char *p = param;
328 fstring upper_case_old_pw;
329 fstring upper_case_new_pw;
330 unsigned char old_pw_hash[16];
331 unsigned char new_pw_hash[16];
332 int data_len;
333 int param_len = 0;
334 char *rparam = NULL;
335 char *rdata = NULL;
336 int rprcnt, rdrcnt;
337 pstring dos_new_password;
339 if (strlen(user) >= sizeof(fstring)-1) {
340 DEBUG(0,("cli_oem_change_password: user name %s is too long.\n", user));
341 return False;
344 SSVAL(p,0,214); /* SamOEMChangePassword command. */
345 p += 2;
346 pstrcpy(p, "zsT");
347 p = skip_string(p,1);
348 pstrcpy(p, "B516B16");
349 p = skip_string(p,1);
350 pstrcpy(p,user);
351 p = skip_string(p,1);
352 SSVAL(p,0,532);
353 p += 2;
355 param_len = PTR_DIFF(p,param);
358 * Get the Lanman hash of the old password, we
359 * use this as the key to make_oem_passwd_hash().
361 memset(upper_case_old_pw, '\0', sizeof(upper_case_old_pw));
362 fstrcpy(upper_case_old_pw, old_password);
363 unix_to_dos(upper_case_old_pw,True);
364 strupper(upper_case_old_pw);
365 E_P16((uchar *)upper_case_old_pw, old_pw_hash);
367 pstrcpy(dos_new_password, new_password);
368 unix_to_dos(dos_new_password, True);
370 if (!make_oem_passwd_hash( data, dos_new_password, old_pw_hash, False))
371 return False;
374 * Now place the old password hash in the data.
376 memset(upper_case_new_pw, '\0', sizeof(upper_case_new_pw));
377 fstrcpy(upper_case_new_pw, new_password);
378 unix_to_dos(upper_case_new_pw,True);
379 strupper(upper_case_new_pw);
381 E_P16((uchar *)upper_case_new_pw, new_pw_hash);
383 E_old_pw_hash( new_pw_hash, old_pw_hash, (uchar *)&data[516]);
385 data_len = 532;
387 if (cli_send_trans(cli,SMBtrans,
388 PIPE_LANMAN,strlen(PIPE_LANMAN), /* name, length */
389 0,0, /* fid, flags */
390 NULL,0,0, /* setup, length, max */
391 param,param_len,2, /* param, length, max */
392 data,data_len,0 /* data, length, max */
393 ) == False) {
394 DEBUG(0,("cli_oem_change_password: Failed to send password change for user %s\n",
395 user ));
396 return False;
399 if (cli_receive_trans(cli,SMBtrans,
400 &rparam, &rprcnt,
401 &rdata, &rdrcnt)) {
402 if (rparam)
403 cli->rap_error = SVAL(rparam,0);
406 if (rparam)
407 free(rparam);
408 if (rdata)
409 free(rdata);
411 return (cli->rap_error == 0);
415 /****************************************************************************
416 send a qpathinfo call
417 ****************************************************************************/
418 BOOL cli_qpathinfo(struct cli_state *cli, const char *fname,
419 time_t *c_time, time_t *a_time, time_t *m_time,
420 size_t *size, uint16 *mode)
422 int data_len = 0;
423 int param_len = 0;
424 uint16 setup = TRANSACT2_QPATHINFO;
425 pstring param;
426 char *rparam=NULL, *rdata=NULL;
427 int count=8;
428 BOOL ret;
429 time_t (*date_fn)(void *);
431 param_len = strlen(fname) + 7;
433 memset(param, 0, param_len);
434 SSVAL(param, 0, SMB_INFO_STANDARD);
435 pstrcpy(&param[6], fname);
436 unix_to_dos(&param[6],True);
438 do {
439 ret = (cli_send_trans(cli, SMBtrans2,
440 NULL, 0, /* Name, length */
441 -1, 0, /* fid, flags */
442 &setup, 1, 0, /* setup, length, max */
443 param, param_len, 10, /* param, length, max */
444 NULL, data_len, cli->max_xmit /* data, length, max */
445 ) &&
446 cli_receive_trans(cli, SMBtrans2,
447 &rparam, &param_len,
448 &rdata, &data_len));
449 if (!ret) {
450 /* we need to work around a Win95 bug - sometimes
451 it gives ERRSRV/ERRerror temprarily */
452 uint8 eclass;
453 uint32 ecode;
454 cli_error(cli, &eclass, &ecode, NULL);
455 if (eclass != ERRSRV || ecode != ERRerror) break;
456 msleep(100);
458 } while (count-- && ret==False);
460 if (!ret || !rdata || data_len < 22) {
461 return False;
464 if (cli->win95) {
465 date_fn = make_unix_date;
466 } else {
467 date_fn = make_unix_date2;
470 if (c_time) {
471 *c_time = date_fn(rdata+0);
473 if (a_time) {
474 *a_time = date_fn(rdata+4);
476 if (m_time) {
477 *m_time = date_fn(rdata+8);
479 if (size) {
480 *size = IVAL(rdata, 12);
482 if (mode) {
483 *mode = SVAL(rdata,l1_attrFile);
486 if (rdata) free(rdata);
487 if (rparam) free(rparam);
488 return True;
491 /****************************************************************************
492 send a qpathinfo call with the SMB_QUERY_FILE_ALL_INFO info level
493 ****************************************************************************/
494 BOOL cli_qpathinfo2(struct cli_state *cli, const char *fname,
495 time_t *c_time, time_t *a_time, time_t *m_time,
496 time_t *w_time, size_t *size, uint16 *mode,
497 SMB_INO_T *ino)
499 int data_len = 0;
500 int param_len = 0;
501 uint16 setup = TRANSACT2_QPATHINFO;
502 pstring param;
503 char *rparam=NULL, *rdata=NULL;
505 param_len = strlen(fname) + 7;
507 memset(param, 0, param_len);
508 SSVAL(param, 0, SMB_QUERY_FILE_ALL_INFO);
509 pstrcpy(&param[6], fname);
510 unix_to_dos(&param[6],True);
512 if (!cli_send_trans(cli, SMBtrans2,
513 NULL, 0, /* name, length */
514 -1, 0, /* fid, flags */
515 &setup, 1, 0, /* setup, length, max */
516 param, param_len, 10, /* param, length, max */
517 NULL, data_len, cli->max_xmit /* data, length, max */
518 )) {
519 return False;
522 if (!cli_receive_trans(cli, SMBtrans2,
523 &rparam, &param_len,
524 &rdata, &data_len)) {
525 return False;
528 if (!rdata || data_len < 22) {
529 return False;
532 if (c_time) {
533 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
535 if (a_time) {
536 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
538 if (m_time) {
539 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
541 if (w_time) {
542 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
544 if (mode) {
545 *mode = SVAL(rdata, 32);
547 if (size) {
548 *size = IVAL(rdata, 48);
550 if (ino) {
551 *ino = IVAL(rdata, 64);
554 if (rdata) free(rdata);
555 if (rparam) free(rparam);
556 return True;
560 /****************************************************************************
561 send a qfileinfo call
562 ****************************************************************************/
563 BOOL cli_qfileinfo(struct cli_state *cli, int fnum,
564 uint16 *mode, size_t *size,
565 time_t *c_time, time_t *a_time, time_t *m_time,
566 time_t *w_time, SMB_INO_T *ino)
568 int data_len = 0;
569 int param_len = 0;
570 uint16 setup = TRANSACT2_QFILEINFO;
571 pstring param;
572 char *rparam=NULL, *rdata=NULL;
574 /* if its a win95 server then fail this - win95 totally screws it
575 up */
576 if (cli->win95) return False;
578 param_len = 4;
580 memset(param, 0, param_len);
581 SSVAL(param, 0, fnum);
582 SSVAL(param, 2, SMB_QUERY_FILE_ALL_INFO);
584 if (!cli_send_trans(cli, SMBtrans2,
585 NULL, 0, /* name, length */
586 -1, 0, /* fid, flags */
587 &setup, 1, 0, /* setup, length, max */
588 param, param_len, 2, /* param, length, max */
589 NULL, data_len, cli->max_xmit /* data, length, max */
590 )) {
591 return False;
594 if (!cli_receive_trans(cli, SMBtrans2,
595 &rparam, &param_len,
596 &rdata, &data_len)) {
597 return False;
600 if (!rdata || data_len < 68) {
601 return False;
604 if (c_time) {
605 *c_time = interpret_long_date(rdata+0) - cli->serverzone;
607 if (a_time) {
608 *a_time = interpret_long_date(rdata+8) - cli->serverzone;
610 if (m_time) {
611 *m_time = interpret_long_date(rdata+16) - cli->serverzone;
613 if (w_time) {
614 *w_time = interpret_long_date(rdata+24) - cli->serverzone;
616 if (mode) {
617 *mode = SVAL(rdata, 32);
619 if (size) {
620 *size = IVAL(rdata, 48);
622 if (ino) {
623 *ino = IVAL(rdata, 64);
626 if (rdata) free(rdata);
627 if (rparam) free(rparam);
628 return True;