source3/libsmb/errormap_wbc.h: fix licence/copyright
[Samba.git] / source3 / libsmb / clirap2.c
blob03b4ca754123ca0d1835c07917075eb248601895
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 admistration 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 "libsmb/clirap.h"
84 #define WORDSIZE 2
85 #define DWORDSIZE 4
87 #define PUTBYTE(p,b) do {SCVAL(p,0,b); p++;} while(0)
89 #define GETBYTE(p,b,endp) \
90 do {\
91 if (p+1 < endp) {\
92 b = CVAL(p,0);\
94 p++;\
95 } while(0)
97 #define PUTWORD(p,w) do {SSVAL(p,0,w); p += WORDSIZE;} while(0)
99 #define GETWORD(p,w,endp) \
100 do {\
101 if (p+WORDSIZE < endp) {\
102 w = SVAL(p,0);\
104 p += WORDSIZE;\
105 } while(0)
107 #define PUTDWORD(p,d) do {SIVAL(p,0,d); p += DWORDSIZE;} while(0)
109 #define GETDWORD(p,d,endp) \
110 do {\
111 if (p+DWORDSIZE < endp) {\
112 d = IVAL(p,0);\
114 p += DWORDSIZE;\
115 } while(0)
117 #define GETRES(p,endp) ((p && p+2 < endp) ? SVAL(p,0) : -1)
119 /* put string s at p with max len n and increment p past string */
120 #define PUTSTRING(p,s,n) \
121 do {\
122 push_ascii(p,s?s:"",n?n:256,STR_TERMINATE);\
123 p = push_skip_string(p);\
124 } while(0)
126 /* put string s and p, using fixed len l, and increment p by l */
127 #define PUTSTRINGF(p,s,l) \
128 do {\
129 push_ascii(p,s?s:"",l,STR_TERMINATE);\
130 p += l;\
131 } while (0)
133 /* put string pointer at p, supplying offset o from rdata r, store */
134 /* dword offset at p, increment p by 4 and o by length of s. This */
135 /* means on the first call, you must calc the offset yourself! */
137 #define PUTSTRINGP(p,s,r,o) \
138 do {\
139 if (s) {\
140 push_ascii(r+o,s,strlen(s)+1,STR_TERMINATE);\
141 PUTDWORD(p,o);\
142 o += strlen(s) + 1;\
143 } else {\
144 PUTDWORD(p,0);\
146 }while(0);
148 /* get asciiz string dest from src, return increment past string */
150 static size_t rap_getstring(TALLOC_CTX *ctx, char *src, char **dest, const char *endp)
152 char *p1;
153 size_t len;
155 *dest = NULL;
156 for (p1 = src, len = 0; *p1 && p1 < endp; len++)
157 p1++;
158 if (!*p1) {
159 len++;
161 pull_string_talloc(ctx,src,0,dest,src,len,STR_ASCII);
162 return len;
165 /* get fixed length l string dest from src, return increment for src */
167 static size_t rap_getstringf(char *src, char *dest, size_t l, size_t dlen, char *endp)
169 char *p1;
170 size_t len;
172 if (dlen) {
173 dest[0] = '\0';
175 for (p1 = src, len = 0; *p1 && p1 < endp; len++) {
176 p1++;
178 if (!*p1) {
179 len++;
181 if (len > l) {
182 len = l;
184 if (len) {
185 pull_ascii(dest,src,len,len,STR_ASCII);
187 return l;
190 /* get string dest from offset (obtained at p) from rdata r - converter c */
191 static size_t rap_getstringp(TALLOC_CTX *ctx, char *p, char **dest, char *r, uint16_t c, char *endp)
193 uint32_t off = 0;
194 const char *src;
195 size_t len=0;
197 *dest = NULL;
198 if (p+4 < endp) {
199 GETDWORD(p,off,endp);
200 off &= 0x0000FFFF; /* mask the obsolete segment number from the offset */
201 off -= c;
203 if (r+off > endp || r+off < r) {
204 src="";
205 len=1;
206 } else {
207 const char *p1;
208 src=r+off;
209 for (p1 = src, len = 0; *p1 && p1 < endp; len++) {
210 p1++;
212 if (!*p1) {
213 len++;
216 pull_string_talloc(ctx,src,0,dest,src,len,STR_ASCII);
217 return 4;
220 static char *make_header(char *param, uint16 apinum, const char *reqfmt, const char *datafmt)
222 PUTWORD(param,apinum);
223 if (reqfmt)
224 PUTSTRING(param,reqfmt,0);
225 else
226 *param++ = (char) 0;
228 if (datafmt)
229 PUTSTRING(param,datafmt,0);
230 else
231 *param++ = (char) 0;
233 return param;
236 /****************************************************************************
237 call a NetGroupDelete - delete user group from remote server
238 ****************************************************************************/
240 int cli_NetGroupDelete(struct cli_state *cli, const char *group_name)
242 char *rparam = NULL;
243 char *rdata = NULL;
244 char *p;
245 unsigned int rdrcnt,rprcnt;
246 int res = -1;
247 char param[WORDSIZE /* api number */
248 +sizeof(RAP_NetGroupDel_REQ) /* parm string */
249 +1 /* no ret string */
250 +RAP_GROUPNAME_LEN /* group to del */
251 +WORDSIZE]; /* reserved word */
253 /* now send a SMBtrans command with api GroupDel */
254 p = make_header(param, RAP_WGroupDel, RAP_NetGroupDel_REQ, NULL);
255 PUTSTRING(p, group_name, RAP_GROUPNAME_LEN);
256 PUTWORD(p,0); /* reserved word MBZ on input */
258 if (cli_api(cli,
259 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
260 NULL, 0, 200, /* data, length, maxlen */
261 &rparam, &rprcnt, /* return params, length */
262 &rdata, &rdrcnt)) /* return data, length */
264 char *endp = rparam + rprcnt;
265 res = GETRES(rparam,endp);
267 if (res == 0) {
268 /* nothing to do */
269 } else if ((res == 5) || (res == 65)) {
270 DEBUG(1, ("Access Denied\n"));
271 } else if (res == 2220) {
272 DEBUG (1, ("Group does not exist\n"));
273 } else {
274 DEBUG(4,("NetGroupDelete res=%d\n", res));
276 } else {
277 res = -1;
278 DEBUG(4,("NetGroupDelete failed\n"));
281 SAFE_FREE(rparam);
282 SAFE_FREE(rdata);
284 return res;
287 /****************************************************************************
288 call a NetGroupAdd - add user group to remote server
289 ****************************************************************************/
291 int cli_NetGroupAdd(struct cli_state *cli, struct rap_group_info_1 *grinfo)
293 char *rparam = NULL;
294 char *rdata = NULL;
295 char *p;
296 unsigned int rdrcnt,rprcnt;
297 int res = -1;
298 char param[WORDSIZE /* api number */
299 +sizeof(RAP_NetGroupAdd_REQ) /* req string */
300 +sizeof(RAP_GROUP_INFO_L1) /* return string */
301 +WORDSIZE /* info level */
302 +WORDSIZE]; /* reserved word */
304 /* offset into data of free format strings. Will be updated */
305 /* by PUTSTRINGP macro and end up with total data length. */
306 int soffset = RAP_GROUPNAME_LEN + 1 + DWORDSIZE;
307 char *data;
308 size_t data_size;
310 /* Allocate data. */
311 data_size = MAX(soffset + strlen(grinfo->comment) + 1, 1024);
313 data = SMB_MALLOC_ARRAY(char, data_size);
314 if (!data) {
315 DEBUG (1, ("Malloc fail\n"));
316 return -1;
319 /* now send a SMBtrans command with api WGroupAdd */
321 p = make_header(param, RAP_WGroupAdd,
322 RAP_NetGroupAdd_REQ, RAP_GROUP_INFO_L1);
323 PUTWORD(p, 1); /* info level */
324 PUTWORD(p, 0); /* reserved word 0 */
326 p = data;
327 PUTSTRINGF(p, (const char *)grinfo->group_name, RAP_GROUPNAME_LEN);
328 PUTBYTE(p, 0); /* pad byte 0 */
329 PUTSTRINGP(p, grinfo->comment, data, soffset);
331 if (cli_api(cli,
332 param, sizeof(param), 1024, /* Param, length, maxlen */
333 data, soffset, sizeof(data), /* data, length, maxlen */
334 &rparam, &rprcnt, /* return params, length */
335 &rdata, &rdrcnt)) /* return data, length */
337 char *endp = rparam + rprcnt;
338 res = GETRES(rparam, endp);
340 if (res == 0) {
341 /* nothing to do */
342 } else if ((res == 5) || (res == 65)) {
343 DEBUG(1, ("Access Denied\n"));
344 } else if (res == 2223) {
345 DEBUG (1, ("Group already exists\n"));
346 } else {
347 DEBUG(4,("NetGroupAdd res=%d\n", res));
349 } else {
350 res = -1;
351 DEBUG(4,("NetGroupAdd failed\n"));
354 SAFE_FREE(data);
355 SAFE_FREE(rparam);
356 SAFE_FREE(rdata);
358 return res;
361 /****************************************************************************
362 Call a NetGroupEnum - try and list user groups on a different host.
363 ****************************************************************************/
365 int cli_RNetGroupEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
367 char param[WORDSIZE /* api number */
368 +sizeof(RAP_NetGroupEnum_REQ) /* parm string */
369 +sizeof(RAP_GROUP_INFO_L1) /* return string */
370 +WORDSIZE /* info level */
371 +WORDSIZE]; /* buffer size */
372 char *p;
373 char *rparam = NULL;
374 char *rdata = NULL;
375 unsigned int rprcnt, rdrcnt;
376 int res = -1;
378 memset(param, '\0', sizeof(param));
379 p = make_header(param, RAP_WGroupEnum,
380 RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L1);
381 PUTWORD(p,1); /* Info level 1 */ /* add level 0 */
382 PUTWORD(p,0xFFE0); /* Return buffer size */
384 if (cli_api(cli,
385 param, PTR_DIFF(p,param),8,
386 NULL, 0, 0xFFE0 /* data area size */,
387 &rparam, &rprcnt,
388 &rdata, &rdrcnt)) {
389 char *endp = rparam + rdrcnt;
391 res = GETRES(rparam, endp);
392 cli->rap_error = res;
393 if(cli->rap_error == 234) {
394 DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
395 } else if (cli->rap_error != 0) {
396 DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
400 if (!rdata) {
401 DEBUG(4,("NetGroupEnum no data returned\n"));
402 goto out;
405 if (res == 0 || res == ERRmoredata) {
406 char *endp = rparam + rprcnt;
407 int i, converter = 0, count = 0;
408 TALLOC_CTX *frame = talloc_stackframe();
410 p = rparam + WORDSIZE; /* skip result */
411 GETWORD(p, converter, endp);
412 GETWORD(p, count, endp);
414 endp = rdata + rdrcnt;
415 for (i=0,p=rdata; i<count && p < endp;i++) {
416 char *comment = NULL;
417 char groupname[RAP_GROUPNAME_LEN];
419 p += rap_getstringf(p,
420 groupname,
421 RAP_GROUPNAME_LEN,
422 RAP_GROUPNAME_LEN,
423 endp);
424 p++; /* pad byte */
425 p += rap_getstringp(frame,
427 &comment,
428 rdata,
429 converter,
430 endp);
432 if (!comment || !groupname[0]) {
433 break;
436 fn(groupname, comment, cli);
438 TALLOC_FREE(frame);
439 } else {
440 DEBUG(4,("NetGroupEnum res=%d\n", res));
443 out:
445 SAFE_FREE(rparam);
446 SAFE_FREE(rdata);
448 return res;
451 int cli_RNetGroupEnum0(struct cli_state *cli,
452 void (*fn)(const char *, void *),
453 void *state)
455 char param[WORDSIZE /* api number */
456 +sizeof(RAP_NetGroupEnum_REQ) /* parm string */
457 +sizeof(RAP_GROUP_INFO_L0) /* return string */
458 +WORDSIZE /* info level */
459 +WORDSIZE]; /* buffer size */
460 char *p;
461 char *rparam = NULL;
462 char *rdata = NULL;
463 unsigned int rprcnt, rdrcnt;
464 int res = -1;
466 memset(param, '\0', sizeof(param));
467 p = make_header(param, RAP_WGroupEnum,
468 RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L0);
469 PUTWORD(p,0); /* Info level 0 */ /* Hmmm. I *very* much suspect this
470 is the resume count, at least
471 that's what smbd believes... */
472 PUTWORD(p,0xFFE0); /* Return buffer size */
474 if (cli_api(cli,
475 param, PTR_DIFF(p,param),8,
476 NULL, 0, 0xFFE0 /* data area size */,
477 &rparam, &rprcnt,
478 &rdata, &rdrcnt)) {
479 char *endp = rparam+rprcnt;
480 res = GETRES(rparam,endp);
481 cli->rap_error = res;
482 if(cli->rap_error == 234) {
483 DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
484 } else if (cli->rap_error != 0) {
485 DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
489 if (!rdata) {
490 DEBUG(4,("NetGroupEnum no data returned\n"));
491 goto out;
494 if (res == 0 || res == ERRmoredata) {
495 char *endp = rparam + rprcnt;
496 int i, count = 0;
498 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
499 GETWORD(p, count, endp);
501 endp = rdata + rdrcnt;
502 for (i=0,p=rdata; i<count && p < endp;i++) {
503 char groupname[RAP_GROUPNAME_LEN];
505 p += rap_getstringf(p,
506 groupname,
507 RAP_GROUPNAME_LEN,
508 RAP_GROUPNAME_LEN,
509 endp);
510 if (groupname[0]) {
511 fn(groupname, cli);
514 } else {
515 DEBUG(4,("NetGroupEnum res=%d\n", res));
518 out:
520 SAFE_FREE(rparam);
521 SAFE_FREE(rdata);
523 return res;
526 int cli_NetGroupDelUser(struct cli_state * cli, const char *group_name, const char *user_name)
528 char *rparam = NULL;
529 char *rdata = NULL;
530 char *p;
531 unsigned int rdrcnt,rprcnt;
532 int res = -1;
533 char param[WORDSIZE /* api number */
534 +sizeof(RAP_NetGroupDelUser_REQ) /* parm string */
535 +1 /* no ret string */
536 +RAP_GROUPNAME_LEN /* group name */
537 +RAP_USERNAME_LEN]; /* user to del */
539 /* now send a SMBtrans command with api GroupMemberAdd */
540 p = make_header(param, RAP_WGroupDelUser, RAP_NetGroupDelUser_REQ, NULL);
541 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
542 PUTSTRING(p,user_name,RAP_USERNAME_LEN);
544 if (cli_api(cli,
545 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
546 NULL, 0, 200, /* data, length, maxlen */
547 &rparam, &rprcnt, /* return params, length */
548 &rdata, &rdrcnt)) /* return data, length */
550 char *endp = rparam + rprcnt;
551 res = GETRES(rparam,endp);
553 switch(res) {
554 case 0:
555 break;
556 case 5:
557 case 65:
558 DEBUG(1, ("Access Denied\n"));
559 break;
560 case 50:
561 DEBUG(1, ("Not supported by server\n"));
562 break;
563 case 2220:
564 DEBUG(1, ("Group does not exist\n"));
565 break;
566 case 2221:
567 DEBUG(1, ("User does not exist\n"));
568 break;
569 case 2237:
570 DEBUG(1, ("User is not in group\n"));
571 break;
572 default:
573 DEBUG(4,("NetGroupDelUser res=%d\n", res));
575 } else {
576 res = -1;
577 DEBUG(4,("NetGroupDelUser failed\n"));
580 SAFE_FREE(rparam);
581 SAFE_FREE(rdata);
583 return res;
586 int cli_NetGroupAddUser(struct cli_state * cli, const char *group_name, const char *user_name)
588 char *rparam = NULL;
589 char *rdata = NULL;
590 char *p;
591 unsigned int rdrcnt,rprcnt;
592 int res = -1;
593 char param[WORDSIZE /* api number */
594 +sizeof(RAP_NetGroupAddUser_REQ) /* parm string */
595 +1 /* no ret string */
596 +RAP_GROUPNAME_LEN /* group name */
597 +RAP_USERNAME_LEN]; /* user to add */
599 /* now send a SMBtrans command with api GroupMemberAdd */
600 p = make_header(param, RAP_WGroupAddUser, RAP_NetGroupAddUser_REQ, NULL);
601 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
602 PUTSTRING(p,user_name,RAP_USERNAME_LEN);
604 if (cli_api(cli,
605 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
606 NULL, 0, 200, /* data, length, maxlen */
607 &rparam, &rprcnt, /* return params, length */
608 &rdata, &rdrcnt)) /* return data, length */
610 char *endp = rparam + rprcnt;
611 res = GETRES(rparam,endp);
613 switch(res) {
614 case 0:
615 break;
616 case 5:
617 case 65:
618 DEBUG(1, ("Access Denied\n"));
619 break;
620 case 50:
621 DEBUG(1, ("Not supported by server\n"));
622 break;
623 case 2220:
624 DEBUG(1, ("Group does not exist\n"));
625 break;
626 case 2221:
627 DEBUG(1, ("User does not exist\n"));
628 break;
629 default:
630 DEBUG(4,("NetGroupAddUser res=%d\n", res));
632 } else {
633 res = -1;
634 DEBUG(4,("NetGroupAddUser failed\n"));
637 SAFE_FREE(rparam);
638 SAFE_FREE(rdata);
640 return res;
644 int cli_NetGroupGetUsers(struct cli_state * cli, const char *group_name, void (*fn)(const char *, void *), void *state )
646 char *rparam = NULL;
647 char *rdata = NULL;
648 char *p;
649 unsigned int rdrcnt,rprcnt;
650 int res = -1;
651 char param[WORDSIZE /* api number */
652 +sizeof(RAP_NetGroupGetUsers_REQ)/* parm string */
653 +sizeof(RAP_GROUP_USERS_INFO_0) /* return string */
654 +RAP_GROUPNAME_LEN /* group name */
655 +WORDSIZE /* info level */
656 +WORDSIZE]; /* buffer size */
658 /* now send a SMBtrans command with api GroupGetUsers */
659 p = make_header(param, RAP_WGroupGetUsers,
660 RAP_NetGroupGetUsers_REQ, RAP_GROUP_USERS_INFO_0);
661 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN-1);
662 PUTWORD(p,0); /* info level 0 */
663 PUTWORD(p,0xFFE0); /* return buffer size */
665 if (cli_api(cli,
666 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
667 NULL, 0, CLI_BUFFER_SIZE,
668 &rparam, &rprcnt,
669 &rdata, &rdrcnt)) {
670 char *endp = rparam + rprcnt;
671 res = GETRES(rparam,endp);
672 cli->rap_error = res;
673 if (res != 0) {
674 DEBUG(1,("NetGroupGetUsers gave error %d\n", res));
678 if (!rdata) {
679 DEBUG(4,("NetGroupGetUsers no data returned\n"));
680 goto out;
683 if (res == 0 || res == ERRmoredata) {
684 char *endp = rparam + rprcnt;
685 int i, count = 0;
686 char username[RAP_USERNAME_LEN];
688 p = rparam + WORDSIZE + WORDSIZE;
689 GETWORD(p, count, endp);
691 endp = rdata + rdrcnt;
692 for (i=0,p=rdata; i<count && p < endp; i++) {
693 p += rap_getstringf(p,
694 username,
695 RAP_USERNAME_LEN,
696 RAP_USERNAME_LEN,
697 endp);
698 if (username[0]) {
699 fn(username, state);
702 } else {
703 DEBUG(4,("NetGroupGetUsers res=%d\n", res));
706 out:
708 SAFE_FREE(rdata);
709 SAFE_FREE(rparam);
710 return res;
713 int cli_NetUserGetGroups(struct cli_state * cli, const char *user_name, void (*fn)(const char *, void *), void *state )
715 char *rparam = NULL;
716 char *rdata = NULL;
717 char *p;
718 unsigned int rdrcnt,rprcnt;
719 int res = -1;
720 char param[WORDSIZE /* api number */
721 +sizeof(RAP_NetUserGetGroups_REQ)/* parm string */
722 +sizeof(RAP_GROUP_USERS_INFO_0) /* return string */
723 +RAP_USERNAME_LEN /* user name */
724 +WORDSIZE /* info level */
725 +WORDSIZE]; /* buffer size */
727 /* now send a SMBtrans command with api GroupGetUsers */
728 p = make_header(param, RAP_WUserGetGroups,
729 RAP_NetUserGetGroups_REQ, RAP_GROUP_USERS_INFO_0);
730 PUTSTRING(p,user_name,RAP_USERNAME_LEN-1);
731 PUTWORD(p,0); /* info level 0 */
732 PUTWORD(p,0xFFE0); /* return buffer size */
734 if (cli_api(cli,
735 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
736 NULL, 0, CLI_BUFFER_SIZE,
737 &rparam, &rprcnt,
738 &rdata, &rdrcnt)) {
739 char *endp = rparam + rprcnt;
740 res = GETRES(rparam,endp);
741 cli->rap_error = res;
742 if (res != 0) {
743 DEBUG(1,("NetUserGetGroups gave error %d\n", res));
747 if (!rdata) {
748 DEBUG(4,("NetUserGetGroups no data returned\n"));
749 goto out;
752 if (res == 0 || res == ERRmoredata) {
753 char *endp = rparam + rprcnt;
754 int i, count = 0;
755 char groupname[RAP_GROUPNAME_LEN];
757 p = rparam + WORDSIZE + WORDSIZE;
758 GETWORD(p, count, endp);
760 endp = rdata + rdrcnt;
761 for (i=0,p=rdata; i<count && p < endp; i++) {
762 p += rap_getstringf(p,
763 groupname,
764 RAP_GROUPNAME_LEN,
765 RAP_GROUPNAME_LEN,
766 endp);
767 if (groupname[0]) {
768 fn(groupname, state);
771 } else {
772 DEBUG(4,("NetUserGetGroups res=%d\n", res));
775 out:
777 SAFE_FREE(rdata);
778 SAFE_FREE(rparam);
779 return res;
782 /****************************************************************************
783 Call a NetUserDelete - delete user from remote server.
784 ****************************************************************************/
786 int cli_NetUserDelete(struct cli_state *cli, const char * user_name )
788 char *rparam = NULL;
789 char *rdata = NULL;
790 char *p;
791 unsigned int rdrcnt,rprcnt;
792 int res = -1;
793 char param[WORDSIZE /* api number */
794 +sizeof(RAP_NetGroupDel_REQ) /* parm string */
795 +1 /* no ret string */
796 +RAP_USERNAME_LEN /* user to del */
797 +WORDSIZE]; /* reserved word */
799 /* now send a SMBtrans command with api UserDel */
800 p = make_header(param, RAP_WUserDel, RAP_NetGroupDel_REQ, NULL);
801 PUTSTRING(p, user_name, RAP_USERNAME_LEN);
802 PUTWORD(p,0); /* reserved word MBZ on input */
804 if (cli_api(cli,
805 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
806 NULL, 0, 200, /* data, length, maxlen */
807 &rparam, &rprcnt, /* return params, length */
808 &rdata, &rdrcnt)) /* return data, length */
810 char *endp = rparam + rprcnt;
811 res = GETRES(rparam,endp);
813 if (res == 0) {
814 /* nothing to do */
815 } else if ((res == 5) || (res == 65)) {
816 DEBUG(1, ("Access Denied\n"));
817 } else if (res == 2221) {
818 DEBUG (1, ("User does not exist\n"));
819 } else {
820 DEBUG(4,("NetUserDelete res=%d\n", res));
822 } else {
823 res = -1;
824 DEBUG(4,("NetUserDelete failed\n"));
827 SAFE_FREE(rparam);
828 SAFE_FREE(rdata);
830 return res;
833 /****************************************************************************
834 Call a NetUserAdd - add user to remote server.
835 ****************************************************************************/
837 int cli_NetUserAdd(struct cli_state *cli, struct rap_user_info_1 * userinfo )
839 char *rparam = NULL;
840 char *rdata = NULL;
841 char *p;
842 unsigned int rdrcnt,rprcnt;
843 int res = -1;
844 char param[WORDSIZE /* api number */
845 +sizeof(RAP_NetUserAdd2_REQ) /* req string */
846 +sizeof(RAP_USER_INFO_L1) /* data string */
847 +WORDSIZE /* info level */
848 +WORDSIZE /* buffer length */
849 +WORDSIZE]; /* reserved */
851 char data[1024];
852 /* offset into data of free format strings. Will be updated */
853 /* by PUTSTRINGP macro and end up with total data length. */
854 int soffset=RAP_USERNAME_LEN+1 /* user name + pad */
855 + RAP_UPASSWD_LEN /* password */
856 + DWORDSIZE /* password age */
857 + WORDSIZE /* privilege */
858 + DWORDSIZE /* home dir ptr */
859 + DWORDSIZE /* comment ptr */
860 + WORDSIZE /* flags */
861 + DWORDSIZE; /* login script ptr*/
863 /* now send a SMBtrans command with api NetUserAdd */
864 p = make_header(param, RAP_WUserAdd2,
865 RAP_NetUserAdd2_REQ, RAP_USER_INFO_L1);
867 PUTWORD(p, 1); /* info level */
868 PUTWORD(p, 0); /* pwencrypt */
869 if(userinfo->passwrd)
870 PUTWORD(p,MIN(strlen((const char *)userinfo->passwrd), RAP_UPASSWD_LEN));
871 else
872 PUTWORD(p, 0); /* password length */
874 p = data;
875 memset(data, '\0', soffset);
877 PUTSTRINGF(p, (const char *)userinfo->user_name, RAP_USERNAME_LEN);
878 PUTBYTE(p, 0); /* pad byte 0 */
879 PUTSTRINGF(p, (const char *)userinfo->passwrd, RAP_UPASSWD_LEN);
880 PUTDWORD(p, 0); /* pw age - n.a. on user add */
881 PUTWORD(p, userinfo->priv);
882 PUTSTRINGP(p, userinfo->home_dir, data, soffset);
883 PUTSTRINGP(p, userinfo->comment, data, soffset);
884 PUTWORD(p, userinfo->userflags);
885 PUTSTRINGP(p, userinfo->logon_script, data, soffset);
887 if (cli_api(cli,
888 param, sizeof(param), 1024, /* Param, length, maxlen */
889 data, soffset, sizeof(data), /* data, length, maxlen */
890 &rparam, &rprcnt, /* return params, length */
891 &rdata, &rdrcnt)) /* return data, length */
893 char *endp = rparam + rprcnt;
894 res = GETRES(rparam, endp);
896 if (res == 0) {
897 /* nothing to do */
898 } else if ((res == 5) || (res == 65)) {
899 DEBUG(1, ("Access Denied\n"));
900 } else if (res == 2224) {
901 DEBUG (1, ("User already exists\n"));
902 } else {
903 DEBUG(4,("NetUserAdd res=%d\n", res));
905 } else {
906 res = -1;
907 DEBUG(4,("NetUserAdd failed\n"));
910 SAFE_FREE(rparam);
911 SAFE_FREE(rdata);
913 return res;
916 /****************************************************************************
917 call a NetUserEnum - try and list users on a different host
918 ****************************************************************************/
920 int cli_RNetUserEnum(struct cli_state *cli, void (*fn)(const char *, const char *, const char *, const char *, void *), void *state)
922 char param[WORDSIZE /* api number */
923 +sizeof(RAP_NetUserEnum_REQ) /* parm string */
924 +sizeof(RAP_USER_INFO_L1) /* return string */
925 +WORDSIZE /* info level */
926 +WORDSIZE]; /* buffer size */
927 char *p;
928 char *rparam = NULL;
929 char *rdata = NULL;
930 unsigned int rprcnt, rdrcnt;
931 int res = -1;
933 memset(param, '\0', sizeof(param));
934 p = make_header(param, RAP_WUserEnum,
935 RAP_NetUserEnum_REQ, RAP_USER_INFO_L1);
936 PUTWORD(p,1); /* Info level 1 */
937 PUTWORD(p,0xFF00); /* Return buffer size */
939 /* BB Fix handling of large numbers of users to be returned */
940 if (cli_api(cli,
941 param, PTR_DIFF(p,param),8,
942 NULL, 0, CLI_BUFFER_SIZE,
943 &rparam, &rprcnt,
944 &rdata, &rdrcnt)) {
945 char *endp = rparam + rprcnt;
946 res = GETRES(rparam,endp);
947 cli->rap_error = res;
948 if (cli->rap_error != 0) {
949 DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
953 if (!rdata) {
954 DEBUG(4,("NetUserEnum no data returned\n"));
955 goto out;
958 if (res == 0 || res == ERRmoredata) {
959 int i, converter = 0, count = 0;
960 char username[RAP_USERNAME_LEN];
961 char userpw[RAP_UPASSWD_LEN];
962 char *endp = rparam + rprcnt;
963 char *comment, *homedir, *logonscript;
964 TALLOC_CTX *frame = talloc_stackframe();
966 p = rparam + WORDSIZE; /* skip result */
967 GETWORD(p, converter, endp);
968 GETWORD(p, count, endp);
970 endp = rdata + rdrcnt;
971 for (i=0,p=rdata;i<count && p < endp;i++) {
972 p += rap_getstringf(p,
973 username,
974 RAP_USERNAME_LEN,
975 RAP_USERNAME_LEN,
976 endp);
977 p++; /* pad byte */
978 p += rap_getstringf(p,
979 userpw,
980 RAP_UPASSWD_LEN,
981 RAP_UPASSWD_LEN,
982 endp);
983 p += DWORDSIZE; /* skip password age */
984 p += WORDSIZE; /* skip priv: 0=guest, 1=user, 2=admin */
985 p += rap_getstringp(frame,
987 &homedir,
988 rdata,
989 converter,
990 endp);
991 p += rap_getstringp(frame,
993 &comment,
994 rdata,
995 converter,
996 endp);
997 p += WORDSIZE; /* skip flags */
998 p += rap_getstringp(frame,
1000 &logonscript,
1001 rdata,
1002 converter,
1003 endp);
1004 if (username[0] && comment &&
1005 homedir && logonscript) {
1006 fn(username,
1007 comment,
1008 homedir,
1009 logonscript,
1010 cli);
1013 TALLOC_FREE(frame);
1014 } else {
1015 DEBUG(4,("NetUserEnum res=%d\n", res));
1018 out:
1020 SAFE_FREE(rparam);
1021 SAFE_FREE(rdata);
1023 return res;
1026 int cli_RNetUserEnum0(struct cli_state *cli,
1027 void (*fn)(const char *, void *),
1028 void *state)
1030 char param[WORDSIZE /* api number */
1031 +sizeof(RAP_NetUserEnum_REQ) /* parm string */
1032 +sizeof(RAP_USER_INFO_L0) /* return string */
1033 +WORDSIZE /* info level */
1034 +WORDSIZE]; /* buffer size */
1035 char *p;
1036 char *rparam = NULL;
1037 char *rdata = NULL;
1038 unsigned int rprcnt, rdrcnt;
1039 int res = -1;
1041 memset(param, '\0', sizeof(param));
1042 p = make_header(param, RAP_WUserEnum,
1043 RAP_NetUserEnum_REQ, RAP_USER_INFO_L0);
1044 PUTWORD(p,0); /* Info level 1 */
1045 PUTWORD(p,0xFF00); /* Return buffer size */
1047 /* BB Fix handling of large numbers of users to be returned */
1048 if (cli_api(cli,
1049 param, PTR_DIFF(p,param),8,
1050 NULL, 0, CLI_BUFFER_SIZE,
1051 &rparam, &rprcnt,
1052 &rdata, &rdrcnt)) {
1053 char *endp = rparam + rprcnt;
1054 res = GETRES(rparam,endp);
1055 cli->rap_error = res;
1056 if (cli->rap_error != 0) {
1057 DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
1061 if (!rdata) {
1062 DEBUG(4,("NetUserEnum no data returned\n"));
1063 goto out;
1066 if (res == 0 || res == ERRmoredata) {
1067 int i, count = 0;
1068 char *endp = rparam + rprcnt;
1069 char username[RAP_USERNAME_LEN];
1071 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1072 GETWORD(p, count, endp);
1074 endp = rdata + rdrcnt;
1075 for (i=0,p=rdata;i<count && p < endp;i++) {
1076 p += rap_getstringf(p,
1077 username,
1078 RAP_USERNAME_LEN,
1079 RAP_USERNAME_LEN,
1080 endp);
1081 if (username[0]) {
1082 fn(username, cli);
1085 } else {
1086 DEBUG(4,("NetUserEnum res=%d\n", res));
1089 out:
1091 SAFE_FREE(rparam);
1092 SAFE_FREE(rdata);
1094 return res;
1097 /****************************************************************************
1098 Call a NetFileClose2 - close open file on another session to server.
1099 ****************************************************************************/
1101 int cli_NetFileClose(struct cli_state *cli, uint32 file_id )
1103 char *rparam = NULL;
1104 char *rdata = NULL;
1105 char *p;
1106 unsigned int rdrcnt,rprcnt;
1107 char param[WORDSIZE /* api number */
1108 +sizeof(RAP_WFileClose2_REQ) /* req string */
1109 +1 /* no ret string */
1110 +DWORDSIZE]; /* file ID */
1111 int res = -1;
1113 /* now send a SMBtrans command with api RNetShareEnum */
1114 p = make_header(param, RAP_WFileClose2, RAP_WFileClose2_REQ, NULL);
1115 PUTDWORD(p, file_id);
1117 if (cli_api(cli,
1118 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1119 NULL, 0, 200, /* data, length, maxlen */
1120 &rparam, &rprcnt, /* return params, length */
1121 &rdata, &rdrcnt)) /* return data, length */
1123 char *endp = rparam + rprcnt;
1124 res = GETRES(rparam, endp);
1126 if (res == 0) {
1127 /* nothing to do */
1128 } else if (res == 2314){
1129 DEBUG(1, ("NetFileClose2 - attempt to close non-existant file open instance\n"));
1130 } else {
1131 DEBUG(4,("NetFileClose2 res=%d\n", res));
1133 } else {
1134 res = -1;
1135 DEBUG(4,("NetFileClose2 failed\n"));
1138 SAFE_FREE(rparam);
1139 SAFE_FREE(rdata);
1141 return res;
1144 /****************************************************************************
1145 Call a NetFileGetInfo - get information about server file opened from other
1146 workstation.
1147 ****************************************************************************/
1149 int cli_NetFileGetInfo(struct cli_state *cli, uint32 file_id, void (*fn)(const char *, const char *, uint16, uint16, uint32))
1151 char *rparam = NULL;
1152 char *rdata = NULL;
1153 char *p;
1154 unsigned int rdrcnt,rprcnt;
1155 int res = -1;
1156 char param[WORDSIZE /* api number */
1157 +sizeof(RAP_WFileGetInfo2_REQ) /* req string */
1158 +sizeof(RAP_FILE_INFO_L3) /* return string */
1159 +DWORDSIZE /* file ID */
1160 +WORDSIZE /* info level */
1161 +WORDSIZE]; /* buffer size */
1163 /* now send a SMBtrans command with api RNetShareEnum */
1164 p = make_header(param, RAP_WFileGetInfo2,
1165 RAP_WFileGetInfo2_REQ, RAP_FILE_INFO_L3);
1166 PUTDWORD(p, file_id);
1167 PUTWORD(p, 3); /* info level */
1168 PUTWORD(p, 0x1000); /* buffer size */
1169 if (cli_api(cli,
1170 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1171 NULL, 0, 0x1000, /* data, length, maxlen */
1172 &rparam, &rprcnt, /* return params, length */
1173 &rdata, &rdrcnt)) /* return data, length */
1175 char *endp = rparam + rprcnt;
1176 res = GETRES(rparam,endp);
1177 if (res == 0 || res == ERRmoredata) {
1178 TALLOC_CTX *frame = talloc_stackframe();
1179 int converter = 0,id = 0, perms = 0, locks = 0;
1180 char *fpath, *fuser;
1182 p = rparam + WORDSIZE; /* skip result */
1183 GETWORD(p, converter, endp);
1185 p = rdata;
1186 endp = rdata + rdrcnt;
1188 GETDWORD(p, id, endp);
1189 GETWORD(p, perms, endp);
1190 GETWORD(p, locks, endp);
1192 p += rap_getstringp(frame,
1194 &fpath,
1195 rdata,
1196 converter,
1197 endp);
1198 p += rap_getstringp(frame,
1200 &fuser,
1201 rdata,
1202 converter,
1203 endp);
1205 if (fpath && fuser) {
1206 fn(fpath, fuser, perms, locks, id);
1209 TALLOC_FREE(frame);
1210 } else {
1211 DEBUG(4,("NetFileGetInfo2 res=%d\n", res));
1213 } else {
1214 res = -1;
1215 DEBUG(4,("NetFileGetInfo2 failed\n"));
1218 SAFE_FREE(rparam);
1219 SAFE_FREE(rdata);
1221 return res;
1224 /****************************************************************************
1225 * Call a NetFileEnum2 - list open files on an SMB server
1227 * PURPOSE: Remotes a NetFileEnum API call to the current server or target
1228 * server listing the files open via the network (and their
1229 * corresponding open instance ids)
1231 * Dependencies: none
1233 * Parameters:
1234 * cli - pointer to cli_state structure
1235 * user - if present, return only files opened by this remote user
1236 * base_path - if present, return only files opened below this
1237 * base path
1238 * fn - display function to invoke for each entry in the result
1241 * Returns:
1242 * True - success
1243 * False - failure
1245 ****************************************************************************/
1247 int cli_NetFileEnum(struct cli_state *cli, const char * user,
1248 const char * base_path,
1249 void (*fn)(const char *, const char *, uint16, uint16,
1250 uint32))
1252 char *rparam = NULL;
1253 char *rdata = NULL;
1254 char *p;
1255 unsigned int rdrcnt,rprcnt;
1256 char param[WORDSIZE /* api number */
1257 +sizeof(RAP_WFileEnum2_REQ) /* req string */
1258 +sizeof(RAP_FILE_INFO_L3) /* return string */
1259 +1024 /* base path (opt) */
1260 +RAP_USERNAME_LEN /* user name (opt) */
1261 +WORDSIZE /* info level */
1262 +WORDSIZE /* buffer size */
1263 +DWORDSIZE /* resume key ? */
1264 +DWORDSIZE]; /* resume key ? */
1265 int count = -1;
1266 int res = -1;
1268 /* now send a SMBtrans command with api RNetShareEnum */
1269 p = make_header(param, RAP_WFileEnum2,
1270 RAP_WFileEnum2_REQ, RAP_FILE_INFO_L3);
1272 PUTSTRING(p, base_path, 1024);
1273 PUTSTRING(p, user, RAP_USERNAME_LEN);
1274 PUTWORD(p, 3); /* info level */
1275 PUTWORD(p, 0xFF00); /* buffer size */
1276 PUTDWORD(p, 0); /* zero out the resume key */
1277 PUTDWORD(p, 0); /* or is this one the resume key? */
1279 if (cli_api(cli,
1280 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1281 NULL, 0, 0xFF00, /* data, length, maxlen */
1282 &rparam, &rprcnt, /* return params, length */
1283 &rdata, &rdrcnt)) /* return data, length */
1285 char *endp = rparam + rprcnt;
1286 res = GETRES(rparam, endp);
1288 if (res == 0 || res == ERRmoredata) {
1289 TALLOC_CTX *frame = talloc_stackframe();
1290 int converter = 0, i;
1292 p = rparam + WORDSIZE; /* skip result */
1293 GETWORD(p, converter, endp);
1294 GETWORD(p, count, endp);
1296 p = rdata;
1297 endp = rdata + rdrcnt;
1298 for (i=0; i<count && p < endp; i++) {
1299 int id = 0, perms = 0, locks = 0;
1300 char *fpath, *fuser;
1302 GETDWORD(p, id, endp);
1303 GETWORD(p, perms, endp);
1304 GETWORD(p, locks, endp);
1305 p += rap_getstringp(frame,
1307 &fpath,
1308 rdata,
1309 converter,
1310 endp);
1311 p += rap_getstringp(frame,
1313 &fuser,
1314 rdata,
1315 converter,
1316 endp);
1318 if (fpath && fuser) {
1319 fn(fpath, fuser, perms, locks, id);
1321 } /* BB fix ERRmoredata case to send resume request */
1322 TALLOC_FREE(frame);
1323 } else {
1324 DEBUG(4,("NetFileEnum2 res=%d\n", res));
1326 } else {
1327 DEBUG(4,("NetFileEnum2 failed\n"));
1330 SAFE_FREE(rparam);
1331 SAFE_FREE(rdata);
1333 return count;
1336 /****************************************************************************
1337 Call a NetShareAdd - share/export directory on remote server.
1338 ****************************************************************************/
1340 int cli_NetShareAdd(struct cli_state *cli, struct rap_share_info_2 * sinfo )
1342 char *rparam = NULL;
1343 char *rdata = NULL;
1344 char *p;
1345 unsigned int rdrcnt,rprcnt;
1346 int res = -1;
1347 char param[WORDSIZE /* api number */
1348 +sizeof(RAP_WShareAdd_REQ) /* req string */
1349 +sizeof(RAP_SHARE_INFO_L2) /* return string */
1350 +WORDSIZE /* info level */
1351 +WORDSIZE]; /* reserved word */
1352 char data[1024];
1353 /* offset to free format string section following fixed length data. */
1354 /* will be updated by PUTSTRINGP macro and will end up with total len */
1355 int soffset = RAP_SHARENAME_LEN + 1 /* share name + pad */
1356 + WORDSIZE /* share type */
1357 + DWORDSIZE /* comment pointer */
1358 + WORDSIZE /* permissions */
1359 + WORDSIZE /* max users */
1360 + WORDSIZE /* active users */
1361 + DWORDSIZE /* share path */
1362 + RAP_SPASSWD_LEN + 1; /* share password + pad */
1364 memset(param,'\0',sizeof(param));
1365 /* now send a SMBtrans command with api RNetShareAdd */
1366 p = make_header(param, RAP_WshareAdd,
1367 RAP_WShareAdd_REQ, RAP_SHARE_INFO_L2);
1368 PUTWORD(p, 2); /* info level */
1369 PUTWORD(p, 0); /* reserved word 0 */
1371 p = data;
1372 PUTSTRINGF(p, (const char *)sinfo->share_name, RAP_SHARENAME_LEN);
1373 PUTBYTE(p, 0); /* pad byte 0 */
1375 PUTWORD(p, sinfo->share_type);
1376 PUTSTRINGP(p, sinfo->comment, data, soffset);
1377 PUTWORD(p, sinfo->perms);
1378 PUTWORD(p, sinfo->maximum_users);
1379 PUTWORD(p, sinfo->active_users);
1380 PUTSTRINGP(p, sinfo->path, data, soffset);
1381 PUTSTRINGF(p, (const char *)sinfo->password, RAP_SPASSWD_LEN);
1382 SCVAL(p,-1,0x0A); /* required 0x0A at end of password */
1384 if (cli_api(cli,
1385 param, sizeof(param), 1024, /* Param, length, maxlen */
1386 data, soffset, sizeof(data), /* data, length, maxlen */
1387 &rparam, &rprcnt, /* return params, length */
1388 &rdata, &rdrcnt)) /* return data, length */
1390 char *endp = rparam + rprcnt;
1391 res = GETRES(rparam, endp);
1393 if (res == 0) {
1394 /* nothing to do */
1395 } else {
1396 DEBUG(4,("NetShareAdd res=%d\n", res));
1398 } else {
1399 DEBUG(4,("NetShareAdd failed\n"));
1402 SAFE_FREE(rparam);
1403 SAFE_FREE(rdata);
1405 return res;
1408 /****************************************************************************
1409 Call a NetShareDelete - unshare exported directory on remote server.
1410 ****************************************************************************/
1412 int cli_NetShareDelete(struct cli_state *cli, const char * share_name )
1414 char *rparam = NULL;
1415 char *rdata = NULL;
1416 char *p;
1417 unsigned int rdrcnt,rprcnt;
1418 int res = -1;
1419 char param[WORDSIZE /* api number */
1420 +sizeof(RAP_WShareDel_REQ) /* req string */
1421 +1 /* no ret string */
1422 +RAP_SHARENAME_LEN /* share to del */
1423 +WORDSIZE]; /* reserved word */
1425 /* now send a SMBtrans command with api RNetShareDelete */
1426 p = make_header(param, RAP_WshareDel, RAP_WShareDel_REQ, NULL);
1427 PUTSTRING(p,share_name,RAP_SHARENAME_LEN);
1428 PUTWORD(p,0); /* reserved word MBZ on input */
1430 if (cli_api(cli,
1431 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1432 NULL, 0, 200, /* data, length, maxlen */
1433 &rparam, &rprcnt, /* return params, length */
1434 &rdata, &rdrcnt)) /* return data, length */
1436 char *endp = rparam + rprcnt;
1437 res = GETRES(rparam, endp);
1439 if (res == 0) {
1440 /* nothing to do */
1441 } else {
1442 DEBUG(4,("NetShareDelete res=%d\n", res));
1444 } else {
1445 DEBUG(4,("NetShareDelete failed\n"));
1448 SAFE_FREE(rparam);
1449 SAFE_FREE(rdata);
1451 return res;
1454 /*************************************************************************
1456 * Function Name: cli_get_pdc_name
1458 * PURPOSE: Remotes a NetServerEnum API call to the current server
1459 * requesting the name of a server matching the server
1460 * type of SV_TYPE_DOMAIN_CTRL (PDC).
1462 * Dependencies: none
1464 * Parameters:
1465 * cli - pointer to cli_state structure
1466 * workgroup - pointer to string containing name of domain
1467 * pdc_name - pointer to string that will contain PDC name
1468 * on successful return
1470 * Returns:
1471 * True - success
1472 * False - failure
1474 ************************************************************************/
1476 bool cli_get_pdc_name(struct cli_state *cli, const char *workgroup, char **pdc_name)
1478 char *rparam = NULL;
1479 char *rdata = NULL;
1480 unsigned int rdrcnt,rprcnt;
1481 char *p;
1482 char param[WORDSIZE /* api number */
1483 +sizeof(RAP_NetServerEnum2_REQ) /* req string */
1484 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1485 +WORDSIZE /* info level */
1486 +WORDSIZE /* buffer size */
1487 +DWORDSIZE /* server type */
1488 +RAP_MACHNAME_LEN]; /* workgroup */
1489 int count = -1;
1490 int res = -1;
1492 *pdc_name = NULL;
1494 /* send a SMBtrans command with api NetServerEnum */
1495 p = make_header(param, RAP_NetServerEnum2,
1496 RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L1);
1497 PUTWORD(p, 1); /* info level */
1498 PUTWORD(p, CLI_BUFFER_SIZE);
1499 PUTDWORD(p, SV_TYPE_DOMAIN_CTRL);
1500 PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1502 if (cli_api(cli,
1503 param, PTR_DIFF(p,param), 8, /* params, length, max */
1504 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1505 &rparam, &rprcnt, /* return params, return size */
1506 &rdata, &rdrcnt /* return data, return size */
1507 )) {
1509 char *endp = rparam + rprcnt;
1510 res = GETRES(rparam, endp);
1511 cli->rap_error = res;
1514 * We only really care to copy a name if the
1515 * API succeeded and we got back a name.
1517 if (cli->rap_error == 0) {
1518 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1519 GETWORD(p, count, endp);
1520 p = rdata;
1521 endp = rdata + rdrcnt;
1523 if (count > 0) {
1524 TALLOC_CTX *frame = talloc_stackframe();
1525 char *dcname;
1526 p += rap_getstring(frame,
1528 &dcname,
1529 endp);
1530 if (dcname) {
1531 *pdc_name = SMB_STRDUP(dcname);
1533 TALLOC_FREE(frame);
1535 } else {
1536 DEBUG(4,("cli_get_pdc_name: machine %s failed the NetServerEnum call. "
1537 "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1541 SAFE_FREE(rparam);
1542 SAFE_FREE(rdata);
1544 return(count > 0);
1547 /*************************************************************************
1549 * Function Name: cli_get_server_domain
1551 * PURPOSE: Remotes a NetWkstaGetInfo API call to the current server
1552 * requesting wksta_info_10 level information to determine
1553 * the domain the server belongs to. On success, this
1554 * routine sets the server_domain field in the cli_state structure
1555 * to the server's domain name.
1557 * Dependencies: none
1559 * Parameters:
1560 * cli - pointer to cli_state structure
1562 * Returns:
1563 * True - success
1564 * False - failure
1566 * Origins: samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1568 ************************************************************************/
1570 bool cli_get_server_domain(struct cli_state *cli)
1572 char *rparam = NULL;
1573 char *rdata = NULL;
1574 unsigned int rdrcnt,rprcnt;
1575 char *p;
1576 char param[WORDSIZE /* api number */
1577 +sizeof(RAP_WWkstaGetInfo_REQ) /* req string */
1578 +sizeof(RAP_WKSTA_INFO_L10) /* return string */
1579 +WORDSIZE /* info level */
1580 +WORDSIZE]; /* buffer size */
1581 int res = -1;
1583 /* send a SMBtrans command with api NetWkstaGetInfo */
1584 p = make_header(param, RAP_WWkstaGetInfo,
1585 RAP_WWkstaGetInfo_REQ, RAP_WKSTA_INFO_L10);
1586 PUTWORD(p, 10); /* info level */
1587 PUTWORD(p, CLI_BUFFER_SIZE);
1589 if (cli_api(cli, param, PTR_DIFF(p,param), 8, /* params, length, max */
1590 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1591 &rparam, &rprcnt, /* return params, return size */
1592 &rdata, &rdrcnt)) { /* return data, return size */
1593 char *endp = rparam + rprcnt;
1594 res = GETRES(rparam, endp);
1596 if (res == 0) {
1597 TALLOC_CTX *frame = talloc_stackframe();
1598 char *server_domain;
1599 int converter = 0;
1601 p = rparam + WORDSIZE;
1602 GETWORD(p, converter, endp);
1604 p = rdata + DWORDSIZE + DWORDSIZE; /* skip computer & user names */
1605 endp = rdata + rdrcnt;
1606 p += rap_getstringp(frame,
1608 &server_domain,
1609 rdata,
1610 converter,
1611 endp);
1613 if (server_domain) {
1614 fstrcpy(cli->server_domain, server_domain);
1616 TALLOC_FREE(frame);
1620 SAFE_FREE(rparam);
1621 SAFE_FREE(rdata);
1623 return(res == 0);
1626 /*************************************************************************
1628 * Function Name: cli_get_server_type
1630 * PURPOSE: Remotes a NetServerGetInfo API call to the current server
1631 * requesting server_info_1 level information to retrieve
1632 * the server type.
1634 * Dependencies: none
1636 * Parameters:
1637 * cli - pointer to cli_state structure
1638 * pstype - pointer to uint32 to contain returned server type
1640 * Returns:
1641 * True - success
1642 * False - failure
1644 * Origins: samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1646 ************************************************************************/
1648 bool cli_get_server_type(struct cli_state *cli, uint32 *pstype)
1650 char *rparam = NULL;
1651 char *rdata = NULL;
1652 unsigned int rdrcnt,rprcnt;
1653 char *p;
1654 char param[WORDSIZE /* api number */
1655 +sizeof(RAP_WserverGetInfo_REQ) /* req string */
1656 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1657 +WORDSIZE /* info level */
1658 +WORDSIZE]; /* buffer size */
1659 int res = -1;
1661 /* send a SMBtrans command with api NetServerGetInfo */
1662 p = make_header(param, RAP_WserverGetInfo,
1663 RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1664 PUTWORD(p, 1); /* info level */
1665 PUTWORD(p, CLI_BUFFER_SIZE);
1667 if (cli_api(cli,
1668 param, PTR_DIFF(p,param), 8, /* params, length, max */
1669 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1670 &rparam, &rprcnt, /* return params, return size */
1671 &rdata, &rdrcnt /* return data, return size */
1672 )) {
1673 char *endp = rparam + rprcnt;
1674 res = GETRES(rparam,endp);
1676 if (res == 0 || res == ERRmoredata) {
1677 p = rdata;
1678 endp = rparam + rprcnt;
1679 p += 18;
1680 GETDWORD(p,*pstype,endp);
1681 *pstype &= ~SV_TYPE_LOCAL_LIST_ONLY;
1685 SAFE_FREE(rparam);
1686 SAFE_FREE(rdata);
1688 return(res == 0 || res == ERRmoredata);
1691 bool cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
1692 char **servername)
1694 char *rparam = NULL;
1695 char *rdata = NULL;
1696 unsigned int rdrcnt,rprcnt;
1697 char *p;
1698 char param[WORDSIZE /* api number */
1699 +sizeof(RAP_WserverGetInfo_REQ) /* req string */
1700 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1701 +WORDSIZE /* info level */
1702 +WORDSIZE]; /* buffer size */
1703 bool res = false;
1704 char *endp;
1705 fstring tmp;
1707 /* send a SMBtrans command with api NetServerGetInfo */
1708 p = make_header(param, RAP_WserverGetInfo,
1709 RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1710 PUTWORD(p, 1); /* info level */
1711 PUTWORD(p, CLI_BUFFER_SIZE);
1713 if (!cli_api(cli,
1714 param, PTR_DIFF(p,param), 8, /* params, length, max */
1715 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1716 &rparam, &rprcnt, /* return params, return size */
1717 &rdata, &rdrcnt /* return data, return size */
1718 )) {
1719 goto failed;
1722 endp = rparam + rprcnt;
1723 if (GETRES(rparam, endp) != 0) {
1724 goto failed;
1727 if (rdrcnt < 16) {
1728 DEBUG(10, ("invalid data count %d, expected >= 16\n", rdrcnt));
1729 goto failed;
1732 if (pull_ascii(tmp, rdata, sizeof(tmp)-1, 16, STR_TERMINATE) == -1) {
1733 DEBUG(10, ("pull_ascii failed\n"));
1734 goto failed;
1737 if (!(*servername = talloc_strdup(mem_ctx, tmp))) {
1738 DEBUG(1, ("talloc_strdup failed\n"));
1739 goto failed;
1742 res = true;
1744 failed:
1745 SAFE_FREE(rparam);
1746 SAFE_FREE(rdata);
1747 return res;
1750 /*************************************************************************
1752 * Function Name: cli_ns_check_server_type
1754 * PURPOSE: Remotes a NetServerEnum2 API call to the current server
1755 * requesting server_info_0 level information of machines
1756 * matching the given server type. If the returned server
1757 * list contains the machine name contained in cli->desthost
1758 * then we conclude the server type checks out. This routine
1759 * is useful to retrieve list of server's of a certain
1760 * type when all you have is a null session connection and
1761 * can't remote API calls such as NetWkstaGetInfo or
1762 * NetServerGetInfo.
1764 * Dependencies: none
1766 * Parameters:
1767 * cli - pointer to cli_state structure
1768 * workgroup - pointer to string containing domain
1769 * stype - server type
1771 * Returns:
1772 * True - success
1773 * False - failure
1775 ************************************************************************/
1777 bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 stype)
1779 char *rparam = NULL;
1780 char *rdata = NULL;
1781 unsigned int rdrcnt,rprcnt;
1782 char *p;
1783 char param[WORDSIZE /* api number */
1784 +sizeof(RAP_NetServerEnum2_REQ) /* req string */
1785 +sizeof(RAP_SERVER_INFO_L0) /* return string */
1786 +WORDSIZE /* info level */
1787 +WORDSIZE /* buffer size */
1788 +DWORDSIZE /* server type */
1789 +RAP_MACHNAME_LEN]; /* workgroup */
1790 bool found_server = false;
1791 int res = -1;
1793 /* send a SMBtrans command with api NetServerEnum */
1794 p = make_header(param, RAP_NetServerEnum2,
1795 RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L0);
1796 PUTWORD(p, 0); /* info level 0 */
1797 PUTWORD(p, CLI_BUFFER_SIZE);
1798 PUTDWORD(p, stype);
1799 PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1801 if (cli_api(cli,
1802 param, PTR_DIFF(p,param), 8, /* params, length, max */
1803 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1804 &rparam, &rprcnt, /* return params, return size */
1805 &rdata, &rdrcnt /* return data, return size */
1806 )) {
1807 char *endp = rparam + rprcnt;
1808 res = GETRES(rparam,endp);
1809 cli->rap_error = res;
1811 if (res == 0 || res == ERRmoredata) {
1812 int i, count = 0;
1814 p = rparam + WORDSIZE + WORDSIZE;
1815 GETWORD(p, count,endp);
1817 p = rdata;
1818 endp = rdata + rdrcnt;
1819 for (i = 0;i < count && p < endp;i++, p += 16) {
1820 char ret_server[RAP_MACHNAME_LEN];
1822 p += rap_getstringf(p,
1823 ret_server,
1824 RAP_MACHNAME_LEN,
1825 RAP_MACHNAME_LEN,
1826 endp);
1827 if (strequal(ret_server, cli->desthost)) {
1828 found_server = true;
1829 break;
1832 } else {
1833 DEBUG(4,("cli_ns_check_server_type: machine %s failed the NetServerEnum call. "
1834 "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1838 SAFE_FREE(rparam);
1839 SAFE_FREE(rdata);
1841 return found_server;
1844 /****************************************************************************
1845 Perform a NetWkstaUserLogoff.
1846 ****************************************************************************/
1848 bool cli_NetWkstaUserLogoff(struct cli_state *cli, const char *user, const char *workstation)
1850 char *rparam = NULL;
1851 char *rdata = NULL;
1852 char *p;
1853 unsigned int rdrcnt,rprcnt;
1854 char param[WORDSIZE /* api number */
1855 +sizeof(RAP_NetWkstaUserLogoff_REQ) /* req string */
1856 +sizeof(RAP_USER_LOGOFF_INFO_L1) /* return string */
1857 +RAP_USERNAME_LEN+1 /* user name+pad */
1858 +RAP_MACHNAME_LEN /* wksta name */
1859 +WORDSIZE /* buffer size */
1860 +WORDSIZE]; /* buffer size? */
1861 char upperbuf[MAX(RAP_USERNAME_LEN,RAP_MACHNAME_LEN)];
1862 int res = -1;
1863 char *tmp = NULL;
1865 memset(param, 0, sizeof(param));
1867 /* send a SMBtrans command with api NetWkstaUserLogoff */
1868 p = make_header(param, RAP_WWkstaUserLogoff,
1869 RAP_NetWkstaUserLogoff_REQ, RAP_USER_LOGOFF_INFO_L1);
1870 PUTDWORD(p, 0); /* Null pointer */
1871 PUTDWORD(p, 0); /* Null pointer */
1872 strlcpy(upperbuf, user, sizeof(upperbuf));
1873 strupper_m(upperbuf);
1874 tmp = upperbuf;
1875 PUTSTRINGF(p, tmp, RAP_USERNAME_LEN);
1876 p++; /* strange format, but ok */
1877 strlcpy(upperbuf, workstation, sizeof(upperbuf));
1878 strupper_m(upperbuf);
1879 tmp = upperbuf;
1880 PUTSTRINGF(p, tmp, RAP_MACHNAME_LEN);
1881 PUTWORD(p, CLI_BUFFER_SIZE);
1882 PUTWORD(p, CLI_BUFFER_SIZE);
1884 if (cli_api(cli,
1885 param, PTR_DIFF(p,param),1024, /* param, length, max */
1886 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1887 &rparam, &rprcnt, /* return params, return size */
1888 &rdata, &rdrcnt /* return data, return size */
1889 )) {
1890 char *endp = rparam + rprcnt;
1891 res = GETRES(rparam,endp);
1892 cli->rap_error = res;
1894 if (cli->rap_error != 0) {
1895 DEBUG(4,("NetwkstaUserLogoff gave error %d\n", cli->rap_error));
1899 SAFE_FREE(rparam);
1900 SAFE_FREE(rdata);
1901 return (cli->rap_error == 0);
1904 int cli_NetPrintQEnum(struct cli_state *cli,
1905 void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
1906 void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,unsigned int,unsigned int,const char*))
1908 char param[WORDSIZE /* api number */
1909 +sizeof(RAP_NetPrintQEnum_REQ) /* req string */
1910 +sizeof(RAP_PRINTQ_INFO_L2) /* return string */
1911 +WORDSIZE /* info level */
1912 +WORDSIZE /* buffer size */
1913 +sizeof(RAP_SMB_PRINT_JOB_L1)]; /* more ret data */
1914 char *p;
1915 char *rparam = NULL;
1916 char *rdata = NULL;
1917 unsigned int rprcnt, rdrcnt;
1918 int res = -1;
1920 memset(param, '\0',sizeof(param));
1921 p = make_header(param, RAP_WPrintQEnum,
1922 RAP_NetPrintQEnum_REQ, RAP_PRINTQ_INFO_L2);
1923 PUTWORD(p,2); /* Info level 2 */
1924 PUTWORD(p,0xFFE0); /* Return buffer size */
1925 PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1927 if (cli_api(cli,
1928 param, PTR_DIFF(p,param),1024,
1929 NULL, 0, CLI_BUFFER_SIZE,
1930 &rparam, &rprcnt,
1931 &rdata, &rdrcnt)) {
1932 char *endp = rparam + rprcnt;
1933 res = GETRES(rparam, endp);
1934 cli->rap_error = res;
1935 if (res != 0) {
1936 DEBUG(1,("NetPrintQEnum gave error %d\n", res));
1940 if (!rdata) {
1941 DEBUG(4,("NetPrintQEnum no data returned\n"));
1942 goto out;
1945 if (res == 0 || res == ERRmoredata) {
1946 TALLOC_CTX *frame = talloc_stackframe();
1947 char *endp = rparam + rprcnt;
1948 int i, converter = 0, count = 0;
1950 p = rparam + WORDSIZE;
1951 GETWORD(p, converter, endp);
1952 GETWORD(p, count, endp);
1954 p = rdata;
1955 endp = rdata + rdrcnt;
1956 for (i=0;i<count && p < endp;i++) {
1957 char qname[RAP_SHARENAME_LEN];
1958 char *sep_file, *print_proc, *dest, *parms, *comment;
1959 uint16_t jobcount = 0, priority = 0;
1960 uint16_t start_time = 0, until_time = 0, status = 0;
1962 p += rap_getstringf(p,
1963 qname,
1964 RAP_SHARENAME_LEN,
1965 RAP_SHARENAME_LEN,
1966 endp);
1967 p++; /* pad */
1968 GETWORD(p, priority, endp);
1969 GETWORD(p, start_time, endp);
1970 GETWORD(p, until_time, endp);
1971 p += rap_getstringp(frame,
1973 &sep_file,
1974 rdata,
1975 converter,
1976 endp);
1977 p += rap_getstringp(frame,
1979 &print_proc,
1980 rdata,
1981 converter,
1982 endp);
1983 p += rap_getstringp(frame,
1985 &dest,
1986 rdata,
1987 converter,
1988 endp);
1989 p += rap_getstringp(frame,
1991 &parms,
1992 rdata,
1993 converter,
1994 endp);
1995 p += rap_getstringp(frame,
1997 &comment,
1998 rdata,
1999 converter,
2000 endp);
2001 GETWORD(p, status, endp);
2002 GETWORD(p, jobcount, endp);
2004 if (sep_file && print_proc && dest && parms &&
2005 comment) {
2006 qfn(qname, priority, start_time, until_time, sep_file, print_proc,
2007 dest, parms, comment, status, jobcount);
2010 if (jobcount) {
2011 int j;
2012 for (j=0;j<jobcount;j++) {
2013 uint16 jid = 0, pos = 0, fsstatus = 0;
2014 char ownername[RAP_USERNAME_LEN];
2015 char notifyname[RAP_MACHNAME_LEN];
2016 char datatype[RAP_DATATYPE_LEN];
2017 char *jparms, *jstatus, *jcomment;
2018 unsigned int submitted = 0, jsize = 0;
2020 GETWORD(p, jid, endp);
2021 p += rap_getstringf(p,
2022 ownername,
2023 RAP_USERNAME_LEN,
2024 RAP_USERNAME_LEN,
2025 endp);
2026 p++; /* pad byte */
2027 p += rap_getstringf(p,
2028 notifyname,
2029 RAP_MACHNAME_LEN,
2030 RAP_MACHNAME_LEN,
2031 endp);
2032 p += rap_getstringf(p,
2033 datatype,
2034 RAP_DATATYPE_LEN,
2035 RAP_DATATYPE_LEN,
2036 endp);
2037 p += rap_getstringp(frame,
2039 &jparms,
2040 rdata,
2041 converter,
2042 endp);
2043 GETWORD(p, pos, endp);
2044 GETWORD(p, fsstatus, endp);
2045 p += rap_getstringp(frame,
2047 &jstatus,
2048 rdata,
2049 converter,
2050 endp);
2051 GETDWORD(p, submitted, endp);
2052 GETDWORD(p, jsize, endp);
2053 p += rap_getstringp(frame,
2055 &jcomment,
2056 rdata,
2057 converter,
2058 endp);
2060 if (jparms && jstatus && jcomment) {
2061 jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
2062 jstatus, submitted, jsize, jcomment);
2067 TALLOC_FREE(frame);
2068 } else {
2069 DEBUG(4,("NetPrintQEnum res=%d\n", res));
2072 out:
2074 SAFE_FREE(rparam);
2075 SAFE_FREE(rdata);
2077 return res;
2080 int cli_NetPrintQGetInfo(struct cli_state *cli, const char *printer,
2081 void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
2082 void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,unsigned int,unsigned int,const char*))
2084 char param[WORDSIZE /* api number */
2085 +sizeof(RAP_NetPrintQGetInfo_REQ) /* req string */
2086 +sizeof(RAP_PRINTQ_INFO_L2) /* return string */
2087 +RAP_SHARENAME_LEN /* printer name */
2088 +WORDSIZE /* info level */
2089 +WORDSIZE /* buffer size */
2090 +sizeof(RAP_SMB_PRINT_JOB_L1)]; /* more ret data */
2091 char *p;
2092 char *rparam = NULL;
2093 char *rdata = NULL;
2094 unsigned int rprcnt, rdrcnt;
2095 int res = -1;
2097 memset(param, '\0',sizeof(param));
2098 p = make_header(param, RAP_WPrintQGetInfo,
2099 RAP_NetPrintQGetInfo_REQ, RAP_PRINTQ_INFO_L2);
2100 PUTSTRING(p, printer, RAP_SHARENAME_LEN-1);
2101 PUTWORD(p, 2); /* Info level 2 */
2102 PUTWORD(p,0xFFE0); /* Return buffer size */
2103 PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
2105 if (cli_api(cli,
2106 param, PTR_DIFF(p,param),1024,
2107 NULL, 0, CLI_BUFFER_SIZE,
2108 &rparam, &rprcnt,
2109 &rdata, &rdrcnt)) {
2110 char *endp = rparam + rprcnt;
2111 res = GETRES(rparam, endp);
2112 cli->rap_error = res;
2113 if (res != 0) {
2114 DEBUG(1,("NetPrintQGetInfo gave error %d\n", res));
2118 if (!rdata) {
2119 DEBUG(4,("NetPrintQGetInfo no data returned\n"));
2120 goto out;
2123 if (res == 0 || res == ERRmoredata) {
2124 TALLOC_CTX *frame = talloc_stackframe();
2125 char *endp = rparam + rprcnt;
2126 int rsize = 0, converter = 0;
2127 char qname[RAP_SHARENAME_LEN];
2128 char *sep_file, *print_proc, *dest, *parms, *comment;
2129 uint16_t jobcount = 0, priority = 0;
2130 uint16_t start_time = 0, until_time = 0, status = 0;
2132 p = rparam + WORDSIZE;
2133 GETWORD(p, converter, endp);
2134 GETWORD(p, rsize, endp);
2136 p = rdata;
2137 endp = rdata + rdrcnt;
2138 p += rap_getstringf(p,
2139 qname,
2140 RAP_SHARENAME_LEN,
2141 RAP_SHARENAME_LEN,
2142 endp);
2143 p++; /* pad */
2144 GETWORD(p, priority, endp);
2145 GETWORD(p, start_time, endp);
2146 GETWORD(p, until_time, endp);
2147 p += rap_getstringp(frame,
2149 &sep_file,
2150 rdata,
2151 converter,
2152 endp);
2153 p += rap_getstringp(frame,
2155 &print_proc,
2156 rdata,
2157 converter,
2158 endp);
2159 p += rap_getstringp(frame,
2161 &dest,
2162 rdata,
2163 converter,
2164 endp);
2165 p += rap_getstringp(frame,
2167 &parms,
2168 rdata,
2169 converter,
2170 endp);
2171 p += rap_getstringp(frame,
2173 &comment,
2174 rdata,
2175 converter,
2176 endp);
2177 GETWORD(p, status, endp);
2178 GETWORD(p, jobcount, endp);
2180 if (sep_file && print_proc && dest &&
2181 parms && comment) {
2182 qfn(qname, priority, start_time, until_time, sep_file, print_proc,
2183 dest, parms, comment, status, jobcount);
2185 if (jobcount) {
2186 int j;
2187 for (j=0;(j<jobcount)&&(PTR_DIFF(p,rdata)< rsize)&&
2188 p<endp;j++) {
2189 uint16_t jid = 0, pos = 0, fsstatus = 0;
2190 char ownername[RAP_USERNAME_LEN];
2191 char notifyname[RAP_MACHNAME_LEN];
2192 char datatype[RAP_DATATYPE_LEN];
2193 char *jparms, *jstatus, *jcomment;
2194 unsigned int submitted = 0, jsize = 0;
2196 GETWORD(p, jid, endp);
2197 p += rap_getstringf(p,
2198 ownername,
2199 RAP_USERNAME_LEN,
2200 RAP_USERNAME_LEN,
2201 endp);
2202 p++; /* pad byte */
2203 p += rap_getstringf(p,
2204 notifyname,
2205 RAP_MACHNAME_LEN,
2206 RAP_MACHNAME_LEN,
2207 endp);
2208 p += rap_getstringf(p,
2209 datatype,
2210 RAP_DATATYPE_LEN,
2211 RAP_DATATYPE_LEN,
2212 endp);
2213 p += rap_getstringp(frame,
2215 &jparms,
2216 rdata,
2217 converter,
2218 endp);
2219 GETWORD(p, pos,endp);
2220 GETWORD(p, fsstatus,endp);
2221 p += rap_getstringp(frame,
2223 &jstatus,
2224 rdata,
2225 converter,
2226 endp);
2227 GETDWORD(p, submitted,endp);
2228 GETDWORD(p, jsize,endp);
2229 p += rap_getstringp(frame,
2231 &jcomment,
2232 rdata,
2233 converter,
2234 endp);
2236 if (jparms && jstatus && jcomment) {
2237 jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
2238 jstatus, submitted, jsize, jcomment);
2242 TALLOC_FREE(frame);
2243 } else {
2244 DEBUG(4,("NetPrintQGetInfo res=%d\n", res));
2247 out:
2249 SAFE_FREE(rparam);
2250 SAFE_FREE(rdata);
2252 return res;
2255 /****************************************************************************
2256 Call a NetServiceEnum - list running services on a different host.
2257 ****************************************************************************/
2259 int cli_RNetServiceEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
2261 char param[WORDSIZE /* api number */
2262 +sizeof(RAP_NetServiceEnum_REQ) /* parm string */
2263 +sizeof(RAP_SERVICE_INFO_L2) /* return string */
2264 +WORDSIZE /* info level */
2265 +WORDSIZE]; /* buffer size */
2266 char *p;
2267 char *rparam = NULL;
2268 char *rdata = NULL;
2269 unsigned int rprcnt, rdrcnt;
2270 int res = -1;
2272 memset(param, '\0', sizeof(param));
2273 p = make_header(param, RAP_WServiceEnum,
2274 RAP_NetServiceEnum_REQ, RAP_SERVICE_INFO_L2);
2275 PUTWORD(p,2); /* Info level 2 */
2276 PUTWORD(p,0xFFE0); /* Return buffer size */
2278 if (cli_api(cli,
2279 param, PTR_DIFF(p,param),8,
2280 NULL, 0, 0xFFE0 /* data area size */,
2281 &rparam, &rprcnt,
2282 &rdata, &rdrcnt)) {
2283 char *endp = rparam + rprcnt;
2284 res = GETRES(rparam, endp);
2285 cli->rap_error = res;
2286 if(cli->rap_error == 234) {
2287 DEBUG(1,("Not all service names were returned (such as those longer than 15 characters)\n"));
2288 } else if (cli->rap_error != 0) {
2289 DEBUG(1,("NetServiceEnum gave error %d\n", cli->rap_error));
2293 if (!rdata) {
2294 DEBUG(4,("NetServiceEnum no data returned\n"));
2295 goto out;
2298 if (res == 0 || res == ERRmoredata) {
2299 char *endp = rparam + rprcnt;
2300 int i, count = 0;
2302 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
2303 GETWORD(p, count,endp);
2305 endp = rdata + rdrcnt;
2306 for (i=0,p=rdata;i<count && p < endp;i++) {
2307 char comment[RAP_SRVCCMNT_LEN];
2308 char servicename[RAP_SRVCNAME_LEN];
2310 p += rap_getstringf(p,
2311 servicename,
2312 RAP_SRVCNAME_LEN,
2313 RAP_SRVCNAME_LEN,
2314 endp);
2315 p+=8; /* pass status words */
2316 p += rap_getstringf(p,
2317 comment,
2318 RAP_SRVCCMNT_LEN,
2319 RAP_SRVCCMNT_LEN,
2320 endp);
2322 if (servicename[0]) {
2323 fn(servicename, comment, cli); /* BB add status too */
2326 } else {
2327 DEBUG(4,("NetServiceEnum res=%d\n", res));
2330 out:
2332 SAFE_FREE(rparam);
2333 SAFE_FREE(rdata);
2335 return res;
2338 /****************************************************************************
2339 Call a NetSessionEnum - list workstations with sessions to an SMB server.
2340 ****************************************************************************/
2342 int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16, uint16, uint16, unsigned int, unsigned int, unsigned int, char *))
2344 char param[WORDSIZE /* api number */
2345 +sizeof(RAP_NetSessionEnum_REQ) /* parm string */
2346 +sizeof(RAP_SESSION_INFO_L2) /* return string */
2347 +WORDSIZE /* info level */
2348 +WORDSIZE]; /* buffer size */
2349 char *p;
2350 char *rparam = NULL;
2351 char *rdata = NULL;
2352 unsigned int rprcnt, rdrcnt;
2353 int res = -1;
2355 memset(param, '\0', sizeof(param));
2356 p = make_header(param, RAP_WsessionEnum,
2357 RAP_NetSessionEnum_REQ, RAP_SESSION_INFO_L2);
2358 PUTWORD(p,2); /* Info level 2 */
2359 PUTWORD(p,0xFF); /* Return buffer size */
2361 if (cli_api(cli,
2362 param, PTR_DIFF(p,param),8,
2363 NULL, 0, CLI_BUFFER_SIZE,
2364 &rparam, &rprcnt,
2365 &rdata, &rdrcnt)) {
2366 char *endp = rparam + rprcnt;
2367 res = GETRES(rparam, endp);
2368 cli->rap_error = res;
2369 if (res != 0) {
2370 DEBUG(1,("NetSessionEnum gave error %d\n", res));
2374 if (!rdata) {
2375 DEBUG(4,("NetSesssionEnum no data returned\n"));
2376 goto out;
2379 if (res == 0 || res == ERRmoredata) {
2380 TALLOC_CTX *frame = talloc_stackframe();
2381 char *endp = rparam + rprcnt;
2382 int i, converter = 0, count = 0;
2384 p = rparam + WORDSIZE;
2385 GETWORD(p, converter, endp);
2386 GETWORD(p, count, endp);
2388 endp = rdata + rdrcnt;
2389 for (i=0,p=rdata;i<count && p < endp;i++) {
2390 char *wsname, *username, *clitype_name;
2391 uint16_t num_conns = 0, num_opens = 0, num_users = 0;
2392 unsigned int sess_time = 0, idle_time = 0, user_flags = 0;
2394 p += rap_getstringp(frame,
2396 &wsname,
2397 rdata,
2398 converter,
2399 endp);
2400 p += rap_getstringp(frame,
2402 &username,
2403 rdata,
2404 converter,
2405 endp);
2406 GETWORD(p, num_conns, endp);
2407 GETWORD(p, num_opens, endp);
2408 GETWORD(p, num_users, endp);
2409 GETDWORD(p, sess_time, endp);
2410 GETDWORD(p, idle_time, endp);
2411 GETDWORD(p, user_flags, endp);
2412 p += rap_getstringp(frame,
2414 &clitype_name,
2415 rdata,
2416 converter,
2417 endp);
2419 if (wsname && username && clitype_name) {
2420 fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2421 idle_time, user_flags, clitype_name);
2424 TALLOC_FREE(frame);
2425 } else {
2426 DEBUG(4,("NetSessionEnum res=%d\n", res));
2429 out:
2431 SAFE_FREE(rparam);
2432 SAFE_FREE(rdata);
2434 return res;
2437 /****************************************************************************
2438 Call a NetSessionGetInfo - get information about other session to an SMB server.
2439 ****************************************************************************/
2441 int cli_NetSessionGetInfo(struct cli_state *cli, const char *workstation,
2442 void (*fn)(const char *, const char *, uint16, uint16, uint16, unsigned int, unsigned int, unsigned int, const char *))
2444 char param[WORDSIZE /* api number */
2445 +sizeof(RAP_NetSessionGetInfo_REQ) /* req string */
2446 +sizeof(RAP_SESSION_INFO_L2) /* return string */
2447 +RAP_MACHNAME_LEN /* wksta name */
2448 +WORDSIZE /* info level */
2449 +WORDSIZE]; /* buffer size */
2450 char *p;
2451 char *rparam = NULL;
2452 char *rdata = NULL;
2453 unsigned int rprcnt, rdrcnt;
2454 char *endp;
2455 int res = -1;
2457 memset(param, '\0', sizeof(param));
2458 p = make_header(param, RAP_WsessionGetInfo,
2459 RAP_NetSessionGetInfo_REQ, RAP_SESSION_INFO_L2);
2460 PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2461 PUTWORD(p,2); /* Info level 2 */
2462 PUTWORD(p,0xFF); /* Return buffer size */
2464 if (cli_api(cli,
2465 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2466 NULL, 0, CLI_BUFFER_SIZE,
2467 &rparam, &rprcnt,
2468 &rdata, &rdrcnt)) {
2469 endp = rparam + rprcnt;
2470 res = GETRES(rparam, endp);
2471 cli->rap_error = res;
2472 if (cli->rap_error != 0) {
2473 DEBUG(1,("NetSessionGetInfo gave error %d\n", cli->rap_error));
2477 if (!rdata) {
2478 DEBUG(4,("NetSessionGetInfo no data returned\n"));
2479 goto out;
2482 endp = rparam + rprcnt;
2483 res = GETRES(rparam, endp);
2485 if (res == 0 || res == ERRmoredata) {
2486 TALLOC_CTX *frame = talloc_stackframe();
2487 int converter = 0;
2488 char *wsname, *username, *clitype_name;
2489 uint16_t num_conns = 0, num_opens = 0, num_users = 0;
2490 unsigned int sess_time = 0, idle_time = 0, user_flags = 0;
2492 p = rparam + WORDSIZE;
2493 GETWORD(p, converter,endp);
2494 p += WORDSIZE; /* skip rsize */
2496 p = rdata;
2497 endp = rdata + rdrcnt;
2498 p += rap_getstringp(frame,
2500 &wsname,
2501 rdata,
2502 converter,
2503 endp);
2504 p += rap_getstringp(frame,
2506 &username,
2507 rdata,
2508 converter,
2509 endp);
2510 GETWORD(p, num_conns, endp);
2511 GETWORD(p, num_opens, endp);
2512 GETWORD(p, num_users, endp);
2513 GETDWORD(p, sess_time, endp);
2514 GETDWORD(p, idle_time, endp);
2515 GETDWORD(p, user_flags, endp);
2516 p += rap_getstringp(frame,
2518 &clitype_name,
2519 rdata,
2520 converter,
2521 endp);
2523 if (wsname && username && clitype_name) {
2524 fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2525 idle_time, user_flags, clitype_name);
2527 TALLOC_FREE(frame);
2528 } else {
2529 DEBUG(4,("NetSessionGetInfo res=%d\n", res));
2532 out:
2534 SAFE_FREE(rparam);
2535 SAFE_FREE(rdata);
2537 return res;
2540 /****************************************************************************
2541 Call a NetSessionDel - close a session to an SMB server.
2542 ****************************************************************************/
2544 int cli_NetSessionDel(struct cli_state *cli, const char *workstation)
2546 char param[WORDSIZE /* api number */
2547 +sizeof(RAP_NetSessionDel_REQ) /* req string */
2548 +1 /* no return string */
2549 +RAP_MACHNAME_LEN /* workstation name */
2550 +WORDSIZE]; /* reserved (0) */
2551 char *p;
2552 char *rparam = NULL;
2553 char *rdata = NULL;
2554 unsigned int rprcnt, rdrcnt;
2555 int res = -1;
2557 memset(param, '\0', sizeof(param));
2558 p = make_header(param, RAP_WsessionDel, RAP_NetSessionDel_REQ, NULL);
2559 PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2560 PUTWORD(p,0); /* reserved word of 0 */
2562 if (cli_api(cli,
2563 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
2564 NULL, 0, 200, /* data, length, maxlen */
2565 &rparam, &rprcnt, /* return params, length */
2566 &rdata, &rdrcnt)) /* return data, length */
2568 char *endp = rparam + rprcnt;
2569 res = GETRES(rparam, endp);
2570 cli->rap_error = res;
2572 if (res == 0) {
2573 /* nothing to do */
2574 } else {
2575 DEBUG(4,("NetFileClose2 res=%d\n", res));
2577 } else {
2578 res = -1;
2579 DEBUG(4,("NetFileClose2 failed\n"));
2582 SAFE_FREE(rparam);
2583 SAFE_FREE(rdata);
2585 return res;
2588 int cli_NetConnectionEnum(struct cli_state *cli, const char *qualifier,
2589 void (*fn)(uint16_t conid, uint16_t contype,
2590 uint16_t numopens, uint16_t numusers,
2591 uint32_t contime, const char *username,
2592 const char *netname))
2594 char param[WORDSIZE /* api number */
2595 +sizeof(RAP_NetConnectionEnum_REQ) /* req string */
2596 +sizeof(RAP_CONNECTION_INFO_L1) /* return string */
2597 +RAP_MACHNAME_LEN /* wksta name */
2598 +WORDSIZE /* info level */
2599 +WORDSIZE]; /* buffer size */
2600 char *p;
2601 char *rparam = NULL;
2602 char *rdata = NULL;
2603 unsigned int rprcnt, rdrcnt;
2604 int res = -1;
2606 memset(param, '\0', sizeof(param));
2607 p = make_header(param, RAP_WconnectionEnum,
2608 RAP_NetConnectionEnum_REQ, RAP_CONNECTION_INFO_L1);
2609 PUTSTRING(p, qualifier, RAP_MACHNAME_LEN-1);/* Workstation name */
2610 PUTWORD(p,1); /* Info level 1 */
2611 PUTWORD(p,0xFFE0); /* Return buffer size */
2613 if (cli_api(cli,
2614 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2615 NULL, 0, CLI_BUFFER_SIZE,
2616 &rparam, &rprcnt,
2617 &rdata, &rdrcnt)) {
2618 char *endp = rparam + rprcnt;
2619 res = GETRES(rparam, endp);
2620 cli->rap_error = res;
2621 if (res != 0) {
2622 DEBUG(1,("NetConnectionEnum gave error %d\n", res));
2626 if (!rdata) {
2627 DEBUG(4,("NetConnectionEnum no data returned\n"));
2628 goto out;
2631 if (res == 0 || res == ERRmoredata) {
2632 TALLOC_CTX *frame = talloc_stackframe();
2633 char *endp = rparam + rprcnt;
2634 int i, converter = 0, count = 0;
2636 p = rparam + WORDSIZE;
2637 GETWORD(p, converter, endp);
2638 GETWORD(p, count, endp);
2640 endp = rdata + rdrcnt;
2641 for (i=0,p=rdata;i<count && p < endp;i++) {
2642 char *netname, *username;
2643 uint16_t conn_id = 0, conn_type = 0, num_opens = 0, num_users = 0;
2644 unsigned int conn_time = 0;
2646 GETWORD(p,conn_id, endp);
2647 GETWORD(p,conn_type, endp);
2648 GETWORD(p,num_opens, endp);
2649 GETWORD(p,num_users, endp);
2650 GETDWORD(p,conn_time, endp);
2651 p += rap_getstringp(frame,
2653 &username,
2654 rdata,
2655 converter,
2656 endp);
2657 p += rap_getstringp(frame,
2659 &netname,
2660 rdata,
2661 converter,
2662 endp);
2664 if (username && netname) {
2665 fn(conn_id, conn_type, num_opens, num_users, conn_time,
2666 username, netname);
2669 TALLOC_FREE(frame);
2670 } else {
2671 DEBUG(4,("NetConnectionEnum res=%d\n", res));
2674 out:
2676 SAFE_FREE(rdata);
2677 SAFE_FREE(rparam);
2678 return res;