add 'html' to release target so as to include output/devel/*
[Samba.git] / source3 / libsmb / clirap2.c
blob9cc81105762f58f3f9bbd06b86fe0826310935fa
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"
80 #define WORDSIZE 2
81 #define DWORDSIZE 4
83 #define PUTBYTE(p,b) do {SCVAL(p,0,b); p++;} while(0)
85 #define GETBYTE(p,b,endp) \
86 do {\
87 if (p+1 < endp) {\
88 b = CVAL(p,0);\
90 p++;\
91 } while(0)
93 #define PUTWORD(p,w) do {SSVAL(p,0,w); p += WORDSIZE;} while(0)
95 #define GETWORD(p,w,endp) \
96 do {\
97 if (p+WORDSIZE < endp) {\
98 w = SVAL(p,0);\
100 p += WORDSIZE;\
101 } while(0)
103 #define PUTDWORD(p,d) do {SIVAL(p,0,d); p += DWORDSIZE;} while(0)
105 #define GETDWORD(p,d,endp) \
106 do {\
107 if (p+DWORDSIZE < endp) {\
108 d = IVAL(p,0);\
110 p += DWORDSIZE;\
111 } while(0)
113 #define GETRES(p,endp) ((p && p+2 < endp) ? SVAL(p,0) : -1)
115 /* put string s at p with max len n and increment p past string */
116 #define PUTSTRING(p,s,n) \
117 do {\
118 push_ascii(p,s?s:"",n?n:256,STR_TERMINATE);\
119 p = push_skip_string(p);\
120 } while(0)
122 /* put string s and p, using fixed len l, and increment p by l */
123 #define PUTSTRINGF(p,s,l) \
124 do {\
125 push_ascii(p,s?s:"",l,STR_TERMINATE);\
126 p += l;\
127 } while (0)
129 /* put string pointer at p, supplying offset o from rdata r, store */
130 /* dword offset at p, increment p by 4 and o by length of s. This */
131 /* means on the first call, you must calc the offset yourself! */
133 #define PUTSTRINGP(p,s,r,o) \
134 do {\
135 if (s) {\
136 push_ascii(r+o,s,strlen(s)+1,STR_TERMINATE);\
137 PUTDWORD(p,o);\
138 o += strlen(s) + 1;\
139 } else {\
140 PUTDWORD(p,0);\
142 }while(0);
144 /* get asciiz string dest from src, return increment past string */
146 static size_t rap_getstring(TALLOC_CTX *ctx, char *src, char **dest, const char *endp)
148 char *p1;
149 size_t len;
151 *dest = NULL;
152 for (p1 = src, len = 0; *p1 && p1 < endp; len++)
153 p1++;
154 if (!*p1) {
155 len++;
157 pull_string_talloc(ctx,src,0,dest,src,len,STR_ASCII);
158 return len;
161 /* get fixed length l string dest from src, return increment for src */
163 static size_t rap_getstringf(char *src, char *dest, size_t l, size_t dlen, char *endp)
165 char *p1;
166 size_t len;
168 if (dlen) {
169 dest[0] = '\0';
171 for (p1 = src, len = 0; *p1 && p1 < endp; len++) {
172 p1++;
174 if (!*p1) {
175 len++;
177 if (len > l) {
178 len = l;
180 if (len) {
181 pull_ascii(dest,src,len,len,STR_ASCII);
183 return l;
186 /* get string dest from offset (obtained at p) from rdata r - converter c */
187 static size_t rap_getstringp(TALLOC_CTX *ctx, char *p, char **dest, char *r, uint16_t c, char *endp)
189 uint32_t off = 0;
190 const char *src;
191 size_t len=0;
193 *dest = NULL;
194 if (p+4 < endp) {
195 GETDWORD(p,off,endp);
196 off &= 0x0000FFFF; /* mask the obsolete segment number from the offset */
197 off -= c;
199 if (r+off > endp || r+off < r) {
200 src="";
201 len=1;
202 } else {
203 const char *p1;
204 src=r+off;
205 for (p1 = src, len = 0; *p1 && p1 < endp; len++) {
206 p1++;
208 if (!*p1) {
209 len++;
212 pull_string_talloc(ctx,src,0,dest,src,len,STR_ASCII);
213 return len;
216 static char *make_header(char *param, uint16 apinum, const char *reqfmt, const char *datafmt)
218 PUTWORD(param,apinum);
219 if (reqfmt)
220 PUTSTRING(param,reqfmt,0);
221 else
222 *param++ = (char) 0;
224 if (datafmt)
225 PUTSTRING(param,datafmt,0);
226 else
227 *param++ = (char) 0;
229 return param;
232 /****************************************************************************
233 call a NetGroupDelete - delete user group from remote server
234 ****************************************************************************/
236 int cli_NetGroupDelete(struct cli_state *cli, const char *group_name)
238 char *rparam = NULL;
239 char *rdata = NULL;
240 char *p;
241 unsigned int rdrcnt,rprcnt;
242 int res = -1;
243 char param[WORDSIZE /* api number */
244 +sizeof(RAP_NetGroupDel_REQ) /* parm string */
245 +1 /* no ret string */
246 +RAP_GROUPNAME_LEN /* group to del */
247 +WORDSIZE]; /* reserved word */
249 /* now send a SMBtrans command with api GroupDel */
250 p = make_header(param, RAP_WGroupDel, RAP_NetGroupDel_REQ, NULL);
251 PUTSTRING(p, group_name, RAP_GROUPNAME_LEN);
252 PUTWORD(p,0); /* reserved word MBZ on input */
254 if (cli_api(cli,
255 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
256 NULL, 0, 200, /* data, length, maxlen */
257 &rparam, &rprcnt, /* return params, length */
258 &rdata, &rdrcnt)) /* return data, length */
260 char *endp = rparam + rprcnt;
261 res = GETRES(rparam,endp);
263 if (res == 0) {
264 /* nothing to do */
265 } else if ((res == 5) || (res == 65)) {
266 DEBUG(1, ("Access Denied\n"));
267 } else if (res == 2220) {
268 DEBUG (1, ("Group does not exist\n"));
269 } else {
270 DEBUG(4,("NetGroupDelete res=%d\n", res));
272 } else {
273 res = -1;
274 DEBUG(4,("NetGroupDelete failed\n"));
277 SAFE_FREE(rparam);
278 SAFE_FREE(rdata);
280 return res;
283 /****************************************************************************
284 call a NetGroupAdd - add user group to remote server
285 ****************************************************************************/
287 int cli_NetGroupAdd(struct cli_state *cli, RAP_GROUP_INFO_1 *grinfo)
289 char *rparam = NULL;
290 char *rdata = NULL;
291 char *p;
292 unsigned int rdrcnt,rprcnt;
293 int res = -1;
294 char param[WORDSIZE /* api number */
295 +sizeof(RAP_NetGroupAdd_REQ) /* req string */
296 +sizeof(RAP_GROUP_INFO_L1) /* return string */
297 +WORDSIZE /* info level */
298 +WORDSIZE]; /* reserved word */
300 /* offset into data of free format strings. Will be updated */
301 /* by PUTSTRINGP macro and end up with total data length. */
302 int soffset = RAP_GROUPNAME_LEN + 1 + DWORDSIZE;
303 char *data;
304 size_t data_size;
306 /* Allocate data. */
307 data_size = MAX(soffset + strlen(grinfo->comment) + 1, 1024);
309 data = SMB_MALLOC_ARRAY(char, data_size);
310 if (!data) {
311 DEBUG (1, ("Malloc fail\n"));
312 return -1;
315 /* now send a SMBtrans command with api WGroupAdd */
317 p = make_header(param, RAP_WGroupAdd,
318 RAP_NetGroupAdd_REQ, RAP_GROUP_INFO_L1);
319 PUTWORD(p, 1); /* info level */
320 PUTWORD(p, 0); /* reserved word 0 */
322 p = data;
323 PUTSTRINGF(p, grinfo->group_name, RAP_GROUPNAME_LEN);
324 PUTBYTE(p, 0); /* pad byte 0 */
325 PUTSTRINGP(p, grinfo->comment, data, soffset);
327 if (cli_api(cli,
328 param, sizeof(param), 1024, /* Param, length, maxlen */
329 data, soffset, sizeof(data), /* data, length, maxlen */
330 &rparam, &rprcnt, /* return params, length */
331 &rdata, &rdrcnt)) /* return data, length */
333 char *endp = rparam + rprcnt;
334 res = GETRES(rparam, endp);
336 if (res == 0) {
337 /* nothing to do */
338 } else if ((res == 5) || (res == 65)) {
339 DEBUG(1, ("Access Denied\n"));
340 } else if (res == 2223) {
341 DEBUG (1, ("Group already exists\n"));
342 } else {
343 DEBUG(4,("NetGroupAdd res=%d\n", res));
345 } else {
346 res = -1;
347 DEBUG(4,("NetGroupAdd failed\n"));
350 SAFE_FREE(data);
351 SAFE_FREE(rparam);
352 SAFE_FREE(rdata);
354 return res;
357 /****************************************************************************
358 Call a NetGroupEnum - try and list user groups on a different host.
359 ****************************************************************************/
361 int cli_RNetGroupEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
363 char param[WORDSIZE /* api number */
364 +sizeof(RAP_NetGroupEnum_REQ) /* parm string */
365 +sizeof(RAP_GROUP_INFO_L1) /* return string */
366 +WORDSIZE /* info level */
367 +WORDSIZE]; /* buffer size */
368 char *p;
369 char *rparam = NULL;
370 char *rdata = NULL;
371 unsigned int rprcnt, rdrcnt;
372 int res = -1;
374 memset(param, '\0', sizeof(param));
375 p = make_header(param, RAP_WGroupEnum,
376 RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L1);
377 PUTWORD(p,1); /* Info level 1 */ /* add level 0 */
378 PUTWORD(p,0xFFE0); /* Return buffer size */
380 if (cli_api(cli,
381 param, PTR_DIFF(p,param),8,
382 NULL, 0, 0xFFE0 /* data area size */,
383 &rparam, &rprcnt,
384 &rdata, &rdrcnt)) {
385 char *endp = rparam + rdrcnt;
387 res = GETRES(rparam, endp);
388 cli->rap_error = res;
389 if(cli->rap_error == 234) {
390 DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
391 } else if (cli->rap_error != 0) {
392 DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
396 if (!rdata) {
397 DEBUG(4,("NetGroupEnum no data returned\n"));
398 goto out;
401 if (res == 0 || res == ERRmoredata) {
402 char *endp = rparam + rprcnt;
403 int i, converter = 0, count = 0;
404 TALLOC_CTX *frame = talloc_stackframe();
406 p = rparam + WORDSIZE; /* skip result */
407 GETWORD(p, converter, endp);
408 GETWORD(p, count, endp);
410 endp = rdata + rdrcnt;
411 for (i=0,p=rdata; i<count && p < endp;i++) {
412 char *comment = NULL;
413 char groupname[RAP_GROUPNAME_LEN];
415 p += rap_getstringf(p,
416 groupname,
417 RAP_GROUPNAME_LEN,
418 RAP_GROUPNAME_LEN,
419 endp);
420 p++; /* pad byte */
421 p += rap_getstringp(frame,
423 &comment,
424 rdata,
425 converter,
426 endp);
428 if (!comment || !groupname[0]) {
429 break;
432 fn(groupname, comment, cli);
434 TALLOC_FREE(frame);
435 } else {
436 DEBUG(4,("NetGroupEnum res=%d\n", res));
439 out:
441 SAFE_FREE(rparam);
442 SAFE_FREE(rdata);
444 return res;
447 int cli_RNetGroupEnum0(struct cli_state *cli,
448 void (*fn)(const char *, void *),
449 void *state)
451 char param[WORDSIZE /* api number */
452 +sizeof(RAP_NetGroupEnum_REQ) /* parm string */
453 +sizeof(RAP_GROUP_INFO_L0) /* return string */
454 +WORDSIZE /* info level */
455 +WORDSIZE]; /* buffer size */
456 char *p;
457 char *rparam = NULL;
458 char *rdata = NULL;
459 unsigned int rprcnt, rdrcnt;
460 int res = -1;
462 memset(param, '\0', sizeof(param));
463 p = make_header(param, RAP_WGroupEnum,
464 RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L0);
465 PUTWORD(p,0); /* Info level 0 */ /* Hmmm. I *very* much suspect this
466 is the resume count, at least
467 that's what smbd believes... */
468 PUTWORD(p,0xFFE0); /* Return buffer size */
470 if (cli_api(cli,
471 param, PTR_DIFF(p,param),8,
472 NULL, 0, 0xFFE0 /* data area size */,
473 &rparam, &rprcnt,
474 &rdata, &rdrcnt)) {
475 char *endp = rparam+rprcnt;
476 res = GETRES(rparam,endp);
477 cli->rap_error = res;
478 if(cli->rap_error == 234) {
479 DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
480 } else if (cli->rap_error != 0) {
481 DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
485 if (!rdata) {
486 DEBUG(4,("NetGroupEnum no data returned\n"));
487 goto out;
490 if (res == 0 || res == ERRmoredata) {
491 char *endp = rparam + rprcnt;
492 int i, count = 0;
494 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
495 GETWORD(p, count, endp);
497 endp = rdata + rdrcnt;
498 for (i=0,p=rdata; i<count && p < endp;i++) {
499 char groupname[RAP_GROUPNAME_LEN];
501 p += rap_getstringf(p,
502 groupname,
503 RAP_GROUPNAME_LEN,
504 RAP_GROUPNAME_LEN,
505 endp);
506 if (groupname[0]) {
507 fn(groupname, cli);
510 } else {
511 DEBUG(4,("NetGroupEnum res=%d\n", res));
514 out:
516 SAFE_FREE(rparam);
517 SAFE_FREE(rdata);
519 return res;
522 int cli_NetGroupDelUser(struct cli_state * cli, const char *group_name, const char *user_name)
524 char *rparam = NULL;
525 char *rdata = NULL;
526 char *p;
527 unsigned int rdrcnt,rprcnt;
528 int res = -1;
529 char param[WORDSIZE /* api number */
530 +sizeof(RAP_NetGroupDelUser_REQ) /* parm string */
531 +1 /* no ret string */
532 +RAP_GROUPNAME_LEN /* group name */
533 +RAP_USERNAME_LEN]; /* user to del */
535 /* now send a SMBtrans command with api GroupMemberAdd */
536 p = make_header(param, RAP_WGroupDelUser, RAP_NetGroupDelUser_REQ, NULL);
537 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
538 PUTSTRING(p,user_name,RAP_USERNAME_LEN);
540 if (cli_api(cli,
541 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
542 NULL, 0, 200, /* data, length, maxlen */
543 &rparam, &rprcnt, /* return params, length */
544 &rdata, &rdrcnt)) /* return data, length */
546 char *endp = rparam + rprcnt;
547 res = GETRES(rparam,endp);
549 switch(res) {
550 case 0:
551 break;
552 case 5:
553 case 65:
554 DEBUG(1, ("Access Denied\n"));
555 break;
556 case 50:
557 DEBUG(1, ("Not supported by server\n"));
558 break;
559 case 2220:
560 DEBUG(1, ("Group does not exist\n"));
561 break;
562 case 2221:
563 DEBUG(1, ("User does not exist\n"));
564 break;
565 case 2237:
566 DEBUG(1, ("User is not in group\n"));
567 break;
568 default:
569 DEBUG(4,("NetGroupDelUser res=%d\n", res));
571 } else {
572 res = -1;
573 DEBUG(4,("NetGroupDelUser failed\n"));
576 SAFE_FREE(rparam);
577 SAFE_FREE(rdata);
579 return res;
582 int cli_NetGroupAddUser(struct cli_state * cli, const char *group_name, const char *user_name)
584 char *rparam = NULL;
585 char *rdata = NULL;
586 char *p;
587 unsigned int rdrcnt,rprcnt;
588 int res = -1;
589 char param[WORDSIZE /* api number */
590 +sizeof(RAP_NetGroupAddUser_REQ) /* parm string */
591 +1 /* no ret string */
592 +RAP_GROUPNAME_LEN /* group name */
593 +RAP_USERNAME_LEN]; /* user to add */
595 /* now send a SMBtrans command with api GroupMemberAdd */
596 p = make_header(param, RAP_WGroupAddUser, RAP_NetGroupAddUser_REQ, NULL);
597 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
598 PUTSTRING(p,user_name,RAP_USERNAME_LEN);
600 if (cli_api(cli,
601 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
602 NULL, 0, 200, /* data, length, maxlen */
603 &rparam, &rprcnt, /* return params, length */
604 &rdata, &rdrcnt)) /* return data, length */
606 char *endp = rparam + rprcnt;
607 res = GETRES(rparam,endp);
609 switch(res) {
610 case 0:
611 break;
612 case 5:
613 case 65:
614 DEBUG(1, ("Access Denied\n"));
615 break;
616 case 50:
617 DEBUG(1, ("Not supported by server\n"));
618 break;
619 case 2220:
620 DEBUG(1, ("Group does not exist\n"));
621 break;
622 case 2221:
623 DEBUG(1, ("User does not exist\n"));
624 break;
625 default:
626 DEBUG(4,("NetGroupAddUser res=%d\n", res));
628 } else {
629 res = -1;
630 DEBUG(4,("NetGroupAddUser failed\n"));
633 SAFE_FREE(rparam);
634 SAFE_FREE(rdata);
636 return res;
640 int cli_NetGroupGetUsers(struct cli_state * cli, const char *group_name, void (*fn)(const char *, void *), void *state )
642 char *rparam = NULL;
643 char *rdata = NULL;
644 char *p;
645 unsigned int rdrcnt,rprcnt;
646 int res = -1;
647 char param[WORDSIZE /* api number */
648 +sizeof(RAP_NetGroupGetUsers_REQ)/* parm string */
649 +sizeof(RAP_GROUP_USERS_INFO_0) /* return string */
650 +RAP_GROUPNAME_LEN /* group name */
651 +WORDSIZE /* info level */
652 +WORDSIZE]; /* buffer size */
654 /* now send a SMBtrans command with api GroupGetUsers */
655 p = make_header(param, RAP_WGroupGetUsers,
656 RAP_NetGroupGetUsers_REQ, RAP_GROUP_USERS_INFO_0);
657 PUTSTRING(p,group_name,RAP_GROUPNAME_LEN-1);
658 PUTWORD(p,0); /* info level 0 */
659 PUTWORD(p,0xFFE0); /* return buffer size */
661 if (cli_api(cli,
662 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
663 NULL, 0, CLI_BUFFER_SIZE,
664 &rparam, &rprcnt,
665 &rdata, &rdrcnt)) {
666 char *endp = rparam + rprcnt;
667 res = GETRES(rparam,endp);
668 cli->rap_error = res;
669 if (res != 0) {
670 DEBUG(1,("NetGroupGetUsers gave error %d\n", res));
674 if (!rdata) {
675 DEBUG(4,("NetGroupGetUsers no data returned\n"));
676 goto out;
679 if (res == 0 || res == ERRmoredata) {
680 char *endp = rparam + rprcnt;
681 int i, count = 0;
682 char username[RAP_USERNAME_LEN];
684 p = rparam + WORDSIZE + WORDSIZE;
685 GETWORD(p, count, endp);
687 endp = rdata + rdrcnt;
688 for (i=0,p=rdata; i<count && p < endp; i++) {
689 p += rap_getstringf(p,
690 username,
691 RAP_USERNAME_LEN,
692 RAP_USERNAME_LEN,
693 endp);
694 if (username[0]) {
695 fn(username, state);
698 } else {
699 DEBUG(4,("NetGroupGetUsers res=%d\n", res));
702 out:
704 SAFE_FREE(rdata);
705 SAFE_FREE(rparam);
706 return res;
709 int cli_NetUserGetGroups(struct cli_state * cli, const char *user_name, void (*fn)(const char *, void *), void *state )
711 char *rparam = NULL;
712 char *rdata = NULL;
713 char *p;
714 unsigned int rdrcnt,rprcnt;
715 int res = -1;
716 char param[WORDSIZE /* api number */
717 +sizeof(RAP_NetUserGetGroups_REQ)/* parm string */
718 +sizeof(RAP_GROUP_USERS_INFO_0) /* return string */
719 +RAP_USERNAME_LEN /* user name */
720 +WORDSIZE /* info level */
721 +WORDSIZE]; /* buffer size */
723 /* now send a SMBtrans command with api GroupGetUsers */
724 p = make_header(param, RAP_WUserGetGroups,
725 RAP_NetUserGetGroups_REQ, RAP_GROUP_USERS_INFO_0);
726 PUTSTRING(p,user_name,RAP_USERNAME_LEN-1);
727 PUTWORD(p,0); /* info level 0 */
728 PUTWORD(p,0xFFE0); /* return buffer size */
730 if (cli_api(cli,
731 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
732 NULL, 0, CLI_BUFFER_SIZE,
733 &rparam, &rprcnt,
734 &rdata, &rdrcnt)) {
735 char *endp = rparam + rprcnt;
736 res = GETRES(rparam,endp);
737 cli->rap_error = res;
738 if (res != 0) {
739 DEBUG(1,("NetUserGetGroups gave error %d\n", res));
743 if (!rdata) {
744 DEBUG(4,("NetUserGetGroups no data returned\n"));
745 goto out;
748 if (res == 0 || res == ERRmoredata) {
749 char *endp = rparam + rprcnt;
750 int i, count = 0;
751 char groupname[RAP_GROUPNAME_LEN];
753 p = rparam + WORDSIZE + WORDSIZE;
754 GETWORD(p, count, endp);
756 endp = rdata + rdrcnt;
757 for (i=0,p=rdata; i<count && p < endp; i++) {
758 p += rap_getstringf(p,
759 groupname,
760 RAP_GROUPNAME_LEN,
761 RAP_GROUPNAME_LEN,
762 endp);
763 if (groupname[0]) {
764 fn(groupname, state);
767 } else {
768 DEBUG(4,("NetUserGetGroups res=%d\n", res));
771 out:
773 SAFE_FREE(rdata);
774 SAFE_FREE(rparam);
775 return res;
778 /****************************************************************************
779 Call a NetUserDelete - delete user from remote server.
780 ****************************************************************************/
782 int cli_NetUserDelete(struct cli_state *cli, const char * user_name )
784 char *rparam = NULL;
785 char *rdata = NULL;
786 char *p;
787 unsigned int rdrcnt,rprcnt;
788 int res = -1;
789 char param[WORDSIZE /* api number */
790 +sizeof(RAP_NetGroupDel_REQ) /* parm string */
791 +1 /* no ret string */
792 +RAP_USERNAME_LEN /* user to del */
793 +WORDSIZE]; /* reserved word */
795 /* now send a SMBtrans command with api UserDel */
796 p = make_header(param, RAP_WUserDel, RAP_NetGroupDel_REQ, NULL);
797 PUTSTRING(p, user_name, RAP_USERNAME_LEN);
798 PUTWORD(p,0); /* reserved word MBZ on input */
800 if (cli_api(cli,
801 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
802 NULL, 0, 200, /* data, length, maxlen */
803 &rparam, &rprcnt, /* return params, length */
804 &rdata, &rdrcnt)) /* return data, length */
806 char *endp = rparam + rprcnt;
807 res = GETRES(rparam,endp);
809 if (res == 0) {
810 /* nothing to do */
811 } else if ((res == 5) || (res == 65)) {
812 DEBUG(1, ("Access Denied\n"));
813 } else if (res == 2221) {
814 DEBUG (1, ("User does not exist\n"));
815 } else {
816 DEBUG(4,("NetUserDelete res=%d\n", res));
818 } else {
819 res = -1;
820 DEBUG(4,("NetUserDelete failed\n"));
823 SAFE_FREE(rparam);
824 SAFE_FREE(rdata);
826 return res;
829 /****************************************************************************
830 Call a NetUserAdd - add user to remote server.
831 ****************************************************************************/
833 int cli_NetUserAdd(struct cli_state *cli, RAP_USER_INFO_1 * userinfo )
835 char *rparam = NULL;
836 char *rdata = NULL;
837 char *p;
838 unsigned int rdrcnt,rprcnt;
839 int res = -1;
840 char param[WORDSIZE /* api number */
841 +sizeof(RAP_NetUserAdd2_REQ) /* req string */
842 +sizeof(RAP_USER_INFO_L1) /* data string */
843 +WORDSIZE /* info level */
844 +WORDSIZE /* buffer length */
845 +WORDSIZE]; /* reserved */
847 char data[1024];
848 /* offset into data of free format strings. Will be updated */
849 /* by PUTSTRINGP macro and end up with total data length. */
850 int soffset=RAP_USERNAME_LEN+1 /* user name + pad */
851 + RAP_UPASSWD_LEN /* password */
852 + DWORDSIZE /* password age */
853 + WORDSIZE /* privilege */
854 + DWORDSIZE /* home dir ptr */
855 + DWORDSIZE /* comment ptr */
856 + WORDSIZE /* flags */
857 + DWORDSIZE; /* login script ptr*/
859 /* now send a SMBtrans command with api NetUserAdd */
860 p = make_header(param, RAP_WUserAdd2,
861 RAP_NetUserAdd2_REQ, RAP_USER_INFO_L1);
863 PUTWORD(p, 1); /* info level */
864 PUTWORD(p, 0); /* pwencrypt */
865 if(userinfo->passwrd)
866 PUTWORD(p,MIN(strlen(userinfo->passwrd), RAP_UPASSWD_LEN));
867 else
868 PUTWORD(p, 0); /* password length */
870 p = data;
871 memset(data, '\0', soffset);
873 PUTSTRINGF(p, userinfo->user_name, RAP_USERNAME_LEN);
874 PUTBYTE(p, 0); /* pad byte 0 */
875 PUTSTRINGF(p, userinfo->passwrd, RAP_UPASSWD_LEN);
876 PUTDWORD(p, 0); /* pw age - n.a. on user add */
877 PUTWORD(p, userinfo->priv);
878 PUTSTRINGP(p, userinfo->home_dir, data, soffset);
879 PUTSTRINGP(p, userinfo->comment, data, soffset);
880 PUTWORD(p, userinfo->userflags);
881 PUTSTRINGP(p, userinfo->logon_script, data, soffset);
883 if (cli_api(cli,
884 param, sizeof(param), 1024, /* Param, length, maxlen */
885 data, soffset, sizeof(data), /* data, length, maxlen */
886 &rparam, &rprcnt, /* return params, length */
887 &rdata, &rdrcnt)) /* return data, length */
889 char *endp = rparam + rprcnt;
890 res = GETRES(rparam, endp);
892 if (res == 0) {
893 /* nothing to do */
894 } else if ((res == 5) || (res == 65)) {
895 DEBUG(1, ("Access Denied\n"));
896 } else if (res == 2224) {
897 DEBUG (1, ("User already exists\n"));
898 } else {
899 DEBUG(4,("NetUserAdd res=%d\n", res));
901 } else {
902 res = -1;
903 DEBUG(4,("NetUserAdd failed\n"));
906 SAFE_FREE(rparam);
907 SAFE_FREE(rdata);
909 return res;
912 /****************************************************************************
913 call a NetUserEnum - try and list users on a different host
914 ****************************************************************************/
916 int cli_RNetUserEnum(struct cli_state *cli, void (*fn)(const char *, const char *, const char *, const char *, void *), void *state)
918 char param[WORDSIZE /* api number */
919 +sizeof(RAP_NetUserEnum_REQ) /* parm string */
920 +sizeof(RAP_USER_INFO_L1) /* return string */
921 +WORDSIZE /* info level */
922 +WORDSIZE]; /* buffer size */
923 char *p;
924 char *rparam = NULL;
925 char *rdata = NULL;
926 unsigned int rprcnt, rdrcnt;
927 int res = -1;
929 memset(param, '\0', sizeof(param));
930 p = make_header(param, RAP_WUserEnum,
931 RAP_NetUserEnum_REQ, RAP_USER_INFO_L1);
932 PUTWORD(p,1); /* Info level 1 */
933 PUTWORD(p,0xFF00); /* Return buffer size */
935 /* BB Fix handling of large numbers of users to be returned */
936 if (cli_api(cli,
937 param, PTR_DIFF(p,param),8,
938 NULL, 0, CLI_BUFFER_SIZE,
939 &rparam, &rprcnt,
940 &rdata, &rdrcnt)) {
941 char *endp = rparam + rprcnt;
942 res = GETRES(rparam,endp);
943 cli->rap_error = res;
944 if (cli->rap_error != 0) {
945 DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
949 if (!rdata) {
950 DEBUG(4,("NetUserEnum no data returned\n"));
951 goto out;
954 if (res == 0 || res == ERRmoredata) {
955 int i, converter = 0, count = 0;
956 char username[RAP_USERNAME_LEN];
957 char userpw[RAP_UPASSWD_LEN];
958 char *endp = rparam + rprcnt;
959 char *comment, *homedir, *logonscript;
960 TALLOC_CTX *frame = talloc_stackframe();
962 p = rparam + WORDSIZE; /* skip result */
963 GETWORD(p, converter, endp);
964 GETWORD(p, count, endp);
966 endp = rdata + rdrcnt;
967 for (i=0,p=rdata;i<count && p < endp;i++) {
968 p += rap_getstringf(p,
969 username,
970 RAP_USERNAME_LEN,
971 RAP_USERNAME_LEN,
972 endp);
973 p++; /* pad byte */
974 p += rap_getstringf(p,
975 userpw,
976 RAP_UPASSWD_LEN,
977 RAP_UPASSWD_LEN,
978 endp);
979 p += DWORDSIZE; /* skip password age */
980 p += WORDSIZE; /* skip priv: 0=guest, 1=user, 2=admin */
981 p += rap_getstringp(frame,
983 &homedir,
984 rdata,
985 converter,
986 endp);
987 p += rap_getstringp(frame,
989 &comment,
990 rdata,
991 converter,
992 endp);
993 p += WORDSIZE; /* skip flags */
994 p += rap_getstringp(frame,
996 &logonscript,
997 rdata,
998 converter,
999 endp);
1000 if (username[0] && comment &&
1001 homedir && logonscript) {
1002 fn(username,
1003 comment,
1004 homedir,
1005 logonscript,
1006 cli);
1009 TALLOC_FREE(frame);
1010 } else {
1011 DEBUG(4,("NetUserEnum res=%d\n", res));
1014 out:
1016 SAFE_FREE(rparam);
1017 SAFE_FREE(rdata);
1019 return res;
1022 int cli_RNetUserEnum0(struct cli_state *cli,
1023 void (*fn)(const char *, void *),
1024 void *state)
1026 char param[WORDSIZE /* api number */
1027 +sizeof(RAP_NetUserEnum_REQ) /* parm string */
1028 +sizeof(RAP_USER_INFO_L0) /* return string */
1029 +WORDSIZE /* info level */
1030 +WORDSIZE]; /* buffer size */
1031 char *p;
1032 char *rparam = NULL;
1033 char *rdata = NULL;
1034 unsigned int rprcnt, rdrcnt;
1035 int res = -1;
1037 memset(param, '\0', sizeof(param));
1038 p = make_header(param, RAP_WUserEnum,
1039 RAP_NetUserEnum_REQ, RAP_USER_INFO_L0);
1040 PUTWORD(p,0); /* Info level 1 */
1041 PUTWORD(p,0xFF00); /* Return buffer size */
1043 /* BB Fix handling of large numbers of users to be returned */
1044 if (cli_api(cli,
1045 param, PTR_DIFF(p,param),8,
1046 NULL, 0, CLI_BUFFER_SIZE,
1047 &rparam, &rprcnt,
1048 &rdata, &rdrcnt)) {
1049 char *endp = rparam + rprcnt;
1050 res = GETRES(rparam,endp);
1051 cli->rap_error = res;
1052 if (cli->rap_error != 0) {
1053 DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
1057 if (!rdata) {
1058 DEBUG(4,("NetUserEnum no data returned\n"));
1059 goto out;
1062 if (res == 0 || res == ERRmoredata) {
1063 int i, count = 0;
1064 char *endp = rparam + rprcnt;
1065 char username[RAP_USERNAME_LEN];
1067 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1068 GETWORD(p, count, endp);
1070 endp = rdata + rdrcnt;
1071 for (i=0,p=rdata;i<count && p < endp;i++) {
1072 p += rap_getstringf(p,
1073 username,
1074 RAP_USERNAME_LEN,
1075 RAP_USERNAME_LEN,
1076 endp);
1077 if (username[0]) {
1078 fn(username, cli);
1081 } else {
1082 DEBUG(4,("NetUserEnum res=%d\n", res));
1085 out:
1087 SAFE_FREE(rparam);
1088 SAFE_FREE(rdata);
1090 return res;
1093 /****************************************************************************
1094 Call a NetFileClose2 - close open file on another session to server.
1095 ****************************************************************************/
1097 int cli_NetFileClose(struct cli_state *cli, uint32 file_id )
1099 char *rparam = NULL;
1100 char *rdata = NULL;
1101 char *p;
1102 unsigned int rdrcnt,rprcnt;
1103 char param[WORDSIZE /* api number */
1104 +sizeof(RAP_WFileClose2_REQ) /* req string */
1105 +1 /* no ret string */
1106 +DWORDSIZE]; /* file ID */
1107 int res = -1;
1109 /* now send a SMBtrans command with api RNetShareEnum */
1110 p = make_header(param, RAP_WFileClose2, RAP_WFileClose2_REQ, NULL);
1111 PUTDWORD(p, file_id);
1113 if (cli_api(cli,
1114 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1115 NULL, 0, 200, /* data, length, maxlen */
1116 &rparam, &rprcnt, /* return params, length */
1117 &rdata, &rdrcnt)) /* return data, length */
1119 char *endp = rparam + rprcnt;
1120 res = GETRES(rparam, endp);
1122 if (res == 0) {
1123 /* nothing to do */
1124 } else if (res == 2314){
1125 DEBUG(1, ("NetFileClose2 - attempt to close non-existant file open instance\n"));
1126 } else {
1127 DEBUG(4,("NetFileClose2 res=%d\n", res));
1129 } else {
1130 res = -1;
1131 DEBUG(4,("NetFileClose2 failed\n"));
1134 SAFE_FREE(rparam);
1135 SAFE_FREE(rdata);
1137 return res;
1140 /****************************************************************************
1141 Call a NetFileGetInfo - get information about server file opened from other
1142 workstation.
1143 ****************************************************************************/
1145 int cli_NetFileGetInfo(struct cli_state *cli, uint32 file_id, void (*fn)(const char *, const char *, uint16, uint16, uint32))
1147 char *rparam = NULL;
1148 char *rdata = NULL;
1149 char *p;
1150 unsigned int rdrcnt,rprcnt;
1151 int res = -1;
1152 char param[WORDSIZE /* api number */
1153 +sizeof(RAP_WFileGetInfo2_REQ) /* req string */
1154 +sizeof(RAP_FILE_INFO_L3) /* return string */
1155 +DWORDSIZE /* file ID */
1156 +WORDSIZE /* info level */
1157 +WORDSIZE]; /* buffer size */
1159 /* now send a SMBtrans command with api RNetShareEnum */
1160 p = make_header(param, RAP_WFileGetInfo2,
1161 RAP_WFileGetInfo2_REQ, RAP_FILE_INFO_L3);
1162 PUTDWORD(p, file_id);
1163 PUTWORD(p, 3); /* info level */
1164 PUTWORD(p, 0x1000); /* buffer size */
1165 if (cli_api(cli,
1166 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1167 NULL, 0, 0x1000, /* data, length, maxlen */
1168 &rparam, &rprcnt, /* return params, length */
1169 &rdata, &rdrcnt)) /* return data, length */
1171 char *endp = rparam + rprcnt;
1172 res = GETRES(rparam,endp);
1173 if (res == 0 || res == ERRmoredata) {
1174 TALLOC_CTX *frame = talloc_stackframe();
1175 int converter = 0,id = 0, perms = 0, locks = 0;
1176 char *fpath, *fuser;
1178 p = rparam + WORDSIZE; /* skip result */
1179 GETWORD(p, converter, endp);
1181 p = rdata;
1182 endp = rdata + rdrcnt;
1184 GETDWORD(p, id, endp);
1185 GETWORD(p, perms, endp);
1186 GETWORD(p, locks, endp);
1188 p += rap_getstringp(frame,
1190 &fpath,
1191 rdata,
1192 converter,
1193 endp);
1194 p += rap_getstringp(frame,
1196 &fuser,
1197 rdata,
1198 converter,
1199 endp);
1201 if (fpath && fuser) {
1202 fn(fpath, fuser, perms, locks, id);
1205 TALLOC_FREE(frame);
1206 } else {
1207 DEBUG(4,("NetFileGetInfo2 res=%d\n", res));
1209 } else {
1210 res = -1;
1211 DEBUG(4,("NetFileGetInfo2 failed\n"));
1214 SAFE_FREE(rparam);
1215 SAFE_FREE(rdata);
1217 return res;
1220 /****************************************************************************
1221 * Call a NetFileEnum2 - list open files on an SMB server
1223 * PURPOSE: Remotes a NetFileEnum API call to the current server or target
1224 * server listing the files open via the network (and their
1225 * corresponding open instance ids)
1227 * Dependencies: none
1229 * Parameters:
1230 * cli - pointer to cli_state structure
1231 * user - if present, return only files opened by this remote user
1232 * base_path - if present, return only files opened below this
1233 * base path
1234 * fn - display function to invoke for each entry in the result
1237 * Returns:
1238 * True - success
1239 * False - failure
1241 ****************************************************************************/
1243 int cli_NetFileEnum(struct cli_state *cli, const char * user,
1244 const char * base_path,
1245 void (*fn)(const char *, const char *, uint16, uint16,
1246 uint32))
1248 char *rparam = NULL;
1249 char *rdata = NULL;
1250 char *p;
1251 unsigned int rdrcnt,rprcnt;
1252 char param[WORDSIZE /* api number */
1253 +sizeof(RAP_WFileEnum2_REQ) /* req string */
1254 +sizeof(RAP_FILE_INFO_L3) /* return string */
1255 +1024 /* base path (opt) */
1256 +RAP_USERNAME_LEN /* user name (opt) */
1257 +WORDSIZE /* info level */
1258 +WORDSIZE /* buffer size */
1259 +DWORDSIZE /* resume key ? */
1260 +DWORDSIZE]; /* resume key ? */
1261 int count = -1;
1262 int res = -1;
1264 /* now send a SMBtrans command with api RNetShareEnum */
1265 p = make_header(param, RAP_WFileEnum2,
1266 RAP_WFileEnum2_REQ, RAP_FILE_INFO_L3);
1268 PUTSTRING(p, base_path, 1024);
1269 PUTSTRING(p, user, RAP_USERNAME_LEN);
1270 PUTWORD(p, 3); /* info level */
1271 PUTWORD(p, 0xFF00); /* buffer size */
1272 PUTDWORD(p, 0); /* zero out the resume key */
1273 PUTDWORD(p, 0); /* or is this one the resume key? */
1275 if (cli_api(cli,
1276 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1277 NULL, 0, 0xFF00, /* data, length, maxlen */
1278 &rparam, &rprcnt, /* return params, length */
1279 &rdata, &rdrcnt)) /* return data, length */
1281 char *endp = rparam + rprcnt;
1282 res = GETRES(rparam, endp);
1284 if (res == 0 || res == ERRmoredata) {
1285 TALLOC_CTX *frame = talloc_stackframe();
1286 int converter = 0, i;
1288 p = rparam + WORDSIZE; /* skip result */
1289 GETWORD(p, converter, endp);
1290 GETWORD(p, count, endp);
1292 p = rdata;
1293 endp = rdata + rdrcnt;
1294 for (i=0; i<count && p < endp; i++) {
1295 int id = 0, perms = 0, locks = 0;
1296 char *fpath, *fuser;
1298 GETDWORD(p, id, endp);
1299 GETWORD(p, perms, endp);
1300 GETWORD(p, locks, endp);
1301 p += rap_getstringp(frame,
1303 &fpath,
1304 rdata,
1305 converter,
1306 endp);
1307 p += rap_getstringp(frame,
1309 &fuser,
1310 rdata,
1311 converter,
1312 endp);
1314 if (fpath && fuser) {
1315 fn(fpath, fuser, perms, locks, id);
1317 } /* BB fix ERRmoredata case to send resume request */
1318 TALLOC_FREE(frame);
1319 } else {
1320 DEBUG(4,("NetFileEnum2 res=%d\n", res));
1322 } else {
1323 DEBUG(4,("NetFileEnum2 failed\n"));
1326 SAFE_FREE(rparam);
1327 SAFE_FREE(rdata);
1329 return count;
1332 /****************************************************************************
1333 Call a NetShareAdd - share/export directory on remote server.
1334 ****************************************************************************/
1336 int cli_NetShareAdd(struct cli_state *cli, RAP_SHARE_INFO_2 * sinfo )
1338 char *rparam = NULL;
1339 char *rdata = NULL;
1340 char *p;
1341 unsigned int rdrcnt,rprcnt;
1342 int res = -1;
1343 char param[WORDSIZE /* api number */
1344 +sizeof(RAP_WShareAdd_REQ) /* req string */
1345 +sizeof(RAP_SHARE_INFO_L2) /* return string */
1346 +WORDSIZE /* info level */
1347 +WORDSIZE]; /* reserved word */
1348 char data[1024];
1349 /* offset to free format string section following fixed length data. */
1350 /* will be updated by PUTSTRINGP macro and will end up with total len */
1351 int soffset = RAP_SHARENAME_LEN + 1 /* share name + pad */
1352 + WORDSIZE /* share type */
1353 + DWORDSIZE /* comment pointer */
1354 + WORDSIZE /* permissions */
1355 + WORDSIZE /* max users */
1356 + WORDSIZE /* active users */
1357 + DWORDSIZE /* share path */
1358 + RAP_SPASSWD_LEN + 1; /* share password + pad */
1360 memset(param,'\0',sizeof(param));
1361 /* now send a SMBtrans command with api RNetShareAdd */
1362 p = make_header(param, RAP_WshareAdd,
1363 RAP_WShareAdd_REQ, RAP_SHARE_INFO_L2);
1364 PUTWORD(p, 2); /* info level */
1365 PUTWORD(p, 0); /* reserved word 0 */
1367 p = data;
1368 PUTSTRINGF(p, sinfo->share_name, RAP_SHARENAME_LEN);
1369 PUTBYTE(p, 0); /* pad byte 0 */
1371 PUTWORD(p, sinfo->share_type);
1372 PUTSTRINGP(p, sinfo->comment, data, soffset);
1373 PUTWORD(p, sinfo->perms);
1374 PUTWORD(p, sinfo->maximum_users);
1375 PUTWORD(p, sinfo->active_users);
1376 PUTSTRINGP(p, sinfo->path, data, soffset);
1377 PUTSTRINGF(p, sinfo->password, RAP_SPASSWD_LEN);
1378 SCVAL(p,-1,0x0A); /* required 0x0A at end of password */
1380 if (cli_api(cli,
1381 param, sizeof(param), 1024, /* Param, length, maxlen */
1382 data, soffset, sizeof(data), /* data, length, maxlen */
1383 &rparam, &rprcnt, /* return params, length */
1384 &rdata, &rdrcnt)) /* return data, length */
1386 char *endp = rparam + rprcnt;
1387 res = GETRES(rparam, endp);
1389 if (res == 0) {
1390 /* nothing to do */
1391 } else {
1392 DEBUG(4,("NetShareAdd res=%d\n", res));
1394 } else {
1395 DEBUG(4,("NetShareAdd failed\n"));
1398 SAFE_FREE(rparam);
1399 SAFE_FREE(rdata);
1401 return res;
1404 /****************************************************************************
1405 Call a NetShareDelete - unshare exported directory on remote server.
1406 ****************************************************************************/
1408 int cli_NetShareDelete(struct cli_state *cli, const char * share_name )
1410 char *rparam = NULL;
1411 char *rdata = NULL;
1412 char *p;
1413 unsigned int rdrcnt,rprcnt;
1414 int res = -1;
1415 char param[WORDSIZE /* api number */
1416 +sizeof(RAP_WShareDel_REQ) /* req string */
1417 +1 /* no ret string */
1418 +RAP_SHARENAME_LEN /* share to del */
1419 +WORDSIZE]; /* reserved word */
1421 /* now send a SMBtrans command with api RNetShareDelete */
1422 p = make_header(param, RAP_WshareDel, RAP_WShareDel_REQ, NULL);
1423 PUTSTRING(p,share_name,RAP_SHARENAME_LEN);
1424 PUTWORD(p,0); /* reserved word MBZ on input */
1426 if (cli_api(cli,
1427 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1428 NULL, 0, 200, /* data, length, maxlen */
1429 &rparam, &rprcnt, /* return params, length */
1430 &rdata, &rdrcnt)) /* return data, length */
1432 char *endp = rparam + rprcnt;
1433 res = GETRES(rparam, endp);
1435 if (res == 0) {
1436 /* nothing to do */
1437 } else {
1438 DEBUG(4,("NetShareDelete res=%d\n", res));
1440 } else {
1441 DEBUG(4,("NetShareDelete failed\n"));
1444 SAFE_FREE(rparam);
1445 SAFE_FREE(rdata);
1447 return res;
1450 /*************************************************************************
1452 * Function Name: cli_get_pdc_name
1454 * PURPOSE: Remotes a NetServerEnum API call to the current server
1455 * requesting the name of a server matching the server
1456 * type of SV_TYPE_DOMAIN_CTRL (PDC).
1458 * Dependencies: none
1460 * Parameters:
1461 * cli - pointer to cli_state structure
1462 * workgroup - pointer to string containing name of domain
1463 * pdc_name - pointer to string that will contain PDC name
1464 * on successful return
1466 * Returns:
1467 * True - success
1468 * False - failure
1470 ************************************************************************/
1472 bool cli_get_pdc_name(struct cli_state *cli, const char *workgroup, char **pdc_name)
1474 char *rparam = NULL;
1475 char *rdata = NULL;
1476 unsigned int rdrcnt,rprcnt;
1477 char *p;
1478 char param[WORDSIZE /* api number */
1479 +sizeof(RAP_NetServerEnum2_REQ) /* req string */
1480 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1481 +WORDSIZE /* info level */
1482 +WORDSIZE /* buffer size */
1483 +DWORDSIZE /* server type */
1484 +RAP_MACHNAME_LEN]; /* workgroup */
1485 int count = -1;
1486 int res = -1;
1488 *pdc_name = NULL;
1490 /* send a SMBtrans command with api NetServerEnum */
1491 p = make_header(param, RAP_NetServerEnum2,
1492 RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L1);
1493 PUTWORD(p, 1); /* info level */
1494 PUTWORD(p, CLI_BUFFER_SIZE);
1495 PUTDWORD(p, SV_TYPE_DOMAIN_CTRL);
1496 PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1498 if (cli_api(cli,
1499 param, PTR_DIFF(p,param), 8, /* params, length, max */
1500 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1501 &rparam, &rprcnt, /* return params, return size */
1502 &rdata, &rdrcnt /* return data, return size */
1503 )) {
1505 char *endp = rparam + rprcnt;
1506 res = GETRES(rparam, endp);
1507 cli->rap_error = res;
1510 * We only really care to copy a name if the
1511 * API succeeded and we got back a name.
1513 if (cli->rap_error == 0) {
1514 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1515 GETWORD(p, count, endp);
1516 p = rdata;
1517 endp = rdata + rdrcnt;
1519 if (count > 0) {
1520 TALLOC_CTX *frame = talloc_stackframe();
1521 char *dcname;
1522 p += rap_getstring(frame,
1524 &dcname,
1525 endp);
1526 if (dcname) {
1527 *pdc_name = SMB_STRDUP(dcname);
1529 TALLOC_FREE(frame);
1531 } else {
1532 DEBUG(4,("cli_get_pdc_name: machine %s failed the NetServerEnum call. "
1533 "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1537 SAFE_FREE(rparam);
1538 SAFE_FREE(rdata);
1540 return(count > 0);
1543 /*************************************************************************
1545 * Function Name: cli_get_server_domain
1547 * PURPOSE: Remotes a NetWkstaGetInfo API call to the current server
1548 * requesting wksta_info_10 level information to determine
1549 * the domain the server belongs to. On success, this
1550 * routine sets the server_domain field in the cli_state structure
1551 * to the server's domain name.
1553 * Dependencies: none
1555 * Parameters:
1556 * cli - pointer to cli_state structure
1558 * Returns:
1559 * True - success
1560 * False - failure
1562 * Origins: samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1564 ************************************************************************/
1566 bool cli_get_server_domain(struct cli_state *cli)
1568 char *rparam = NULL;
1569 char *rdata = NULL;
1570 unsigned int rdrcnt,rprcnt;
1571 char *p;
1572 char param[WORDSIZE /* api number */
1573 +sizeof(RAP_WWkstaGetInfo_REQ) /* req string */
1574 +sizeof(RAP_WKSTA_INFO_L10) /* return string */
1575 +WORDSIZE /* info level */
1576 +WORDSIZE]; /* buffer size */
1577 int res = -1;
1579 /* send a SMBtrans command with api NetWkstaGetInfo */
1580 p = make_header(param, RAP_WWkstaGetInfo,
1581 RAP_WWkstaGetInfo_REQ, RAP_WKSTA_INFO_L10);
1582 PUTWORD(p, 10); /* info level */
1583 PUTWORD(p, CLI_BUFFER_SIZE);
1585 if (cli_api(cli, param, PTR_DIFF(p,param), 8, /* params, length, max */
1586 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1587 &rparam, &rprcnt, /* return params, return size */
1588 &rdata, &rdrcnt)) { /* return data, return size */
1589 char *endp = rparam + rprcnt;
1590 res = GETRES(rparam, endp);
1592 if (res == 0) {
1593 TALLOC_CTX *frame = talloc_stackframe();
1594 char *server_domain;
1595 int converter = 0;
1597 p = rparam + WORDSIZE;
1598 GETWORD(p, converter, endp);
1600 p = rdata + DWORDSIZE + DWORDSIZE; /* skip computer & user names */
1601 endp = rdata + rdrcnt;
1602 p += rap_getstringp(frame,
1604 &server_domain,
1605 rdata,
1606 converter,
1607 endp);
1609 if (server_domain) {
1610 fstrcpy(cli->server_domain, server_domain);
1612 TALLOC_FREE(frame);
1616 SAFE_FREE(rparam);
1617 SAFE_FREE(rdata);
1619 return(res == 0);
1622 /*************************************************************************
1624 * Function Name: cli_get_server_type
1626 * PURPOSE: Remotes a NetServerGetInfo API call to the current server
1627 * requesting server_info_1 level information to retrieve
1628 * the server type.
1630 * Dependencies: none
1632 * Parameters:
1633 * cli - pointer to cli_state structure
1634 * pstype - pointer to uint32 to contain returned server type
1636 * Returns:
1637 * True - success
1638 * False - failure
1640 * Origins: samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1642 ************************************************************************/
1644 bool cli_get_server_type(struct cli_state *cli, uint32 *pstype)
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_WserverGetInfo_REQ) /* req string */
1652 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1653 +WORDSIZE /* info level */
1654 +WORDSIZE]; /* buffer size */
1655 int res = -1;
1657 /* send a SMBtrans command with api NetServerGetInfo */
1658 p = make_header(param, RAP_WserverGetInfo,
1659 RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1660 PUTWORD(p, 1); /* info level */
1661 PUTWORD(p, CLI_BUFFER_SIZE);
1663 if (cli_api(cli,
1664 param, PTR_DIFF(p,param), 8, /* params, length, max */
1665 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1666 &rparam, &rprcnt, /* return params, return size */
1667 &rdata, &rdrcnt /* return data, return size */
1668 )) {
1669 char *endp = rparam + rprcnt;
1670 res = GETRES(rparam,endp);
1672 if (res == 0 || res == ERRmoredata) {
1673 p = rdata;
1674 endp = rparam + rprcnt;
1675 p += 18;
1676 GETDWORD(p,*pstype,endp);
1677 *pstype &= ~SV_TYPE_LOCAL_LIST_ONLY;
1681 SAFE_FREE(rparam);
1682 SAFE_FREE(rdata);
1684 return(res == 0 || res == ERRmoredata);
1687 bool cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
1688 char **servername)
1690 char *rparam = NULL;
1691 char *rdata = NULL;
1692 unsigned int rdrcnt,rprcnt;
1693 char *p;
1694 char param[WORDSIZE /* api number */
1695 +sizeof(RAP_WserverGetInfo_REQ) /* req string */
1696 +sizeof(RAP_SERVER_INFO_L1) /* return string */
1697 +WORDSIZE /* info level */
1698 +WORDSIZE]; /* buffer size */
1699 bool res = false;
1700 char *endp;
1701 fstring tmp;
1703 /* send a SMBtrans command with api NetServerGetInfo */
1704 p = make_header(param, RAP_WserverGetInfo,
1705 RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1706 PUTWORD(p, 1); /* info level */
1707 PUTWORD(p, CLI_BUFFER_SIZE);
1709 if (!cli_api(cli,
1710 param, PTR_DIFF(p,param), 8, /* params, length, max */
1711 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1712 &rparam, &rprcnt, /* return params, return size */
1713 &rdata, &rdrcnt /* return data, return size */
1714 )) {
1715 goto failed;
1718 endp = rparam + rprcnt;
1719 if (GETRES(rparam, endp) != 0) {
1720 goto failed;
1723 if (rdrcnt < 16) {
1724 DEBUG(10, ("invalid data count %d, expected >= 16\n", rdrcnt));
1725 goto failed;
1728 if (pull_ascii(tmp, rdata, sizeof(tmp)-1, 16, STR_TERMINATE) == -1) {
1729 DEBUG(10, ("pull_ascii failed\n"));
1730 goto failed;
1733 if (!(*servername = talloc_strdup(mem_ctx, tmp))) {
1734 DEBUG(1, ("talloc_strdup failed\n"));
1735 goto failed;
1738 res = true;
1740 failed:
1741 SAFE_FREE(rparam);
1742 SAFE_FREE(rdata);
1743 return res;
1746 /*************************************************************************
1748 * Function Name: cli_ns_check_server_type
1750 * PURPOSE: Remotes a NetServerEnum2 API call to the current server
1751 * requesting server_info_0 level information of machines
1752 * matching the given server type. If the returned server
1753 * list contains the machine name contained in cli->desthost
1754 * then we conclude the server type checks out. This routine
1755 * is useful to retrieve list of server's of a certain
1756 * type when all you have is a null session connection and
1757 * can't remote API calls such as NetWkstaGetInfo or
1758 * NetServerGetInfo.
1760 * Dependencies: none
1762 * Parameters:
1763 * cli - pointer to cli_state structure
1764 * workgroup - pointer to string containing domain
1765 * stype - server type
1767 * Returns:
1768 * True - success
1769 * False - failure
1771 ************************************************************************/
1773 bool cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 stype)
1775 char *rparam = NULL;
1776 char *rdata = NULL;
1777 unsigned int rdrcnt,rprcnt;
1778 char *p;
1779 char param[WORDSIZE /* api number */
1780 +sizeof(RAP_NetServerEnum2_REQ) /* req string */
1781 +sizeof(RAP_SERVER_INFO_L0) /* return string */
1782 +WORDSIZE /* info level */
1783 +WORDSIZE /* buffer size */
1784 +DWORDSIZE /* server type */
1785 +RAP_MACHNAME_LEN]; /* workgroup */
1786 bool found_server = false;
1787 int res = -1;
1789 /* send a SMBtrans command with api NetServerEnum */
1790 p = make_header(param, RAP_NetServerEnum2,
1791 RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L0);
1792 PUTWORD(p, 0); /* info level 0 */
1793 PUTWORD(p, CLI_BUFFER_SIZE);
1794 PUTDWORD(p, stype);
1795 PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1797 if (cli_api(cli,
1798 param, PTR_DIFF(p,param), 8, /* params, length, max */
1799 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1800 &rparam, &rprcnt, /* return params, return size */
1801 &rdata, &rdrcnt /* return data, return size */
1802 )) {
1803 char *endp = rparam + rprcnt;
1804 res = GETRES(rparam,endp);
1805 cli->rap_error = res;
1807 if (res == 0 || res == ERRmoredata) {
1808 int i, count = 0;
1810 p = rparam + WORDSIZE + WORDSIZE;
1811 GETWORD(p, count,endp);
1813 p = rdata;
1814 endp = rdata + rdrcnt;
1815 for (i = 0;i < count && p < endp;i++, p += 16) {
1816 char ret_server[RAP_MACHNAME_LEN];
1818 p += rap_getstringf(p,
1819 ret_server,
1820 RAP_MACHNAME_LEN,
1821 RAP_MACHNAME_LEN,
1822 endp);
1823 if (strequal(ret_server, cli->desthost)) {
1824 found_server = true;
1825 break;
1828 } else {
1829 DEBUG(4,("cli_ns_check_server_type: machine %s failed the NetServerEnum call. "
1830 "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1834 SAFE_FREE(rparam);
1835 SAFE_FREE(rdata);
1837 return found_server;
1840 /****************************************************************************
1841 Perform a NetWkstaUserLogoff.
1842 ****************************************************************************/
1844 bool cli_NetWkstaUserLogoff(struct cli_state *cli, const char *user, const char *workstation)
1846 char *rparam = NULL;
1847 char *rdata = NULL;
1848 char *p;
1849 unsigned int rdrcnt,rprcnt;
1850 char param[WORDSIZE /* api number */
1851 +sizeof(RAP_NetWkstaUserLogoff_REQ) /* req string */
1852 +sizeof(RAP_USER_LOGOFF_INFO_L1) /* return string */
1853 +RAP_USERNAME_LEN+1 /* user name+pad */
1854 +RAP_MACHNAME_LEN /* wksta name */
1855 +WORDSIZE /* buffer size */
1856 +WORDSIZE]; /* buffer size? */
1857 char upperbuf[MAX(RAP_USERNAME_LEN,RAP_MACHNAME_LEN)];
1858 int res = -1;
1860 memset(param, 0, sizeof(param));
1862 /* send a SMBtrans command with api NetWkstaUserLogoff */
1863 p = make_header(param, RAP_WWkstaUserLogoff,
1864 RAP_NetWkstaUserLogoff_REQ, RAP_USER_LOGOFF_INFO_L1);
1865 PUTDWORD(p, 0); /* Null pointer */
1866 PUTDWORD(p, 0); /* Null pointer */
1867 strlcpy(upperbuf, user, sizeof(upperbuf));
1868 strupper_m(upperbuf);
1869 PUTSTRINGF(p, upperbuf, RAP_USERNAME_LEN);
1870 p++; /* strange format, but ok */
1871 strlcpy(upperbuf, workstation, sizeof(upperbuf));
1872 strupper_m(upperbuf);
1873 PUTSTRINGF(p, upperbuf, RAP_MACHNAME_LEN);
1874 PUTWORD(p, CLI_BUFFER_SIZE);
1875 PUTWORD(p, CLI_BUFFER_SIZE);
1877 if (cli_api(cli,
1878 param, PTR_DIFF(p,param),1024, /* param, length, max */
1879 NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1880 &rparam, &rprcnt, /* return params, return size */
1881 &rdata, &rdrcnt /* return data, return size */
1882 )) {
1883 char *endp = rparam + rprcnt;
1884 res = GETRES(rparam,endp);
1885 cli->rap_error = res;
1887 if (cli->rap_error != 0) {
1888 DEBUG(4,("NetwkstaUserLogoff gave error %d\n", cli->rap_error));
1892 SAFE_FREE(rparam);
1893 SAFE_FREE(rdata);
1894 return (cli->rap_error == 0);
1897 int cli_NetPrintQEnum(struct cli_state *cli,
1898 void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
1899 void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,uint,uint,const char*))
1901 char param[WORDSIZE /* api number */
1902 +sizeof(RAP_NetPrintQEnum_REQ) /* req string */
1903 +sizeof(RAP_PRINTQ_INFO_L2) /* return string */
1904 +WORDSIZE /* info level */
1905 +WORDSIZE /* buffer size */
1906 +sizeof(RAP_SMB_PRINT_JOB_L1)]; /* more ret data */
1907 char *p;
1908 char *rparam = NULL;
1909 char *rdata = NULL;
1910 unsigned int rprcnt, rdrcnt;
1911 int res = -1;
1913 memset(param, '\0',sizeof(param));
1914 p = make_header(param, RAP_WPrintQEnum,
1915 RAP_NetPrintQEnum_REQ, RAP_PRINTQ_INFO_L2);
1916 PUTWORD(p,2); /* Info level 2 */
1917 PUTWORD(p,0xFFE0); /* Return buffer size */
1918 PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1920 if (cli_api(cli,
1921 param, PTR_DIFF(p,param),1024,
1922 NULL, 0, CLI_BUFFER_SIZE,
1923 &rparam, &rprcnt,
1924 &rdata, &rdrcnt)) {
1925 char *endp = rparam + rprcnt;
1926 res = GETRES(rparam, endp);
1927 cli->rap_error = res;
1928 if (res != 0) {
1929 DEBUG(1,("NetPrintQEnum gave error %d\n", res));
1933 if (!rdata) {
1934 DEBUG(4,("NetPrintQEnum no data returned\n"));
1935 goto out;
1938 if (res == 0 || res == ERRmoredata) {
1939 TALLOC_CTX *frame = talloc_stackframe();
1940 char *endp = rparam + rprcnt;
1941 int i, converter = 0, count = 0;
1943 p = rparam + WORDSIZE;
1944 GETWORD(p, converter, endp);
1945 GETWORD(p, count, endp);
1947 p = rdata;
1948 endp = rdata + rdrcnt;
1949 for (i=0;i<count && p < endp;i++) {
1950 char qname[RAP_SHARENAME_LEN];
1951 char *sep_file, *print_proc, *dest, *parms, *comment;
1952 uint16_t jobcount = 0, priority = 0;
1953 uint16_t start_time = 0, until_time = 0, status = 0;
1955 p += rap_getstringf(p,
1956 qname,
1957 RAP_SHARENAME_LEN,
1958 RAP_SHARENAME_LEN,
1959 endp);
1960 p++; /* pad */
1961 GETWORD(p, priority, endp);
1962 GETWORD(p, start_time, endp);
1963 GETWORD(p, until_time, endp);
1964 p += rap_getstringp(frame,
1966 &sep_file,
1967 rdata,
1968 converter,
1969 endp);
1970 p += rap_getstringp(frame,
1972 &print_proc,
1973 rdata,
1974 converter,
1975 endp);
1976 p += rap_getstringp(frame,
1978 &dest,
1979 rdata,
1980 converter,
1981 endp);
1982 p += rap_getstringp(frame,
1984 &parms,
1985 rdata,
1986 converter,
1987 endp);
1988 p += rap_getstringp(frame,
1990 &comment,
1991 rdata,
1992 converter,
1993 endp);
1994 GETWORD(p, status, endp);
1995 GETWORD(p, jobcount, endp);
1997 if (sep_file && print_proc && dest && parms &&
1998 comment) {
1999 qfn(qname, priority, start_time, until_time, sep_file, print_proc,
2000 dest, parms, comment, status, jobcount);
2003 if (jobcount) {
2004 int j;
2005 for (j=0;j<jobcount;j++) {
2006 uint16 jid = 0, pos = 0, fsstatus = 0;
2007 char ownername[RAP_USERNAME_LEN];
2008 char notifyname[RAP_MACHNAME_LEN];
2009 char datatype[RAP_DATATYPE_LEN];
2010 char *jparms, *jstatus, *jcomment;
2011 unsigned int submitted = 0, jsize = 0;
2013 GETWORD(p, jid, endp);
2014 p += rap_getstringf(p,
2015 ownername,
2016 RAP_USERNAME_LEN,
2017 RAP_USERNAME_LEN,
2018 endp);
2019 p++; /* pad byte */
2020 p += rap_getstringf(p,
2021 notifyname,
2022 RAP_MACHNAME_LEN,
2023 RAP_MACHNAME_LEN,
2024 endp);
2025 p += rap_getstringf(p,
2026 datatype,
2027 RAP_DATATYPE_LEN,
2028 RAP_DATATYPE_LEN,
2029 endp);
2030 p += rap_getstringp(frame,
2032 &jparms,
2033 rdata,
2034 converter,
2035 endp);
2036 GETWORD(p, pos, endp);
2037 GETWORD(p, fsstatus, endp);
2038 p += rap_getstringp(frame,
2040 &jstatus,
2041 rdata,
2042 converter,
2043 endp);
2044 GETDWORD(p, submitted, endp);
2045 GETDWORD(p, jsize, endp);
2046 p += rap_getstringp(frame,
2048 &jcomment,
2049 rdata,
2050 converter,
2051 endp);
2053 if (jparms && jstatus && jcomment) {
2054 jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
2055 jstatus, submitted, jsize, jcomment);
2060 TALLOC_FREE(frame);
2061 } else {
2062 DEBUG(4,("NetPrintQEnum res=%d\n", res));
2065 out:
2067 SAFE_FREE(rparam);
2068 SAFE_FREE(rdata);
2070 return res;
2073 int cli_NetPrintQGetInfo(struct cli_state *cli, const char *printer,
2074 void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
2075 void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,uint,uint,const char*))
2077 char param[WORDSIZE /* api number */
2078 +sizeof(RAP_NetPrintQGetInfo_REQ) /* req string */
2079 +sizeof(RAP_PRINTQ_INFO_L2) /* return string */
2080 +RAP_SHARENAME_LEN /* printer name */
2081 +WORDSIZE /* info level */
2082 +WORDSIZE /* buffer size */
2083 +sizeof(RAP_SMB_PRINT_JOB_L1)]; /* more ret data */
2084 char *p;
2085 char *rparam = NULL;
2086 char *rdata = NULL;
2087 unsigned int rprcnt, rdrcnt;
2088 int res = -1;
2090 memset(param, '\0',sizeof(param));
2091 p = make_header(param, RAP_WPrintQGetInfo,
2092 RAP_NetPrintQGetInfo_REQ, RAP_PRINTQ_INFO_L2);
2093 PUTSTRING(p, printer, RAP_SHARENAME_LEN-1);
2094 PUTWORD(p, 2); /* Info level 2 */
2095 PUTWORD(p,0xFFE0); /* Return buffer size */
2096 PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
2098 if (cli_api(cli,
2099 param, PTR_DIFF(p,param),1024,
2100 NULL, 0, CLI_BUFFER_SIZE,
2101 &rparam, &rprcnt,
2102 &rdata, &rdrcnt)) {
2103 char *endp = rparam + rprcnt;
2104 res = GETRES(rparam, endp);
2105 cli->rap_error = res;
2106 if (res != 0) {
2107 DEBUG(1,("NetPrintQGetInfo gave error %d\n", res));
2111 if (!rdata) {
2112 DEBUG(4,("NetPrintQGetInfo no data returned\n"));
2113 goto out;
2116 if (res == 0 || res == ERRmoredata) {
2117 TALLOC_CTX *frame = talloc_stackframe();
2118 char *endp = rparam + rprcnt;
2119 int rsize = 0, converter = 0;
2120 char qname[RAP_SHARENAME_LEN];
2121 char *sep_file, *print_proc, *dest, *parms, *comment;
2122 uint16_t jobcount = 0, priority = 0;
2123 uint16_t start_time = 0, until_time = 0, status = 0;
2125 p = rparam + WORDSIZE;
2126 GETWORD(p, converter, endp);
2127 GETWORD(p, rsize, endp);
2129 p = rdata;
2130 endp = rdata + rdrcnt;
2131 p += rap_getstringf(p,
2132 qname,
2133 RAP_SHARENAME_LEN,
2134 RAP_SHARENAME_LEN,
2135 endp);
2136 p++; /* pad */
2137 GETWORD(p, priority, endp);
2138 GETWORD(p, start_time, endp);
2139 GETWORD(p, until_time, endp);
2140 p += rap_getstringp(frame,
2142 &sep_file,
2143 rdata,
2144 converter,
2145 endp);
2146 p += rap_getstringp(frame,
2148 &print_proc,
2149 rdata,
2150 converter,
2151 endp);
2152 p += rap_getstringp(frame,
2154 &dest,
2155 rdata,
2156 converter,
2157 endp);
2158 p += rap_getstringp(frame,
2160 &parms,
2161 rdata,
2162 converter,
2163 endp);
2164 p += rap_getstringp(frame,
2166 &comment,
2167 rdata,
2168 converter,
2169 endp);
2170 GETWORD(p, status, endp);
2171 GETWORD(p, jobcount, endp);
2173 if (sep_file && print_proc && dest &&
2174 parms && comment) {
2175 qfn(qname, priority, start_time, until_time, sep_file, print_proc,
2176 dest, parms, comment, status, jobcount);
2178 if (jobcount) {
2179 int j;
2180 for (j=0;(j<jobcount)&&(PTR_DIFF(p,rdata)< rsize)&&
2181 p<endp;j++) {
2182 uint16_t jid = 0, pos = 0, fsstatus = 0;
2183 char ownername[RAP_USERNAME_LEN];
2184 char notifyname[RAP_MACHNAME_LEN];
2185 char datatype[RAP_DATATYPE_LEN];
2186 char *jparms, *jstatus, *jcomment;
2187 unsigned int submitted = 0, jsize = 0;
2189 GETWORD(p, jid, endp);
2190 p += rap_getstringf(p,
2191 ownername,
2192 RAP_USERNAME_LEN,
2193 RAP_USERNAME_LEN,
2194 endp);
2195 p++; /* pad byte */
2196 p += rap_getstringf(p,
2197 notifyname,
2198 RAP_MACHNAME_LEN,
2199 RAP_MACHNAME_LEN,
2200 endp);
2201 p += rap_getstringf(p,
2202 datatype,
2203 RAP_DATATYPE_LEN,
2204 RAP_DATATYPE_LEN,
2205 endp);
2206 p += rap_getstringp(frame,
2208 &jparms,
2209 rdata,
2210 converter,
2211 endp);
2212 GETWORD(p, pos,endp);
2213 GETWORD(p, fsstatus,endp);
2214 p += rap_getstringp(frame,
2216 &jstatus,
2217 rdata,
2218 converter,
2219 endp);
2220 GETDWORD(p, submitted,endp);
2221 GETDWORD(p, jsize,endp);
2222 p += rap_getstringp(frame,
2224 &jcomment,
2225 rdata,
2226 converter,
2227 endp);
2229 if (jparms && jstatus && jcomment) {
2230 jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
2231 jstatus, submitted, jsize, jcomment);
2235 TALLOC_FREE(frame);
2236 } else {
2237 DEBUG(4,("NetPrintQGetInfo res=%d\n", res));
2240 out:
2242 SAFE_FREE(rparam);
2243 SAFE_FREE(rdata);
2245 return res;
2248 /****************************************************************************
2249 Call a NetServiceEnum - list running services on a different host.
2250 ****************************************************************************/
2252 int cli_RNetServiceEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
2254 char param[WORDSIZE /* api number */
2255 +sizeof(RAP_NetServiceEnum_REQ) /* parm string */
2256 +sizeof(RAP_SERVICE_INFO_L2) /* return string */
2257 +WORDSIZE /* info level */
2258 +WORDSIZE]; /* buffer size */
2259 char *p;
2260 char *rparam = NULL;
2261 char *rdata = NULL;
2262 unsigned int rprcnt, rdrcnt;
2263 int res = -1;
2265 memset(param, '\0', sizeof(param));
2266 p = make_header(param, RAP_WServiceEnum,
2267 RAP_NetServiceEnum_REQ, RAP_SERVICE_INFO_L2);
2268 PUTWORD(p,2); /* Info level 2 */
2269 PUTWORD(p,0xFFE0); /* Return buffer size */
2271 if (cli_api(cli,
2272 param, PTR_DIFF(p,param),8,
2273 NULL, 0, 0xFFE0 /* data area size */,
2274 &rparam, &rprcnt,
2275 &rdata, &rdrcnt)) {
2276 char *endp = rparam + rprcnt;
2277 res = GETRES(rparam, endp);
2278 cli->rap_error = res;
2279 if(cli->rap_error == 234) {
2280 DEBUG(1,("Not all service names were returned (such as those longer than 15 characters)\n"));
2281 } else if (cli->rap_error != 0) {
2282 DEBUG(1,("NetServiceEnum gave error %d\n", cli->rap_error));
2286 if (!rdata) {
2287 DEBUG(4,("NetServiceEnum no data returned\n"));
2288 goto out;
2291 if (res == 0 || res == ERRmoredata) {
2292 char *endp = rparam + rprcnt;
2293 int i, count = 0;
2295 p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
2296 GETWORD(p, count,endp);
2298 endp = rdata + rdrcnt;
2299 for (i=0,p=rdata;i<count && p < endp;i++) {
2300 char comment[RAP_SRVCCMNT_LEN];
2301 char servicename[RAP_SRVCNAME_LEN];
2303 p += rap_getstringf(p,
2304 servicename,
2305 RAP_SRVCNAME_LEN,
2306 RAP_SRVCNAME_LEN,
2307 endp);
2308 p+=8; /* pass status words */
2309 p += rap_getstringf(p,
2310 comment,
2311 RAP_SRVCCMNT_LEN,
2312 RAP_SRVCCMNT_LEN,
2313 endp);
2315 if (servicename[0]) {
2316 fn(servicename, comment, cli); /* BB add status too */
2319 } else {
2320 DEBUG(4,("NetServiceEnum res=%d\n", res));
2323 out:
2325 SAFE_FREE(rparam);
2326 SAFE_FREE(rdata);
2328 return res;
2331 /****************************************************************************
2332 Call a NetSessionEnum - list workstations with sessions to an SMB server.
2333 ****************************************************************************/
2335 int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16, uint16, uint16, uint, uint, uint, char *))
2337 char param[WORDSIZE /* api number */
2338 +sizeof(RAP_NetSessionEnum_REQ) /* parm string */
2339 +sizeof(RAP_SESSION_INFO_L2) /* return string */
2340 +WORDSIZE /* info level */
2341 +WORDSIZE]; /* buffer size */
2342 char *p;
2343 char *rparam = NULL;
2344 char *rdata = NULL;
2345 unsigned int rprcnt, rdrcnt;
2346 int res = -1;
2348 memset(param, '\0', sizeof(param));
2349 p = make_header(param, RAP_WsessionEnum,
2350 RAP_NetSessionEnum_REQ, RAP_SESSION_INFO_L2);
2351 PUTWORD(p,2); /* Info level 2 */
2352 PUTWORD(p,0xFF); /* Return buffer size */
2354 if (cli_api(cli,
2355 param, PTR_DIFF(p,param),8,
2356 NULL, 0, CLI_BUFFER_SIZE,
2357 &rparam, &rprcnt,
2358 &rdata, &rdrcnt)) {
2359 char *endp = rparam + rprcnt;
2360 res = GETRES(rparam, endp);
2361 cli->rap_error = res;
2362 if (res != 0) {
2363 DEBUG(1,("NetSessionEnum gave error %d\n", res));
2367 if (!rdata) {
2368 DEBUG(4,("NetSesssionEnum no data returned\n"));
2369 goto out;
2372 if (res == 0 || res == ERRmoredata) {
2373 TALLOC_CTX *frame = talloc_stackframe();
2374 char *endp = rparam + rprcnt;
2375 int i, converter = 0, count = 0;
2377 p = rparam + WORDSIZE;
2378 GETWORD(p, converter, endp);
2379 GETWORD(p, count, endp);
2381 endp = rdata + rdrcnt;
2382 for (i=0,p=rdata;i<count && p < endp;i++) {
2383 char *wsname, *username, *clitype_name;
2384 uint16_t num_conns = 0, num_opens = 0, num_users = 0;
2385 unsigned int sess_time = 0, idle_time = 0, user_flags = 0;
2387 p += rap_getstringp(frame,
2389 &wsname,
2390 rdata,
2391 converter,
2392 endp);
2393 p += rap_getstringp(frame,
2395 &username,
2396 rdata,
2397 converter,
2398 endp);
2399 GETWORD(p, num_conns, endp);
2400 GETWORD(p, num_opens, endp);
2401 GETWORD(p, num_users, endp);
2402 GETDWORD(p, sess_time, endp);
2403 GETDWORD(p, idle_time, endp);
2404 GETDWORD(p, user_flags, endp);
2405 p += rap_getstringp(frame,
2407 &clitype_name,
2408 rdata,
2409 converter,
2410 endp);
2412 if (wsname && username && clitype_name) {
2413 fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2414 idle_time, user_flags, clitype_name);
2417 TALLOC_FREE(frame);
2418 } else {
2419 DEBUG(4,("NetSessionEnum res=%d\n", res));
2422 out:
2424 SAFE_FREE(rparam);
2425 SAFE_FREE(rdata);
2427 return res;
2430 /****************************************************************************
2431 Call a NetSessionGetInfo - get information about other session to an SMB server.
2432 ****************************************************************************/
2434 int cli_NetSessionGetInfo(struct cli_state *cli, const char *workstation,
2435 void (*fn)(const char *, const char *, uint16, uint16, uint16, uint, uint, uint, const char *))
2437 char param[WORDSIZE /* api number */
2438 +sizeof(RAP_NetSessionGetInfo_REQ) /* req string */
2439 +sizeof(RAP_SESSION_INFO_L2) /* return string */
2440 +RAP_MACHNAME_LEN /* wksta name */
2441 +WORDSIZE /* info level */
2442 +WORDSIZE]; /* buffer size */
2443 char *p;
2444 char *rparam = NULL;
2445 char *rdata = NULL;
2446 unsigned int rprcnt, rdrcnt;
2447 char *endp;
2448 int res = -1;
2450 memset(param, '\0', sizeof(param));
2451 p = make_header(param, RAP_WsessionGetInfo,
2452 RAP_NetSessionGetInfo_REQ, RAP_SESSION_INFO_L2);
2453 PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2454 PUTWORD(p,2); /* Info level 2 */
2455 PUTWORD(p,0xFF); /* Return buffer size */
2457 if (cli_api(cli,
2458 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2459 NULL, 0, CLI_BUFFER_SIZE,
2460 &rparam, &rprcnt,
2461 &rdata, &rdrcnt)) {
2462 endp = rparam + rprcnt;
2463 res = GETRES(rparam, endp);
2464 cli->rap_error = res;
2465 if (cli->rap_error != 0) {
2466 DEBUG(1,("NetSessionGetInfo gave error %d\n", cli->rap_error));
2470 if (!rdata) {
2471 DEBUG(4,("NetSessionGetInfo no data returned\n"));
2472 goto out;
2475 endp = rparam + rprcnt;
2476 res = GETRES(rparam, endp);
2478 if (res == 0 || res == ERRmoredata) {
2479 TALLOC_CTX *frame = talloc_stackframe();
2480 int converter = 0;
2481 char *wsname, *username, *clitype_name;
2482 uint16_t num_conns = 0, num_opens = 0, num_users = 0;
2483 unsigned int sess_time = 0, idle_time = 0, user_flags = 0;
2485 p = rparam + WORDSIZE;
2486 GETWORD(p, converter,endp);
2487 p += WORDSIZE; /* skip rsize */
2489 p = rdata;
2490 endp = rdata + rdrcnt;
2491 p += rap_getstringp(frame,
2493 &wsname,
2494 rdata,
2495 converter,
2496 endp);
2497 p += rap_getstringp(frame,
2499 &username,
2500 rdata,
2501 converter,
2502 endp);
2503 GETWORD(p, num_conns, endp);
2504 GETWORD(p, num_opens, endp);
2505 GETWORD(p, num_users, endp);
2506 GETDWORD(p, sess_time, endp);
2507 GETDWORD(p, idle_time, endp);
2508 GETDWORD(p, user_flags, endp);
2509 p += rap_getstringp(frame,
2511 &clitype_name,
2512 rdata,
2513 converter,
2514 endp);
2516 if (wsname && username && clitype_name) {
2517 fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2518 idle_time, user_flags, clitype_name);
2520 TALLOC_FREE(frame);
2521 } else {
2522 DEBUG(4,("NetSessionGetInfo res=%d\n", res));
2525 out:
2527 SAFE_FREE(rparam);
2528 SAFE_FREE(rdata);
2530 return res;
2533 /****************************************************************************
2534 Call a NetSessionDel - close a session to an SMB server.
2535 ****************************************************************************/
2537 int cli_NetSessionDel(struct cli_state *cli, const char *workstation)
2539 char param[WORDSIZE /* api number */
2540 +sizeof(RAP_NetSessionDel_REQ) /* req string */
2541 +1 /* no return string */
2542 +RAP_MACHNAME_LEN /* workstation name */
2543 +WORDSIZE]; /* reserved (0) */
2544 char *p;
2545 char *rparam = NULL;
2546 char *rdata = NULL;
2547 unsigned int rprcnt, rdrcnt;
2548 int res = -1;
2550 memset(param, '\0', sizeof(param));
2551 p = make_header(param, RAP_WsessionDel, RAP_NetSessionDel_REQ, NULL);
2552 PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2553 PUTWORD(p,0); /* reserved word of 0 */
2555 if (cli_api(cli,
2556 param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
2557 NULL, 0, 200, /* data, length, maxlen */
2558 &rparam, &rprcnt, /* return params, length */
2559 &rdata, &rdrcnt)) /* return data, length */
2561 char *endp = rparam + rprcnt;
2562 res = GETRES(rparam, endp);
2563 cli->rap_error = res;
2565 if (res == 0) {
2566 /* nothing to do */
2567 } else {
2568 DEBUG(4,("NetFileClose2 res=%d\n", res));
2570 } else {
2571 res = -1;
2572 DEBUG(4,("NetFileClose2 failed\n"));
2575 SAFE_FREE(rparam);
2576 SAFE_FREE(rdata);
2578 return res;
2581 int cli_NetConnectionEnum(struct cli_state *cli, const char *qualifier,
2582 void (*fn)(uint16_t conid, uint16_t contype,
2583 uint16_t numopens, uint16_t numusers,
2584 uint32_t contime, const char *username,
2585 const char *netname))
2587 char param[WORDSIZE /* api number */
2588 +sizeof(RAP_NetConnectionEnum_REQ) /* req string */
2589 +sizeof(RAP_CONNECTION_INFO_L1) /* return string */
2590 +RAP_MACHNAME_LEN /* wksta name */
2591 +WORDSIZE /* info level */
2592 +WORDSIZE]; /* buffer size */
2593 char *p;
2594 char *rparam = NULL;
2595 char *rdata = NULL;
2596 unsigned int rprcnt, rdrcnt;
2597 int res = -1;
2599 memset(param, '\0', sizeof(param));
2600 p = make_header(param, RAP_WconnectionEnum,
2601 RAP_NetConnectionEnum_REQ, RAP_CONNECTION_INFO_L1);
2602 PUTSTRING(p, qualifier, RAP_MACHNAME_LEN-1);/* Workstation name */
2603 PUTWORD(p,1); /* Info level 1 */
2604 PUTWORD(p,0xFFE0); /* Return buffer size */
2606 if (cli_api(cli,
2607 param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2608 NULL, 0, CLI_BUFFER_SIZE,
2609 &rparam, &rprcnt,
2610 &rdata, &rdrcnt)) {
2611 char *endp = rparam + rprcnt;
2612 res = GETRES(rparam, endp);
2613 cli->rap_error = res;
2614 if (res != 0) {
2615 DEBUG(1,("NetConnectionEnum gave error %d\n", res));
2619 if (!rdata) {
2620 DEBUG(4,("NetConnectionEnum no data returned\n"));
2621 goto out;
2624 if (res == 0 || res == ERRmoredata) {
2625 TALLOC_CTX *frame = talloc_stackframe();
2626 char *endp = rparam + rprcnt;
2627 int i, converter = 0, count = 0;
2629 p = rparam + WORDSIZE;
2630 GETWORD(p, converter, endp);
2631 GETWORD(p, count, endp);
2633 endp = rdata + rdrcnt;
2634 for (i=0,p=rdata;i<count && p < endp;i++) {
2635 char *netname, *username;
2636 uint16_t conn_id = 0, conn_type = 0, num_opens = 0, num_users = 0;
2637 unsigned int conn_time = 0;
2639 GETWORD(p,conn_id, endp);
2640 GETWORD(p,conn_type, endp);
2641 GETWORD(p,num_opens, endp);
2642 GETWORD(p,num_users, endp);
2643 GETDWORD(p,conn_time, endp);
2644 p += rap_getstringp(frame,
2646 &username,
2647 rdata,
2648 converter,
2649 endp);
2650 p += rap_getstringp(frame,
2652 &netname,
2653 rdata,
2654 converter,
2655 endp);
2657 if (username && netname) {
2658 fn(conn_id, conn_type, num_opens, num_users, conn_time,
2659 username, netname);
2662 TALLOC_FREE(frame);
2663 } else {
2664 DEBUG(4,("NetConnectionEnum res=%d\n", res));
2667 out:
2669 SAFE_FREE(rdata);
2670 SAFE_FREE(rparam);
2671 return res;