smbXsrv_session: Remove a "can't happen" NULL check
[Samba.git] / source3 / utils / clirap2.c
blobb2e51876afddebd2410552aeb8c214954cc8e109
1 /*
2 Samba Unix/Linux SMB client library
3 More client RAP (SMB Remote Procedure Calls) functions
4 Copyright (C) 2001 Steve French (sfrench@us.ibm.com)
5 Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
6 Copyright (C) 2007 Jeremy Allison. jra@samba.org
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /*****************************************************/
23 /* */
24 /* Additional RAP functionality */
25 /* */
26 /* RAP is the original SMB RPC, documented */
27 /* by Microsoft and X/Open in the 1990s and */
28 /* supported by most SMB/CIFS servers although */
29 /* it is unlikely that any one implementation */
30 /* supports all RAP command codes since some */
31 /* are quite obsolete and a few are specific */
32 /* to a particular network operating system */
33 /* */
34 /* Although it has largely been replaced */
35 /* for complex remote administration and management */
36 /* (of servers) by the relatively newer */
37 /* DCE/RPC based remote API (which better handles */
38 /* large >64K data structures), there are many */
39 /* important administrative and resource location */
40 /* tasks and user tasks (e.g. password change) */
41 /* that are performed via RAP. */
42 /* */
43 /* Although a few of the RAP calls are implemented */
44 /* in the Samba client library already (clirap.c) */
45 /* the new ones are in clirap2.c for easy patching */
46 /* and integration and a corresponding header */
47 /* file, rap.h, has been created. */
48 /* */
49 /* This is based on data from the CIFS spec */
50 /* and the LAN Server and LAN Manager */
51 /* Programming Reference books and published */
52 /* RAP document and CIFS forum postings and */
53 /* lots of trial and error */
54 /* */
55 /* Function names changed from API_ (as they are */
56 /* in the CIFS specification) to RAP_ in order */
57 /* to avoid confusion with other API calls */
58 /* sent via DCE RPC */
59 /* */
60 /*****************************************************/
62 /*****************************************************/
63 /* */
64 /* cifsrap.c already includes support for: */
65 /* */
66 /* WshareEnum ( API number 0, level 1) */
67 /* NetServerEnum2 (API num 104, level 1) */
68 /* WWkstaUserLogon (132) */
69 /* SamOEMchgPasswordUser2_P (214) */
70 /* */
71 /* cifsprint.c already includes support for: */
72 /* */
73 /* WPrintJobEnum (API num 76, level 2) */
74 /* WPrintJobDel (API num 81) */
75 /* */
76 /*****************************************************/
78 #include "includes.h"
79 #include "libsmb/libsmb.h"
80 #include "../librpc/gen_ndr/rap.h"
81 #include "../librpc/gen_ndr/svcctl.h"
82 #include "clirap2.h"
83 #include "../libcli/smb/smbXcli_base.h"
85 #define WORDSIZE 2
86 #define DWORDSIZE 4
88 #define PUTBYTE(p,b) do {SCVAL(p,0,b); p++;} while(0)
90 #define GETBYTE(p,b,endp) \
91 do {\
92 if (p+1 < endp) {\
93 b = CVAL(p,0);\
95 p++;\
96 } while(0)
98 #define PUTWORD(p,w) do {SSVAL(p,0,w); p += WORDSIZE;} while(0)
100 #define GETWORD(p,w,endp) \
101 do {\
102 if (p+WORDSIZE < endp) {\
103 w = SVAL(p,0);\
105 p += WORDSIZE;\
106 } while(0)
108 #define PUTDWORD(p,d) do {SIVAL(p,0,d); p += DWORDSIZE;} while(0)
110 #define GETDWORD(p,d,endp) \
111 do {\
112 if (p+DWORDSIZE < endp) {\
113 d = IVAL(p,0);\
115 p += DWORDSIZE;\
116 } while(0)
118 #define GETRES(p,endp) ((p && p+2 < endp) ? SVAL(p,0) : -1)
121 * Skip past some strings in a buffer - old version - no checks.
122 * **/
124 static char *push_skip_string(char *buf)
126 buf += strlen(buf) + 1;
127 return(buf);
130 /* put string s at p with max len n and increment p past string */
131 #define PUTSTRING(p,s,n) \
132 do {\
133 push_ascii(p,s?s:"",n?n:256,STR_TERMINATE);\
134 p = push_skip_string(p);\
135 } while(0)
137 /* put string s and p, using fixed len l, and increment p by l */
138 #define PUTSTRINGF(p,s,l) \
139 do {\
140 push_ascii(p,s?s:"",l,STR_TERMINATE);\
141 p += l;\
142 } while (0)
144 /* put string pointer at p, supplying offset o from rdata r, store */
145 /* dword offset at p, increment p by 4 and o by length of s. This */
146 /* means on the first call, you must calc the offset yourself! */
148 #define PUTSTRINGP(p,s,r,o) \
149 do {\
150 if (s) {\
151 push_ascii(r+o,s,strlen(s)+1,STR_TERMINATE);\
152 PUTDWORD(p,o);\
153 o += strlen(s) + 1;\
154 } else {\
155 PUTDWORD(p,0);\
157 }while(0);
159 /* get asciiz string dest from src, return increment past string */
161 static size_t rap_getstring(TALLOC_CTX *ctx, char *src, char **dest, const char *endp)
163 char *p1;
164 size_t len;
166 *dest = NULL;
167 for (p1 = src, len = 0; *p1 && p1 < endp; len++)
168 p1++;
169 if (!*p1) {
170 len++;
172 pull_string_talloc(ctx,src,0,dest,src,len,STR_ASCII);
173 return len;
176 /* get fixed length l string dest from src, return increment for src */
178 static size_t rap_getstringf(char *src, char *dest, size_t l, size_t dlen, char *endp)
180 char *p1;
181 size_t len;
183 if (dlen) {
184 dest[0] = '\0';
186 for (p1 = src, len = 0; *p1 && p1 < endp; len++) {
187 p1++;
189 if (!*p1) {
190 len++;
192 if (len > l) {
193 len = l;
195 if (len) {
196 pull_ascii(dest,src,len,len,STR_ASCII);
198 return l;
201 /* get string dest from offset (obtained at p) from rdata r - converter c */
202 static size_t rap_getstringp(TALLOC_CTX *ctx, char *p, char **dest, char *r, uint16_t c, char *endp)
204 uint32_t off = 0;
205 const char *src;
206 size_t len=0;
208 *dest = NULL;
209 if (p+4 < endp) {
210 GETDWORD(p,off,endp);
211 off &= 0x0000FFFF; /* mask the obsolete segment number from the offset */
212 off -= c;
214 if (r+off > endp || r+off < r) {
215 src="";
216 len=1;
217 } else {
218 const char *p1;
219 src=r+off;
220 for (p1 = src, len = 0; *p1 && p1 < endp; len++) {
221 p1++;
223 if (!*p1) {
224 len++;
227 pull_string_talloc(ctx,src,0,dest,src,len,STR_ASCII);
228 return 4;
231 static char *make_header(char *param, uint16_t apinum, const char *reqfmt, const char *datafmt)
233 PUTWORD(param,apinum);
234 if (reqfmt)
235 PUTSTRING(param,reqfmt,0);
236 else
237 *param++ = (char) 0;
239 if (datafmt)
240 PUTSTRING(param,datafmt,0);
241 else
242 *param++ = (char) 0;
244 return param;
247 /****************************************************************************
248 call a NetGroupDelete - delete user group from remote server
249 ****************************************************************************/
251 int cli_NetGroupDelete(struct cli_state *cli, const char *group_name)
253 char *rparam = NULL;
254 char *rdata = NULL;
255 char *p;
256 unsigned int rdrcnt,rprcnt;
257 int res = -1;
258 char param[WORDSIZE /* api number */
259 +sizeof(RAP_NetGroupDel_REQ) /* parm string */
260 +1 /* no ret string */
261 +RAP_GROUPNAME_LEN /* group to del */
262 +WORDSIZE]; /* reserved word */
264 /* now send a SMBtrans command with api GroupDel */
265 p = make_header(param, RAP_WGroupDel, RAP_NetGroupDel_REQ, NULL);
266 PUTSTRING(p, group_name, RAP_GROUPNAME_LEN);
267 PUTWORD(p,0); /* reserved word MBZ on input */
269 if (cli_api(cli,
270 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
271 NULL, 0, 200, /* data, length, maxlen */
272 &rparam, &rprcnt, /* return params, length */
273 &rdata, &rdrcnt)) /* return data, length */
275 char *endp = rparam + rprcnt;
276 res = GETRES(rparam,endp);
278 if (res == 0) {
279 /* nothing to do */
280 } else if ((res == 5) || (res == 65)) {
281 DEBUG(1, ("Access Denied\n"));
282 } else if (res == 2220) {
283 DEBUG (1, ("Group does not exist\n"));
284 } else {
285 DEBUG(4,("NetGroupDelete res=%d\n", res));
287 } else {
288 res = -1;
289 DEBUG(4,("NetGroupDelete failed\n"));
292 SAFE_FREE(rparam);
293 SAFE_FREE(rdata);
295 return res;
298 /****************************************************************************
299 call a NetGroupAdd - add user group to remote server
300 ****************************************************************************/
302 int cli_NetGroupAdd(struct cli_state *cli, struct rap_group_info_1 *grinfo)
304 char *rparam = NULL;
305 char *rdata = NULL;
306 char *p;
307 unsigned int rdrcnt,rprcnt;
308 int res = -1;
309 char param[WORDSIZE /* api number */
310 +sizeof(RAP_NetGroupAdd_REQ) /* req string */
311 +sizeof(RAP_GROUP_INFO_L1) /* return string */
312 +WORDSIZE /* info level */
313 +WORDSIZE]; /* reserved word */
315 /* offset into data of free format strings. Will be updated */
316 /* by PUTSTRINGP macro and end up with total data length. */
317 int soffset = RAP_GROUPNAME_LEN + 1 + DWORDSIZE;
318 char *data;
319 size_t data_size;
321 /* Allocate data. */
322 data_size = MAX(soffset + strlen(grinfo->comment) + 1, 1024);
324 data = SMB_MALLOC_ARRAY(char, data_size);
325 if (!data) {
326 DEBUG (1, ("Malloc fail\n"));
327 return -1;
330 /* now send a SMBtrans command with api WGroupAdd */
332 p = make_header(param, RAP_WGroupAdd,
333 RAP_NetGroupAdd_REQ, RAP_GROUP_INFO_L1);
334 PUTWORD(p, 1); /* info level */
335 PUTWORD(p, 0); /* reserved word 0 */
337 p = data;
338 PUTSTRINGF(p, (const char *)grinfo->group_name, RAP_GROUPNAME_LEN);
339 PUTBYTE(p, 0); /* pad byte 0 */
340 PUTSTRINGP(p, grinfo->comment, data, soffset);
342 if (cli_api(cli,
343 param, sizeof(param), 1024, /* Param, length, maxlen */
344 data, soffset, data_size, /* data, length, maxlen */
345 &rparam, &rprcnt, /* return params, length */
346 &rdata, &rdrcnt)) /* return data, length */
348 char *endp = rparam + rprcnt;
349 res = GETRES(rparam, endp);
351 if (res == 0) {
352 /* nothing to do */
353 } else if ((res == 5) || (res == 65)) {
354 DEBUG(1, ("Access Denied\n"));
355 } else if (res == 2223) {
356 DEBUG (1, ("Group already exists\n"));
357 } else {
358 DEBUG(4,("NetGroupAdd res=%d\n", res));
360 } else {
361 res = -1;
362 DEBUG(4,("NetGroupAdd failed\n"));
365 SAFE_FREE(data);
366 SAFE_FREE(rparam);
367 SAFE_FREE(rdata);
369 return res;
372 /****************************************************************************
373 Call a NetGroupEnum - try and list user groups on a different host.
374 ****************************************************************************/
376 int cli_RNetGroupEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
378 char param[WORDSIZE /* api number */
379 +sizeof(RAP_NetGroupEnum_REQ) /* parm string */
380 +sizeof(RAP_GROUP_INFO_L1) /* return string */
381 +WORDSIZE /* info level */
382 +WORDSIZE]; /* buffer size */
383 char *p;
384 char *rparam = NULL;
385 char *rdata = NULL;
386 unsigned int rprcnt, rdrcnt;
387 int res = -1;
389 memset(param, '\0', sizeof(param));
390 p = make_header(param, RAP_WGroupEnum,
391 RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L1);
392 PUTWORD(p,1); /* Info level 1 */ /* add level 0 */
393 PUTWORD(p,0xFFE0); /* Return buffer size */
395 if (cli_api(cli,
396 param, PTR_DIFF(p,param),8,
397 NULL, 0, 0xFFE0 /* data area size */,
398 &rparam, &rprcnt,
399 &rdata, &rdrcnt)) {
400 char *endp = rparam + rdrcnt;
402 res = GETRES(rparam, endp);
403 cli->rap_error = res;
404 if(cli->rap_error == 234) {
405 DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
406 } else if (cli->rap_error != 0) {
407 DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
411 if (!rdata) {
412 DEBUG(4,("NetGroupEnum no data returned\n"));
413 goto out;
416 if (res == 0 || res == ERRmoredata) {
417 char *endp = rparam + rprcnt;
418 int i, converter = 0, count = 0;
419 TALLOC_CTX *frame = talloc_stackframe();
421 p = rparam + WORDSIZE; /* skip result */
422 GETWORD(p, converter, endp);
423 GETWORD(p, count, endp);
425 endp = rdata + rdrcnt;
426 for (i=0,p=rdata; i<count && p < endp;i++) {
427 char *comment = NULL;
428 char groupname[RAP_GROUPNAME_LEN];
430 p += rap_getstringf(p,
431 groupname,
432 RAP_GROUPNAME_LEN,
433 RAP_GROUPNAME_LEN,
434 endp);
435 p++; /* pad byte */
436 p += rap_getstringp(frame,
438 &comment,
439 rdata,
440 converter,
441 endp);
443 if (!comment || !groupname[0]) {
444 break;
447 fn(groupname, comment, cli);
449 TALLOC_FREE(frame);
450 } else {
451 DEBUG(4,("NetGroupEnum res=%d\n", res));
454 out:
456 SAFE_FREE(rparam);
457 SAFE_FREE(rdata);
459 return res;
462 int cli_RNetGroupEnum0(struct cli_state *cli,
463 void (*fn)(const char *, void *),
464 void *state)
466 char param[WORDSIZE /* api number */
467 +sizeof(RAP_NetGroupEnum_REQ) /* parm string */
468 +sizeof(RAP_GROUP_INFO_L0) /* return string */
469 +WORDSIZE /* info level */
470 +WORDSIZE]; /* buffer size */
471 char *p;
472 char *rparam = NULL;
473 char *rdata = NULL;
474 unsigned int rprcnt, rdrcnt;
475 int res = -1;
477 memset(param, '\0', sizeof(param));
478 p = make_header(param, RAP_WGroupEnum,
479 RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L0);
480 PUTWORD(p,0); /* Info level 0 */ /* Hmmm. I *very* much suspect this
481 is the resume count, at least
482 that's what smbd believes... */
483 PUTWORD(p,0xFFE0); /* Return buffer size */
485 if (cli_api(cli,
486 param, PTR_DIFF(p,param),8,
487 NULL, 0, 0xFFE0 /* data area size */,
488 &rparam, &rprcnt,
489 &rdata, &rdrcnt)) {
490 char *endp = rparam+rprcnt;
491 res = GETRES(rparam,endp);
492 cli->rap_error = res;
493 if(cli->rap_error == 234) {
494 DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
495 } else if (cli->rap_error != 0) {
496 DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
500 if (!rdata) {
501 DEBUG(4,("NetGroupEnum no data returned\n"));
502 goto out;
505 if (res == 0 || res == ERRmoredata) {
506 char *endp = rparam + rprcnt;
507 int i, count = 0;
509 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
510 GETWORD(p, count, endp);
512 endp = rdata + rdrcnt;
513 for (i=0,p=rdata; i<count && p < endp;i++) {
514 char groupname[RAP_GROUPNAME_LEN];
516 p += rap_getstringf(p,
517 groupname,
518 RAP_GROUPNAME_LEN,
519 RAP_GROUPNAME_LEN,
520 endp);
521 if (groupname[0]) {
522 fn(groupname, cli);
525 } else {
526 DEBUG(4,("NetGroupEnum res=%d\n", res));
529 out:
531 SAFE_FREE(rparam);
532 SAFE_FREE(rdata);
534 return res;
537 int cli_NetGroupDelUser(struct cli_state * cli, const char *group_name, const char *user_name)
539 char *rparam = NULL;
540 char *rdata = NULL;
541 char *p;
542 unsigned int rdrcnt,rprcnt;
543 int res = -1;
544 char param[WORDSIZE /* api number */
545 +sizeof(RAP_NetGroupDelUser_REQ) /* parm string */
546 +1 /* no ret string */
547 +RAP_GROUPNAME_LEN /* group name */
548 +RAP_USERNAME_LEN]; /* user to del */
550 /* now send a SMBtrans command with api GroupMemberAdd */
551 p = make_header(param, RAP_WGroupDelUser, RAP_NetGroupDelUser_REQ, NULL);
552 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
553 PUTSTRING(p,user_name,RAP_USERNAME_LEN);
555 if (cli_api(cli,
556 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
557 NULL, 0, 200, /* data, length, maxlen */
558 &rparam, &rprcnt, /* return params, length */
559 &rdata, &rdrcnt)) /* return data, length */
561 char *endp = rparam + rprcnt;
562 res = GETRES(rparam,endp);
564 switch(res) {
565 case 0:
566 break;
567 case 5:
568 case 65:
569 DEBUG(1, ("Access Denied\n"));
570 break;
571 case 50:
572 DEBUG(1, ("Not supported by server\n"));
573 break;
574 case 2220:
575 DEBUG(1, ("Group does not exist\n"));
576 break;
577 case 2221:
578 DEBUG(1, ("User does not exist\n"));
579 break;
580 case 2237:
581 DEBUG(1, ("User is not in group\n"));
582 break;
583 default:
584 DEBUG(4,("NetGroupDelUser res=%d\n", res));
586 } else {
587 res = -1;
588 DEBUG(4,("NetGroupDelUser failed\n"));
591 SAFE_FREE(rparam);
592 SAFE_FREE(rdata);
594 return res;
597 int cli_NetGroupAddUser(struct cli_state * cli, const char *group_name, const char *user_name)
599 char *rparam = NULL;
600 char *rdata = NULL;
601 char *p;
602 unsigned int rdrcnt,rprcnt;
603 int res = -1;
604 char param[WORDSIZE /* api number */
605 +sizeof(RAP_NetGroupAddUser_REQ) /* parm string */
606 +1 /* no ret string */
607 +RAP_GROUPNAME_LEN /* group name */
608 +RAP_USERNAME_LEN]; /* user to add */
610 /* now send a SMBtrans command with api GroupMemberAdd */
611 p = make_header(param, RAP_WGroupAddUser, RAP_NetGroupAddUser_REQ, NULL);
612 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
613 PUTSTRING(p,user_name,RAP_USERNAME_LEN);
615 if (cli_api(cli,
616 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
617 NULL, 0, 200, /* data, length, maxlen */
618 &rparam, &rprcnt, /* return params, length */
619 &rdata, &rdrcnt)) /* return data, length */
621 char *endp = rparam + rprcnt;
622 res = GETRES(rparam,endp);
624 switch(res) {
625 case 0:
626 break;
627 case 5:
628 case 65:
629 DEBUG(1, ("Access Denied\n"));
630 break;
631 case 50:
632 DEBUG(1, ("Not supported by server\n"));
633 break;
634 case 2220:
635 DEBUG(1, ("Group does not exist\n"));
636 break;
637 case 2221:
638 DEBUG(1, ("User does not exist\n"));
639 break;
640 default:
641 DEBUG(4,("NetGroupAddUser res=%d\n", res));
643 } else {
644 res = -1;
645 DEBUG(4,("NetGroupAddUser failed\n"));
648 SAFE_FREE(rparam);
649 SAFE_FREE(rdata);
651 return res;
655 int cli_NetGroupGetUsers(struct cli_state * cli, const char *group_name, void (*fn)(const char *, void *), void *state )
657 char *rparam = NULL;
658 char *rdata = NULL;
659 char *p;
660 unsigned int rdrcnt,rprcnt;
661 int res = -1;
662 char param[WORDSIZE /* api number */
663 +sizeof(RAP_NetGroupGetUsers_REQ)/* parm string */
664 +sizeof(RAP_GROUP_USERS_INFO_0) /* return string */
665 +RAP_GROUPNAME_LEN /* group name */
666 +WORDSIZE /* info level */
667 +WORDSIZE]; /* buffer size */
669 /* now send a SMBtrans command with api GroupGetUsers */
670 p = make_header(param, RAP_WGroupGetUsers,
671 RAP_NetGroupGetUsers_REQ, RAP_GROUP_USERS_INFO_0);
672 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN-1);
673 PUTWORD(p,0); /* info level 0 */
674 PUTWORD(p,0xFFE0); /* return buffer size */
676 if (cli_api(cli,
677 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
678 NULL, 0, CLI_BUFFER_SIZE,
679 &rparam, &rprcnt,
680 &rdata, &rdrcnt)) {
681 char *endp = rparam + rprcnt;
682 res = GETRES(rparam,endp);
683 cli->rap_error = res;
684 if (res != 0) {
685 DEBUG(1,("NetGroupGetUsers gave error %d\n", res));
689 if (!rdata) {
690 DEBUG(4,("NetGroupGetUsers no data returned\n"));
691 goto out;
694 if (res == 0 || res == ERRmoredata) {
695 char *endp = rparam + rprcnt;
696 int i, count = 0;
697 char username[RAP_USERNAME_LEN];
699 p = rparam + WORDSIZE + WORDSIZE;
700 GETWORD(p, count, endp);
702 endp = rdata + rdrcnt;
703 for (i=0,p=rdata; i<count && p < endp; i++) {
704 p += rap_getstringf(p,
705 username,
706 RAP_USERNAME_LEN,
707 RAP_USERNAME_LEN,
708 endp);
709 if (username[0]) {
710 fn(username, state);
713 } else {
714 DEBUG(4,("NetGroupGetUsers res=%d\n", res));
717 out:
719 SAFE_FREE(rdata);
720 SAFE_FREE(rparam);
721 return res;
724 int cli_NetUserGetGroups(struct cli_state * cli, const char *user_name, void (*fn)(const char *, void *), void *state )
726 char *rparam = NULL;
727 char *rdata = NULL;
728 char *p;
729 unsigned int rdrcnt,rprcnt;
730 int res = -1;
731 char param[WORDSIZE /* api number */
732 +sizeof(RAP_NetUserGetGroups_REQ)/* parm string */
733 +sizeof(RAP_GROUP_USERS_INFO_0) /* return string */
734 +RAP_USERNAME_LEN /* user name */
735 +WORDSIZE /* info level */
736 +WORDSIZE]; /* buffer size */
738 /* now send a SMBtrans command with api GroupGetUsers */
739 p = make_header(param, RAP_WUserGetGroups,
740 RAP_NetUserGetGroups_REQ, RAP_GROUP_USERS_INFO_0);
741 PUTSTRING(p,user_name,RAP_USERNAME_LEN-1);
742 PUTWORD(p,0); /* info level 0 */
743 PUTWORD(p,0xFFE0); /* return buffer size */
745 if (cli_api(cli,
746 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
747 NULL, 0, CLI_BUFFER_SIZE,
748 &rparam, &rprcnt,
749 &rdata, &rdrcnt)) {
750 char *endp = rparam + rprcnt;
751 res = GETRES(rparam,endp);
752 cli->rap_error = res;
753 if (res != 0) {
754 DEBUG(1,("NetUserGetGroups gave error %d\n", res));
758 if (!rdata) {
759 DEBUG(4,("NetUserGetGroups no data returned\n"));
760 goto out;
763 if (res == 0 || res == ERRmoredata) {
764 char *endp = rparam + rprcnt;
765 int i, count = 0;
766 char groupname[RAP_GROUPNAME_LEN];
768 p = rparam + WORDSIZE + WORDSIZE;
769 GETWORD(p, count, endp);
771 endp = rdata + rdrcnt;
772 for (i=0,p=rdata; i<count && p < endp; i++) {
773 p += rap_getstringf(p,
774 groupname,
775 RAP_GROUPNAME_LEN,
776 RAP_GROUPNAME_LEN,
777 endp);
778 if (groupname[0]) {
779 fn(groupname, state);
782 } else {
783 DEBUG(4,("NetUserGetGroups res=%d\n", res));
786 out:
788 SAFE_FREE(rdata);
789 SAFE_FREE(rparam);
790 return res;
793 /****************************************************************************
794 Call a NetUserDelete - delete user from remote server.
795 ****************************************************************************/
797 int cli_NetUserDelete(struct cli_state *cli, const char * user_name )
799 char *rparam = NULL;
800 char *rdata = NULL;
801 char *p;
802 unsigned int rdrcnt,rprcnt;
803 int res = -1;
804 char param[WORDSIZE /* api number */
805 +sizeof(RAP_NetGroupDel_REQ) /* parm string */
806 +1 /* no ret string */
807 +RAP_USERNAME_LEN /* user to del */
808 +WORDSIZE]; /* reserved word */
810 /* now send a SMBtrans command with api UserDel */
811 p = make_header(param, RAP_WUserDel, RAP_NetGroupDel_REQ, NULL);
812 PUTSTRING(p, user_name, RAP_USERNAME_LEN);
813 PUTWORD(p,0); /* reserved word MBZ on input */
815 if (cli_api(cli,
816 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
817 NULL, 0, 200, /* data, length, maxlen */
818 &rparam, &rprcnt, /* return params, length */
819 &rdata, &rdrcnt)) /* return data, length */
821 char *endp = rparam + rprcnt;
822 res = GETRES(rparam,endp);
824 if (res == 0) {
825 /* nothing to do */
826 } else if ((res == 5) || (res == 65)) {
827 DEBUG(1, ("Access Denied\n"));
828 } else if (res == 2221) {
829 DEBUG (1, ("User does not exist\n"));
830 } else {
831 DEBUG(4,("NetUserDelete res=%d\n", res));
833 } else {
834 res = -1;
835 DEBUG(4,("NetUserDelete failed\n"));
838 SAFE_FREE(rparam);
839 SAFE_FREE(rdata);
841 return res;
844 /****************************************************************************
845 Call a NetUserAdd - add user to remote server.
846 ****************************************************************************/
848 int cli_NetUserAdd(struct cli_state *cli, struct rap_user_info_1 * userinfo )
850 char *rparam = NULL;
851 char *rdata = NULL;
852 char *p;
853 unsigned int rdrcnt,rprcnt;
854 int res = -1;
855 char param[WORDSIZE /* api number */
856 +sizeof(RAP_NetUserAdd2_REQ) /* req string */
857 +sizeof(RAP_USER_INFO_L1) /* data string */
858 +WORDSIZE /* info level */
859 +WORDSIZE /* buffer length */
860 +WORDSIZE]; /* reserved */
862 char data[1024];
863 /* offset into data of free format strings. Will be updated */
864 /* by PUTSTRINGP macro and end up with total data length. */
865 int soffset=RAP_USERNAME_LEN+1 /* user name + pad */
866 + RAP_UPASSWD_LEN /* password */
867 + DWORDSIZE /* password age */
868 + WORDSIZE /* privilege */
869 + DWORDSIZE /* home dir ptr */
870 + DWORDSIZE /* comment ptr */
871 + WORDSIZE /* flags */
872 + DWORDSIZE; /* login script ptr*/
874 /* now send a SMBtrans command with api NetUserAdd */
875 p = make_header(param, RAP_WUserAdd2,
876 RAP_NetUserAdd2_REQ, RAP_USER_INFO_L1);
878 PUTWORD(p, 1); /* info level */
879 PUTWORD(p, 0); /* pwencrypt */
880 PUTWORD(p, MIN(strlen((const char *)userinfo->passwrd),
881 RAP_UPASSWD_LEN));
883 p = data;
884 memset(data, '\0', soffset);
886 PUTSTRINGF(p, (const char *)userinfo->user_name, RAP_USERNAME_LEN);
887 PUTBYTE(p, 0); /* pad byte 0 */
888 PUTSTRINGF(p, (const char *)userinfo->passwrd, RAP_UPASSWD_LEN);
889 PUTDWORD(p, 0); /* pw age - n.a. on user add */
890 PUTWORD(p, userinfo->priv);
891 PUTSTRINGP(p, userinfo->home_dir, data, soffset);
892 PUTSTRINGP(p, userinfo->comment, data, soffset);
893 PUTWORD(p, userinfo->userflags);
894 PUTSTRINGP(p, userinfo->logon_script, data, soffset);
896 if (cli_api(cli,
897 param, sizeof(param), 1024, /* Param, length, maxlen */
898 data, soffset, sizeof(data), /* data, length, maxlen */
899 &rparam, &rprcnt, /* return params, length */
900 &rdata, &rdrcnt)) /* return data, length */
902 char *endp = rparam + rprcnt;
903 res = GETRES(rparam, endp);
905 if (res == 0) {
906 /* nothing to do */
907 } else if ((res == 5) || (res == 65)) {
908 DEBUG(1, ("Access Denied\n"));
909 } else if (res == 2224) {
910 DEBUG (1, ("User already exists\n"));
911 } else {
912 DEBUG(4,("NetUserAdd res=%d\n", res));
914 } else {
915 res = -1;
916 DEBUG(4,("NetUserAdd failed\n"));
919 SAFE_FREE(rparam);
920 SAFE_FREE(rdata);
922 return res;
925 /****************************************************************************
926 call a NetUserEnum - try and list users on a different host
927 ****************************************************************************/
929 int cli_RNetUserEnum(struct cli_state *cli, void (*fn)(const char *, const char *, const char *, const char *, void *), void *state)
931 char param[WORDSIZE /* api number */
932 +sizeof(RAP_NetUserEnum_REQ) /* parm string */
933 +sizeof(RAP_USER_INFO_L1) /* return string */
934 +WORDSIZE /* info level */
935 +WORDSIZE]; /* buffer size */
936 char *p;
937 char *rparam = NULL;
938 char *rdata = NULL;
939 unsigned int rprcnt, rdrcnt;
940 int res = -1;
942 memset(param, '\0', sizeof(param));
943 p = make_header(param, RAP_WUserEnum,
944 RAP_NetUserEnum_REQ, RAP_USER_INFO_L1);
945 PUTWORD(p,1); /* Info level 1 */
946 PUTWORD(p,0xFF00); /* Return buffer size */
948 /* BB Fix handling of large numbers of users to be returned */
949 if (cli_api(cli,
950 param, PTR_DIFF(p,param),8,
951 NULL, 0, CLI_BUFFER_SIZE,
952 &rparam, &rprcnt,
953 &rdata, &rdrcnt)) {
954 char *endp = rparam + rprcnt;
955 res = GETRES(rparam,endp);
956 cli->rap_error = res;
957 if (cli->rap_error != 0) {
958 DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
962 if (!rdata) {
963 DEBUG(4,("NetUserEnum no data returned\n"));
964 goto out;
967 if (res == 0 || res == ERRmoredata) {
968 int i, converter = 0, count = 0;
969 char username[RAP_USERNAME_LEN];
970 char userpw[RAP_UPASSWD_LEN];
971 char *endp = rparam + rprcnt;
972 char *comment, *homedir, *logonscript;
973 TALLOC_CTX *frame = talloc_stackframe();
975 p = rparam + WORDSIZE; /* skip result */
976 GETWORD(p, converter, endp);
977 GETWORD(p, count, endp);
979 endp = rdata + rdrcnt;
980 for (i=0,p=rdata;i<count && p < endp;i++) {
981 p += rap_getstringf(p,
982 username,
983 RAP_USERNAME_LEN,
984 RAP_USERNAME_LEN,
985 endp);
986 p++; /* pad byte */
987 p += rap_getstringf(p,
988 userpw,
989 RAP_UPASSWD_LEN,
990 RAP_UPASSWD_LEN,
991 endp);
992 p += DWORDSIZE; /* skip password age */
993 p += WORDSIZE; /* skip priv: 0=guest, 1=user, 2=admin */
994 p += rap_getstringp(frame,
996 &homedir,
997 rdata,
998 converter,
999 endp);
1000 p += rap_getstringp(frame,
1002 &comment,
1003 rdata,
1004 converter,
1005 endp);
1006 p += WORDSIZE; /* skip flags */
1007 p += rap_getstringp(frame,
1009 &logonscript,
1010 rdata,
1011 converter,
1012 endp);
1013 if (username[0] && comment &&
1014 homedir && logonscript) {
1015 fn(username,
1016 comment,
1017 homedir,
1018 logonscript,
1019 cli);
1022 TALLOC_FREE(frame);
1023 } else {
1024 DEBUG(4,("NetUserEnum res=%d\n", res));
1027 out:
1029 SAFE_FREE(rparam);
1030 SAFE_FREE(rdata);
1032 return res;
1035 int cli_RNetUserEnum0(struct cli_state *cli,
1036 void (*fn)(const char *, void *),
1037 void *state)
1039 char param[WORDSIZE /* api number */
1040 +sizeof(RAP_NetUserEnum_REQ) /* parm string */
1041 +sizeof(RAP_USER_INFO_L0) /* return string */
1042 +WORDSIZE /* info level */
1043 +WORDSIZE]; /* buffer size */
1044 char *p;
1045 char *rparam = NULL;
1046 char *rdata = NULL;
1047 unsigned int rprcnt, rdrcnt;
1048 int res = -1;
1050 memset(param, '\0', sizeof(param));
1051 p = make_header(param, RAP_WUserEnum,
1052 RAP_NetUserEnum_REQ, RAP_USER_INFO_L0);
1053 PUTWORD(p,0); /* Info level 1 */
1054 PUTWORD(p,0xFF00); /* Return buffer size */
1056 /* BB Fix handling of large numbers of users to be returned */
1057 if (cli_api(cli,
1058 param, PTR_DIFF(p,param),8,
1059 NULL, 0, CLI_BUFFER_SIZE,
1060 &rparam, &rprcnt,
1061 &rdata, &rdrcnt)) {
1062 char *endp = rparam + rprcnt;
1063 res = GETRES(rparam,endp);
1064 cli->rap_error = res;
1065 if (cli->rap_error != 0) {
1066 DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
1070 if (!rdata) {
1071 DEBUG(4,("NetUserEnum no data returned\n"));
1072 goto out;
1075 if (res == 0 || res == ERRmoredata) {
1076 int i, count = 0;
1077 char *endp = rparam + rprcnt;
1078 char username[RAP_USERNAME_LEN];
1080 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1081 GETWORD(p, count, endp);
1083 endp = rdata + rdrcnt;
1084 for (i=0,p=rdata;i<count && p < endp;i++) {
1085 p += rap_getstringf(p,
1086 username,
1087 RAP_USERNAME_LEN,
1088 RAP_USERNAME_LEN,
1089 endp);
1090 if (username[0]) {
1091 fn(username, cli);
1094 } else {
1095 DEBUG(4,("NetUserEnum res=%d\n", res));
1098 out:
1100 SAFE_FREE(rparam);
1101 SAFE_FREE(rdata);
1103 return res;
1106 /****************************************************************************
1107 Call a NetFileClose2 - close open file on another session to server.
1108 ****************************************************************************/
1110 int cli_NetFileClose(struct cli_state *cli, uint32_t file_id )
1112 char *rparam = NULL;
1113 char *rdata = NULL;
1114 char *p;
1115 unsigned int rdrcnt,rprcnt;
1116 char param[WORDSIZE /* api number */
1117 +sizeof(RAP_WFileClose2_REQ) /* req string */
1118 +1 /* no ret string */
1119 +DWORDSIZE]; /* file ID */
1120 int res = -1;
1122 /* now send a SMBtrans command with api RNetShareEnum */
1123 p = make_header(param, RAP_WFileClose2, RAP_WFileClose2_REQ, NULL);
1124 PUTDWORD(p, file_id);
1126 if (cli_api(cli,
1127 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1128 NULL, 0, 200, /* data, length, maxlen */
1129 &rparam, &rprcnt, /* return params, length */
1130 &rdata, &rdrcnt)) /* return data, length */
1132 char *endp = rparam + rprcnt;
1133 res = GETRES(rparam, endp);
1135 if (res == 0) {
1136 /* nothing to do */
1137 } else if (res == 2314){
1138 DEBUG(1, ("NetFileClose2 - attempt to close non-existent file open instance\n"));
1139 } else {
1140 DEBUG(4,("NetFileClose2 res=%d\n", res));
1142 } else {
1143 res = -1;
1144 DEBUG(4,("NetFileClose2 failed\n"));
1147 SAFE_FREE(rparam);
1148 SAFE_FREE(rdata);
1150 return res;
1153 /****************************************************************************
1154 Call a NetFileGetInfo - get information about server file opened from other
1155 workstation.
1156 ****************************************************************************/
1158 int cli_NetFileGetInfo(struct cli_state *cli, uint32_t file_id, void (*fn)(const char *, const char *, uint16_t, uint16_t, uint32_t))
1160 char *rparam = NULL;
1161 char *rdata = NULL;
1162 char *p;
1163 unsigned int rdrcnt,rprcnt;
1164 int res = -1;
1165 char param[WORDSIZE /* api number */
1166 +sizeof(RAP_WFileGetInfo2_REQ) /* req string */
1167 +sizeof(RAP_FILE_INFO_L3) /* return string */
1168 +DWORDSIZE /* file ID */
1169 +WORDSIZE /* info level */
1170 +WORDSIZE]; /* buffer size */
1172 /* now send a SMBtrans command with api RNetShareEnum */
1173 p = make_header(param, RAP_WFileGetInfo2,
1174 RAP_WFileGetInfo2_REQ, RAP_FILE_INFO_L3);
1175 PUTDWORD(p, file_id);
1176 PUTWORD(p, 3); /* info level */
1177 PUTWORD(p, 0x1000); /* buffer size */
1178 if (cli_api(cli,
1179 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1180 NULL, 0, 0x1000, /* data, length, maxlen */
1181 &rparam, &rprcnt, /* return params, length */
1182 &rdata, &rdrcnt)) /* return data, length */
1184 char *endp = rparam + rprcnt;
1185 res = GETRES(rparam,endp);
1186 if (res == 0 || res == ERRmoredata) {
1187 TALLOC_CTX *frame = talloc_stackframe();
1188 int converter = 0,id = 0, perms = 0, locks = 0;
1189 char *fpath, *fuser;
1191 p = rparam + WORDSIZE; /* skip result */
1192 GETWORD(p, converter, endp);
1194 p = rdata;
1195 endp = rdata + rdrcnt;
1197 GETDWORD(p, id, endp);
1198 GETWORD(p, perms, endp);
1199 GETWORD(p, locks, endp);
1201 p += rap_getstringp(frame,
1203 &fpath,
1204 rdata,
1205 converter,
1206 endp);
1207 rap_getstringp(frame,
1209 &fuser,
1210 rdata,
1211 converter,
1212 endp);
1214 if (fpath && fuser) {
1215 fn(fpath, fuser, perms, locks, id);
1218 TALLOC_FREE(frame);
1219 } else {
1220 DEBUG(4,("NetFileGetInfo2 res=%d\n", res));
1222 } else {
1223 res = -1;
1224 DEBUG(4,("NetFileGetInfo2 failed\n"));
1227 SAFE_FREE(rparam);
1228 SAFE_FREE(rdata);
1230 return res;
1233 /****************************************************************************
1234 * Call a NetFileEnum2 - list open files on an SMB server
1236 * PURPOSE: Remotes a NetFileEnum API call to the current server or target
1237 * server listing the files open via the network (and their
1238 * corresponding open instance ids)
1240 * Dependencies: none
1242 * Parameters:
1243 * cli - pointer to cli_state structure
1244 * user - if present, return only files opened by this remote user
1245 * base_path - if present, return only files opened below this
1246 * base path
1247 * fn - display function to invoke for each entry in the result
1250 * Returns:
1251 * True - success
1252 * False - failure
1254 ****************************************************************************/
1256 int cli_NetFileEnum(struct cli_state *cli, const char * user,
1257 const char * base_path,
1258 void (*fn)(const char *, const char *, uint16_t, uint16_t,
1259 uint32_t))
1261 char *rparam = NULL;
1262 char *rdata = NULL;
1263 char *p;
1264 unsigned int rdrcnt,rprcnt;
1265 char param[WORDSIZE /* api number */
1266 +sizeof(RAP_WFileEnum2_REQ) /* req string */
1267 +sizeof(RAP_FILE_INFO_L3) /* return string */
1268 +1024 /* base path (opt) */
1269 +RAP_USERNAME_LEN /* user name (opt) */
1270 +WORDSIZE /* info level */
1271 +WORDSIZE /* buffer size */
1272 +DWORDSIZE /* resume key ? */
1273 +DWORDSIZE]; /* resume key ? */
1274 int count = -1;
1275 int res = -1;
1277 /* now send a SMBtrans command with api RNetShareEnum */
1278 p = make_header(param, RAP_WFileEnum2,
1279 RAP_WFileEnum2_REQ, RAP_FILE_INFO_L3);
1281 PUTSTRING(p, base_path, 1024);
1282 PUTSTRING(p, user, RAP_USERNAME_LEN);
1283 PUTWORD(p, 3); /* info level */
1284 PUTWORD(p, 0xFF00); /* buffer size */
1285 PUTDWORD(p, 0); /* zero out the resume key */
1286 PUTDWORD(p, 0); /* or is this one the resume key? */
1288 if (cli_api(cli,
1289 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1290 NULL, 0, 0xFF00, /* data, length, maxlen */
1291 &rparam, &rprcnt, /* return params, length */
1292 &rdata, &rdrcnt)) /* return data, length */
1294 char *endp = rparam + rprcnt;
1295 res = GETRES(rparam, endp);
1297 if (res == 0 || res == ERRmoredata) {
1298 TALLOC_CTX *frame = talloc_stackframe();
1299 int converter = 0, i;
1301 p = rparam + WORDSIZE; /* skip result */
1302 GETWORD(p, converter, endp);
1303 GETWORD(p, count, endp);
1305 p = rdata;
1306 endp = rdata + rdrcnt;
1307 for (i=0; i<count && p < endp; i++) {
1308 int id = 0, perms = 0, locks = 0;
1309 char *fpath, *fuser;
1311 GETDWORD(p, id, endp);
1312 GETWORD(p, perms, endp);
1313 GETWORD(p, locks, endp);
1314 p += rap_getstringp(frame,
1316 &fpath,
1317 rdata,
1318 converter,
1319 endp);
1320 p += rap_getstringp(frame,
1322 &fuser,
1323 rdata,
1324 converter,
1325 endp);
1327 if (fpath && fuser) {
1328 fn(fpath, fuser, perms, locks, id);
1330 } /* BB fix ERRmoredata case to send resume request */
1331 TALLOC_FREE(frame);
1332 } else {
1333 DEBUG(4,("NetFileEnum2 res=%d\n", res));
1335 } else {
1336 DEBUG(4,("NetFileEnum2 failed\n"));
1339 SAFE_FREE(rparam);
1340 SAFE_FREE(rdata);
1342 return count;
1345 /****************************************************************************
1346 Call a NetShareAdd - share/export directory on remote server.
1347 ****************************************************************************/
1349 int cli_NetShareAdd(struct cli_state *cli, struct rap_share_info_2 * sinfo )
1351 char *rparam = NULL;
1352 char *rdata = NULL;
1353 char *p;
1354 unsigned int rdrcnt,rprcnt;
1355 int res = -1;
1356 char param[WORDSIZE /* api number */
1357 +sizeof(RAP_WShareAdd_REQ) /* req string */
1358 +sizeof(RAP_SHARE_INFO_L2) /* return string */
1359 +WORDSIZE /* info level */
1360 +WORDSIZE]; /* reserved word */
1361 char data[1024];
1362 /* offset to free format string section following fixed length data. */
1363 /* will be updated by PUTSTRINGP macro and will end up with total len */
1364 int soffset = RAP_SHARENAME_LEN + 1 /* share name + pad */
1365 + WORDSIZE /* share type */
1366 + DWORDSIZE /* comment pointer */
1367 + WORDSIZE /* permissions */
1368 + WORDSIZE /* max users */
1369 + WORDSIZE /* active users */
1370 + DWORDSIZE /* share path */
1371 + RAP_SPASSWD_LEN + 1; /* share password + pad */
1373 memset(param,'\0',sizeof(param));
1374 /* now send a SMBtrans command with api RNetShareAdd */
1375 p = make_header(param, RAP_WshareAdd,
1376 RAP_WShareAdd_REQ, RAP_SHARE_INFO_L2);
1377 PUTWORD(p, 2); /* info level */
1378 PUTWORD(p, 0); /* reserved word 0 */
1380 p = data;
1381 PUTSTRINGF(p, (const char *)sinfo->share_name, RAP_SHARENAME_LEN);
1382 PUTBYTE(p, 0); /* pad byte 0 */
1384 PUTWORD(p, sinfo->share_type);
1385 PUTSTRINGP(p, sinfo->comment, data, soffset);
1386 PUTWORD(p, sinfo->perms);
1387 PUTWORD(p, sinfo->maximum_users);
1388 PUTWORD(p, sinfo->active_users);
1389 PUTSTRINGP(p, sinfo->path, data, soffset);
1390 PUTSTRINGF(p, (const char *)sinfo->password, RAP_SPASSWD_LEN);
1391 SCVAL(p,-1,0x0A); /* required 0x0A at end of password */
1393 if (cli_api(cli,
1394 param, sizeof(param), 1024, /* Param, length, maxlen */
1395 data, soffset, sizeof(data), /* data, length, maxlen */
1396 &rparam, &rprcnt, /* return params, length */
1397 &rdata, &rdrcnt)) /* return data, length */
1399 char *endp = rparam + rprcnt;
1400 res = GETRES(rparam, endp);
1402 if (res == 0) {
1403 /* nothing to do */
1404 } else {
1405 DEBUG(4,("NetShareAdd res=%d\n", res));
1407 } else {
1408 DEBUG(4,("NetShareAdd failed\n"));
1411 SAFE_FREE(rparam);
1412 SAFE_FREE(rdata);
1414 return res;
1417 /****************************************************************************
1418 Call a NetShareDelete - unshare exported directory on remote server.
1419 ****************************************************************************/
1421 int cli_NetShareDelete(struct cli_state *cli, const char * share_name )
1423 char *rparam = NULL;
1424 char *rdata = NULL;
1425 char *p;
1426 unsigned int rdrcnt,rprcnt;
1427 int res = -1;
1428 char param[WORDSIZE /* api number */
1429 +sizeof(RAP_WShareDel_REQ) /* req string */
1430 +1 /* no ret string */
1431 +RAP_SHARENAME_LEN /* share to del */
1432 +WORDSIZE]; /* reserved word */
1434 /* now send a SMBtrans command with api RNetShareDelete */
1435 p = make_header(param, RAP_WshareDel, RAP_WShareDel_REQ, NULL);
1436 PUTSTRING(p,share_name,RAP_SHARENAME_LEN);
1437 PUTWORD(p,0); /* reserved word MBZ on input */
1439 if (cli_api(cli,
1440 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1441 NULL, 0, 200, /* data, length, maxlen */
1442 &rparam, &rprcnt, /* return params, length */
1443 &rdata, &rdrcnt)) /* return data, length */
1445 char *endp = rparam + rprcnt;
1446 res = GETRES(rparam, endp);
1448 if (res == 0) {
1449 /* nothing to do */
1450 } else {
1451 DEBUG(4,("NetShareDelete res=%d\n", res));
1453 } else {
1454 DEBUG(4,("NetShareDelete failed\n"));
1457 SAFE_FREE(rparam);
1458 SAFE_FREE(rdata);
1460 return res;
1463 /*************************************************************************
1465 * Function Name: cli_get_pdc_name
1467 * PURPOSE: Remotes a NetServerEnum API call to the current server
1468 * requesting the name of a server matching the server
1469 * type of SV_TYPE_DOMAIN_CTRL (PDC).
1471 * Dependencies: none
1473 * Parameters:
1474 * cli - pointer to cli_state structure
1475 * workgroup - pointer to string containing name of domain
1476 * pdc_name - pointer to string that will contain PDC name
1477 * on successful return
1479 * Returns:
1480 * True - success
1481 * False - failure
1483 ************************************************************************/
1485 bool cli_get_pdc_name(struct cli_state *cli, const char *workgroup, char **pdc_name)
1487 char *rparam = NULL;
1488 char *rdata = NULL;
1489 unsigned int rdrcnt,rprcnt;
1490 char *p;
1491 char param[WORDSIZE /* api number */
1492 +sizeof(RAP_NetServerEnum2_REQ) /* req string */
1493 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1494 +WORDSIZE /* info level */
1495 +WORDSIZE /* buffer size */
1496 +DWORDSIZE /* server type */
1497 +RAP_MACHNAME_LEN]; /* workgroup */
1498 int count = -1;
1499 int res = -1;
1501 *pdc_name = NULL;
1503 /* send a SMBtrans command with api NetServerEnum */
1504 p = make_header(param, RAP_NetServerEnum2,
1505 RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L1);
1506 PUTWORD(p, 1); /* info level */
1507 PUTWORD(p, CLI_BUFFER_SIZE);
1508 PUTDWORD(p, SV_TYPE_DOMAIN_CTRL);
1509 PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1511 if (cli_api(cli,
1512 param, PTR_DIFF(p,param), 8, /* params, length, max */
1513 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1514 &rparam, &rprcnt, /* return params, return size */
1515 &rdata, &rdrcnt /* return data, return size */
1516 )) {
1518 char *endp = rparam + rprcnt;
1519 res = GETRES(rparam, endp);
1520 cli->rap_error = res;
1523 * We only really care to copy a name if the
1524 * API succeeded and we got back a name.
1526 if (cli->rap_error == 0) {
1527 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1528 GETWORD(p, count, endp);
1529 p = rdata;
1530 endp = rdata + rdrcnt;
1532 if (count > 0) {
1533 TALLOC_CTX *frame = talloc_stackframe();
1534 char *dcname;
1535 rap_getstring(frame,
1537 &dcname,
1538 endp);
1539 if (dcname) {
1540 *pdc_name = SMB_STRDUP(dcname);
1542 TALLOC_FREE(frame);
1544 } else {
1545 DEBUG(4, ("cli_get_pdc_name: machine %s failed the "
1546 "NetServerEnum call. Error was : %s.\n",
1547 smbXcli_conn_remote_name(cli->conn),
1548 win_errstr(W_ERROR(cli->rap_error))));
1552 SAFE_FREE(rparam);
1553 SAFE_FREE(rdata);
1555 return(count > 0);
1558 bool cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
1559 char **servername)
1561 char *rparam = NULL;
1562 char *rdata = NULL;
1563 unsigned int rdrcnt,rprcnt;
1564 char *p;
1565 char param[WORDSIZE /* api number */
1566 +sizeof(RAP_WserverGetInfo_REQ) /* req string */
1567 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1568 +WORDSIZE /* info level */
1569 +WORDSIZE]; /* buffer size */
1570 bool res = false;
1571 char *endp;
1572 fstring tmp;
1574 /* send a SMBtrans command with api NetServerGetInfo */
1575 p = make_header(param, RAP_WserverGetInfo,
1576 RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1577 PUTWORD(p, 1); /* info level */
1578 PUTWORD(p, CLI_BUFFER_SIZE);
1580 if (!cli_api(cli,
1581 param, PTR_DIFF(p,param), 8, /* params, length, max */
1582 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1583 &rparam, &rprcnt, /* return params, return size */
1584 &rdata, &rdrcnt /* return data, return size */
1585 )) {
1586 goto failed;
1589 endp = rparam + rprcnt;
1590 if (GETRES(rparam, endp) != 0) {
1591 goto failed;
1594 if (rdrcnt < 16) {
1595 DEBUG(10, ("invalid data count %d, expected >= 16\n", rdrcnt));
1596 goto failed;
1599 if (pull_ascii(tmp, rdata, sizeof(tmp)-1, 16, STR_TERMINATE) == -1) {
1600 DEBUG(10, ("pull_ascii failed\n"));
1601 goto failed;
1604 if (!(*servername = talloc_strdup(mem_ctx, tmp))) {
1605 DEBUG(1, ("talloc_strdup failed\n"));
1606 goto failed;
1609 res = true;
1611 failed:
1612 SAFE_FREE(rparam);
1613 SAFE_FREE(rdata);
1614 return res;
1617 /*************************************************************************
1619 * Function Name: cli_ns_check_server_type
1621 * PURPOSE: Remotes a NetServerEnum2 API call to the current server
1622 * requesting server_info_0 level information of machines
1623 * matching the given server type. If the returned server
1624 * list contains the machine name contained in smbXcli_conn_remote_name(->conn)
1625 * then we conclude the server type checks out. This routine
1626 * is useful to retrieve list of server's of a certain
1627 * type when all you have is a null session connection and
1628 * can't remote API calls such as NetWkstaGetInfo or
1629 * NetServerGetInfo.
1631 * Dependencies: none
1633 * Parameters:
1634 * cli - pointer to cli_state structure
1635 * workgroup - pointer to string containing domain
1636 * stype - server type
1638 * Returns:
1639 * True - success
1640 * False - failure
1642 ************************************************************************/
1644 bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32_t stype)
1646 char *rparam = NULL;
1647 char *rdata = NULL;
1648 unsigned int rdrcnt,rprcnt;
1649 char *p;
1650 char param[WORDSIZE /* api number */
1651 +sizeof(RAP_NetServerEnum2_REQ) /* req string */
1652 +sizeof(RAP_SERVER_INFO_L0) /* return string */
1653 +WORDSIZE /* info level */
1654 +WORDSIZE /* buffer size */
1655 +DWORDSIZE /* server type */
1656 +RAP_MACHNAME_LEN]; /* workgroup */
1657 bool found_server = false;
1658 int res = -1;
1659 const char *remote_name = smbXcli_conn_remote_name(cli->conn);
1661 /* send a SMBtrans command with api NetServerEnum */
1662 p = make_header(param, RAP_NetServerEnum2,
1663 RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L0);
1664 PUTWORD(p, 0); /* info level 0 */
1665 PUTWORD(p, CLI_BUFFER_SIZE);
1666 PUTDWORD(p, stype);
1667 PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1669 if (cli_api(cli,
1670 param, PTR_DIFF(p,param), 8, /* params, length, max */
1671 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1672 &rparam, &rprcnt, /* return params, return size */
1673 &rdata, &rdrcnt /* return data, return size */
1674 )) {
1675 char *endp = rparam + rprcnt;
1676 res = GETRES(rparam,endp);
1677 cli->rap_error = res;
1679 if (res == 0 || res == ERRmoredata) {
1680 int i, count = 0;
1682 p = rparam + WORDSIZE + WORDSIZE;
1683 GETWORD(p, count,endp);
1685 p = rdata;
1686 endp = rdata + rdrcnt;
1687 for (i = 0;i < count && p < endp;i++, p += 16) {
1688 char ret_server[RAP_MACHNAME_LEN];
1690 p += rap_getstringf(p,
1691 ret_server,
1692 RAP_MACHNAME_LEN,
1693 RAP_MACHNAME_LEN,
1694 endp);
1695 if (strequal(ret_server, remote_name)) {
1696 found_server = true;
1697 break;
1700 } else {
1701 DEBUG(4, ("cli_ns_check_server_type: machine %s "
1702 "failed the NetServerEnum call. Error was : "
1703 "%s.\n", remote_name,
1704 win_errstr(W_ERROR(cli->rap_error))));
1708 SAFE_FREE(rparam);
1709 SAFE_FREE(rdata);
1711 return found_server;
1714 /****************************************************************************
1715 Perform a NetWkstaUserLogoff.
1716 ****************************************************************************/
1718 bool cli_NetWkstaUserLogoff(struct cli_state *cli, const char *user, const char *workstation)
1720 char *rparam = NULL;
1721 char *rdata = NULL;
1722 char *p;
1723 unsigned int rdrcnt,rprcnt;
1724 char param[WORDSIZE /* api number */
1725 +sizeof(RAP_NetWkstaUserLogoff_REQ) /* req string */
1726 +sizeof(RAP_USER_LOGOFF_INFO_L1) /* return string */
1727 +RAP_USERNAME_LEN+1 /* user name+pad */
1728 +RAP_MACHNAME_LEN /* wksta name */
1729 +WORDSIZE /* buffer size */
1730 +WORDSIZE]; /* buffer size? */
1731 char upperbuf[MAX(RAP_USERNAME_LEN,RAP_MACHNAME_LEN)];
1732 int res = -1;
1733 char *tmp = NULL;
1735 memset(param, 0, sizeof(param));
1737 /* send a SMBtrans command with api NetWkstaUserLogoff */
1738 p = make_header(param, RAP_WWkstaUserLogoff,
1739 RAP_NetWkstaUserLogoff_REQ, RAP_USER_LOGOFF_INFO_L1);
1740 PUTDWORD(p, 0); /* Null pointer */
1741 PUTDWORD(p, 0); /* Null pointer */
1742 strlcpy(upperbuf, user, sizeof(upperbuf));
1743 if (!strupper_m(upperbuf)) {
1744 return false;
1746 tmp = upperbuf;
1747 PUTSTRINGF(p, tmp, RAP_USERNAME_LEN);
1748 p++; /* strange format, but ok */
1749 strlcpy(upperbuf, workstation, sizeof(upperbuf));
1750 if (!strupper_m(upperbuf)) {
1751 return false;
1753 tmp = upperbuf;
1754 PUTSTRINGF(p, tmp, RAP_MACHNAME_LEN);
1755 PUTWORD(p, CLI_BUFFER_SIZE);
1756 PUTWORD(p, CLI_BUFFER_SIZE);
1758 if (cli_api(cli,
1759 param, PTR_DIFF(p,param),1024, /* param, length, max */
1760 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1761 &rparam, &rprcnt, /* return params, return size */
1762 &rdata, &rdrcnt /* return data, return size */
1763 )) {
1764 char *endp = rparam + rprcnt;
1765 res = GETRES(rparam,endp);
1766 cli->rap_error = res;
1768 if (cli->rap_error != 0) {
1769 DEBUG(4,("NetwkstaUserLogoff gave error %d\n", cli->rap_error));
1773 SAFE_FREE(rparam);
1774 SAFE_FREE(rdata);
1775 return (cli->rap_error == 0);
1778 int cli_NetPrintQEnum(struct cli_state *cli,
1779 void (*qfn)(const char*,uint16_t,uint16_t,uint16_t,const char*,const char*,const char*,const char*,const char*,uint16_t,uint16_t),
1780 void (*jfn)(uint16_t,const char*,const char*,const char*,const char*,uint16_t,uint16_t,const char*,unsigned int,unsigned int,const char*))
1782 char param[WORDSIZE /* api number */
1783 +sizeof(RAP_NetPrintQEnum_REQ) /* req string */
1784 +sizeof(RAP_PRINTQ_INFO_L2) /* return string */
1785 +WORDSIZE /* info level */
1786 +WORDSIZE /* buffer size */
1787 +sizeof(RAP_SMB_PRINT_JOB_L1)]; /* more ret data */
1788 char *p;
1789 char *rparam = NULL;
1790 char *rdata = NULL;
1791 unsigned int rprcnt, rdrcnt;
1792 int res = -1;
1794 memset(param, '\0',sizeof(param));
1795 p = make_header(param, RAP_WPrintQEnum,
1796 RAP_NetPrintQEnum_REQ, RAP_PRINTQ_INFO_L2);
1797 PUTWORD(p,2); /* Info level 2 */
1798 PUTWORD(p,0xFFE0); /* Return buffer size */
1799 PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1801 if (cli_api(cli,
1802 param, PTR_DIFF(p,param),1024,
1803 NULL, 0, CLI_BUFFER_SIZE,
1804 &rparam, &rprcnt,
1805 &rdata, &rdrcnt)) {
1806 char *endp = rparam + rprcnt;
1807 res = GETRES(rparam, endp);
1808 cli->rap_error = res;
1809 if (res != 0) {
1810 DEBUG(1,("NetPrintQEnum gave error %d\n", res));
1814 if (!rdata) {
1815 DEBUG(4,("NetPrintQEnum no data returned\n"));
1816 goto out;
1819 if (res == 0 || res == ERRmoredata) {
1820 TALLOC_CTX *frame = talloc_stackframe();
1821 char *endp = rparam + rprcnt;
1822 int i, converter = 0, count = 0;
1824 p = rparam + WORDSIZE;
1825 GETWORD(p, converter, endp);
1826 GETWORD(p, count, endp);
1828 p = rdata;
1829 endp = rdata + rdrcnt;
1830 for (i=0;i<count && p < endp;i++) {
1831 char qname[RAP_SHARENAME_LEN];
1832 char *sep_file, *print_proc, *dest, *parms, *comment;
1833 uint16_t jobcount = 0, priority = 0;
1834 uint16_t start_time = 0, until_time = 0, status = 0;
1836 p += rap_getstringf(p,
1837 qname,
1838 RAP_SHARENAME_LEN,
1839 RAP_SHARENAME_LEN,
1840 endp);
1841 p++; /* pad */
1842 GETWORD(p, priority, endp);
1843 GETWORD(p, start_time, endp);
1844 GETWORD(p, until_time, endp);
1845 p += rap_getstringp(frame,
1847 &sep_file,
1848 rdata,
1849 converter,
1850 endp);
1851 p += rap_getstringp(frame,
1853 &print_proc,
1854 rdata,
1855 converter,
1856 endp);
1857 p += rap_getstringp(frame,
1859 &dest,
1860 rdata,
1861 converter,
1862 endp);
1863 p += rap_getstringp(frame,
1865 &parms,
1866 rdata,
1867 converter,
1868 endp);
1869 p += rap_getstringp(frame,
1871 &comment,
1872 rdata,
1873 converter,
1874 endp);
1875 GETWORD(p, status, endp);
1876 GETWORD(p, jobcount, endp);
1878 if (sep_file && print_proc && dest && parms &&
1879 comment) {
1880 qfn(qname, priority, start_time, until_time, sep_file, print_proc,
1881 dest, parms, comment, status, jobcount);
1884 if (jobcount) {
1885 int j;
1886 for (j=0;j<jobcount;j++) {
1887 uint16_t jid = 0, pos = 0, fsstatus = 0;
1888 char ownername[RAP_USERNAME_LEN];
1889 char notifyname[RAP_MACHNAME_LEN];
1890 char datatype[RAP_DATATYPE_LEN];
1891 char *jparms, *jstatus, *jcomment;
1892 unsigned int submitted = 0, jsize = 0;
1894 GETWORD(p, jid, endp);
1895 p += rap_getstringf(p,
1896 ownername,
1897 RAP_USERNAME_LEN,
1898 RAP_USERNAME_LEN,
1899 endp);
1900 p++; /* pad byte */
1901 p += rap_getstringf(p,
1902 notifyname,
1903 RAP_MACHNAME_LEN,
1904 RAP_MACHNAME_LEN,
1905 endp);
1906 p += rap_getstringf(p,
1907 datatype,
1908 RAP_DATATYPE_LEN,
1909 RAP_DATATYPE_LEN,
1910 endp);
1911 p += rap_getstringp(frame,
1913 &jparms,
1914 rdata,
1915 converter,
1916 endp);
1917 GETWORD(p, pos, endp);
1918 GETWORD(p, fsstatus, endp);
1919 p += rap_getstringp(frame,
1921 &jstatus,
1922 rdata,
1923 converter,
1924 endp);
1925 GETDWORD(p, submitted, endp);
1926 GETDWORD(p, jsize, endp);
1927 p += rap_getstringp(frame,
1929 &jcomment,
1930 rdata,
1931 converter,
1932 endp);
1934 if (jparms && jstatus && jcomment) {
1935 jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
1936 jstatus, submitted, jsize, jcomment);
1941 TALLOC_FREE(frame);
1942 } else {
1943 DEBUG(4,("NetPrintQEnum res=%d\n", res));
1946 out:
1948 SAFE_FREE(rparam);
1949 SAFE_FREE(rdata);
1951 return res;
1954 int cli_NetPrintQGetInfo(struct cli_state *cli, const char *printer,
1955 void (*qfn)(const char*,uint16_t,uint16_t,uint16_t,const char*,const char*,const char*,const char*,const char*,uint16_t,uint16_t),
1956 void (*jfn)(uint16_t,const char*,const char*,const char*,const char*,uint16_t,uint16_t,const char*,unsigned int,unsigned int,const char*))
1958 char param[WORDSIZE /* api number */
1959 +sizeof(RAP_NetPrintQGetInfo_REQ) /* req string */
1960 +sizeof(RAP_PRINTQ_INFO_L2) /* return string */
1961 +RAP_SHARENAME_LEN /* printer name */
1962 +WORDSIZE /* info level */
1963 +WORDSIZE /* buffer size */
1964 +sizeof(RAP_SMB_PRINT_JOB_L1)]; /* more ret data */
1965 char *p;
1966 char *rparam = NULL;
1967 char *rdata = NULL;
1968 unsigned int rprcnt, rdrcnt;
1969 int res = -1;
1971 memset(param, '\0',sizeof(param));
1972 p = make_header(param, RAP_WPrintQGetInfo,
1973 RAP_NetPrintQGetInfo_REQ, RAP_PRINTQ_INFO_L2);
1974 PUTSTRING(p, printer, RAP_SHARENAME_LEN-1);
1975 PUTWORD(p, 2); /* Info level 2 */
1976 PUTWORD(p,0xFFE0); /* Return buffer size */
1977 PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1979 if (cli_api(cli,
1980 param, PTR_DIFF(p,param),1024,
1981 NULL, 0, CLI_BUFFER_SIZE,
1982 &rparam, &rprcnt,
1983 &rdata, &rdrcnt)) {
1984 char *endp = rparam + rprcnt;
1985 res = GETRES(rparam, endp);
1986 cli->rap_error = res;
1987 if (res != 0) {
1988 DEBUG(1,("NetPrintQGetInfo gave error %d\n", res));
1992 if (!rdata) {
1993 DEBUG(4,("NetPrintQGetInfo no data returned\n"));
1994 goto out;
1997 if (res == 0 || res == ERRmoredata) {
1998 TALLOC_CTX *frame = talloc_stackframe();
1999 char *endp = rparam + rprcnt;
2000 int rsize = 0, converter = 0;
2001 char qname[RAP_SHARENAME_LEN];
2002 char *sep_file, *print_proc, *dest, *parms, *comment;
2003 uint16_t jobcount = 0, priority = 0;
2004 uint16_t start_time = 0, until_time = 0, status = 0;
2006 p = rparam + WORDSIZE;
2007 GETWORD(p, converter, endp);
2008 GETWORD(p, rsize, endp);
2010 p = rdata;
2011 endp = rdata + rdrcnt;
2012 p += rap_getstringf(p,
2013 qname,
2014 RAP_SHARENAME_LEN,
2015 RAP_SHARENAME_LEN,
2016 endp);
2017 p++; /* pad */
2018 GETWORD(p, priority, endp);
2019 GETWORD(p, start_time, endp);
2020 GETWORD(p, until_time, endp);
2021 p += rap_getstringp(frame,
2023 &sep_file,
2024 rdata,
2025 converter,
2026 endp);
2027 p += rap_getstringp(frame,
2029 &print_proc,
2030 rdata,
2031 converter,
2032 endp);
2033 p += rap_getstringp(frame,
2035 &dest,
2036 rdata,
2037 converter,
2038 endp);
2039 p += rap_getstringp(frame,
2041 &parms,
2042 rdata,
2043 converter,
2044 endp);
2045 p += rap_getstringp(frame,
2047 &comment,
2048 rdata,
2049 converter,
2050 endp);
2051 GETWORD(p, status, endp);
2052 GETWORD(p, jobcount, endp);
2054 if (sep_file && print_proc && dest &&
2055 parms && comment) {
2056 qfn(qname, priority, start_time, until_time, sep_file, print_proc,
2057 dest, parms, comment, status, jobcount);
2059 if (jobcount) {
2060 int j;
2061 for (j=0;(j<jobcount)&&(PTR_DIFF(p,rdata)< rsize)&&
2062 p<endp;j++) {
2063 uint16_t jid = 0, pos = 0, fsstatus = 0;
2064 char ownername[RAP_USERNAME_LEN];
2065 char notifyname[RAP_MACHNAME_LEN];
2066 char datatype[RAP_DATATYPE_LEN];
2067 char *jparms, *jstatus, *jcomment;
2068 unsigned int submitted = 0, jsize = 0;
2070 GETWORD(p, jid, endp);
2071 p += rap_getstringf(p,
2072 ownername,
2073 RAP_USERNAME_LEN,
2074 RAP_USERNAME_LEN,
2075 endp);
2076 p++; /* pad byte */
2077 p += rap_getstringf(p,
2078 notifyname,
2079 RAP_MACHNAME_LEN,
2080 RAP_MACHNAME_LEN,
2081 endp);
2082 p += rap_getstringf(p,
2083 datatype,
2084 RAP_DATATYPE_LEN,
2085 RAP_DATATYPE_LEN,
2086 endp);
2087 p += rap_getstringp(frame,
2089 &jparms,
2090 rdata,
2091 converter,
2092 endp);
2093 GETWORD(p, pos,endp);
2094 GETWORD(p, fsstatus,endp);
2095 p += rap_getstringp(frame,
2097 &jstatus,
2098 rdata,
2099 converter,
2100 endp);
2101 GETDWORD(p, submitted,endp);
2102 GETDWORD(p, jsize,endp);
2103 p += rap_getstringp(frame,
2105 &jcomment,
2106 rdata,
2107 converter,
2108 endp);
2110 if (jparms && jstatus && jcomment) {
2111 jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
2112 jstatus, submitted, jsize, jcomment);
2116 TALLOC_FREE(frame);
2117 } else {
2118 DEBUG(4,("NetPrintQGetInfo res=%d\n", res));
2121 out:
2123 SAFE_FREE(rparam);
2124 SAFE_FREE(rdata);
2126 return res;
2129 /****************************************************************************
2130 Call a NetServiceEnum - list running services on a different host.
2131 ****************************************************************************/
2133 int cli_RNetServiceEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
2135 char param[WORDSIZE /* api number */
2136 +sizeof(RAP_NetServiceEnum_REQ) /* parm string */
2137 +sizeof(RAP_SERVICE_INFO_L2) /* return string */
2138 +WORDSIZE /* info level */
2139 +WORDSIZE]; /* buffer size */
2140 char *p;
2141 char *rparam = NULL;
2142 char *rdata = NULL;
2143 unsigned int rprcnt, rdrcnt;
2144 int res = -1;
2146 memset(param, '\0', sizeof(param));
2147 p = make_header(param, RAP_WServiceEnum,
2148 RAP_NetServiceEnum_REQ, RAP_SERVICE_INFO_L2);
2149 PUTWORD(p,2); /* Info level 2 */
2150 PUTWORD(p,0xFFE0); /* Return buffer size */
2152 if (cli_api(cli,
2153 param, PTR_DIFF(p,param),8,
2154 NULL, 0, 0xFFE0 /* data area size */,
2155 &rparam, &rprcnt,
2156 &rdata, &rdrcnt)) {
2157 char *endp = rparam + rprcnt;
2158 res = GETRES(rparam, endp);
2159 cli->rap_error = res;
2160 if(cli->rap_error == 234) {
2161 DEBUG(1,("Not all service names were returned (such as those longer than 15 characters)\n"));
2162 } else if (cli->rap_error != 0) {
2163 DEBUG(1,("NetServiceEnum gave error %d\n", cli->rap_error));
2167 if (!rdata) {
2168 DEBUG(4,("NetServiceEnum no data returned\n"));
2169 goto out;
2172 if (res == 0 || res == ERRmoredata) {
2173 char *endp = rparam + rprcnt;
2174 int i, count = 0;
2176 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
2177 GETWORD(p, count,endp);
2179 endp = rdata + rdrcnt;
2180 for (i=0,p=rdata;i<count && p < endp;i++) {
2181 char comment[RAP_SRVCCMNT_LEN];
2182 char servicename[RAP_SRVCNAME_LEN];
2184 p += rap_getstringf(p,
2185 servicename,
2186 RAP_SRVCNAME_LEN,
2187 RAP_SRVCNAME_LEN,
2188 endp);
2189 p+=8; /* pass status words */
2190 p += rap_getstringf(p,
2191 comment,
2192 RAP_SRVCCMNT_LEN,
2193 RAP_SRVCCMNT_LEN,
2194 endp);
2196 if (servicename[0]) {
2197 fn(servicename, comment, cli); /* BB add status too */
2200 } else {
2201 DEBUG(4,("NetServiceEnum res=%d\n", res));
2204 out:
2206 SAFE_FREE(rparam);
2207 SAFE_FREE(rdata);
2209 return res;
2212 /****************************************************************************
2213 Call a NetSessionEnum - list workstations with sessions to an SMB server.
2214 ****************************************************************************/
2216 int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16_t, uint16_t, uint16_t, unsigned int, unsigned int, unsigned int, char *))
2218 char param[WORDSIZE /* api number */
2219 +sizeof(RAP_NetSessionEnum_REQ) /* parm string */
2220 +sizeof(RAP_SESSION_INFO_L2) /* return string */
2221 +WORDSIZE /* info level */
2222 +WORDSIZE]; /* buffer size */
2223 char *p;
2224 char *rparam = NULL;
2225 char *rdata = NULL;
2226 unsigned int rprcnt, rdrcnt;
2227 int res = -1;
2229 memset(param, '\0', sizeof(param));
2230 p = make_header(param, RAP_WsessionEnum,
2231 RAP_NetSessionEnum_REQ, RAP_SESSION_INFO_L2);
2232 PUTWORD(p,2); /* Info level 2 */
2233 PUTWORD(p,0xFF); /* Return buffer size */
2235 if (cli_api(cli,
2236 param, PTR_DIFF(p,param),8,
2237 NULL, 0, CLI_BUFFER_SIZE,
2238 &rparam, &rprcnt,
2239 &rdata, &rdrcnt)) {
2240 char *endp = rparam + rprcnt;
2241 res = GETRES(rparam, endp);
2242 cli->rap_error = res;
2243 if (res != 0) {
2244 DEBUG(1,("NetSessionEnum gave error %d\n", res));
2248 if (!rdata) {
2249 DEBUG(4,("NetSessionEnum no data returned\n"));
2250 goto out;
2253 if (res == 0 || res == ERRmoredata) {
2254 TALLOC_CTX *frame = talloc_stackframe();
2255 char *endp = rparam + rprcnt;
2256 int i, converter = 0, count = 0;
2258 p = rparam + WORDSIZE;
2259 GETWORD(p, converter, endp);
2260 GETWORD(p, count, endp);
2262 endp = rdata + rdrcnt;
2263 for (i=0,p=rdata;i<count && p < endp;i++) {
2264 char *wsname, *username, *clitype_name;
2265 uint16_t num_conns = 0, num_opens = 0, num_users = 0;
2266 unsigned int sess_time = 0, idle_time = 0, user_flags = 0;
2268 p += rap_getstringp(frame,
2270 &wsname,
2271 rdata,
2272 converter,
2273 endp);
2274 p += rap_getstringp(frame,
2276 &username,
2277 rdata,
2278 converter,
2279 endp);
2280 GETWORD(p, num_conns, endp);
2281 GETWORD(p, num_opens, endp);
2282 GETWORD(p, num_users, endp);
2283 GETDWORD(p, sess_time, endp);
2284 GETDWORD(p, idle_time, endp);
2285 GETDWORD(p, user_flags, endp);
2286 p += rap_getstringp(frame,
2288 &clitype_name,
2289 rdata,
2290 converter,
2291 endp);
2293 if (wsname && username && clitype_name) {
2294 fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2295 idle_time, user_flags, clitype_name);
2298 TALLOC_FREE(frame);
2299 } else {
2300 DEBUG(4,("NetSessionEnum res=%d\n", res));
2303 out:
2305 SAFE_FREE(rparam);
2306 SAFE_FREE(rdata);
2308 return res;
2311 /****************************************************************************
2312 Call a NetSessionGetInfo - get information about other session to an SMB server.
2313 ****************************************************************************/
2315 int cli_NetSessionGetInfo(struct cli_state *cli, const char *workstation,
2316 void (*fn)(const char *, const char *, uint16_t, uint16_t, uint16_t, unsigned int, unsigned int, unsigned int, const char *))
2318 char param[WORDSIZE /* api number */
2319 +sizeof(RAP_NetSessionGetInfo_REQ) /* req string */
2320 +sizeof(RAP_SESSION_INFO_L2) /* return string */
2321 +RAP_MACHNAME_LEN /* wksta name */
2322 +WORDSIZE /* info level */
2323 +WORDSIZE]; /* buffer size */
2324 char *p;
2325 char *rparam = NULL;
2326 char *rdata = NULL;
2327 unsigned int rprcnt, rdrcnt;
2328 char *endp;
2329 int res = -1;
2331 memset(param, '\0', sizeof(param));
2332 p = make_header(param, RAP_WsessionGetInfo,
2333 RAP_NetSessionGetInfo_REQ, RAP_SESSION_INFO_L2);
2334 PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2335 PUTWORD(p,2); /* Info level 2 */
2336 PUTWORD(p,0xFF); /* Return buffer size */
2338 if (cli_api(cli,
2339 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2340 NULL, 0, CLI_BUFFER_SIZE,
2341 &rparam, &rprcnt,
2342 &rdata, &rdrcnt)) {
2343 endp = rparam + rprcnt;
2344 res = GETRES(rparam, endp);
2345 cli->rap_error = res;
2346 if (cli->rap_error != 0) {
2347 DEBUG(1,("NetSessionGetInfo gave error %d\n", cli->rap_error));
2351 if (!rdata) {
2352 DEBUG(4,("NetSessionGetInfo no data returned\n"));
2353 goto out;
2356 endp = rparam + rprcnt;
2357 res = GETRES(rparam, endp);
2359 if (res == 0 || res == ERRmoredata) {
2360 TALLOC_CTX *frame = talloc_stackframe();
2361 int converter = 0;
2362 char *wsname, *username, *clitype_name;
2363 uint16_t num_conns = 0, num_opens = 0, num_users = 0;
2364 unsigned int sess_time = 0, idle_time = 0, user_flags = 0;
2366 p = rparam + WORDSIZE;
2367 GETWORD(p, converter,endp);
2369 p = rdata;
2370 endp = rdata + rdrcnt;
2371 p += rap_getstringp(frame,
2373 &wsname,
2374 rdata,
2375 converter,
2376 endp);
2377 p += rap_getstringp(frame,
2379 &username,
2380 rdata,
2381 converter,
2382 endp);
2383 GETWORD(p, num_conns, endp);
2384 GETWORD(p, num_opens, endp);
2385 GETWORD(p, num_users, endp);
2386 GETDWORD(p, sess_time, endp);
2387 GETDWORD(p, idle_time, endp);
2388 GETDWORD(p, user_flags, endp);
2389 rap_getstringp(frame,
2391 &clitype_name,
2392 rdata,
2393 converter,
2394 endp);
2396 if (wsname && username && clitype_name) {
2397 fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2398 idle_time, user_flags, clitype_name);
2400 TALLOC_FREE(frame);
2401 } else {
2402 DEBUG(4,("NetSessionGetInfo res=%d\n", res));
2405 out:
2407 SAFE_FREE(rparam);
2408 SAFE_FREE(rdata);
2410 return res;
2413 /****************************************************************************
2414 Call a NetSessionDel - close a session to an SMB server.
2415 ****************************************************************************/
2417 int cli_NetSessionDel(struct cli_state *cli, const char *workstation)
2419 char param[WORDSIZE /* api number */
2420 +sizeof(RAP_NetSessionDel_REQ) /* req string */
2421 +1 /* no return string */
2422 +RAP_MACHNAME_LEN /* workstation name */
2423 +WORDSIZE]; /* reserved (0) */
2424 char *p;
2425 char *rparam = NULL;
2426 char *rdata = NULL;
2427 unsigned int rprcnt, rdrcnt;
2428 int res = -1;
2430 memset(param, '\0', sizeof(param));
2431 p = make_header(param, RAP_WsessionDel, RAP_NetSessionDel_REQ, NULL);
2432 PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2433 PUTWORD(p,0); /* reserved word of 0 */
2435 if (cli_api(cli,
2436 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
2437 NULL, 0, 200, /* data, length, maxlen */
2438 &rparam, &rprcnt, /* return params, length */
2439 &rdata, &rdrcnt)) /* return data, length */
2441 char *endp = rparam + rprcnt;
2442 res = GETRES(rparam, endp);
2443 cli->rap_error = res;
2445 if (res == 0) {
2446 /* nothing to do */
2447 } else {
2448 DEBUG(4,("NetFileClose2 res=%d\n", res));
2450 } else {
2451 res = -1;
2452 DEBUG(4,("NetFileClose2 failed\n"));
2455 SAFE_FREE(rparam);
2456 SAFE_FREE(rdata);
2458 return res;
2461 int cli_NetConnectionEnum(struct cli_state *cli, const char *qualifier,
2462 void (*fn)(uint16_t conid, uint16_t contype,
2463 uint16_t numopens, uint16_t numusers,
2464 uint32_t contime, const char *username,
2465 const char *netname))
2467 char param[WORDSIZE /* api number */
2468 +sizeof(RAP_NetConnectionEnum_REQ) /* req string */
2469 +sizeof(RAP_CONNECTION_INFO_L1) /* return string */
2470 +RAP_MACHNAME_LEN /* wksta name */
2471 +WORDSIZE /* info level */
2472 +WORDSIZE]; /* buffer size */
2473 char *p;
2474 char *rparam = NULL;
2475 char *rdata = NULL;
2476 unsigned int rprcnt, rdrcnt;
2477 int res = -1;
2479 memset(param, '\0', sizeof(param));
2480 p = make_header(param, RAP_WconnectionEnum,
2481 RAP_NetConnectionEnum_REQ, RAP_CONNECTION_INFO_L1);
2482 PUTSTRING(p, qualifier, RAP_MACHNAME_LEN-1);/* Workstation name */
2483 PUTWORD(p,1); /* Info level 1 */
2484 PUTWORD(p,0xFFE0); /* Return buffer size */
2486 if (cli_api(cli,
2487 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2488 NULL, 0, CLI_BUFFER_SIZE,
2489 &rparam, &rprcnt,
2490 &rdata, &rdrcnt)) {
2491 char *endp = rparam + rprcnt;
2492 res = GETRES(rparam, endp);
2493 cli->rap_error = res;
2494 if (res != 0) {
2495 DEBUG(1,("NetConnectionEnum gave error %d\n", res));
2499 if (!rdata) {
2500 DEBUG(4,("NetConnectionEnum no data returned\n"));
2501 goto out;
2504 if (res == 0 || res == ERRmoredata) {
2505 TALLOC_CTX *frame = talloc_stackframe();
2506 char *endp = rparam + rprcnt;
2507 int i, converter = 0, count = 0;
2509 p = rparam + WORDSIZE;
2510 GETWORD(p, converter, endp);
2511 GETWORD(p, count, endp);
2513 endp = rdata + rdrcnt;
2514 for (i=0,p=rdata;i<count && p < endp;i++) {
2515 char *netname, *username;
2516 uint16_t conn_id = 0, conn_type = 0, num_opens = 0, num_users = 0;
2517 unsigned int conn_time = 0;
2519 GETWORD(p,conn_id, endp);
2520 GETWORD(p,conn_type, endp);
2521 GETWORD(p,num_opens, endp);
2522 GETWORD(p,num_users, endp);
2523 GETDWORD(p,conn_time, endp);
2524 p += rap_getstringp(frame,
2526 &username,
2527 rdata,
2528 converter,
2529 endp);
2530 p += rap_getstringp(frame,
2532 &netname,
2533 rdata,
2534 converter,
2535 endp);
2537 if (username && netname) {
2538 fn(conn_id, conn_type, num_opens, num_users, conn_time,
2539 username, netname);
2542 TALLOC_FREE(frame);
2543 } else {
2544 DEBUG(4,("NetConnectionEnum res=%d\n", res));
2547 out:
2549 SAFE_FREE(rdata);
2550 SAFE_FREE(rparam);
2551 return res;