don't skip some tests which samba3 passes (at least on my laptop)
[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, char * user, char * base_path, void (*fn)(const char *, const char *, uint16, uint16, uint32))
1025 {
1026   char *rparam = NULL;
1027   char *rdata = NULL;
1028   char *p;
1029   unsigned int rdrcnt,rprcnt;
1030   char param[WORDSIZE                   /* api number      */
1031             +sizeof(RAP_WFileEnum2_REQ) /* req string      */
1032             +sizeof(RAP_FILE_INFO_L3)   /* return string   */
1033             +256                        /* base path (opt) */
1034             +RAP_USERNAME_LEN           /* user name (opt) */
1035             +WORDSIZE                   /* info level      */
1036             +WORDSIZE                   /* buffer size     */
1037             +DWORDSIZE                  /* resume key ?    */
1038             +DWORDSIZE];                /* resume key ?    */
1039   int count = -1;
1040
1041   /* now send a SMBtrans command with api RNetShareEnum */
1042   p = make_header(param, RAP_WFileEnum2,
1043                   RAP_WFileEnum2_REQ, RAP_FILE_INFO_L3); 
1044
1045   PUTSTRING(p, base_path, 256);
1046   PUTSTRING(p, user, RAP_USERNAME_LEN);
1047   PUTWORD(p, 3); /* info level */
1048   PUTWORD(p, 0xFF00);  /* buffer size */ 
1049   PUTDWORD(p, 0);  /* zero out the resume key */
1050   PUTDWORD(p, 0);  /* or is this one the resume key? */
1051                  
1052   if (cli_api(cli, 
1053               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1054               NULL, 0, 0xFF00,  /* data, length, maxlen */
1055               &rparam, &rprcnt,               /* return params, length */
1056               &rdata, &rdrcnt))               /* return data, length */
1057     {
1058       int res = GETRES(rparam);
1059       
1060       if (res == 0 || res == ERRmoredata) {
1061         int converter, i;
1062
1063         p = rparam + WORDSIZE; /* skip result */
1064         GETWORD(p, converter);
1065         GETWORD(p, count);
1066         
1067         p = rdata;
1068         for (i=0; i<count; i++) {
1069           int id, perms, locks;
1070           pstring fpath, fuser;
1071           
1072           GETDWORD(p, id);
1073           GETWORD(p, perms);
1074           GETWORD(p, locks);
1075           GETSTRINGP(p, fpath, rdata, converter);
1076           GETSTRINGP(p, fuser, rdata, converter);
1077
1078           fn(fpath, fuser, perms, locks, id);
1079         }  /* BB fix ERRmoredata case to send resume request */
1080       } else {
1081         DEBUG(4,("NetFileEnum2 res=%d\n", res));
1082       }      
1083     } else {
1084       DEBUG(4,("NetFileEnum2 failed\n"));
1085     }
1086   
1087   SAFE_FREE(rparam);
1088   SAFE_FREE(rdata);
1089   
1090   return count;
1091 }
1092
1093 /****************************************************************************
1094  call a NetShareAdd - share/export directory on remote server
1095 ****************************************************************************/
1096 int cli_NetShareAdd(struct cli_state *cli, RAP_SHARE_INFO_2 * sinfo )
1097 {
1098   char *rparam = NULL;
1099   char *rdata = NULL;
1100   char *p;
1101   unsigned int rdrcnt,rprcnt;
1102   int res;
1103   char param[WORDSIZE                  /* api number    */
1104             +sizeof(RAP_WShareAdd_REQ) /* req string    */
1105             +sizeof(RAP_SHARE_INFO_L2) /* return string */
1106             +WORDSIZE                  /* info level    */
1107             +WORDSIZE];                /* reserved word */
1108   char data[1024];
1109   /* offset to free format string section following fixed length data.  */
1110   /* will be updated by PUTSTRINGP macro and will end up with total len */
1111   int soffset = RAP_SHARENAME_LEN + 1 /* share name + pad   */
1112     + WORDSIZE                        /* share type    */
1113     + DWORDSIZE                       /* comment pointer */
1114     + WORDSIZE                        /* permissions */
1115     + WORDSIZE                        /* max users */
1116     + WORDSIZE                        /* active users */
1117     + DWORDSIZE                       /* share path */
1118     + RAP_SPASSWD_LEN + 1;            /* share password + pad */
1119
1120   memset(param,'\0',sizeof(param));
1121   /* now send a SMBtrans command with api RNetShareAdd */
1122   p = make_header(param, RAP_WshareAdd,
1123                   RAP_WShareAdd_REQ, RAP_SHARE_INFO_L2); 
1124   PUTWORD(p, 2); /* info level */
1125   PUTWORD(p, 0); /* reserved word 0 */
1126
1127   p = data;
1128   PUTSTRINGF(p, sinfo->share_name, RAP_SHARENAME_LEN);
1129   PUTBYTE(p, 0); /* pad byte 0 */
1130
1131   PUTWORD(p, sinfo->share_type);
1132   PUTSTRINGP(p, sinfo->comment, data, soffset);
1133   PUTWORD(p, sinfo->perms);
1134   PUTWORD(p, sinfo->maximum_users);
1135   PUTWORD(p, sinfo->active_users);
1136   PUTSTRINGP(p, sinfo->path, data, soffset);
1137   PUTSTRINGF(p, sinfo->password, RAP_SPASSWD_LEN);
1138   SCVAL(p,-1,0x0A); /* required 0x0A at end of password */
1139   
1140   if (cli_api(cli, 
1141               param, sizeof(param), 1024, /* Param, length, maxlen */
1142               data, soffset, sizeof(data), /* data, length, maxlen */
1143               &rparam, &rprcnt,   /* return params, length */
1144               &rdata, &rdrcnt))   /* return data, length */
1145     {
1146       res = rparam? SVAL(rparam,0) : -1;
1147                         
1148       if (res == 0) {
1149         /* nothing to do */             
1150       }
1151       else {
1152         DEBUG(4,("NetShareAdd res=%d\n", res));
1153       }      
1154     } else {
1155       res = -1;
1156       DEBUG(4,("NetShareAdd failed\n"));
1157     }
1158   
1159   SAFE_FREE(rparam);
1160   SAFE_FREE(rdata);
1161   
1162   return res;
1163 }
1164 /****************************************************************************
1165  call a NetShareDelete - unshare exported directory on remote server
1166 ****************************************************************************/
1167 int cli_NetShareDelete(struct cli_state *cli, const char * share_name )
1168 {
1169   char *rparam = NULL;
1170   char *rdata = NULL;
1171   char *p;
1172   unsigned int rdrcnt,rprcnt;
1173   int res;
1174   char param[WORDSIZE                  /* api number    */
1175             +sizeof(RAP_WShareDel_REQ) /* req string    */
1176             +1                         /* no ret string */
1177             +RAP_SHARENAME_LEN         /* share to del  */
1178             +WORDSIZE];                /* reserved word */
1179             
1180
1181   /* now send a SMBtrans command with api RNetShareDelete */
1182   p = make_header(param, RAP_WshareDel, RAP_WShareDel_REQ, NULL);
1183   PUTSTRING(p,share_name,RAP_SHARENAME_LEN);
1184   PUTWORD(p,0);  /* reserved word MBZ on input */
1185                  
1186   if (cli_api(cli, 
1187               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
1188               NULL, 0, 200,       /* data, length, maxlen */
1189               &rparam, &rprcnt,   /* return params, length */
1190               &rdata, &rdrcnt))   /* return data, length */
1191     {
1192       res = GETRES(rparam);
1193                         
1194       if (res == 0) {
1195         /* nothing to do */             
1196       }
1197       else {
1198         DEBUG(4,("NetShareDelete res=%d\n", res));
1199       }      
1200     } else {
1201       res = -1;
1202       DEBUG(4,("NetShareDelete failed\n"));
1203     }
1204   
1205   SAFE_FREE(rparam);
1206   SAFE_FREE(rdata);
1207         
1208   return res;
1209 }
1210 /*************************************************************************
1211 *
1212 * Function Name:  cli_get_pdc_name
1213 *
1214 * PURPOSE:  Remotes a NetServerEnum API call to the current server
1215 *           requesting the name of a server matching the server
1216 *           type of SV_TYPE_DOMAIN_CTRL (PDC).
1217 *
1218 * Dependencies: none
1219 *
1220 * Parameters: 
1221 *             cli       - pointer to cli_state structure
1222 *             workgroup - pointer to string containing name of domain
1223 *             pdc_name  - pointer to string that will contain PDC name
1224 *                         on successful return
1225 *
1226 * Returns:
1227 *             True      - success
1228 *             False     - failure
1229 *
1230 ************************************************************************/
1231 BOOL cli_get_pdc_name(struct cli_state *cli, char *workgroup, char *pdc_name)
1232 {
1233   char *rparam = NULL;
1234   char *rdata = NULL;
1235   unsigned int rdrcnt,rprcnt;
1236   char *p;
1237   char param[WORDSIZE                       /* api number    */
1238             +sizeof(RAP_NetServerEnum2_REQ) /* req string    */
1239             +sizeof(RAP_SERVER_INFO_L1)     /* return string */
1240             +WORDSIZE                       /* info level    */
1241             +WORDSIZE                       /* buffer size   */
1242             +DWORDSIZE                      /* server type   */
1243             +RAP_MACHNAME_LEN];             /* workgroup     */
1244   int count = -1;
1245   
1246   *pdc_name = '\0';
1247
1248   /* send a SMBtrans command with api NetServerEnum */
1249   p = make_header(param, RAP_NetServerEnum2,
1250                   RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L1);
1251   PUTWORD(p, 1); /* info level */
1252   PUTWORD(p, CLI_BUFFER_SIZE);
1253   PUTDWORD(p, SV_TYPE_DOMAIN_CTRL);
1254   PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1255         
1256   if (cli_api(cli, 
1257               param, PTR_DIFF(p,param), 8,        /* params, length, max */
1258               NULL, 0, CLI_BUFFER_SIZE,               /* data, length, max */
1259               &rparam, &rprcnt,                   /* return params, return size */
1260               &rdata, &rdrcnt                     /* return data, return size */
1261               )) {
1262     cli->rap_error = GETRES(rparam);
1263                         
1264         /*
1265          * We only really care to copy a name if the
1266          * API succeeded and we got back a name.
1267          */
1268     if (cli->rap_error == 0) {
1269       p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1270       GETWORD(p, count);
1271       p = rdata;
1272       
1273       if (count > 0)
1274         GETSTRING(p, pdc_name);
1275     }
1276     else {
1277         DEBUG(4,("cli_get_pdc_name: machine %s failed the NetServerEnum call. "
1278                  "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1279     }
1280   }
1281   
1282   SAFE_FREE(rparam);
1283   SAFE_FREE(rdata);
1284   
1285   return(count > 0);
1286 }
1287
1288
1289 /*************************************************************************
1290 *
1291 * Function Name:  cli_get_server_domain
1292 *
1293 * PURPOSE:  Remotes a NetWkstaGetInfo API call to the current server
1294 *           requesting wksta_info_10 level information to determine
1295 *           the domain the server belongs to. On success, this
1296 *           routine sets the server_domain field in the cli_state structure
1297 *           to the server's domain name.
1298 *
1299 * Dependencies: none
1300 *
1301 * Parameters: 
1302 *             cli       - pointer to cli_state structure
1303 *
1304 * Returns:
1305 *             True      - success
1306 *             False     - failure
1307 *
1308 * Origins:  samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1309 *
1310 ************************************************************************/
1311 BOOL cli_get_server_domain(struct cli_state *cli)
1312 {
1313   char *rparam = NULL;
1314   char *rdata = NULL;
1315   unsigned int rdrcnt,rprcnt;
1316   char *p;
1317   char param[WORDSIZE                      /* api number    */
1318             +sizeof(RAP_WWkstaGetInfo_REQ) /* req string    */
1319             +sizeof(RAP_WKSTA_INFO_L10)    /* return string */
1320             +WORDSIZE                      /* info level    */
1321             +WORDSIZE];                    /* buffer size   */
1322   int res = -1;
1323   
1324   /* send a SMBtrans command with api NetWkstaGetInfo */
1325   p = make_header(param, RAP_WWkstaGetInfo,
1326                   RAP_WWkstaGetInfo_REQ, RAP_WKSTA_INFO_L10);
1327   PUTWORD(p, 10); /* info level */
1328   PUTWORD(p, CLI_BUFFER_SIZE);
1329         
1330   if (cli_api(cli, param, PTR_DIFF(p,param), 8, /* params, length, max */
1331               NULL, 0, CLI_BUFFER_SIZE,         /* data, length, max */
1332               &rparam, &rprcnt,         /* return params, return size */
1333               &rdata, &rdrcnt)) {       /* return data, return size */
1334     res = GETRES(rparam);
1335     p = rdata;          
1336     
1337     if (res == 0) {
1338       int converter;
1339
1340       p = rparam + WORDSIZE;
1341       GETWORD(p, converter);
1342       
1343       p = rdata + DWORDSIZE + DWORDSIZE; /* skip computer & user names */
1344       GETSTRINGP(p, cli->server_domain, rdata, converter);
1345     }
1346   }
1347   
1348   SAFE_FREE(rparam);
1349   SAFE_FREE(rdata);
1350   
1351   return(res == 0);
1352 }
1353
1354
1355 /*************************************************************************
1356 *
1357 * Function Name:  cli_get_server_type
1358 *
1359 * PURPOSE:  Remotes a NetServerGetInfo API call to the current server
1360 *           requesting server_info_1 level information to retrieve
1361 *           the server type.
1362 *
1363 * Dependencies: none
1364 *
1365 * Parameters: 
1366 *             cli       - pointer to cli_state structure
1367 *             pstype    - pointer to uint32 to contain returned server type
1368 *
1369 * Returns:
1370 *             True      - success
1371 *             False     - failure
1372 *
1373 * Origins:  samba 2.0.6 source/libsmb/clientgen.c cli_NetServerEnum()
1374 *
1375 ************************************************************************/
1376 BOOL cli_get_server_type(struct cli_state *cli, uint32 *pstype)
1377 {
1378   char *rparam = NULL;
1379   char *rdata = NULL;
1380   unsigned int rdrcnt,rprcnt;
1381   char *p;
1382   char param[WORDSIZE                       /* api number    */
1383             +sizeof(RAP_WserverGetInfo_REQ) /* req string    */
1384             +sizeof(RAP_SERVER_INFO_L1)     /* return string */
1385             +WORDSIZE                       /* info level    */
1386             +WORDSIZE];                     /* buffer size   */
1387   int res = -1;
1388   
1389   /* send a SMBtrans command with api NetServerGetInfo */
1390   p = make_header(param, RAP_WserverGetInfo,
1391                   RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1392   PUTWORD(p, 1); /* info level */
1393   PUTWORD(p, CLI_BUFFER_SIZE);
1394         
1395   if (cli_api(cli, 
1396               param, PTR_DIFF(p,param), 8, /* params, length, max */
1397               NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1398               &rparam, &rprcnt,         /* return params, return size */
1399               &rdata, &rdrcnt           /* return data, return size */
1400               )) {
1401     
1402     res = GETRES(rparam);
1403     
1404     if (res == 0 || res == ERRmoredata) {
1405       p = rdata;                                        
1406       *pstype = IVAL(p,18) & ~SV_TYPE_LOCAL_LIST_ONLY;
1407     }
1408   }
1409   
1410   SAFE_FREE(rparam);
1411   SAFE_FREE(rdata);
1412   
1413   return(res == 0 || res == ERRmoredata);
1414 }
1415
1416 BOOL cli_get_server_name(TALLOC_CTX *mem_ctx, struct cli_state *cli,
1417                          char **servername)
1418 {
1419         char *rparam = NULL;
1420         char *rdata = NULL;
1421         unsigned int rdrcnt,rprcnt;
1422         char *p;
1423         char param[WORDSIZE                       /* api number    */
1424                    +sizeof(RAP_WserverGetInfo_REQ) /* req string    */
1425                    +sizeof(RAP_SERVER_INFO_L1)     /* return string */
1426                    +WORDSIZE                       /* info level    */
1427                    +WORDSIZE];                     /* buffer size   */
1428         BOOL res = False;
1429         fstring tmp;
1430   
1431         /* send a SMBtrans command with api NetServerGetInfo */
1432         p = make_header(param, RAP_WserverGetInfo,
1433                         RAP_WserverGetInfo_REQ, RAP_SERVER_INFO_L1);
1434         PUTWORD(p, 1); /* info level */
1435         PUTWORD(p, CLI_BUFFER_SIZE);
1436         
1437         if (!cli_api(cli, 
1438                      param, PTR_DIFF(p,param), 8, /* params, length, max */
1439                      NULL, 0, CLI_BUFFER_SIZE, /* data, length, max */
1440                      &rparam, &rprcnt,         /* return params, return size */
1441                      &rdata, &rdrcnt           /* return data, return size */
1442                     )) {
1443                 goto failed;
1444         }
1445     
1446         if (GETRES(rparam) != 0) {
1447                 goto failed;
1448         }
1449
1450         if (rdrcnt < 16) {
1451                 DEBUG(10, ("invalid data count %d, expected >= 16\n", rdrcnt));
1452                 goto failed;
1453         }
1454
1455         if (pull_ascii(tmp, rdata, sizeof(tmp)-1, 16, STR_TERMINATE) == -1) {
1456                 DEBUG(10, ("pull_ascii failed\n"));
1457                 goto failed;
1458         }
1459
1460         if (!(*servername = talloc_strdup(mem_ctx, tmp))) {
1461                 DEBUG(1, ("talloc_strdup failed\n"));
1462                 goto failed;
1463         }
1464
1465         res = True;
1466
1467  failed:
1468         SAFE_FREE(rparam);
1469         SAFE_FREE(rdata);
1470         return res;
1471 }
1472
1473 /*************************************************************************
1474 *
1475 * Function Name:  cli_ns_check_server_type
1476 *
1477 * PURPOSE:  Remotes a NetServerEnum2 API call to the current server
1478 *           requesting server_info_0 level information of machines
1479 *           matching the given server type. If the returned server
1480 *           list contains the machine name contained in cli->desthost
1481 *           then we conclude the server type checks out. This routine
1482 *           is useful to retrieve list of server's of a certain
1483 *           type when all you have is a null session connection and
1484 *           can't remote API calls such as NetWkstaGetInfo or 
1485 *           NetServerGetInfo.
1486 *
1487 * Dependencies: none
1488 *
1489 * Parameters: 
1490 *             cli       - pointer to cli_state structure
1491 *             workgroup - pointer to string containing domain
1492 *             stype     - server type
1493 *
1494 * Returns:
1495 *             True      - success
1496 *             False     - failure
1497 *
1498 ************************************************************************/
1499 BOOL cli_ns_check_server_type(struct cli_state *cli, char *workgroup, uint32 stype)
1500 {
1501   char *rparam = NULL;
1502   char *rdata = NULL;
1503   unsigned int rdrcnt,rprcnt;
1504   char *p;
1505   char param[WORDSIZE                       /* api number    */
1506             +sizeof(RAP_NetServerEnum2_REQ) /* req string    */
1507             +sizeof(RAP_SERVER_INFO_L0)     /* return string */
1508             +WORDSIZE                       /* info level    */
1509             +WORDSIZE                       /* buffer size   */
1510             +DWORDSIZE                      /* server type   */
1511             +RAP_MACHNAME_LEN];             /* workgroup     */
1512   BOOL found_server = False;
1513   int res = -1;
1514   
1515   /* send a SMBtrans command with api NetServerEnum */
1516   p = make_header(param, RAP_NetServerEnum2,
1517                   RAP_NetServerEnum2_REQ, RAP_SERVER_INFO_L0);
1518   PUTWORD(p, 0); /* info level 0 */
1519   PUTWORD(p, CLI_BUFFER_SIZE);
1520   PUTDWORD(p, stype);
1521   PUTSTRING(p, workgroup, RAP_MACHNAME_LEN);
1522         
1523   if (cli_api(cli, 
1524               param, PTR_DIFF(p,param), 8, /* params, length, max */
1525               NULL, 0, CLI_BUFFER_SIZE,  /* data, length, max */
1526               &rparam, &rprcnt,          /* return params, return size */
1527               &rdata, &rdrcnt            /* return data, return size */
1528               )) {
1529         
1530     res = GETRES(rparam);
1531     cli->rap_error = res;
1532
1533     if (res == 0 || res == ERRmoredata) {
1534       int i, count;
1535
1536       p = rparam + WORDSIZE + WORDSIZE;
1537       GETWORD(p, count);
1538
1539       p = rdata;
1540       for (i = 0;i < count;i++, p += 16) {
1541         char ret_server[RAP_MACHNAME_LEN];
1542
1543         GETSTRINGF(p, ret_server, RAP_MACHNAME_LEN);
1544         if (strequal(ret_server, cli->desthost)) {
1545           found_server = True;
1546           break;
1547         }
1548       }
1549     }
1550     else {
1551       DEBUG(4,("cli_ns_check_server_type: machine %s failed the NetServerEnum call. "
1552                "Error was : %s.\n", cli->desthost, cli_errstr(cli) ));
1553     }
1554   }
1555   
1556   SAFE_FREE(rparam);
1557   SAFE_FREE(rdata);
1558         
1559   return found_server;
1560  }
1561
1562
1563 /****************************************************************************
1564  perform a NetWkstaUserLogoff
1565 ****************************************************************************/
1566 BOOL cli_NetWkstaUserLogoff(struct cli_state *cli,char *user, char *workstation)
1567 {
1568   char *rparam = NULL;
1569   char *rdata = NULL;
1570   char *p;
1571   unsigned int rdrcnt,rprcnt;
1572   char param[WORDSIZE                           /* api number    */
1573             +sizeof(RAP_NetWkstaUserLogoff_REQ) /* req string    */
1574             +sizeof(RAP_USER_LOGOFF_INFO_L1)    /* return string */
1575             +RAP_USERNAME_LEN+1                 /* user name+pad */
1576             +RAP_MACHNAME_LEN                   /* wksta name    */
1577             +WORDSIZE                           /* buffer size   */
1578             +WORDSIZE];                         /* buffer size?  */
1579   fstring upperbuf;
1580   
1581   memset(param, 0, sizeof(param));
1582
1583   /* send a SMBtrans command with api NetWkstaUserLogoff */
1584   p = make_header(param, RAP_WWkstaUserLogoff,
1585                   RAP_NetWkstaUserLogoff_REQ, RAP_USER_LOGOFF_INFO_L1);
1586   PUTDWORD(p, 0); /* Null pointer */
1587   PUTDWORD(p, 0); /* Null pointer */
1588   fstrcpy(upperbuf, user);
1589   strupper_m(upperbuf);
1590   PUTSTRINGF(p, upperbuf, RAP_USERNAME_LEN);
1591   p++; /* strange format, but ok */
1592   fstrcpy(upperbuf, workstation);
1593   strupper_m(upperbuf);
1594   PUTSTRINGF(p, upperbuf, RAP_MACHNAME_LEN);
1595   PUTWORD(p, CLI_BUFFER_SIZE);
1596   PUTWORD(p, CLI_BUFFER_SIZE);
1597   
1598   if (cli_api(cli,
1599               param, PTR_DIFF(p,param),1024,  /* param, length, max */
1600               NULL, 0, CLI_BUFFER_SIZE,       /* data, length, max */
1601               &rparam, &rprcnt,               /* return params, return size */
1602               &rdata, &rdrcnt                 /* return data, return size */
1603               )) {
1604     cli->rap_error = GETRES(rparam);
1605     
1606     if (cli->rap_error != 0) {
1607       DEBUG(4,("NetwkstaUserLogoff gave error %d\n", cli->rap_error));
1608     }
1609   }
1610   
1611   SAFE_FREE(rparam);
1612   SAFE_FREE(rdata);
1613   return (cli->rap_error == 0);
1614 }
1615  
1616 int cli_NetPrintQEnum(struct cli_state *cli,
1617                 void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
1618                 void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,uint,uint,const char*))
1619 {
1620   char param[WORDSIZE                         /* api number    */
1621             +sizeof(RAP_NetPrintQEnum_REQ)    /* req string    */
1622             +sizeof(RAP_PRINTQ_INFO_L2)       /* return string */
1623             +WORDSIZE                         /* info level    */
1624             +WORDSIZE                         /* buffer size   */
1625             +sizeof(RAP_SMB_PRINT_JOB_L1)];   /* more ret data */
1626   char *p;
1627   char *rparam = NULL;
1628   char *rdata = NULL; 
1629   unsigned int rprcnt, rdrcnt;
1630   int res = -1;
1631   
1632
1633   memset(param, '\0',sizeof(param));
1634   p = make_header(param, RAP_WPrintQEnum, 
1635                   RAP_NetPrintQEnum_REQ, RAP_PRINTQ_INFO_L2);
1636   PUTWORD(p,2); /* Info level 2 */
1637   PUTWORD(p,0xFFE0); /* Return buffer size */
1638   PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1639
1640   if (cli_api(cli,
1641               param, PTR_DIFF(p,param),1024,
1642               NULL, 0, CLI_BUFFER_SIZE,
1643               &rparam, &rprcnt,
1644               &rdata, &rdrcnt)) {
1645     res = GETRES(rparam);
1646     cli->rap_error = res;
1647     if (res != 0) {
1648       DEBUG(1,("NetPrintQEnum gave error %d\n", res));
1649     }
1650   }
1651
1652   if (rdata) {
1653     if (res == 0 || res == ERRmoredata) {
1654       int i, converter, count;
1655
1656       p = rparam + WORDSIZE;
1657       GETWORD(p, converter);
1658       GETWORD(p, count);
1659
1660       p = rdata;
1661       for (i=0;i<count;i++) {
1662         pstring qname, sep_file, print_proc, dest, parms, comment;
1663         uint16 jobcount, priority, start_time, until_time, status;
1664
1665         GETSTRINGF(p, qname, RAP_SHARENAME_LEN);
1666         p++; /* pad */
1667         GETWORD(p, priority);
1668         GETWORD(p, start_time);
1669         GETWORD(p, until_time);
1670         GETSTRINGP(p, sep_file, rdata, converter);
1671         GETSTRINGP(p, print_proc, rdata, converter);
1672         GETSTRINGP(p, dest, rdata, converter);
1673         GETSTRINGP(p, parms, rdata, converter);
1674         GETSTRINGP(p, parms, comment, converter);
1675         GETWORD(p, status);
1676         GETWORD(p, jobcount);
1677
1678         qfn(qname, priority, start_time, until_time, sep_file, print_proc,
1679             dest, parms, comment, status, jobcount);
1680
1681         if (jobcount) {
1682           int j;
1683           for (j=0;j<jobcount;j++) {
1684             uint16 jid, pos, fsstatus;
1685             pstring ownername, notifyname, datatype, jparms, jstatus, jcomment;
1686             unsigned int submitted, jsize;
1687             
1688             GETWORD(p, jid);
1689             GETSTRINGF(p, ownername, RAP_USERNAME_LEN);
1690             p++; /* pad byte */
1691             GETSTRINGF(p, notifyname, RAP_MACHNAME_LEN);
1692             GETSTRINGF(p, datatype, RAP_DATATYPE_LEN);
1693             GETSTRINGP(p, jparms, rdata, converter);
1694             GETWORD(p, pos);
1695             GETWORD(p, fsstatus);
1696             GETSTRINGP(p, jstatus, rdata, converter);
1697             GETDWORD(p, submitted);
1698             GETDWORD(p, jsize);
1699             GETSTRINGP(p, jcomment, rdata, converter);
1700           
1701             jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
1702                 jstatus, submitted, jsize, jcomment);
1703           }
1704         }
1705       }
1706     } else {
1707       DEBUG(4,("NetPrintQEnum res=%d\n", res));
1708     }
1709   } else {
1710     DEBUG(4,("NetPrintQEnum no data returned\n"));
1711   }
1712     
1713   SAFE_FREE(rparam);
1714   SAFE_FREE(rdata);
1715
1716   return res;  
1717 }
1718
1719 int cli_NetPrintQGetInfo(struct cli_state *cli, const char *printer,
1720         void (*qfn)(const char*,uint16,uint16,uint16,const char*,const char*,const char*,const char*,const char*,uint16,uint16),
1721         void (*jfn)(uint16,const char*,const char*,const char*,const char*,uint16,uint16,const char*,uint,uint,const char*))
1722 {
1723   char param[WORDSIZE                         /* api number    */
1724             +sizeof(RAP_NetPrintQGetInfo_REQ) /* req string    */
1725             +sizeof(RAP_PRINTQ_INFO_L2)       /* return string */ 
1726             +RAP_SHARENAME_LEN                /* printer name  */
1727             +WORDSIZE                         /* info level    */
1728             +WORDSIZE                         /* buffer size   */
1729             +sizeof(RAP_SMB_PRINT_JOB_L1)];   /* more ret data */
1730   char *p;
1731   char *rparam = NULL;
1732   char *rdata = NULL; 
1733   unsigned int rprcnt, rdrcnt;
1734   int res = -1;
1735   
1736
1737   memset(param, '\0',sizeof(param));
1738   p = make_header(param, RAP_WPrintQGetInfo,
1739                   RAP_NetPrintQGetInfo_REQ, RAP_PRINTQ_INFO_L2);
1740   PUTSTRING(p, printer, RAP_SHARENAME_LEN-1);
1741   PUTWORD(p, 2);     /* Info level 2 */
1742   PUTWORD(p,0xFFE0); /* Return buffer size */
1743   PUTSTRING(p, RAP_SMB_PRINT_JOB_L1, 0);
1744
1745   if (cli_api(cli,
1746               param, PTR_DIFF(p,param),1024,
1747               NULL, 0, CLI_BUFFER_SIZE,
1748               &rparam, &rprcnt,
1749               &rdata, &rdrcnt)) {
1750     res = GETRES(rparam);
1751     cli->rap_error = res;
1752     if (res != 0) {
1753       DEBUG(1,("NetPrintQGetInfo gave error %d\n", res));
1754     }
1755   }
1756
1757   if (rdata) {
1758     if (res == 0 || res == ERRmoredata) {
1759       int rsize, converter;
1760       pstring qname, sep_file, print_proc, dest, parms, comment;
1761       uint16 jobcount, priority, start_time, until_time, status;
1762       
1763       p = rparam + WORDSIZE;
1764       GETWORD(p, converter);
1765       GETWORD(p, rsize);
1766
1767       p = rdata;
1768       GETSTRINGF(p, qname, RAP_SHARENAME_LEN);
1769       p++; /* pad */
1770       GETWORD(p, priority);
1771       GETWORD(p, start_time);
1772       GETWORD(p, until_time);
1773       GETSTRINGP(p, sep_file, rdata, converter);
1774       GETSTRINGP(p, print_proc, rdata, converter);
1775       GETSTRINGP(p, dest, rdata, converter);
1776       GETSTRINGP(p, parms, rdata, converter);
1777       GETSTRINGP(p, comment, rdata, converter);
1778       GETWORD(p, status);
1779       GETWORD(p, jobcount);
1780       qfn(qname, priority, start_time, until_time, sep_file, print_proc,
1781           dest, parms, comment, status, jobcount);
1782       if (jobcount) {
1783         int j;
1784         for (j=0;(j<jobcount)&&(PTR_DIFF(p,rdata)< rsize);j++) {
1785           uint16 jid, pos, fsstatus;
1786           pstring ownername, notifyname, datatype, jparms, jstatus, jcomment;
1787           unsigned int submitted, jsize;
1788
1789           GETWORD(p, jid);
1790           GETSTRINGF(p, ownername, RAP_USERNAME_LEN);
1791           p++; /* pad byte */
1792           GETSTRINGF(p, notifyname, RAP_MACHNAME_LEN);
1793           GETSTRINGF(p, datatype, RAP_DATATYPE_LEN);
1794           GETSTRINGP(p, jparms, rdata, converter);
1795           GETWORD(p, pos);
1796           GETWORD(p, fsstatus);
1797           GETSTRINGP(p, jstatus, rdata, converter);
1798           GETDWORD(p, submitted);
1799           GETDWORD(p, jsize);
1800           GETSTRINGP(p, jcomment, rdata, converter);
1801           
1802           jfn(jid, ownername, notifyname, datatype, jparms, pos, fsstatus,
1803               jstatus, submitted, jsize, jcomment);
1804         }
1805       }
1806     } else {
1807       DEBUG(4,("NetPrintQGetInfo res=%d\n", res));
1808     }
1809   } else {
1810     DEBUG(4,("NetPrintQGetInfo no data returned\n"));
1811   }
1812     
1813   SAFE_FREE(rparam);
1814   SAFE_FREE(rdata);
1815
1816   return res;  
1817 }
1818
1819 /****************************************************************************
1820 call a NetServiceEnum - list running services on a different host
1821 ****************************************************************************/
1822 int cli_RNetServiceEnum(struct cli_state *cli, void (*fn)(const char *, const char *, void *), void *state)
1823 {
1824   char param[WORDSIZE                     /* api number    */
1825             +sizeof(RAP_NetServiceEnum_REQ) /* parm string   */
1826             +sizeof(RAP_SERVICE_INFO_L2)    /* return string */
1827             +WORDSIZE                     /* info level    */
1828             +WORDSIZE];                   /* buffer size   */
1829   char *p;
1830   char *rparam = NULL;
1831   char *rdata = NULL; 
1832   unsigned int rprcnt, rdrcnt;
1833   int res = -1;
1834   
1835   
1836   memset(param, '\0', sizeof(param));
1837   p = make_header(param, RAP_WServiceEnum,
1838                   RAP_NetServiceEnum_REQ, RAP_SERVICE_INFO_L2);
1839   PUTWORD(p,2); /* Info level 2 */  
1840   PUTWORD(p,0xFFE0); /* Return buffer size */
1841
1842   if (cli_api(cli,
1843               param, PTR_DIFF(p,param),8,
1844               NULL, 0, 0xFFE0 /* data area size */,
1845               &rparam, &rprcnt,
1846               &rdata, &rdrcnt)) {
1847     res = GETRES(rparam);
1848     cli->rap_error = res;
1849     if(cli->rap_error == 234) 
1850         DEBUG(1,("Not all service names were returned (such as those longer than 15 characters)\n"));
1851     else if (cli->rap_error != 0) {
1852       DEBUG(1,("NetServiceEnum gave error %d\n", cli->rap_error));
1853     }
1854   }
1855
1856   if (rdata) {
1857     if (res == 0 || res == ERRmoredata) {
1858       int i, count;
1859
1860       p = rparam + WORDSIZE + WORDSIZE; /* skip result and converter */
1861       GETWORD(p, count);
1862
1863       for (i=0,p=rdata;i<count;i++) {
1864             pstring comment;
1865             char servicename[RAP_SRVCNAME_LEN];
1866
1867             GETSTRINGF(p, servicename, RAP_SRVCNAME_LEN);
1868             p+=8; /* pass status words */
1869             GETSTRINGF(p, comment, RAP_SRVCCMNT_LEN);
1870
1871             fn(servicename, comment, cli);  /* BB add status too */
1872       } 
1873     } else {
1874       DEBUG(4,("NetServiceEnum res=%d\n", res));
1875     }
1876   } else {
1877     DEBUG(4,("NetServiceEnum no data returned\n"));
1878   }
1879     
1880   SAFE_FREE(rparam);
1881   SAFE_FREE(rdata);
1882
1883   return res;
1884 }
1885
1886
1887 /****************************************************************************
1888 call a NetSessionEnum - list workstations with sessions to an SMB server
1889 ****************************************************************************/
1890 int cli_NetSessionEnum(struct cli_state *cli, void (*fn)(char *, char *, uint16, uint16, uint16, uint, uint, uint, char *))
1891 {
1892   char param[WORDSIZE                       /* api number    */
1893             +sizeof(RAP_NetSessionEnum_REQ) /* parm string   */
1894             +sizeof(RAP_SESSION_INFO_L2)    /* return string */
1895             +WORDSIZE                       /* info level    */
1896             +WORDSIZE];                     /* buffer size   */
1897   char *p;
1898   char *rparam = NULL;
1899   char *rdata = NULL; 
1900   unsigned int rprcnt, rdrcnt;
1901   int res = -1;
1902   
1903   memset(param, '\0', sizeof(param));
1904   p = make_header(param, RAP_WsessionEnum, 
1905                   RAP_NetSessionEnum_REQ, RAP_SESSION_INFO_L2);
1906   PUTWORD(p,2);    /* Info level 2 */
1907   PUTWORD(p,0xFF); /* Return buffer size */
1908
1909   if (cli_api(cli,
1910               param, PTR_DIFF(p,param),8,
1911               NULL, 0, CLI_BUFFER_SIZE,
1912               &rparam, &rprcnt,
1913               &rdata, &rdrcnt)) {
1914     res = GETRES(rparam);
1915     cli->rap_error = res;
1916     if (res != 0) {
1917       DEBUG(1,("NetSessionEnum gave error %d\n", res));
1918     }
1919   }
1920
1921   if (rdata) {
1922     if (res == 0 || res == ERRmoredata) {
1923       int i, converter, count;
1924       
1925       p = rparam + WORDSIZE;
1926       GETWORD(p, converter);
1927       GETWORD(p, count);
1928
1929       for (i=0,p=rdata;i<count;i++) {
1930         pstring wsname, username, clitype_name;
1931         uint16  num_conns, num_opens, num_users;
1932         unsigned int    sess_time, idle_time, user_flags;
1933
1934         GETSTRINGP(p, wsname, rdata, converter);
1935         GETSTRINGP(p, username, rdata, converter);
1936         GETWORD(p, num_conns);
1937         GETWORD(p, num_opens);
1938         GETWORD(p, num_users);
1939         GETDWORD(p, sess_time);
1940         GETDWORD(p, idle_time);
1941         GETDWORD(p, user_flags);
1942         GETSTRINGP(p, clitype_name, rdata, converter);
1943
1944         fn(wsname, username, num_conns, num_opens, num_users, sess_time,
1945            idle_time, user_flags, clitype_name);
1946       }
1947         
1948     } else {
1949       DEBUG(4,("NetSessionEnum res=%d\n", res));
1950     }
1951   } else {
1952     DEBUG(4,("NetSesssionEnum no data returned\n"));
1953   }
1954     
1955   SAFE_FREE(rparam);
1956   SAFE_FREE(rdata);
1957
1958   return res;
1959 }
1960
1961 /****************************************************************************
1962  Call a NetSessionGetInfo - get information about other session to an SMB server.
1963 ****************************************************************************/
1964
1965 int cli_NetSessionGetInfo(struct cli_state *cli, const char *workstation, void (*fn)(const char *, const char *, uint16, uint16, uint16, uint, uint, uint, const char *))
1966 {
1967   char param[WORDSIZE                          /* api number    */
1968             +sizeof(RAP_NetSessionGetInfo_REQ) /* req string    */
1969             +sizeof(RAP_SESSION_INFO_L2)       /* return string */ 
1970             +RAP_MACHNAME_LEN                  /* wksta name    */
1971             +WORDSIZE                          /* info level    */
1972             +WORDSIZE];                        /* buffer size   */
1973   char *p;
1974   char *rparam = NULL;
1975   char *rdata = NULL; 
1976   unsigned int rprcnt, rdrcnt;
1977   int res = -1;
1978   
1979
1980   memset(param, '\0', sizeof(param));
1981   p = make_header(param, RAP_WsessionGetInfo, 
1982                   RAP_NetSessionGetInfo_REQ, RAP_SESSION_INFO_L2);
1983   PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
1984   PUTWORD(p,2); /* Info level 2 */
1985   PUTWORD(p,0xFF); /* Return buffer size */
1986
1987   if (cli_api(cli,
1988               param, PTR_DIFF(p,param),PTR_DIFF(p,param),
1989               NULL, 0, CLI_BUFFER_SIZE,
1990               &rparam, &rprcnt,
1991               &rdata, &rdrcnt)) {
1992     cli->rap_error = SVAL(rparam,0);
1993     if (cli->rap_error != 0) {
1994       DEBUG(1,("NetSessionGetInfo gave error %d\n", cli->rap_error));
1995     }
1996   }
1997
1998   if (rdata) {
1999     res = GETRES(rparam);
2000     
2001     if (res == 0 || res == ERRmoredata) {
2002       int converter;
2003       pstring wsname, username, clitype_name;
2004       uint16  num_conns, num_opens, num_users;
2005       unsigned int    sess_time, idle_time, user_flags;
2006
2007       p = rparam + WORDSIZE;
2008       GETWORD(p, converter);
2009       p += WORDSIZE;            /* skip rsize */
2010
2011       p = rdata;
2012       GETSTRINGP(p, wsname, rdata, converter);
2013       GETSTRINGP(p, username, rdata, converter);
2014       GETWORD(p, num_conns);
2015       GETWORD(p, num_opens);
2016       GETWORD(p, num_users);
2017       GETDWORD(p, sess_time);
2018       GETDWORD(p, idle_time);
2019       GETDWORD(p, user_flags);
2020       GETSTRINGP(p, clitype_name, rdata, converter);
2021       
2022       fn(wsname, username, num_conns, num_opens, num_users, sess_time,
2023          idle_time, user_flags, clitype_name);
2024     } else {
2025       DEBUG(4,("NetSessionGetInfo res=%d\n", res));
2026     }
2027   } else {
2028     DEBUG(4,("NetSessionGetInfo no data returned\n"));
2029   }
2030     
2031   SAFE_FREE(rparam);
2032   SAFE_FREE(rdata);
2033
2034   return res;  
2035 }
2036
2037 /****************************************************************************
2038 call a NetSessionDel - close a session to an SMB server
2039 ****************************************************************************/
2040 int cli_NetSessionDel(struct cli_state *cli, const char *workstation)
2041 {
2042   char param[WORDSIZE                      /* api number       */
2043             +sizeof(RAP_NetSessionDel_REQ) /* req string       */
2044             +1                             /* no return string */
2045             +RAP_MACHNAME_LEN              /* workstation name */
2046             +WORDSIZE];                    /* reserved (0)     */
2047   char *p;
2048   char *rparam = NULL;
2049   char *rdata = NULL;
2050   unsigned int rprcnt, rdrcnt;
2051   int res;
2052
2053   memset(param, '\0', sizeof(param));
2054   p = make_header(param, RAP_WsessionDel, RAP_NetSessionDel_REQ, NULL);
2055   PUTSTRING(p, workstation, RAP_MACHNAME_LEN-1);
2056   PUTWORD(p,0); /* reserved word of 0 */
2057   if (cli_api(cli, 
2058               param, PTR_DIFF(p,param), 1024, /* Param, length, maxlen */
2059               NULL, 0, 200,       /* data, length, maxlen */
2060               &rparam, &rprcnt,   /* return params, length */
2061               &rdata, &rdrcnt))   /* return data, length */
2062     {
2063       res = GETRES(rparam);
2064       cli->rap_error = res;
2065       
2066       if (res == 0) {
2067         /* nothing to do */             
2068       }
2069       else {
2070         DEBUG(4,("NetFileClose2 res=%d\n", res));
2071       }      
2072     } else {
2073       res = -1;
2074       DEBUG(4,("NetFileClose2 failed\n"));
2075     }
2076   
2077   SAFE_FREE(rparam);
2078   SAFE_FREE(rdata);
2079
2080   return res;
2081 }
2082   
2083
2084 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))
2085 {
2086   char param[WORDSIZE                          /* api number    */
2087             +sizeof(RAP_NetConnectionEnum_REQ) /* req string    */
2088             +sizeof(RAP_CONNECTION_INFO_L1)    /* return string */ 
2089             +RAP_MACHNAME_LEN                  /* wksta name    */
2090             +WORDSIZE                          /* info level    */
2091             +WORDSIZE];                        /* buffer size   */
2092   char *p;
2093   char *rparam = NULL;
2094   char *rdata = NULL; 
2095   unsigned int rprcnt, rdrcnt;
2096   int res = -1;
2097
2098   memset(param, '\0', sizeof(param));
2099   p = make_header(param, RAP_WconnectionEnum,
2100                   RAP_NetConnectionEnum_REQ, RAP_CONNECTION_INFO_L1);
2101   PUTSTRING(p, qualifier, RAP_MACHNAME_LEN-1);/* Workstation name */
2102   PUTWORD(p,1);            /* Info level 1 */
2103   PUTWORD(p,0xFFE0);       /* Return buffer size */
2104
2105   if (cli_api(cli,
2106               param, PTR_DIFF(p,param),PTR_DIFF(p,param),
2107               NULL, 0, CLI_BUFFER_SIZE,
2108               &rparam, &rprcnt,
2109               &rdata, &rdrcnt)) {
2110     res = GETRES(rparam);
2111     cli->rap_error = res;
2112     if (res != 0) {
2113       DEBUG(1,("NetConnectionEnum gave error %d\n", res));
2114     }
2115   }
2116   if (rdata) {
2117     if (res == 0 || res == ERRmoredata) {
2118       int i, converter, count;
2119
2120       p = rparam + WORDSIZE;
2121       GETWORD(p, converter);
2122       GETWORD(p, count);
2123
2124       for (i=0,p=rdata;i<count;i++) {
2125         pstring netname, username;
2126         uint16  conn_id, conn_type, num_opens, num_users;
2127         unsigned int    conn_time;
2128
2129         GETWORD(p,conn_id);
2130         GETWORD(p,conn_type);
2131         GETWORD(p,num_opens);
2132         GETWORD(p,num_users);
2133         GETDWORD(p,conn_time);
2134         GETSTRINGP(p, username, rdata, converter);
2135         GETSTRINGP(p, netname, rdata, converter);
2136
2137         fn(conn_id, conn_type, num_opens, num_users, conn_time,
2138            username, netname);
2139       }
2140         
2141     } else {
2142       DEBUG(4,("NetConnectionEnum res=%d\n", res));
2143     }
2144   } else {
2145     DEBUG(4,("NetConnectionEnum no data returned\n"));
2146   }
2147   SAFE_FREE(rdata);
2148   SAFE_FREE(rparam);
2149   return res;
2150 }