Add net rap file user
[ira/wip.git] / source3 / libsmb / clirap2.c
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                     
7    
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.
12    
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.
17    
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/>.
20 */
21
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 /*****************************************************/
61
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 /*****************************************************/ 
77
78 #include "includes.h" 
79
80 #define WORDSIZE 2
81 #define DWORDSIZE 4
82
83 #define PUTBYTE(p,b) do {SCVAL(p,0,b); p++;} while(0)
84 #define GETBYTE(p,b) do {b = CVAL(p,0); p++;} while(0)
85 #define PUTWORD(p,w) do {SSVAL(p,0,w); p += WORDSIZE;} while(0)
86 #define GETWORD(p,w) do {w = SVAL(p,0); p += WORDSIZE;} while(0)
87 #define PUTDWORD(p,d) do {SIVAL(p,0,d); p += DWORDSIZE;} while(0)
88 #define GETDWORD(p,d) do {d = IVAL(p,0); p += DWORDSIZE;} while(0)
89 #define GETRES(p) p ? SVAL(p,0) : -1
90 /* put string s at p with max len n and increment p past string */
91 #define PUTSTRING(p,s,n) do {\
92   push_ascii(p,s?s:"",n?n:256,STR_TERMINATE);\
93   p = push_skip_string(p);\
94   } while(0)
95 /* put string s and p, using fixed len l, and increment p by l */
96 #define PUTSTRINGF(p,s,l) do {\
97   push_ascii(p,s?s:"",l,STR_TERMINATE);\
98   p += l;\
99   } while (0)
100 /* put string pointer at p, supplying offset o from rdata r, store   */
101 /* dword offset at p, increment p by 4 and o by length of s.  This   */
102 /* means on the first call, you must calc the offset yourself!       */
103 #define PUTSTRINGP(p,s,r,o) do {\
104   if (s) {\
105     push_ascii(r+o,s,strlen(s)+1,STR_TERMINATE);\
106     PUTDWORD(p,o);\
107     o += strlen(s) + 1;\
108   } else PUTDWORD(p,0);\
109   }while(0);
110 /* get asciiz string s from p, increment p past string */
111 #define GETSTRING(p,s) do {\
112   pull_ascii_pstring(s,p);\
113   p = push_skip_string(p);\
114   } while(0)
115 /* get fixed length l string s from p, increment p by l */
116 #define GETSTRINGF(p,s,l) do {\
117   pull_ascii_pstring(s,p);\
118   p += l;\
119   } while(0)
120 /* get string s from offset (obtained at p) from rdata r - converter c */
121 #define GETSTRINGP(p,s,r,c) do {\
122   uint32 off;\
123   GETDWORD(p,off);\
124   off &= 0x0000FFFF; /* mask the obsolete segment number from the offset */ \
125   pull_ascii_pstring(s, off?(r+off-c):"");\
126   } while(0)
127
128 static char *make_header(char *param, uint16 apinum, const char *reqfmt, const char *datafmt)
129 {
130   PUTWORD(param,apinum);
131   if (reqfmt) 
132     PUTSTRING(param,reqfmt,0);
133   else 
134     *param++ = (char) 0;
135
136   if (datafmt)
137     PUTSTRING(param,datafmt,0);
138   else
139     *param++ = (char) 0;
140
141   return param;
142 }
143     
144
145 /****************************************************************************
146  call a NetGroupDelete - delete user group from remote server
147 ****************************************************************************/
148 int cli_NetGroupDelete(struct cli_state *cli, const char *group_name )
149 {
150   char *rparam = NULL;
151   char *rdata = NULL;
152   char *p;
153   unsigned int rdrcnt,rprcnt;
154   int res;
155   char param[WORDSIZE                    /* api number    */
156             +sizeof(RAP_NetGroupDel_REQ) /* parm string   */
157             +1                           /* no ret string */
158             +RAP_GROUPNAME_LEN           /* group to del  */
159             +WORDSIZE];                  /* reserved word */
160
161   /* now send a SMBtrans command with api GroupDel */
162   p = make_header(param, RAP_WGroupDel, RAP_NetGroupDel_REQ, NULL);  
163   PUTSTRING(p, group_name, RAP_GROUPNAME_LEN);
164   PUTWORD(p,0);  /* reserved word MBZ on input */
165                  
166   if (cli_api(cli, 
167               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
168               NULL, 0, 200,       /* data, length, maxlen */
169               &rparam, &rprcnt,   /* return params, length */
170               &rdata, &rdrcnt))   /* return data, length */
171     {
172       res = GETRES(rparam);
173                         
174       if (res == 0) {
175         /* nothing to do */             
176       }
177       else if ((res == 5) || (res == 65)) {
178           DEBUG(1, ("Access Denied\n"));
179       }
180       else if (res == 2220) {
181          DEBUG (1, ("Group does not exist\n"));
182       }
183       else {
184         DEBUG(4,("NetGroupDelete res=%d\n", res));
185       }      
186     } else {
187       res = -1;
188       DEBUG(4,("NetGroupDelete failed\n"));
189     }
190   
191   SAFE_FREE(rparam);
192   SAFE_FREE(rdata);
193   
194   return res;
195 }
196
197 /****************************************************************************
198  call a NetGroupAdd - add user group to remote server
199 ****************************************************************************/
200 int cli_NetGroupAdd(struct cli_state *cli, RAP_GROUP_INFO_1 * grinfo )
201 {
202   char *rparam = NULL;
203   char *rdata = NULL;
204   char *p;
205   unsigned int rdrcnt,rprcnt;
206   int res;
207   char param[WORDSIZE                    /* api number    */
208             +sizeof(RAP_NetGroupAdd_REQ) /* req string    */
209             +sizeof(RAP_GROUP_INFO_L1)   /* return string */
210             +WORDSIZE                    /* info level    */
211             +WORDSIZE];                  /* reserved word */
212
213   /* offset into data of free format strings.  Will be updated */
214   /* by PUTSTRINGP macro and end up with total data length.    */
215   int soffset = RAP_GROUPNAME_LEN + 1 + DWORDSIZE; 
216   char *data;
217   size_t data_size;
218
219   /* Allocate data. */
220   data_size = MAX(soffset + strlen(grinfo->comment) + 1, 1024);
221
222   data = SMB_MALLOC_ARRAY(char, data_size);
223   if (!data) {
224     DEBUG (1, ("Malloc fail\n"));
225     return -1;
226   }
227
228   /* now send a SMBtrans command with api WGroupAdd */
229   
230   p = make_header(param, RAP_WGroupAdd,
231                   RAP_NetGroupAdd_REQ, RAP_GROUP_INFO_L1); 
232   PUTWORD(p, 1); /* info level */
233   PUTWORD(p, 0); /* reserved word 0 */
234   
235   p = data;
236   PUTSTRINGF(p, grinfo->group_name, RAP_GROUPNAME_LEN);
237   PUTBYTE(p, 0); /* pad byte 0 */
238   PUTSTRINGP(p, grinfo->comment, data, soffset);
239   
240   if (cli_api(cli, 
241               param, sizeof(param), 1024, /* Param, length, maxlen */
242               data, soffset, sizeof(data), /* data, length, maxlen */
243               &rparam, &rprcnt,   /* return params, length */
244               &rdata, &rdrcnt))   /* return data, length */
245     {
246       res = GETRES(rparam);
247       
248       if (res == 0) {
249         /* nothing to do */             
250       } else if ((res == 5) || (res == 65)) {
251         DEBUG(1, ("Access Denied\n"));
252       }
253       else if (res == 2223) {
254         DEBUG (1, ("Group already exists\n"));
255       }
256       else {
257         DEBUG(4,("NetGroupAdd res=%d\n", res));
258       }
259     } else {
260       res = -1;
261       DEBUG(4,("NetGroupAdd failed\n"));
262     }
263   
264   SAFE_FREE(data);
265   SAFE_FREE(rparam);
266   SAFE_FREE(rdata);
267
268   return res;
269 }
270
271 /****************************************************************************
272 call a NetGroupEnum - try and list user groups on a different host
273 ****************************************************************************/
274 int cli_RNetGroupEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
275 {
276   char param[WORDSIZE                     /* api number    */
277             +sizeof(RAP_NetGroupEnum_REQ) /* parm string   */
278             +sizeof(RAP_GROUP_INFO_L1)    /* return string */
279             +WORDSIZE                     /* info level    */
280             +WORDSIZE];                   /* buffer size   */
281   char *p;
282   char *rparam = NULL;
283   char *rdata = NULL; 
284   unsigned int rprcnt, rdrcnt;
285   int res = -1;
286   
287   
288   memset(param, '\0', sizeof(param));
289   p = make_header(param, RAP_WGroupEnum,
290                   RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L1);
291   PUTWORD(p,1); /* Info level 1 */  /* add level 0 */
292   PUTWORD(p,0xFFE0); /* Return buffer size */
293
294   if (cli_api(cli,
295               param, PTR_DIFF(p,param),8,
296               NULL, 0, 0xFFE0 /* data area size */,
297               &rparam, &rprcnt,
298               &rdata, &rdrcnt)) {
299     res = GETRES(rparam);
300     cli->rap_error = res;
301     if(cli->rap_error == 234) 
302         DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
303     else if (cli->rap_error != 0) {
304       DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
305     }
306   }
307
308   if (rdata) {
309     if (res == 0 || res == ERRmoredata) {
310       int i, converter, count;
311
312       p = rparam + WORDSIZE; /* skip result */
313       GETWORD(p, converter);
314       GETWORD(p, count);
315
316       for (i=0,p=rdata;i<count;i++) {
317             pstring comment;
318             char groupname[RAP_GROUPNAME_LEN];
319
320             GETSTRINGF(p, groupname, RAP_GROUPNAME_LEN);
321             p++; /* pad byte */
322             GETSTRINGP(p, comment, rdata, converter);
323
324             fn(groupname, comment, cli);
325       } 
326     } else {
327       DEBUG(4,("NetGroupEnum res=%d\n", res));
328     }
329   } else {
330     DEBUG(4,("NetGroupEnum no data returned\n"));
331   }
332     
333   SAFE_FREE(rparam);
334   SAFE_FREE(rdata);
335
336   return res;
337 }
338
339 int cli_RNetGroupEnum0(struct cli_state *cli,
340                        void (*fn)(const char *, void *),
341                        void *state)
342 {
343   char param[WORDSIZE                     /* api number    */
344             +sizeof(RAP_NetGroupEnum_REQ) /* parm string   */
345             +sizeof(RAP_GROUP_INFO_L0)    /* return string */
346             +WORDSIZE                     /* info level    */
347             +WORDSIZE];                   /* buffer size   */
348   char *p;
349   char *rparam = NULL;
350   char *rdata = NULL; 
351   unsigned int rprcnt, rdrcnt;
352   int res = -1;
353   
354   
355   memset(param, '\0', sizeof(param));
356   p = make_header(param, RAP_WGroupEnum,
357                   RAP_NetGroupEnum_REQ, RAP_GROUP_INFO_L0);
358   PUTWORD(p,0); /* Info level 0 */ /* Hmmm. I *very* much suspect this
359                                       is the resume count, at least
360                                       that's what smbd believes... */
361   PUTWORD(p,0xFFE0); /* Return buffer size */
362
363   if (cli_api(cli,
364               param, PTR_DIFF(p,param),8,
365               NULL, 0, 0xFFE0 /* data area size */,
366               &rparam, &rprcnt,
367               &rdata, &rdrcnt)) {
368     res = GETRES(rparam);
369     cli->rap_error = res;
370     if(cli->rap_error == 234) 
371         DEBUG(1,("Not all group names were returned (such as those longer than 21 characters)\n"));
372     else if (cli->rap_error != 0) {
373       DEBUG(1,("NetGroupEnum gave error %d\n", cli->rap_error));
374     }
375   }
376
377   if (rdata) {
378     if (res == 0 || res == ERRmoredata) {
379       int i, count;
380
381       p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
382       GETWORD(p, count);
383
384       for (i=0,p=rdata;i<count;i++) {
385             char groupname[RAP_GROUPNAME_LEN];
386             GETSTRINGF(p, groupname, RAP_GROUPNAME_LEN);
387             fn(groupname, cli);
388       } 
389     } else {
390       DEBUG(4,("NetGroupEnum res=%d\n", res));
391     }
392   } else {
393     DEBUG(4,("NetGroupEnum no data returned\n"));
394   }
395     
396   SAFE_FREE(rparam);
397   SAFE_FREE(rdata);
398
399   return res;
400 }
401
402 int cli_NetGroupDelUser(struct cli_state * cli, const char *group_name, const char *user_name)
403 {
404   char *rparam = NULL;
405   char *rdata = NULL;
406   char *p;
407   unsigned int rdrcnt,rprcnt;
408   int res;
409   char param[WORDSIZE                        /* api number    */
410             +sizeof(RAP_NetGroupDelUser_REQ) /* parm string   */
411             +1                               /* no ret string */
412             +RAP_GROUPNAME_LEN               /* group name    */
413             +RAP_USERNAME_LEN];              /* user to del   */
414
415   /* now send a SMBtrans command with api GroupMemberAdd */
416   p = make_header(param, RAP_WGroupDelUser, RAP_NetGroupDelUser_REQ, NULL);
417   PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
418   PUTSTRING(p,user_name,RAP_USERNAME_LEN);
419
420   if (cli_api(cli, 
421               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
422               NULL, 0, 200,       /* data, length, maxlen */
423               &rparam, &rprcnt,   /* return params, length */
424               &rdata, &rdrcnt))   /* return data, length */
425     {
426       res = GETRES(rparam);
427       
428       switch(res) {
429         case 0:
430           break;
431         case 5:
432         case 65:
433           DEBUG(1, ("Access Denied\n"));
434           break;
435         case 50:
436           DEBUG(1, ("Not supported by server\n"));
437           break;
438         case 2220:
439           DEBUG(1, ("Group does not exist\n"));
440           break;
441         case 2221:
442           DEBUG(1, ("User does not exist\n"));
443           break;
444         case 2237:
445           DEBUG(1, ("User is not in group\n"));
446           break;
447         default:
448           DEBUG(4,("NetGroupDelUser res=%d\n", res));
449       }
450     } else {
451       res = -1;
452       DEBUG(4,("NetGroupDelUser failed\n"));
453     }
454   
455   SAFE_FREE(rparam);
456   SAFE_FREE(rdata);
457         
458   return res; 
459 }
460
461 int cli_NetGroupAddUser(struct cli_state * cli, const char *group_name, const char *user_name)
462 {
463   char *rparam = NULL;
464   char *rdata = NULL;
465   char *p;
466   unsigned int rdrcnt,rprcnt;
467   int res;
468   char param[WORDSIZE                        /* api number    */
469             +sizeof(RAP_NetGroupAddUser_REQ) /* parm string   */
470             +1                               /* no ret string */
471             +RAP_GROUPNAME_LEN               /* group name    */
472             +RAP_USERNAME_LEN];              /* user to add   */
473
474   /* now send a SMBtrans command with api GroupMemberAdd */
475   p = make_header(param, RAP_WGroupAddUser, RAP_NetGroupAddUser_REQ, NULL);
476   PUTSTRING(p,group_name,RAP_GROUPNAME_LEN);
477   PUTSTRING(p,user_name,RAP_USERNAME_LEN);
478
479   if (cli_api(cli, 
480               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
481               NULL, 0, 200,       /* data, length, maxlen */
482               &rparam, &rprcnt,   /* return params, length */
483               &rdata, &rdrcnt))   /* return data, length */
484     {
485       res = GETRES(rparam);
486       
487       switch(res) {
488         case 0:
489           break;
490         case 5:
491         case 65:
492           DEBUG(1, ("Access Denied\n"));
493           break;
494         case 50:
495           DEBUG(1, ("Not supported by server\n"));
496           break;
497         case 2220:
498           DEBUG(1, ("Group does not exist\n"));
499           break;
500         case 2221:
501           DEBUG(1, ("User does not exist\n"));
502           break;
503         default:
504           DEBUG(4,("NetGroupAddUser res=%d\n", res));
505       }
506     } else {
507       res = -1;
508       DEBUG(4,("NetGroupAddUser failed\n"));
509     }
510   
511   SAFE_FREE(rparam);
512   SAFE_FREE(rdata);
513         
514   return res;
515 }
516
517
518 int cli_NetGroupGetUsers(struct cli_state * cli, const char *group_name, void (*fn)(const char *, void *), void *state )
519 {
520   char *rparam = NULL;
521   char *rdata = NULL;
522   char *p;
523   unsigned int rdrcnt,rprcnt;
524   int res = -1;
525   char param[WORDSIZE                        /* api number    */
526             +sizeof(RAP_NetGroupGetUsers_REQ)/* parm string   */
527             +sizeof(RAP_GROUP_USERS_INFO_0)  /* return string */
528             +RAP_GROUPNAME_LEN               /* group name    */
529             +WORDSIZE                        /* info level    */
530             +WORDSIZE];                      /* buffer size   */
531
532   /* now send a SMBtrans command with api GroupGetUsers */
533   p = make_header(param, RAP_WGroupGetUsers,
534                   RAP_NetGroupGetUsers_REQ, RAP_GROUP_USERS_INFO_0);
535   PUTSTRING(p,group_name,RAP_GROUPNAME_LEN-1);
536   PUTWORD(p,0); /* info level 0 */
537   PUTWORD(p,0xFFE0); /* return buffer size */
538
539   if (cli_api(cli,
540               param, PTR_DIFF(p,param),PTR_DIFF(p,param),
541               NULL, 0, CLI_BUFFER_SIZE,
542               &rparam, &rprcnt,
543               &rdata, &rdrcnt)) {
544     res = GETRES(rparam);
545     cli->rap_error = res;
546     if (res != 0) {
547       DEBUG(1,("NetGroupGetUsers gave error %d\n", res));
548     }
549   }
550   if (rdata) {
551     if (res == 0 || res == ERRmoredata) {
552       int i, count;
553       fstring username;
554       p = rparam + WORDSIZE + WORDSIZE;
555       GETWORD(p, count);
556
557       for (i=0,p=rdata; i<count; i++) {
558         GETSTRINGF(p, username, RAP_USERNAME_LEN);
559         fn(username, state);
560       }
561     } else {
562       DEBUG(4,("NetGroupGetUsers res=%d\n", res));
563     }
564   } else {
565     DEBUG(4,("NetGroupGetUsers no data returned\n"));
566   }
567   SAFE_FREE(rdata);
568   SAFE_FREE(rparam);
569   return res;
570 }
571
572 int cli_NetUserGetGroups(struct cli_state * cli, const char *user_name, void (*fn)(const char *, void *), void *state )
573 {
574   char *rparam = NULL;
575   char *rdata = NULL;
576   char *p;
577   unsigned int rdrcnt,rprcnt;
578   int res = -1;
579   char param[WORDSIZE                        /* api number    */
580             +sizeof(RAP_NetUserGetGroups_REQ)/* parm string   */
581             +sizeof(RAP_GROUP_USERS_INFO_0)  /* return string */
582             +RAP_USERNAME_LEN               /* user name    */
583             +WORDSIZE                        /* info level    */
584             +WORDSIZE];                      /* buffer size   */
585
586   /* now send a SMBtrans command with api GroupGetUsers */
587   p = make_header(param, RAP_WUserGetGroups,
588                   RAP_NetUserGetGroups_REQ, RAP_GROUP_USERS_INFO_0);
589   PUTSTRING(p,user_name,RAP_USERNAME_LEN-1);
590   PUTWORD(p,0); /* info level 0 */
591   PUTWORD(p,0xFFE0); /* return buffer size */
592
593   if (cli_api(cli,
594               param, PTR_DIFF(p,param),PTR_DIFF(p,param),
595               NULL, 0, CLI_BUFFER_SIZE,
596               &rparam, &rprcnt,
597               &rdata, &rdrcnt)) {
598     res = GETRES(rparam);
599     cli->rap_error = res;
600     if (res != 0) {
601       DEBUG(1,("NetUserGetGroups gave error %d\n", res));
602     }
603   }
604   if (rdata) {
605     if (res == 0 || res == ERRmoredata) {
606       int i, count;
607       fstring groupname;
608       p = rparam + WORDSIZE + WORDSIZE;
609       GETWORD(p, count);
610
611       for (i=0,p=rdata; i<count; i++) {
612         GETSTRINGF(p, groupname, RAP_USERNAME_LEN);
613             fn(groupname, state);
614       }
615     } else {
616       DEBUG(4,("NetUserGetGroups res=%d\n", res));
617     }
618   } else {
619     DEBUG(4,("NetUserGetGroups no data returned\n"));
620   }
621   SAFE_FREE(rdata);
622   SAFE_FREE(rparam);
623   return res;
624 }
625
626
627 /****************************************************************************
628  call a NetUserDelete - delete user from remote server
629 ****************************************************************************/
630 int cli_NetUserDelete(struct cli_state *cli, const char * user_name )
631 {
632   char *rparam = NULL;
633   char *rdata = NULL;
634   char *p;
635   unsigned int rdrcnt,rprcnt;
636   int res;
637   char param[WORDSIZE                    /* api number    */
638             +sizeof(RAP_NetGroupDel_REQ) /* parm string   */
639             +1                           /* no ret string */
640             +RAP_USERNAME_LEN            /* user to del   */
641             +WORDSIZE];                  /* reserved word */
642
643   /* now send a SMBtrans command with api UserDel */
644   p = make_header(param, RAP_WUserDel, RAP_NetGroupDel_REQ, NULL);  
645   PUTSTRING(p, user_name, RAP_USERNAME_LEN);
646   PUTWORD(p,0);  /* reserved word MBZ on input */
647                  
648   if (cli_api(cli, 
649               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
650               NULL, 0, 200,       /* data, length, maxlen */
651               &rparam, &rprcnt,   /* return params, length */
652               &rdata, &rdrcnt))   /* return data, length */
653     {
654       res = GETRES(rparam);
655       
656       if (res == 0) {
657         /* nothing to do */             
658       }
659       else if ((res == 5) || (res == 65)) {
660          DEBUG(1, ("Access Denied\n"));
661       }
662       else if (res == 2221) {
663          DEBUG (1, ("User does not exist\n"));
664       }
665       else {
666           DEBUG(4,("NetUserDelete res=%d\n", res));
667       }      
668     } else {
669       res = -1;
670       DEBUG(4,("NetUserDelete failed\n"));
671     }
672   
673   SAFE_FREE(rparam);
674   SAFE_FREE(rdata);
675         
676   return res;
677 }
678
679 /****************************************************************************
680  call a NetUserAdd - add user to remote server
681 ****************************************************************************/
682 int cli_NetUserAdd(struct cli_state *cli, RAP_USER_INFO_1 * userinfo )
683 {
684    
685
686
687   char *rparam = NULL;
688   char *rdata = NULL;
689   char *p;                                          
690   unsigned int rdrcnt,rprcnt;
691   int res;
692   char param[WORDSIZE                    /* api number    */
693             +sizeof(RAP_NetUserAdd2_REQ) /* req string    */
694             +sizeof(RAP_USER_INFO_L1)    /* data string   */
695             +WORDSIZE                    /* info level    */
696             +WORDSIZE                    /* buffer length */
697             +WORDSIZE];                  /* reserved      */
698  
699   char data[1024];
700   /* offset into data of free format strings.  Will be updated */
701   /* by PUTSTRINGP macro and end up with total data length.    */
702   int soffset=RAP_USERNAME_LEN+1 /* user name + pad */
703     + RAP_UPASSWD_LEN            /* password        */
704     + DWORDSIZE                  /* password age    */
705     + WORDSIZE                   /* privilege       */
706     + DWORDSIZE                  /* home dir ptr    */
707     + DWORDSIZE                  /* comment ptr     */
708     + WORDSIZE                   /* flags           */
709     + DWORDSIZE;                 /* login script ptr*/
710
711   /* now send a SMBtrans command with api NetUserAdd */
712   p = make_header(param, RAP_WUserAdd2,
713                   RAP_NetUserAdd2_REQ, RAP_USER_INFO_L1);
714   PUTWORD(p, 1); /* info level */
715
716   PUTWORD(p, 0); /* pwencrypt */
717   if(userinfo->passwrd)
718     PUTWORD(p,MIN(strlen(userinfo->passwrd), RAP_UPASSWD_LEN));
719   else
720     PUTWORD(p, 0); /* password length */
721
722   p = data;
723   memset(data, '\0', soffset);
724
725   PUTSTRINGF(p, userinfo->user_name, RAP_USERNAME_LEN);
726   PUTBYTE(p, 0); /* pad byte 0 */
727   PUTSTRINGF(p, userinfo->passwrd, RAP_UPASSWD_LEN);
728   PUTDWORD(p, 0); /* pw age - n.a. on user add */
729   PUTWORD(p, userinfo->priv);
730   PUTSTRINGP(p, userinfo->home_dir, data, soffset);
731   PUTSTRINGP(p, userinfo->comment, data, soffset);
732   PUTWORD(p, userinfo->userflags);
733   PUTSTRINGP(p, userinfo->logon_script, data, soffset);
734
735   if (cli_api(cli, 
736               param, sizeof(param), 1024, /* Param, length, maxlen */
737               data, soffset, sizeof(data), /* data, length, maxlen */
738               &rparam, &rprcnt,   /* return params, length */
739               &rdata, &rdrcnt))   /* return data, length */
740     {
741       res = GETRES(rparam);
742       
743       if (res == 0) {
744         /* nothing to do */             
745       }       
746       else if ((res == 5) || (res == 65)) {
747         DEBUG(1, ("Access Denied\n"));
748       }
749       else if (res == 2224) {
750         DEBUG (1, ("User already exists\n"));
751       }
752       else {
753             DEBUG(4,("NetUserAdd res=%d\n", res));
754       }
755     } else {
756       res = -1;
757       DEBUG(4,("NetUserAdd failed\n"));
758     }
759   
760   SAFE_FREE(rparam);
761   SAFE_FREE(rdata);
762
763   return res;
764 }
765
766 /****************************************************************************
767 call a NetUserEnum - try and list users on a different host
768 ****************************************************************************/
769 int cli_RNetUserEnum(struct cli_state *cli, void (*fn)(const char *, const char *, const char *, const char *, void *), void *state)
770 {
771   char param[WORDSIZE                 /* api number    */
772             +sizeof(RAP_NetUserEnum_REQ) /* parm string   */
773             +sizeof(RAP_USER_INFO_L1)    /* return string */
774             +WORDSIZE                 /* info level    */
775             +WORDSIZE];               /* buffer size   */
776   char *p;
777   char *rparam = NULL;
778   char *rdata = NULL; 
779   unsigned int rprcnt, rdrcnt;
780   int res = -1;
781   
782
783   memset(param, '\0', sizeof(param));
784   p = make_header(param, RAP_WUserEnum,
785                   RAP_NetUserEnum_REQ, RAP_USER_INFO_L1);
786   PUTWORD(p,1); /* Info level 1 */
787   PUTWORD(p,0xFF00); /* Return buffer size */
788
789 /* BB Fix handling of large numbers of users to be returned */
790   if (cli_api(cli,
791               param, PTR_DIFF(p,param),8,
792               NULL, 0, CLI_BUFFER_SIZE,
793               &rparam, &rprcnt,
794               &rdata, &rdrcnt)) {
795     res = GETRES(rparam);
796     cli->rap_error = res;
797     if (cli->rap_error != 0) {
798       DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
799     }
800   }
801   if (rdata) {
802     if (res == 0 || res == ERRmoredata) {
803       int i, converter, count;
804       char username[RAP_USERNAME_LEN];
805       char userpw[RAP_UPASSWD_LEN];
806       pstring comment, homedir, logonscript;
807
808       p = rparam + WORDSIZE; /* skip result */
809       GETWORD(p, converter);
810       GETWORD(p, count);
811
812       for (i=0,p=rdata;i<count;i++) {
813         GETSTRINGF(p, username, RAP_USERNAME_LEN);
814         p++; /* pad byte */
815         GETSTRINGF(p, userpw, RAP_UPASSWD_LEN);
816         p += DWORDSIZE; /* skip password age */
817         p += WORDSIZE;  /* skip priv: 0=guest, 1=user, 2=admin */
818         GETSTRINGP(p, homedir, rdata, converter);
819         GETSTRINGP(p, comment, rdata, converter);
820         p += WORDSIZE;  /* skip flags */
821         GETSTRINGP(p, logonscript, rdata, converter);
822
823         fn(username, comment, homedir, logonscript, cli);
824       }
825     } else {
826       DEBUG(4,("NetUserEnum res=%d\n", res));
827     }
828   } else {
829     DEBUG(4,("NetUserEnum no data returned\n"));
830   }
831     
832   SAFE_FREE(rparam);
833   SAFE_FREE(rdata);
834
835   return res;
836 }
837
838 int cli_RNetUserEnum0(struct cli_state *cli,
839                       void (*fn)(const char *, void *),
840                       void *state)
841 {
842   char param[WORDSIZE                 /* api number    */
843             +sizeof(RAP_NetUserEnum_REQ) /* parm string   */
844             +sizeof(RAP_USER_INFO_L0)    /* return string */
845             +WORDSIZE                 /* info level    */
846             +WORDSIZE];               /* buffer size   */
847   char *p;
848   char *rparam = NULL;
849   char *rdata = NULL; 
850   unsigned int rprcnt, rdrcnt;
851   int res = -1;
852   
853
854   memset(param, '\0', sizeof(param));
855   p = make_header(param, RAP_WUserEnum,
856                   RAP_NetUserEnum_REQ, RAP_USER_INFO_L0);
857   PUTWORD(p,0); /* Info level 1 */
858   PUTWORD(p,0xFF00); /* Return buffer size */
859
860 /* BB Fix handling of large numbers of users to be returned */
861   if (cli_api(cli,
862               param, PTR_DIFF(p,param),8,
863               NULL, 0, CLI_BUFFER_SIZE,
864               &rparam, &rprcnt,
865               &rdata, &rdrcnt)) {
866     res = GETRES(rparam);
867     cli->rap_error = res;
868     if (cli->rap_error != 0) {
869       DEBUG(1,("NetUserEnum gave error %d\n", cli->rap_error));
870     }
871   }
872   if (rdata) {
873     if (res == 0 || res == ERRmoredata) {
874       int i, count;
875       char username[RAP_USERNAME_LEN];
876
877       p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
878       GETWORD(p, count);
879
880       for (i=0,p=rdata;i<count;i++) {
881         GETSTRINGF(p, username, RAP_USERNAME_LEN);
882         fn(username, cli);
883       }
884     } else {
885       DEBUG(4,("NetUserEnum res=%d\n", res));
886     }
887   } else {
888     DEBUG(4,("NetUserEnum no data returned\n"));
889   }
890     
891   SAFE_FREE(rparam);
892   SAFE_FREE(rdata);
893
894   return res;
895 }
896
897 /****************************************************************************
898  call a NetFileClose2 - close open file on another session to server
899 ****************************************************************************/
900 int cli_NetFileClose(struct cli_state *cli, uint32 file_id )
901 {
902   char *rparam = NULL;
903   char *rdata = NULL;
904   char *p;
905   unsigned int rdrcnt,rprcnt;
906   char param[WORDSIZE                    /* api number    */
907             +sizeof(RAP_WFileClose2_REQ) /* req string    */
908             +1                           /* no ret string */
909             +DWORDSIZE];                 /* file ID          */
910   int res = -1;
911
912   /* now send a SMBtrans command with api RNetShareEnum */
913   p = make_header(param, RAP_WFileClose2, RAP_WFileClose2_REQ, NULL);
914   PUTDWORD(p, file_id);  
915                  
916   if (cli_api(cli, 
917               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
918               NULL, 0, 200,       /* data, length, maxlen */
919               &rparam, &rprcnt,   /* return params, length */
920               &rdata, &rdrcnt))   /* return data, length */
921     {
922       res = GETRES(rparam);
923       
924       if (res == 0) {
925         /* nothing to do */             
926       } else if (res == 2314){
927          DEBUG(1, ("NetFileClose2 - attempt to close non-existant file open instance\n"));
928       } else {
929         DEBUG(4,("NetFileClose2 res=%d\n", res));
930       }      
931     } else {
932       res = -1;
933       DEBUG(4,("NetFileClose2 failed\n"));
934     }
935   
936   SAFE_FREE(rparam);
937   SAFE_FREE(rdata);
938   
939   return res;
940 }
941
942 /****************************************************************************
943 call a NetFileGetInfo - get information about server file opened from other
944      workstation
945 ****************************************************************************/
946 int cli_NetFileGetInfo(struct cli_state *cli, uint32 file_id, void (*fn)(const char *, const char *, uint16, uint16, uint32))
947 {
948   char *rparam = NULL;
949   char *rdata = NULL;
950   char *p;
951   unsigned int rdrcnt,rprcnt;
952   int res;
953   char param[WORDSIZE                      /* api number      */
954             +sizeof(RAP_WFileGetInfo2_REQ) /* req string      */
955             +sizeof(RAP_FILE_INFO_L3)      /* return string   */
956             +DWORDSIZE                     /* file ID          */
957             +WORDSIZE                      /* info level      */
958             +WORDSIZE];                    /* buffer size     */
959
960   /* now send a SMBtrans command with api RNetShareEnum */
961   p = make_header(param, RAP_WFileGetInfo2,
962                   RAP_WFileGetInfo2_REQ, RAP_FILE_INFO_L3); 
963   PUTDWORD(p, file_id);
964   PUTWORD(p, 3);  /* info level */
965   PUTWORD(p, 0x1000);   /* buffer size */ 
966   if (cli_api(cli, 
967               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
968               NULL, 0, 0x1000,  /* data, length, maxlen */
969               &rparam, &rprcnt,               /* return params, length */
970               &rdata, &rdrcnt))               /* return data, length */
971     {
972       res = GETRES(rparam);
973       if (res == 0 || res == ERRmoredata) {
974         int converter,id, perms, locks;
975         pstring fpath, fuser;
976           
977         p = rparam + WORDSIZE; /* skip result */
978         GETWORD(p, converter);
979
980         p = rdata;
981         GETDWORD(p, id);
982         GETWORD(p, perms);
983         GETWORD(p, locks);
984         GETSTRINGP(p, fpath, rdata, converter);
985         GETSTRINGP(p, fuser, rdata, converter);
986         
987         fn(fpath, fuser, perms, locks, id);
988       } else {
989         DEBUG(4,("NetFileGetInfo2 res=%d\n", res));
990       }      
991     } else {
992       res = -1;
993       DEBUG(4,("NetFileGetInfo2 failed\n"));
994     }
995   
996   SAFE_FREE(rparam);
997   SAFE_FREE(rdata);
998   
999   return res;
1000 }
1001
1002 /****************************************************************************
1003 * Call a NetFileEnum2 - list open files on an SMB server
1004
1005 * PURPOSE:  Remotes a NetFileEnum API call to the current server or target 
1006 *           server listing the files open via the network (and their
1007 *           corresponding open instance ids)
1008 *          
1009 * Dependencies: none
1010 *
1011 * Parameters: 
1012 *             cli    - pointer to cli_state structure
1013 *             user   - if present, return only files opened by this remote user
1014 *             base_path - if present, return only files opened below this 
1015 *                         base path
1016 *             fn     - display function to invoke for each entry in the result
1017 *                        
1018 *
1019 * Returns:
1020 *             True      - success
1021 *             False     - failure
1022 *
1023 ****************************************************************************/
1024 int cli_NetFileEnum(struct cli_state *cli, const char * user,
1025                     const char * base_path,
1026                     void (*fn)(const char *, const char *, uint16, uint16,
1027                                uint32))
1028 {
1029   char *rparam = NULL;
1030   char *rdata = NULL;
1031   char *p;
1032   unsigned int rdrcnt,rprcnt;
1033   char param[WORDSIZE                   /* api number      */
1034             +sizeof(RAP_WFileEnum2_REQ) /* req string      */
1035             +sizeof(RAP_FILE_INFO_L3)   /* return string   */
1036             +256                        /* base path (opt) */
1037             +RAP_USERNAME_LEN           /* user name (opt) */
1038             +WORDSIZE                   /* info level      */
1039             +WORDSIZE                   /* buffer size     */
1040             +DWORDSIZE                  /* resume key ?    */
1041             +DWORDSIZE];                /* resume key ?    */
1042   int count = -1;
1043
1044   /* now send a SMBtrans command with api RNetShareEnum */
1045   p = make_header(param, RAP_WFileEnum2,
1046                   RAP_WFileEnum2_REQ, RAP_FILE_INFO_L3); 
1047
1048   PUTSTRING(p, base_path, 256);
1049   PUTSTRING(p, user, RAP_USERNAME_LEN);
1050   PUTWORD(p, 3); /* info level */
1051   PUTWORD(p, 0xFF00);  /* buffer size */ 
1052   PUTDWORD(p, 0);  /* zero out the resume key */
1053   PUTDWORD(p, 0);  /* or is this one the resume key? */
1054                  
1055   if (cli_api(cli, 
1056               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1057               NULL, 0, 0xFF00,  /* data, length, maxlen */
1058               &rparam, &rprcnt,               /* return params, length */
1059               &rdata, &rdrcnt))               /* return data, length */
1060     {
1061       int res = GETRES(rparam);
1062       
1063       if (res == 0 || res == ERRmoredata) {
1064         int converter, i;
1065
1066         p = rparam + WORDSIZE; /* skip result */
1067         GETWORD(p, converter);
1068         GETWORD(p, count);
1069         
1070         p = rdata;
1071         for (i=0; i<count; i++) {
1072           int id, perms, locks;
1073           pstring fpath, fuser;
1074           
1075           GETDWORD(p, id);
1076           GETWORD(p, perms);
1077           GETWORD(p, locks);
1078           GETSTRINGP(p, fpath, rdata, converter);
1079           GETSTRINGP(p, fuser, rdata, converter);
1080
1081           fn(fpath, fuser, perms, locks, id);
1082         }  /* BB fix ERRmoredata case to send resume request */
1083       } else {
1084         DEBUG(4,("NetFileEnum2 res=%d\n", res));
1085       }      
1086     } else {
1087       DEBUG(4,("NetFileEnum2 failed\n"));
1088     }
1089   
1090   SAFE_FREE(rparam);
1091   SAFE_FREE(rdata);
1092   
1093   return count;
1094 }
1095
1096 /****************************************************************************
1097  call a NetShareAdd - share/export directory on remote server
1098 ****************************************************************************/
1099 int cli_NetShareAdd(struct cli_state *cli, RAP_SHARE_INFO_2 * sinfo )
1100 {
1101   char *rparam = NULL;
1102   char *rdata = NULL;
1103   char *p;
1104   unsigned int rdrcnt,rprcnt;
1105   int res;
1106   char param[WORDSIZE                  /* api number    */
1107             +sizeof(RAP_WShareAdd_REQ) /* req string    */
1108             +sizeof(RAP_SHARE_INFO_L2) /* return string */
1109             +WORDSIZE                  /* info level    */
1110             +WORDSIZE];                /* reserved word */
1111   char data[1024];
1112   /* offset to free format string section following fixed length data.  */
1113   /* will be updated by PUTSTRINGP macro and will end up with total len */
1114   int soffset = RAP_SHARENAME_LEN + 1 /* share name + pad   */
1115     + WORDSIZE                        /* share type    */
1116     + DWORDSIZE                       /* comment pointer */
1117     + WORDSIZE                        /* permissions */
1118     + WORDSIZE                        /* max users */
1119     + WORDSIZE                        /* active users */
1120     + DWORDSIZE                       /* share path */
1121     + RAP_SPASSWD_LEN + 1;            /* share password + pad */
1122
1123   memset(param,'\0',sizeof(param));
1124   /* now send a SMBtrans command with api RNetShareAdd */
1125   p = make_header(param, RAP_WshareAdd,
1126                   RAP_WShareAdd_REQ, RAP_SHARE_INFO_L2); 
1127   PUTWORD(p, 2); /* info level */
1128   PUTWORD(p, 0); /* reserved word 0 */
1129
1130   p = data;
1131   PUTSTRINGF(p, sinfo->share_name, RAP_SHARENAME_LEN);
1132   PUTBYTE(p, 0); /* pad byte 0 */
1133
1134   PUTWORD(p, sinfo->share_type);
1135   PUTSTRINGP(p, sinfo->comment, data, soffset);
1136   PUTWORD(p, sinfo->perms);
1137   PUTWORD(p, sinfo->maximum_users);
1138   PUTWORD(p, sinfo->active_users);
1139   PUTSTRINGP(p, sinfo->path, data, soffset);
1140   PUTSTRINGF(p, sinfo->password, RAP_SPASSWD_LEN);
1141   SCVAL(p,-1,0x0A); /* required 0x0A at end of password */
1142   
1143   if (cli_api(cli, 
1144               param, sizeof(param), 1024, /* Param, length, maxlen */
1145               data, soffset, sizeof(data), /* data, length, maxlen */
1146               &rparam, &rprcnt,   /* return params, length */
1147               &rdata, &rdrcnt))   /* return data, length */
1148     {
1149       res = rparam? SVAL(rparam,0) : -1;
1150                         
1151       if (res == 0) {
1152         /* nothing to do */             
1153       }
1154       else {
1155         DEBUG(4,("NetShareAdd res=%d\n", res));
1156       }      
1157     } else {
1158       res = -1;
1159       DEBUG(4,("NetShareAdd failed\n"));
1160     }
1161   
1162   SAFE_FREE(rparam);
1163   SAFE_FREE(rdata);
1164   
1165   return res;
1166 }
1167 /****************************************************************************
1168  call a NetShareDelete - unshare exported directory on remote server
1169 ****************************************************************************/
1170 int cli_NetShareDelete(struct cli_state *cli, const char * share_name )
1171 {
1172   char *rparam = NULL;
1173   char *rdata = NULL;
1174   char *p;
1175   unsigned int rdrcnt,rprcnt;
1176   int res;
1177   char param[WORDSIZE                  /* api number    */
1178             +sizeof(RAP_WShareDel_REQ) /* req string    */
1179             +1                         /* no ret string */
1180             +RAP_SHARENAME_LEN         /* share to del  */
1181             +WORDSIZE];                /* reserved word */
1182             
1183
1184   /* now send a SMBtrans command with api RNetShareDelete */
1185   p = make_header(param, RAP_WshareDel, RAP_WShareDel_REQ, NULL);
1186   PUTSTRING(p,share_name,RAP_SHARENAME_LEN);
1187   PUTWORD(p,0);  /* reserved word MBZ on input */
1188                  
1189   if (cli_api(cli, 
1190               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1191               NULL, 0, 200,       /* data, length, maxlen */
1192               &rparam, &rprcnt,   /* return params, length */
1193               &rdata, &rdrcnt))   /* return data, length */
1194     {
1195       res = GETRES(rparam);
1196                         
1197       if (res == 0) {
1198         /* nothing to do */             
1199       }
1200       else {
1201         DEBUG(4,("NetShareDelete res=%d\n", res));
1202       }      
1203     } else {
1204       res = -1;
1205       DEBUG(4,("NetShareDelete failed\n"));
1206     }
1207   
1208   SAFE_FREE(rparam);
1209   SAFE_FREE(rdata);
1210         
1211   return res;
1212 }
1213 /*************************************************************************
1214 *
1215 * Function Name:  cli_get_pdc_name
1216 *
1217 * PURPOSE:  Remotes a NetServerEnum API call to the current server
1218 *           requesting the name of a server matching the server
1219 *           type of SV_TYPE_DOMAIN_CTRL (PDC).
1220 *
1221 * Dependencies: none
1222 *
1223 * Parameters: 
1224 *             cli       - pointer to cli_state structure
1225 *             workgroup - pointer to string containing name of domain
1226 *             pdc_name  - pointer to string that will contain PDC name
1227 *                         on successful return
1228 *
1229 * Returns:
1230 *             True      - success
1231 *             False     - failure
1232 *
1233 ************************************************************************/
1234 BOOL cli_get_pdc_name(struct cli_state *cli, char *workgroup, char *pdc_name)
1235 {
1236   char *rparam = NULL;
1237   char *rdata = NULL;
1238   unsigned int rdrcnt,rprcnt;
1239   char *p;
1240   char param[WORDSIZE                       /* api number    */
1241             +sizeof(RAP_NetServerEnum2_REQ) /* req string    */
1242             +sizeof(RAP_SERVER_INFO_L1)     /* return string */
1243             +WORDSIZE                       /* info level    */
1244             +WORDSIZE                       /* buffer size   */
1245             +DWORDSIZE                      /* server type   */
1246             +RAP_MACHNAME_LEN];             /* workgroup     */
1247   int count = -1;
1248   
1249   *pdc_name = '\0';
1250
1251   /* send a SMBtrans command with api NetServerEnum */
1252   p = make_header(param, RAP_NetServerEnum2,
1253                   RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L1);
1254   PUTWORD(p, 1); /* info level */
1255   PUTWORD(p, CLI_BUFFER_SIZE);
1256   PUTDWORD(p, SV_TYPE_DOMAIN_CTRL);
1257   PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1258         
1259   if (cli_api(cli, 
1260               param, PTR_DIFF(p,param), 8,        /* params, length, max */
1261               NULL, 0, CLI_BUFFER_SIZE,               /* data, length, max */
1262               &rparam, &rprcnt,                   /* return params, return size */
1263               &rdata, &rdrcnt                     /* return data, return size */
1264               )) {
1265     cli->rap_error = GETRES(rparam);
1266                         
1267         /*
1268          * We only really care to copy a name if the
1269          * API succeeded and we got back a name.
1270          */
1271     if (cli->rap_error == 0) {
1272       p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1273       GETWORD(p, count);
1274       p = rdata;
1275       
1276       if (count > 0)
1277         GETSTRING(p, pdc_name);
1278     }
1279     else {
1280         DEBUG(4,("cli_get_pdc_name: machine %s failed the NetServerEnum call. "
1281                  "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1282     }
1283   }
1284   
1285   SAFE_FREE(rparam);
1286   SAFE_FREE(rdata);
1287   
1288   return(count > 0);
1289 }
1290
1291
1292 /*************************************************************************
1293 *
1294 * Function Name:  cli_get_server_domain
1295 *
1296 * PURPOSE:  Remotes a NetWkstaGetInfo API call to the current server
1297 *           requesting wksta_info_10 level information to determine
1298 *           the domain the server belongs to. On success, this
1299 *           routine sets the server_domain field in the cli_state structure
1300 *           to the server's domain name.
1301 *
1302 * Dependencies: none
1303 *
1304 * Parameters: 
1305 *             cli       - pointer to cli_state structure
1306 *
1307 * Returns:
1308 *             True      - success
1309 *             False     - failure
1310 *
1311 * Origins:  samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1312 *
1313 ************************************************************************/
1314 BOOL cli_get_server_domain(struct cli_state *cli)
1315 {
1316   char *rparam = NULL;
1317   char *rdata = NULL;
1318   unsigned int rdrcnt,rprcnt;
1319   char *p;
1320   char param[WORDSIZE                      /* api number    */
1321             +sizeof(RAP_WWkstaGetInfo_REQ) /* req string    */
1322             +sizeof(RAP_WKSTA_INFO_L10)    /* return string */
1323             +WORDSIZE                      /* info level    */
1324             +WORDSIZE];                    /* buffer size   */
1325   int res = -1;
1326   
1327   /* send a SMBtrans command with api NetWkstaGetInfo */
1328   p = make_header(param, RAP_WWkstaGetInfo,
1329                   RAP_WWkstaGetInfo_REQ, RAP_WKSTA_INFO_L10);
1330   PUTWORD(p, 10); /* info level */
1331   PUTWORD(p, CLI_BUFFER_SIZE);
1332         
1333   if (cli_api(cli, param, PTR_DIFF(p,param), 8, /* params, length, max */
1334               NULL, 0, CLI_BUFFER_SIZE,         /* data, length, max */
1335               &rparam, &rprcnt,         /* return params, return size */
1336               &rdata, &rdrcnt)) {       /* return data, return size */
1337     res = GETRES(rparam);
1338     p = rdata;          
1339     
1340     if (res == 0) {
1341       int converter;
1342
1343       p = rparam + WORDSIZE;
1344       GETWORD(p, converter);
1345       
1346       p = rdata + DWORDSIZE + DWORDSIZE; /* skip computer & user names */
1347       GETSTRINGP(p, cli->server_domain, rdata, converter);
1348     }
1349   }
1350   
1351   SAFE_FREE(rparam);
1352   SAFE_FREE(rdata);
1353   
1354   return(res == 0);
1355 }
1356
1357
1358 /*************************************************************************
1359 *
1360 * Function Name:  cli_get_server_type
1361 *
1362 * PURPOSE:  Remotes a NetServerGetInfo API call to the current server
1363 *           requesting server_info_1 level information to retrieve
1364 *           the server type.
1365 *
1366 * Dependencies: none
1367 *
1368 * Parameters: 
1369 *             cli       - pointer to cli_state structure
1370 *             pstype    - pointer to uint32 to contain returned server type
1371 *
1372 * Returns:
1373 *             True      - success
1374 *             False     - failure
1375 *
1376 * Origins:  samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1377 *
1378 ************************************************************************/
1379 BOOL cli_get_server_type(struct cli_state *cli, uint32 *pstype)
1380 {
1381   char *rparam = NULL;
1382   char *rdata = NULL;
1383   unsigned int rdrcnt,rprcnt;
1384   char *p;
1385   char param[WORDSIZE                       /* api number    */
1386             +sizeof(RAP_WserverGetInfo_REQ) /* req string    */
1387             +sizeof(RAP_SERVER_INFO_L1)     /* return string */
1388             +WORDSIZE                       /* info level    */
1389             +WORDSIZE];                     /* buffer size   */
1390   int res = -1;
1391   
1392   /* send a SMBtrans command with api NetServerGetInfo */
1393   p = make_header(param, RAP_WserverGetInfo,
1394                   RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1395   PUTWORD(p, 1); /* info level */
1396   PUTWORD(p, CLI_BUFFER_SIZE);
1397         
1398   if (cli_api(cli, 
1399               param, PTR_DIFF(p,param), 8, /* params, length, max */
1400               NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1401               &rparam, &rprcnt,         /* return params, return size */
1402               &rdata, &rdrcnt           /* return data, return size */
1403               )) {
1404     
1405     res = GETRES(rparam);
1406     
1407     if (res == 0 || res == ERRmoredata) {
1408       p = rdata;                                        
1409       *pstype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
1410     }
1411   }
1412   
1413   SAFE_FREE(rparam);
1414   SAFE_FREE(rdata);
1415   
1416   return(res == 0 || res == ERRmoredata);
1417 }
1418
1419 BOOL cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
1420                          char **servername)
1421 {
1422         char *rparam = NULL;
1423         char *rdata = NULL;
1424         unsigned int rdrcnt,rprcnt;
1425         char *p;
1426         char param[WORDSIZE                       /* api number    */
1427                    +sizeof(RAP_WserverGetInfo_REQ) /* req string    */
1428                    +sizeof(RAP_SERVER_INFO_L1)     /* return string */
1429                    +WORDSIZE                       /* info level    */
1430                    +WORDSIZE];                     /* buffer size   */
1431         BOOL res = False;
1432         fstring tmp;
1433   
1434         /* send a SMBtrans command with api NetServerGetInfo */
1435         p = make_header(param, RAP_WserverGetInfo,
1436                         RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1437         PUTWORD(p, 1); /* info level */
1438         PUTWORD(p, CLI_BUFFER_SIZE);
1439         
1440         if (!cli_api(cli, 
1441                      param, PTR_DIFF(p,param), 8, /* params, length, max */
1442                      NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1443                      &rparam, &rprcnt,         /* return params, return size */
1444                      &rdata, &rdrcnt           /* return data, return size */
1445                     )) {
1446                 goto failed;
1447         }
1448     
1449         if (GETRES(rparam) != 0) {
1450                 goto failed;
1451         }
1452
1453         if (rdrcnt < 16) {
1454                 DEBUG(10, ("invalid data count %d, expected >= 16\n", rdrcnt));
1455                 goto failed;
1456         }
1457
1458         if (pull_ascii(tmp, rdata, sizeof(tmp)-1, 16, STR_TERMINATE) == -1) {
1459                 DEBUG(10, ("pull_ascii failed\n"));
1460                 goto failed;
1461         }
1462
1463         if (!(*servername = talloc_strdup(mem_ctx, tmp))) {
1464                 DEBUG(1, ("talloc_strdup failed\n"));
1465                 goto failed;
1466         }
1467
1468         res = True;
1469
1470  failed:
1471         SAFE_FREE(rparam);
1472         SAFE_FREE(rdata);
1473         return res;
1474 }
1475
1476 /*************************************************************************
1477 *
1478 * Function Name:  cli_ns_check_server_type
1479 *
1480 * PURPOSE:  Remotes a NetServerEnum2 API call to the current server
1481 *           requesting server_info_0 level information of machines
1482 *           matching the given server type. If the returned server
1483 *           list contains the machine name contained in cli->desthost
1484 *           then we conclude the server type checks out. This routine
1485 *           is useful to retrieve list of server's of a certain
1486 *           type when all you have is a null session connection and
1487 *           can't remote API calls such as NetWkstaGetInfo or 
1488 *           NetServerGetInfo.
1489 *
1490 * Dependencies: none
1491 *
1492 * Parameters: 
1493 *             cli       - pointer to cli_state structure
1494 *             workgroup - pointer to string containing domain
1495 *             stype     - server type
1496 *
1497 * Returns:
1498 *             True      - success
1499 *             False     - failure
1500 *
1501 ************************************************************************/
1502 BOOL cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 stype)
1503 {
1504   char *rparam = NULL;
1505   char *rdata = NULL;
1506   unsigned int rdrcnt,rprcnt;
1507   char *p;
1508   char param[WORDSIZE                       /* api number    */
1509             +sizeof(RAP_NetServerEnum2_REQ) /* req string    */
1510             +sizeof(RAP_SERVER_INFO_L0)     /* return string */
1511             +WORDSIZE                       /* info level    */
1512             +WORDSIZE                       /* buffer size   */
1513             +DWORDSIZE                      /* server type   */
1514             +RAP_MACHNAME_LEN];             /* workgroup     */
1515   BOOL found_server = False;
1516   int res = -1;
1517   
1518   /* send a SMBtrans command with api NetServerEnum */
1519   p = make_header(param, RAP_NetServerEnum2,
1520                   RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L0);
1521   PUTWORD(p, 0); /* info level 0 */
1522   PUTWORD(p, CLI_BUFFER_SIZE);
1523   PUTDWORD(p, stype);
1524   PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1525         
1526   if (cli_api(cli, 
1527               param, PTR_DIFF(p,param), 8, /* params, length, max */
1528               NULL, 0, CLI_BUFFER_SIZE,  /* data, length, max */
1529               &rparam, &rprcnt,          /* return params, return size */
1530               &rdata, &rdrcnt            /* return data, return size */
1531               )) {
1532         
1533     res = GETRES(rparam);
1534     cli->rap_error = res;
1535
1536     if (res == 0 || res == ERRmoredata) {
1537       int i, count;
1538
1539       p = rparam + WORDSIZE + WORDSIZE;
1540       GETWORD(p, count);
1541
1542       p = rdata;
1543       for (i = 0;i < count;i++, p += 16) {
1544         char ret_server[RAP_MACHNAME_LEN];
1545
1546         GETSTRINGF(p, ret_server, RAP_MACHNAME_LEN);
1547         if (strequal(ret_server, cli->desthost)) {
1548           found_server = True;
1549           break;
1550         }
1551       }
1552     }
1553     else {
1554       DEBUG(4,("cli_ns_check_server_type: machine %s failed the NetServerEnum call. "
1555                "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1556     }
1557   }
1558   
1559   SAFE_FREE(rparam);
1560   SAFE_FREE(rdata);
1561         
1562   return found_server;
1563  }
1564
1565
1566 /****************************************************************************
1567  perform a NetWkstaUserLogoff
1568 ****************************************************************************/
1569 BOOL cli_NetWkstaUserLogoff(struct cli_state *cli,char *user, char *workstation)
1570 {
1571   char *rparam = NULL;
1572   char *rdata = NULL;
1573   char *p;
1574   unsigned int rdrcnt,rprcnt;
1575   char param[WORDSIZE                           /* api number    */
1576             +sizeof(RAP_NetWkstaUserLogoff_REQ) /* req string    */
1577             +sizeof(RAP_USER_LOGOFF_INFO_L1)    /* return string */
1578             +RAP_USERNAME_LEN+1                 /* user name+pad */
1579             +RAP_MACHNAME_LEN                   /* wksta name    */
1580             +WORDSIZE                           /* buffer size   */
1581             +WORDSIZE];                         /* buffer size?  */
1582   fstring upperbuf;
1583   
1584   memset(param, 0, sizeof(param));
1585
1586   /* send a SMBtrans command with api NetWkstaUserLogoff */
1587   p = make_header(param, RAP_WWkstaUserLogoff,
1588                   RAP_NetWkstaUserLogoff_REQ, RAP_USER_LOGOFF_INFO_L1);
1589   PUTDWORD(p, 0); /* Null pointer */
1590   PUTDWORD(p, 0); /* Null pointer */
1591   fstrcpy(upperbuf, user);
1592   strupper_m(upperbuf);
1593   PUTSTRINGF(p, upperbuf, RAP_USERNAME_LEN);
1594   p++; /* strange format, but ok */
1595   fstrcpy(upperbuf, workstation);
1596   strupper_m(upperbuf);
1597   PUTSTRINGF(p, upperbuf, RAP_MACHNAME_LEN);
1598   PUTWORD(p, CLI_BUFFER_SIZE);
1599   PUTWORD(p, CLI_BUFFER_SIZE);
1600   
1601   if (cli_api(cli,
1602               param, PTR_DIFF(p,param),1024,  /* param, length, max */
1603               NULL, 0, CLI_BUFFER_SIZE,       /* data, length, max */
1604               &rparam, &rprcnt,               /* return params, return size */
1605               &rdata, &rdrcnt                 /* return data, return size */
1606               )) {
1607     cli->rap_error = GETRES(rparam);
1608     
1609     if (cli->rap_error != 0) {
1610       DEBUG(4,("NetwkstaUserLogoff gave error %d\n", cli->rap_error));
1611     }
1612   }
1613   
1614   SAFE_FREE(rparam);
1615   SAFE_FREE(rdata);
1616   return (cli->rap_error == 0);
1617 }
1618  
1619 int cli_NetPrintQEnum(struct cli_state *cli,
1620                 void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
1621                 void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,uint,uint,const char*))
1622 {
1623   char param[WORDSIZE                         /* api number    */
1624             +sizeof(RAP_NetPrintQEnum_REQ)    /* req string    */
1625             +sizeof(RAP_PRINTQ_INFO_L2)       /* return string */
1626             +WORDSIZE                         /* info level    */
1627             +WORDSIZE                         /* buffer size   */
1628             +sizeof(RAP_SMB_PRINT_JOB_L1)];   /* more ret data */
1629   char *p;
1630   char *rparam = NULL;
1631   char *rdata = NULL; 
1632   unsigned int rprcnt, rdrcnt;
1633   int res = -1;
1634   
1635
1636   memset(param, '\0',sizeof(param));
1637   p = make_header(param, RAP_WPrintQEnum, 
1638                   RAP_NetPrintQEnum_REQ, RAP_PRINTQ_INFO_L2);
1639   PUTWORD(p,2); /* Info level 2 */
1640   PUTWORD(p,0xFFE0); /* Return buffer size */
1641   PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1642
1643   if (cli_api(cli,
1644               param, PTR_DIFF(p,param),1024,
1645               NULL, 0, CLI_BUFFER_SIZE,
1646               &rparam, &rprcnt,
1647               &rdata, &rdrcnt)) {
1648     res = GETRES(rparam);
1649     cli->rap_error = res;
1650     if (res != 0) {
1651       DEBUG(1,("NetPrintQEnum gave error %d\n", res));
1652     }
1653   }
1654
1655   if (rdata) {
1656     if (res == 0 || res == ERRmoredata) {
1657       int i, converter, count;
1658
1659       p = rparam + WORDSIZE;
1660       GETWORD(p, converter);
1661       GETWORD(p, count);
1662
1663       p = rdata;
1664       for (i=0;i<count;i++) {
1665         pstring qname, sep_file, print_proc, dest, parms, comment;
1666         uint16 jobcount, priority, start_time, until_time, status;
1667
1668         GETSTRINGF(p, qname, RAP_SHARENAME_LEN);
1669         p++; /* pad */
1670         GETWORD(p, priority);
1671         GETWORD(p, start_time);
1672         GETWORD(p, until_time);
1673         GETSTRINGP(p, sep_file, rdata, converter);
1674         GETSTRINGP(p, print_proc, rdata, converter);
1675         GETSTRINGP(p, dest, rdata, converter);
1676         GETSTRINGP(p, parms, rdata, converter);
1677         GETSTRINGP(p, parms, comment, converter);
1678         GETWORD(p, status);
1679         GETWORD(p, jobcount);
1680
1681         qfn(qname, priority, start_time, until_time, sep_file, print_proc,
1682             dest, parms, comment, status, jobcount);
1683
1684         if (jobcount) {
1685           int j;
1686           for (j=0;j<jobcount;j++) {
1687             uint16 jid, pos, fsstatus;
1688             pstring ownername, notifyname, datatype, jparms, jstatus, jcomment;
1689             unsigned int submitted, jsize;
1690             
1691             GETWORD(p, jid);
1692             GETSTRINGF(p, ownername, RAP_USERNAME_LEN);
1693             p++; /* pad byte */
1694             GETSTRINGF(p, notifyname, RAP_MACHNAME_LEN);
1695             GETSTRINGF(p, datatype, RAP_DATATYPE_LEN);
1696             GETSTRINGP(p, jparms, rdata, converter);
1697             GETWORD(p, pos);
1698             GETWORD(p, fsstatus);
1699             GETSTRINGP(p, jstatus, rdata, converter);
1700             GETDWORD(p, submitted);
1701             GETDWORD(p, jsize);
1702             GETSTRINGP(p, jcomment, rdata, converter);
1703           
1704             jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
1705                 jstatus, submitted, jsize, jcomment);
1706           }
1707         }
1708       }
1709     } else {
1710       DEBUG(4,("NetPrintQEnum res=%d\n", res));
1711     }
1712   } else {
1713     DEBUG(4,("NetPrintQEnum no data returned\n"));
1714   }
1715     
1716   SAFE_FREE(rparam);
1717   SAFE_FREE(rdata);
1718
1719   return res;  
1720 }
1721
1722 int cli_NetPrintQGetInfo(struct cli_state *cli, const char *printer,
1723         void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
1724         void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,uint,uint,const char*))
1725 {
1726   char param[WORDSIZE                         /* api number    */
1727             +sizeof(RAP_NetPrintQGetInfo_REQ) /* req string    */
1728             +sizeof(RAP_PRINTQ_INFO_L2)       /* return string */ 
1729             +RAP_SHARENAME_LEN                /* printer name  */
1730             +WORDSIZE                         /* info level    */
1731             +WORDSIZE                         /* buffer size   */
1732             +sizeof(RAP_SMB_PRINT_JOB_L1)];   /* more ret data */
1733   char *p;
1734   char *rparam = NULL;
1735   char *rdata = NULL; 
1736   unsigned int rprcnt, rdrcnt;
1737   int res = -1;
1738   
1739
1740   memset(param, '\0',sizeof(param));
1741   p = make_header(param, RAP_WPrintQGetInfo,
1742                   RAP_NetPrintQGetInfo_REQ, RAP_PRINTQ_INFO_L2);
1743   PUTSTRING(p, printer, RAP_SHARENAME_LEN-1);
1744   PUTWORD(p, 2);     /* Info level 2 */
1745   PUTWORD(p,0xFFE0); /* Return buffer size */
1746   PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1747
1748   if (cli_api(cli,
1749               param, PTR_DIFF(p,param),1024,
1750               NULL, 0, CLI_BUFFER_SIZE,
1751               &rparam, &rprcnt,
1752               &rdata, &rdrcnt)) {
1753     res = GETRES(rparam);
1754     cli->rap_error = res;
1755     if (res != 0) {
1756       DEBUG(1,("NetPrintQGetInfo gave error %d\n", res));
1757     }
1758   }
1759
1760   if (rdata) {
1761     if (res == 0 || res == ERRmoredata) {
1762       int rsize, converter;
1763       pstring qname, sep_file, print_proc, dest, parms, comment;
1764       uint16 jobcount, priority, start_time, until_time, status;
1765       
1766       p = rparam + WORDSIZE;
1767       GETWORD(p, converter);
1768       GETWORD(p, rsize);
1769
1770       p = rdata;
1771       GETSTRINGF(p, qname, RAP_SHARENAME_LEN);
1772       p++; /* pad */
1773       GETWORD(p, priority);
1774       GETWORD(p, start_time);
1775       GETWORD(p, until_time);
1776       GETSTRINGP(p, sep_file, rdata, converter);
1777       GETSTRINGP(p, print_proc, rdata, converter);
1778       GETSTRINGP(p, dest, rdata, converter);
1779       GETSTRINGP(p, parms, rdata, converter);
1780       GETSTRINGP(p, comment, rdata, converter);
1781       GETWORD(p, status);
1782       GETWORD(p, jobcount);
1783       qfn(qname, priority, start_time, until_time, sep_file, print_proc,
1784           dest, parms, comment, status, jobcount);
1785       if (jobcount) {
1786         int j;
1787         for (j=0;(j<jobcount)&&(PTR_DIFF(p,rdata)< rsize);j++) {
1788           uint16 jid, pos, fsstatus;
1789           pstring ownername, notifyname, datatype, jparms, jstatus, jcomment;
1790           unsigned int submitted, jsize;
1791
1792           GETWORD(p, jid);
1793           GETSTRINGF(p, ownername, RAP_USERNAME_LEN);
1794           p++; /* pad byte */
1795           GETSTRINGF(p, notifyname, RAP_MACHNAME_LEN);
1796           GETSTRINGF(p, datatype, RAP_DATATYPE_LEN);
1797           GETSTRINGP(p, jparms, rdata, converter);
1798           GETWORD(p, pos);
1799           GETWORD(p, fsstatus);
1800           GETSTRINGP(p, jstatus, rdata, converter);
1801           GETDWORD(p, submitted);
1802           GETDWORD(p, jsize);
1803           GETSTRINGP(p, jcomment, rdata, converter);
1804           
1805           jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
1806               jstatus, submitted, jsize, jcomment);
1807         }
1808       }
1809     } else {
1810       DEBUG(4,("NetPrintQGetInfo res=%d\n", res));
1811     }
1812   } else {
1813     DEBUG(4,("NetPrintQGetInfo no data returned\n"));
1814   }
1815     
1816   SAFE_FREE(rparam);
1817   SAFE_FREE(rdata);
1818
1819   return res;  
1820 }
1821
1822 /****************************************************************************
1823 call a NetServiceEnum - list running services on a different host
1824 ****************************************************************************/
1825 int cli_RNetServiceEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
1826 {
1827   char param[WORDSIZE                     /* api number    */
1828             +sizeof(RAP_NetServiceEnum_REQ) /* parm string   */
1829             +sizeof(RAP_SERVICE_INFO_L2)    /* return string */
1830             +WORDSIZE                     /* info level    */
1831             +WORDSIZE];                   /* buffer size   */
1832   char *p;
1833   char *rparam = NULL;
1834   char *rdata = NULL; 
1835   unsigned int rprcnt, rdrcnt;
1836   int res = -1;
1837   
1838   
1839   memset(param, '\0', sizeof(param));
1840   p = make_header(param, RAP_WServiceEnum,
1841                   RAP_NetServiceEnum_REQ, RAP_SERVICE_INFO_L2);
1842   PUTWORD(p,2); /* Info level 2 */  
1843   PUTWORD(p,0xFFE0); /* Return buffer size */
1844
1845   if (cli_api(cli,
1846               param, PTR_DIFF(p,param),8,
1847               NULL, 0, 0xFFE0 /* data area size */,
1848               &rparam, &rprcnt,
1849               &rdata, &rdrcnt)) {
1850     res = GETRES(rparam);
1851     cli->rap_error = res;
1852     if(cli->rap_error == 234) 
1853         DEBUG(1,("Not all service names were returned (such as those longer than 15 characters)\n"));
1854     else if (cli->rap_error != 0) {
1855       DEBUG(1,("NetServiceEnum gave error %d\n", cli->rap_error));
1856     }
1857   }
1858
1859   if (rdata) {
1860     if (res == 0 || res == ERRmoredata) {
1861       int i, count;
1862
1863       p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1864       GETWORD(p, count);
1865
1866       for (i=0,p=rdata;i<count;i++) {
1867             pstring comment;
1868             char servicename[RAP_SRVCNAME_LEN];
1869
1870             GETSTRINGF(p, servicename, RAP_SRVCNAME_LEN);
1871             p+=8; /* pass status words */
1872             GETSTRINGF(p, comment, RAP_SRVCCMNT_LEN);
1873
1874             fn(servicename, comment, cli);  /* BB add status too */
1875       } 
1876     } else {
1877       DEBUG(4,("NetServiceEnum res=%d\n", res));
1878     }
1879   } else {
1880     DEBUG(4,("NetServiceEnum no data returned\n"));
1881   }
1882     
1883   SAFE_FREE(rparam);
1884   SAFE_FREE(rdata);
1885
1886   return res;
1887 }
1888
1889
1890 /****************************************************************************
1891 call a NetSessionEnum - list workstations with sessions to an SMB server
1892 ****************************************************************************/
1893 int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16, uint16, uint16, uint, uint, uint, char *))
1894 {
1895   char param[WORDSIZE                       /* api number    */
1896             +sizeof(RAP_NetSessionEnum_REQ) /* parm string   */
1897             +sizeof(RAP_SESSION_INFO_L2)    /* return string */
1898             +WORDSIZE                       /* info level    */
1899             +WORDSIZE];                     /* buffer size   */
1900   char *p;
1901   char *rparam = NULL;
1902   char *rdata = NULL; 
1903   unsigned int rprcnt, rdrcnt;
1904   int res = -1;
1905   
1906   memset(param, '\0', sizeof(param));
1907   p = make_header(param, RAP_WsessionEnum, 
1908                   RAP_NetSessionEnum_REQ, RAP_SESSION_INFO_L2);
1909   PUTWORD(p,2);    /* Info level 2 */
1910   PUTWORD(p,0xFF); /* Return buffer size */
1911
1912   if (cli_api(cli,
1913               param, PTR_DIFF(p,param),8,
1914               NULL, 0, CLI_BUFFER_SIZE,
1915               &rparam, &rprcnt,
1916               &rdata, &rdrcnt)) {
1917     res = GETRES(rparam);
1918     cli->rap_error = res;
1919     if (res != 0) {
1920       DEBUG(1,("NetSessionEnum gave error %d\n", res));
1921     }
1922   }
1923
1924   if (rdata) {
1925     if (res == 0 || res == ERRmoredata) {
1926       int i, converter, count;
1927       
1928       p = rparam + WORDSIZE;
1929       GETWORD(p, converter);
1930       GETWORD(p, count);
1931
1932       for (i=0,p=rdata;i<count;i++) {
1933         pstring wsname, username, clitype_name;
1934         uint16  num_conns, num_opens, num_users;
1935         unsigned int    sess_time, idle_time, user_flags;
1936
1937         GETSTRINGP(p, wsname, rdata, converter);
1938         GETSTRINGP(p, username, rdata, converter);
1939         GETWORD(p, num_conns);
1940         GETWORD(p, num_opens);
1941         GETWORD(p, num_users);
1942         GETDWORD(p, sess_time);
1943         GETDWORD(p, idle_time);
1944         GETDWORD(p, user_flags);
1945         GETSTRINGP(p, clitype_name, rdata, converter);
1946
1947         fn(wsname, username, num_conns, num_opens, num_users, sess_time,
1948            idle_time, user_flags, clitype_name);
1949       }
1950         
1951     } else {
1952       DEBUG(4,("NetSessionEnum res=%d\n", res));
1953     }
1954   } else {
1955     DEBUG(4,("NetSesssionEnum no data returned\n"));
1956   }
1957     
1958   SAFE_FREE(rparam);
1959   SAFE_FREE(rdata);
1960
1961   return res;
1962 }
1963
1964 /****************************************************************************
1965  Call a NetSessionGetInfo - get information about other session to an SMB server.
1966 ****************************************************************************/
1967
1968 int cli_NetSessionGetInfo(struct cli_state *cli, const char *workstation, void (*fn)(const char *, const char *, uint16, uint16, uint16, uint, uint, uint, const char *))
1969 {
1970   char param[WORDSIZE                          /* api number    */
1971             +sizeof(RAP_NetSessionGetInfo_REQ) /* req string    */
1972             +sizeof(RAP_SESSION_INFO_L2)       /* return string */ 
1973             +RAP_MACHNAME_LEN                  /* wksta name    */
1974             +WORDSIZE                          /* info level    */
1975             +WORDSIZE];                        /* buffer size   */
1976   char *p;
1977   char *rparam = NULL;
1978   char *rdata = NULL; 
1979   unsigned int rprcnt, rdrcnt;
1980   int res = -1;
1981   
1982
1983   memset(param, '\0', sizeof(param));
1984   p = make_header(param, RAP_WsessionGetInfo, 
1985                   RAP_NetSessionGetInfo_REQ, RAP_SESSION_INFO_L2);
1986   PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
1987   PUTWORD(p,2); /* Info level 2 */
1988   PUTWORD(p,0xFF); /* Return buffer size */
1989
1990   if (cli_api(cli,
1991               param, PTR_DIFF(p,param),PTR_DIFF(p,param),
1992               NULL, 0, CLI_BUFFER_SIZE,
1993               &rparam, &rprcnt,
1994               &rdata, &rdrcnt)) {
1995     cli->rap_error = SVAL(rparam,0);
1996     if (cli->rap_error != 0) {
1997       DEBUG(1,("NetSessionGetInfo gave error %d\n", cli->rap_error));
1998     }
1999   }
2000
2001   if (rdata) {
2002     res = GETRES(rparam);
2003     
2004     if (res == 0 || res == ERRmoredata) {
2005       int converter;
2006       pstring wsname, username, clitype_name;
2007       uint16  num_conns, num_opens, num_users;
2008       unsigned int    sess_time, idle_time, user_flags;
2009
2010       p = rparam + WORDSIZE;
2011       GETWORD(p, converter);
2012       p += WORDSIZE;            /* skip rsize */
2013
2014       p = rdata;
2015       GETSTRINGP(p, wsname, rdata, converter);
2016       GETSTRINGP(p, username, rdata, converter);
2017       GETWORD(p, num_conns);
2018       GETWORD(p, num_opens);
2019       GETWORD(p, num_users);
2020       GETDWORD(p, sess_time);
2021       GETDWORD(p, idle_time);
2022       GETDWORD(p, user_flags);
2023       GETSTRINGP(p, clitype_name, rdata, converter);
2024       
2025       fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2026          idle_time, user_flags, clitype_name);
2027     } else {
2028       DEBUG(4,("NetSessionGetInfo res=%d\n", res));
2029     }
2030   } else {
2031     DEBUG(4,("NetSessionGetInfo no data returned\n"));
2032   }
2033     
2034   SAFE_FREE(rparam);
2035   SAFE_FREE(rdata);
2036
2037   return res;  
2038 }
2039
2040 /****************************************************************************
2041 call a NetSessionDel - close a session to an SMB server
2042 ****************************************************************************/
2043 int cli_NetSessionDel(struct cli_state *cli, const char *workstation)
2044 {
2045   char param[WORDSIZE                      /* api number       */
2046             +sizeof(RAP_NetSessionDel_REQ) /* req string       */
2047             +1                             /* no return string */
2048             +RAP_MACHNAME_LEN              /* workstation name */
2049             +WORDSIZE];                    /* reserved (0)     */
2050   char *p;
2051   char *rparam = NULL;
2052   char *rdata = NULL;
2053   unsigned int rprcnt, rdrcnt;
2054   int res;
2055
2056   memset(param, '\0', sizeof(param));
2057   p = make_header(param, RAP_WsessionDel, RAP_NetSessionDel_REQ, NULL);
2058   PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2059   PUTWORD(p,0); /* reserved word of 0 */
2060   if (cli_api(cli, 
2061               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
2062               NULL, 0, 200,       /* data, length, maxlen */
2063               &rparam, &rprcnt,   /* return params, length */
2064               &rdata, &rdrcnt))   /* return data, length */
2065     {
2066       res = GETRES(rparam);
2067       cli->rap_error = res;
2068       
2069       if (res == 0) {
2070         /* nothing to do */             
2071       }
2072       else {
2073         DEBUG(4,("NetFileClose2 res=%d\n", res));
2074       }      
2075     } else {
2076       res = -1;
2077       DEBUG(4,("NetFileClose2 failed\n"));
2078     }
2079   
2080   SAFE_FREE(rparam);
2081   SAFE_FREE(rdata);
2082
2083   return res;
2084 }
2085   
2086
2087 int cli_NetConnectionEnum(struct cli_state *cli, const char *qualifier, void (*fn)(uint16 conid, uint16 contype, uint16 numopens, uint16 numusers, uint32 contime, const char *username, const char *netname))
2088 {
2089   char param[WORDSIZE                          /* api number    */
2090             +sizeof(RAP_NetConnectionEnum_REQ) /* req string    */
2091             +sizeof(RAP_CONNECTION_INFO_L1)    /* return string */ 
2092             +RAP_MACHNAME_LEN                  /* wksta name    */
2093             +WORDSIZE                          /* info level    */
2094             +WORDSIZE];                        /* buffer size   */
2095   char *p;
2096   char *rparam = NULL;
2097   char *rdata = NULL; 
2098   unsigned int rprcnt, rdrcnt;
2099   int res = -1;
2100
2101   memset(param, '\0', sizeof(param));
2102   p = make_header(param, RAP_WconnectionEnum,
2103                   RAP_NetConnectionEnum_REQ, RAP_CONNECTION_INFO_L1);
2104   PUTSTRING(p, qualifier, RAP_MACHNAME_LEN-1);/* Workstation name */
2105   PUTWORD(p,1);            /* Info level 1 */
2106   PUTWORD(p,0xFFE0);       /* Return buffer size */
2107
2108   if (cli_api(cli,
2109               param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2110               NULL, 0, CLI_BUFFER_SIZE,
2111               &rparam, &rprcnt,
2112               &rdata, &rdrcnt)) {
2113     res = GETRES(rparam);
2114     cli->rap_error = res;
2115     if (res != 0) {
2116       DEBUG(1,("NetConnectionEnum gave error %d\n", res));
2117     }
2118   }
2119   if (rdata) {
2120     if (res == 0 || res == ERRmoredata) {
2121       int i, converter, count;
2122
2123       p = rparam + WORDSIZE;
2124       GETWORD(p, converter);
2125       GETWORD(p, count);
2126
2127       for (i=0,p=rdata;i<count;i++) {
2128         pstring netname, username;
2129         uint16  conn_id, conn_type, num_opens, num_users;
2130         unsigned int    conn_time;
2131
2132         GETWORD(p,conn_id);
2133         GETWORD(p,conn_type);
2134         GETWORD(p,num_opens);
2135         GETWORD(p,num_users);
2136         GETDWORD(p,conn_time);
2137         GETSTRINGP(p, username, rdata, converter);
2138         GETSTRINGP(p, netname, rdata, converter);
2139
2140         fn(conn_id, conn_type, num_opens, num_users, conn_time,
2141            username, netname);
2142       }
2143         
2144     } else {
2145       DEBUG(4,("NetConnectionEnum res=%d\n", res));
2146     }
2147   } else {
2148     DEBUG(4,("NetConnectionEnum no data returned\n"));
2149   }
2150   SAFE_FREE(rdata);
2151   SAFE_FREE(rparam);
2152   return res;
2153 }