449bec674488c558b5db7693731f9ca033e1b847
[samba.git] / source3 / utils / net_rap.c
1 /*
2    Samba Unix/Linux SMB client library
3    Distributed SMB/CIFS Server Management Utility
4    Copyright (C) 2001 Steve French  (sfrench@us.ibm.com)
5    Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
6    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
7    Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
8
9    Originally written by Steve and Jim. Largely rewritten by tridge in
10    November 2001.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
24
25 #include "includes.h"
26 #include "utils/net.h"
27
28 /* The following messages were for error checking that is not properly
29    reported at the moment.  Which should be reinstated? */
30 #define ERRMSG_TARGET_WG_NOT_VALID      "\nTarget workgroup option not valid "\
31                                         "except on net rap server command, ignored"
32 #define ERRMSG_INVALID_HELP_OPTION      "\nInvalid help option\n"
33
34 #define ERRMSG_BOTH_SERVER_IPADDRESS    "\nTarget server and IP address both "\
35   "specified. Do not set both at the same time.  The target IP address was used\n"
36
37 const char *share_type[] = {
38   "Disk",
39   "Print",
40   "Dev",
41   "IPC"
42 };
43
44 static int errmsg_not_implemented(void)
45 {
46         d_printf("\nNot implemented\n");
47         return 0;
48 }
49
50 int net_rap_file_usage(struct net_context *c, int argc, const char **argv)
51 {
52         return net_file_usage(c, argc, argv);
53 }
54
55 /***************************************************************************
56   list info on an open file
57 ***************************************************************************/
58 static void file_fn(const char * pPath, const char * pUser, uint16 perms,
59                     uint16 locks, uint32 id)
60 {
61         d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
62                  id, pUser, perms, locks, pPath);
63 }
64
65 static void one_file_fn(const char *pPath, const char *pUser, uint16 perms,
66                         uint16 locks, uint32 id)
67 {
68         d_printf("File ID          %d\n"\
69                  "User name        %s\n"\
70                  "Locks            0x%-4.2x\n"\
71                  "Path             %s\n"\
72                  "Permissions      0x%x\n",
73                  id, pUser, locks, pPath, perms);
74 }
75
76
77 static int rap_file_close(struct net_context *c, int argc, const char **argv)
78 {
79         struct cli_state *cli;
80         int ret;
81         if (argc == 0) {
82                 d_printf("\nMissing fileid of file to close\n\n");
83                 return net_rap_file_usage(c, argc, argv);
84         }
85
86         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
87                 return -1;
88
89         ret = cli_NetFileClose(cli, atoi(argv[0]));
90         cli_shutdown(cli);
91         return ret;
92 }
93
94 static int rap_file_info(struct net_context *c, int argc, const char **argv)
95 {
96         struct cli_state *cli;
97         int ret;
98         if (argc == 0)
99                 return net_rap_file_usage(c, argc, argv);
100
101         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
102                 return -1;
103
104         ret = cli_NetFileGetInfo(cli, atoi(argv[0]), one_file_fn);
105         cli_shutdown(cli);
106         return ret;
107 }
108
109 static int rap_file_user(struct net_context *c, int argc, const char **argv)
110 {
111         struct cli_state *cli;
112         int ret;
113
114         if (argc == 0)
115                 return net_rap_file_usage(c, argc, argv);
116
117         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
118                 return -1;
119
120         /* list open files */
121
122         d_printf("\nEnumerating open files on remote server:\n\n"
123                  "\nFileId  Opened by            Perms  Locks  Path \n"
124                  "------  ---------            -----  -----  ---- \n");
125         ret = cli_NetFileEnum(cli, argv[0], NULL, file_fn);
126
127         if (ret == -1)
128                 d_printf("\nOperation not supported by server!\n\n");
129
130         cli_shutdown(cli);
131         return ret;
132 }
133
134 int net_rap_file(struct net_context *c, int argc, const char **argv)
135 {
136         struct functable func[] = {
137                 {"CLOSE", rap_file_close},
138                 {"USER", rap_file_user},
139                 {"INFO", rap_file_info},
140                 {NULL, NULL}
141         };
142
143         if (argc == 0) {
144                 struct cli_state *cli;
145                 int ret;
146
147                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
148                         return -1;
149
150                 /* list open files */
151
152                 d_printf("\nEnumerating open files on remote server:\n\n"
153                          "\nFileId  Opened by            Perms  Locks  Path \n"
154                          "------  ---------            -----  -----  ---- \n");
155                 ret = cli_NetFileEnum(cli, NULL, NULL, file_fn);
156
157                 if (ret == -1)
158                         d_printf("\nOperation not supported by server!\n\n");
159
160                 cli_shutdown(cli);
161                 return ret;
162         }
163
164         return net_run_function(c, argc, argv, func, net_rap_file_usage);
165 }
166
167 int net_rap_share_usage(struct net_context *c, int argc, const char **argv)
168 {
169         return net_share_usage(c, argc, argv);
170 }
171
172 static void long_share_fn(const char *share_name, uint32 type,
173                           const char *comment, void *state)
174 {
175         d_printf("%-12s %-8.8s %-50s\n",
176                  share_name, share_type[type], comment);
177 }
178
179 static void share_fn(const char *share_name, uint32 type,
180                      const char *comment, void *state)
181 {
182         d_printf("%s\n", share_name);
183 }
184
185 static int rap_share_delete(struct net_context *c, int argc, const char **argv)
186 {
187         struct cli_state *cli;
188         int ret;
189
190         if (argc == 0) {
191                 d_printf("\n\nShare name not specified\n");
192                 return net_rap_share_usage(c, argc, argv);
193         }
194
195         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
196                 return -1;
197
198         ret = cli_NetShareDelete(cli, argv[0]);
199         cli_shutdown(cli);
200         return ret;
201 }
202
203 static int rap_share_add(struct net_context *c, int argc, const char **argv)
204 {
205         struct cli_state *cli;
206         int ret;
207
208         RAP_SHARE_INFO_2 sinfo;
209         char *p;
210         char *sharename;
211
212         if (argc == 0) {
213                 d_printf("\n\nShare name not specified\n");
214                 return net_rap_share_usage(c, argc, argv);
215         }
216
217         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
218                 return -1;
219
220         sharename = SMB_STRDUP(argv[0]);
221         p = strchr(sharename, '=');
222         if (p == NULL) {
223                 d_printf("Server path not specified\n");
224                 SAFE_FREE(sharename);
225                 return net_rap_share_usage(c, argc, argv);
226         }
227         *p = 0;
228         strlcpy(sinfo.share_name, sharename, sizeof(sinfo.share_name));
229         sinfo.reserved1 = '\0';
230         sinfo.share_type = 0;
231         sinfo.comment = smb_xstrdup(c->opt_comment);
232         sinfo.perms = 0;
233         sinfo.maximum_users = c->opt_maxusers;
234         sinfo.active_users = 0;
235         sinfo.path = p+1;
236         memset(sinfo.password, '\0', sizeof(sinfo.password));
237         sinfo.reserved2 = '\0';
238
239         ret = cli_NetShareAdd(cli, &sinfo);
240         cli_shutdown(cli);
241         SAFE_FREE(sharename);
242         return ret;
243 }
244
245
246 int net_rap_share(struct net_context *c, int argc, const char **argv)
247 {
248         struct functable func[] = {
249                 {"DELETE", rap_share_delete},
250                 {"CLOSE", rap_share_delete},
251                 {"ADD", rap_share_add},
252                 {NULL, NULL}
253         };
254
255         if (argc == 0) {
256                 struct cli_state *cli;
257                 int ret;
258
259                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
260                         return -1;
261
262                 if (c->opt_long_list_entries) {
263                         d_printf(
264         "\nEnumerating shared resources (exports) on remote server:\n\n"\
265         "\nShare name   Type     Description\n"\
266         "----------   ----     -----------\n");
267                         ret = cli_RNetShareEnum(cli, long_share_fn, NULL);
268                 } else {
269                         ret = cli_RNetShareEnum(cli, share_fn, NULL);
270                 }
271                 cli_shutdown(cli);
272                 return ret;
273         }
274
275         return net_run_function(c, argc, argv, func, net_rap_share_usage);
276 }
277
278 int net_rap_session_usage(struct net_context *c, int argc, const char **argv)
279 {
280         d_printf(
281          "\nnet rap session [misc. options] [targets]"\
282          "\n\tenumerates all active SMB/CIFS sessions on target server\n");
283         d_printf(
284          "\nnet rap session DELETE <client_name> [misc. options] [targets] \n"\
285          "\tor"\
286          "\nnet rap session CLOSE <client_name> [misc. options] [targets]"\
287          "\n\tDeletes (closes) a session from specified client to server\n");
288         d_printf(
289         "\nnet rap session INFO <client_name>"\
290         "\n\tEnumerates all open files in specified session\n");
291
292         net_common_flags_usage(c, argc, argv);
293         return -1;
294 }
295
296 static void list_sessions_func(char *wsname, char *username, uint16 conns,
297                         uint16 opens, uint16 users, uint32 sess_time,
298                         uint32 idle_time, uint32 user_flags, char *clitype)
299 {
300         int hrs = idle_time / 3600;
301         int min = (idle_time / 60) % 60;
302         int sec = idle_time % 60;
303
304         d_printf("\\\\%-18.18s %-20.20s %-18.18s %5d %2.2d:%2.2d:%2.2d\n",
305                  wsname, username, clitype, opens, hrs, min, sec);
306 }
307
308 static void display_session_func(const char *wsname, const char *username,
309                                  uint16 conns, uint16 opens, uint16 users,
310                                  uint32 sess_time, uint32 idle_time,
311                                  uint32 user_flags, const char *clitype)
312 {
313         int ihrs = idle_time / 3600;
314         int imin = (idle_time / 60) % 60;
315         int isec = idle_time % 60;
316         int shrs = sess_time / 3600;
317         int smin = (sess_time / 60) % 60;
318         int ssec = sess_time % 60;
319         d_printf("User name       %-20.20s\n"\
320                  "Computer        %-20.20s\n"\
321                  "Guest logon     %-20.20s\n"\
322                  "Client Type     %-40.40s\n"\
323                  "Sess time       %2.2d:%2.2d:%2.2d\n"\
324                  "Idle time       %2.2d:%2.2d:%2.2d\n",
325                  username, wsname,
326                  (user_flags&0x0)?"yes":"no", clitype,
327                  shrs, smin, ssec, ihrs, imin, isec);
328 }
329
330 static void display_conns_func(uint16 conn_id, uint16 conn_type, uint16 opens,
331                                uint16 users, uint32 conn_time,
332                                const char *username, const char *netname)
333 {
334         d_printf("%-14.14s %-8.8s %5d\n",
335                  netname, share_type[conn_type], opens);
336 }
337
338 static int rap_session_info(struct net_context *c, int argc, const char **argv)
339 {
340         const char *sessname;
341         struct cli_state *cli;
342         int ret;
343
344         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
345                 return -1;
346
347         if (argc == 0)
348                 return net_rap_session_usage(c, argc, argv);
349
350         sessname = argv[0];
351
352         ret = cli_NetSessionGetInfo(cli, sessname, display_session_func);
353         if (ret < 0) {
354                 cli_shutdown(cli);
355                 return ret;
356         }
357
358         d_printf("Share name     Type     # Opens\n-------------------------"\
359                  "-----------------------------------------------------\n");
360         ret = cli_NetConnectionEnum(cli, sessname, display_conns_func);
361         cli_shutdown(cli);
362         return ret;
363 }
364
365 static int rap_session_delete(struct net_context *c, int argc, const char **argv)
366 {
367         struct cli_state *cli;
368         int ret;
369
370         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
371                 return -1;
372
373         if (argc == 0)
374                 return net_rap_session_usage(c, argc, argv);
375
376         ret = cli_NetSessionDel(cli, argv[0]);
377         cli_shutdown(cli);
378         return ret;
379 }
380
381 int net_rap_session(struct net_context *c, int argc, const char **argv)
382 {
383         struct functable func[] = {
384                 {"INFO", rap_session_info},
385                 {"DELETE", rap_session_delete},
386                 {"CLOSE", rap_session_delete},
387                 {NULL, NULL}
388         };
389
390         if (argc == 0) {
391                 struct cli_state *cli;
392                 int ret;
393
394                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
395                         return -1;
396
397                 d_printf("Computer             User name            "\
398                          "Client Type        Opens Idle time\n"\
399                          "------------------------------------------"\
400                          "------------------------------------\n");
401                 ret = cli_NetSessionEnum(cli, list_sessions_func);
402
403                 cli_shutdown(cli);
404                 return ret;
405         }
406
407         return net_run_function(c, argc, argv, func, net_rap_session_usage);
408 }
409
410 /****************************************************************************
411 list a server name
412 ****************************************************************************/
413 static void display_server_func(const char *name, uint32 m,
414                                 const char *comment, void * reserved)
415 {
416         d_printf("\t%-16.16s     %s\n", name, comment);
417 }
418
419
420 int net_rap_server_usage(struct net_context *c, int argc, const char **argv)
421 {
422         d_printf("net rap server [misc. options] [target]\n\t"\
423                  "lists the servers in the specified domain or workgroup.\n");
424         d_printf("\n\tIf domain is not specified, it uses the current"\
425                  " domain or workgroup as\n\tthe default.\n");
426
427         net_common_flags_usage(c, argc, argv);
428         return -1;
429 }
430
431 static int net_rap_server_name(struct net_context *c, int argc, const char *argv[])
432 {
433         struct cli_state *cli;
434         char *name;
435
436         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
437                 return -1;
438
439         if (!cli_get_server_name(NULL, cli, &name)) {
440                 d_fprintf(stderr, "cli_get_server_name failed\n");
441                 cli_shutdown(cli);
442                 return -1;
443         }
444
445         d_printf("Server name = %s\n", name);
446
447         TALLOC_FREE(name);
448         cli_shutdown(cli);
449         return 0;
450 }
451
452 int net_rap_server(struct net_context *c, int argc, const char **argv)
453 {
454         struct cli_state *cli;
455         int ret;
456
457         if (argc > 0) {
458                 if (strequal(argv[0], "name")) {
459                         return net_rap_server_name(c, argc, argv);
460                 }
461                 /* smb4k uses 'net [rap|rpc] server domain' to query servers in a domain */
462                 /* Fall through for 'domain', any other forms will cause to show usage message */
463                 if (!strequal(argv[0], "domain")) {
464                         return net_rap_server_usage(c, argc-1, argv+1);
465                 }
466         }
467
468         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
469                 return -1;
470
471         d_printf("\nEnumerating servers in this domain or workgroup: \n\n"\
472                  "\tServer name          Server description\n"\
473                  "\t-------------        ----------------------------\n");
474
475         ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL,
476                                 display_server_func,NULL);
477         cli_shutdown(cli);
478         return ret;
479 }
480
481 int net_rap_domain_usage(struct net_context *c, int argc, const char **argv)
482 {
483         d_printf("net rap domain [misc. options] [target]\n\tlists the"\
484                  " domains or workgroups visible on the current network\n");
485
486         net_common_flags_usage(c, argc, argv);
487         return -1;
488 }
489
490 int net_rap_domain(struct net_context *c, int argc, const char **argv)
491 {
492         struct cli_state *cli;
493         int ret;
494
495         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
496                 return -1;
497
498         d_printf("\nEnumerating domains:\n\n"\
499                  "\tDomain name          Server name of Browse Master\n"\
500                  "\t-------------        ----------------------------\n");
501
502         ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
503                                 display_server_func,NULL);
504         cli_shutdown(cli);
505         return ret;
506 }
507
508 int net_rap_printq_usage(struct net_context *c, int argc, const char **argv)
509 {
510         d_printf(
511          "net rap printq [misc. options] [targets]\n"\
512          "\tor\n"\
513          "net rap printq list [<queue_name>] [misc. options] [targets]\n"\
514          "\tlists the specified queue and jobs on the target server.\n"\
515          "\tIf the queue name is not specified, all queues are listed.\n\n");
516         d_printf(
517          "net rap printq delete [<queue name>] [misc. options] [targets]\n"\
518          "\tdeletes the specified job number on the target server, or the\n"\
519          "\tprinter queue if no job number is specified\n");
520
521         net_common_flags_usage(c, argc, argv);
522
523         return -1;
524 }
525
526 static void enum_queue(const char *queuename, uint16 pri, uint16 start,
527                        uint16 until, const char *sep, const char *pproc,
528                        const char *dest, const char *qparms,
529                        const char *qcomment, uint16 status, uint16 jobcount)
530 {
531         d_printf("%-17.17s Queue %5d jobs                      ",
532                  queuename, jobcount);
533
534         switch (status) {
535         case 0:
536                 d_printf("*Printer Active*\n");
537                 break;
538         case 1:
539                 d_printf("*Printer Paused*\n");
540                 break;
541         case 2:
542                 d_printf("*Printer error*\n");
543                 break;
544         case 3:
545                 d_printf("*Delete Pending*\n");
546                 break;
547         default:
548                 d_printf("**UNKNOWN STATUS**\n");
549         }
550 }
551
552 static void enum_jobs(uint16 jobid, const char *ownername,
553                       const char *notifyname, const char *datatype,
554                       const char *jparms, uint16 pos, uint16 status,
555                       const char *jstatus, unsigned int submitted, unsigned int jobsize,
556                       const char *comment)
557 {
558         d_printf("     %-23.23s %5d %9d            ",
559                  ownername, jobid, jobsize);
560         switch (status) {
561         case 0:
562                 d_printf("Waiting\n");
563                 break;
564         case 1:
565                 d_printf("Held in queue\n");
566                 break;
567         case 2:
568                 d_printf("Spooling\n");
569                 break;
570         case 3:
571                 d_printf("Printing\n");
572                 break;
573         default:
574                 d_printf("**UNKNOWN STATUS**\n");
575         }
576 }
577
578 #define PRINTQ_ENUM_DISPLAY \
579     "Print queues at \\\\%s\n\n"\
580     "Name                         Job #      Size            Status\n\n"\
581     "------------------------------------------------------------------"\
582     "-------------\n"
583
584 static int rap_printq_info(struct net_context *c, int argc, const char **argv)
585 {
586         struct cli_state *cli;
587         int ret;
588
589         if (argc == 0)
590                 return net_rap_printq_usage(c, argc, argv);
591
592         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
593                 return -1;
594
595         d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
596         ret = cli_NetPrintQGetInfo(cli, argv[0], enum_queue, enum_jobs);
597         cli_shutdown(cli);
598         return ret;
599 }
600
601 static int rap_printq_delete(struct net_context *c, int argc, const char **argv)
602 {
603         struct cli_state *cli;
604         int ret;
605
606         if (argc == 0)
607                 return net_rap_printq_usage(c, argc, argv);
608
609         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
610                 return -1;
611
612         ret = cli_printjob_del(cli, atoi(argv[0]));
613         cli_shutdown(cli);
614         return ret;
615 }
616
617 int net_rap_printq(struct net_context *c, int argc, const char **argv)
618 {
619         struct cli_state *cli;
620         int ret;
621
622         struct functable func[] = {
623                 {"INFO", rap_printq_info},
624                 {"DELETE", rap_printq_delete},
625                 {NULL, NULL}
626         };
627
628         if (argc == 0) {
629                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
630                         return -1;
631
632                 d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
633                 ret = cli_NetPrintQEnum(cli, enum_queue, enum_jobs);
634                 cli_shutdown(cli);
635                 return ret;
636         }
637
638         return net_run_function(c, argc, argv, func, net_rap_printq_usage);
639 }
640
641 static int net_rap_user_usage(struct net_context *c, int argc, const char **argv)
642 {
643         return net_user_usage(c, argc, argv);
644 }
645
646 static void user_fn(const char *user_name, void *state)
647 {
648         d_printf("%-21.21s\n", user_name);
649 }
650
651 static void long_user_fn(const char *user_name, const char *comment,
652                          const char * home_dir, const char * logon_script,
653                          void *state)
654 {
655         d_printf("%-21.21s %s\n",
656                  user_name, comment);
657 }
658
659 static void group_member_fn(const char *user_name, void *state)
660 {
661         d_printf("%-21.21s\n", user_name);
662 }
663
664 static int rap_user_delete(struct net_context *c, int argc, const char **argv)
665 {
666         struct cli_state *cli;
667         int ret;
668
669         if (argc == 0) {
670                 d_printf("\n\nUser name not specified\n");
671                 return net_rap_user_usage(c, argc, argv);
672         }
673
674         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
675                 return -1;
676
677         ret = cli_NetUserDelete(cli, argv[0]);
678         cli_shutdown(cli);
679         return ret;
680 }
681
682 static int rap_user_add(struct net_context *c, int argc, const char **argv)
683 {
684         struct cli_state *cli;
685         int ret;
686         RAP_USER_INFO_1 userinfo;
687
688         if (argc == 0) {
689                 d_printf("\n\nUser name not specified\n");
690                 return net_rap_user_usage(c, argc, argv);
691         }
692
693         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
694                 return -1;
695
696         safe_strcpy(userinfo.user_name, argv[0], sizeof(userinfo.user_name)-1);
697         if (c->opt_flags == -1)
698                 c->opt_flags = 0x21;
699
700         userinfo.userflags = c->opt_flags;
701         userinfo.reserved1 = '\0';
702         userinfo.comment = smb_xstrdup(c->opt_comment);
703         userinfo.priv = 1;
704         userinfo.home_dir = NULL;
705         userinfo.logon_script = NULL;
706
707         ret = cli_NetUserAdd(cli, &userinfo);
708
709         cli_shutdown(cli);
710         return ret;
711 }
712
713 static int rap_user_info(struct net_context *c, int argc, const char **argv)
714 {
715         struct cli_state *cli;
716         int ret;
717         if (argc == 0) {
718                 d_printf("\n\nUser name not specified\n");
719                 return net_rap_user_usage(c, argc, argv);
720         }
721
722         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
723                 return -1;
724
725         ret = cli_NetUserGetGroups(cli, argv[0], group_member_fn, NULL);
726         cli_shutdown(cli);
727         return ret;
728 }
729
730 int net_rap_user(struct net_context *c, int argc, const char **argv)
731 {
732         int ret = -1;
733         struct functable func[] = {
734                 {"ADD", rap_user_add},
735                 {"INFO", rap_user_info},
736                 {"DELETE", rap_user_delete},
737                 {NULL, NULL}
738         };
739
740         if (argc == 0) {
741                 struct cli_state *cli;
742                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
743                         goto done;
744                 if (c->opt_long_list_entries) {
745                         d_printf("\nUser name             Comment"\
746                                  "\n-----------------------------\n");
747                         ret = cli_RNetUserEnum(cli, long_user_fn, NULL);
748                         cli_shutdown(cli);
749                         goto done;
750                 }
751                 ret = cli_RNetUserEnum0(cli, user_fn, NULL);
752                 cli_shutdown(cli);
753                 goto done;
754         }
755
756         ret = net_run_function(c, argc, argv, func, net_rap_user_usage);
757  done:
758         if (ret != 0) {
759                 DEBUG(1, ("Net user returned: %d\n", ret));
760         }
761         return ret;
762 }
763
764
765 int net_rap_group_usage(struct net_context *c, int argc, const char **argv)
766 {
767         return net_group_usage(c, argc, argv);
768 }
769
770 static void long_group_fn(const char *group_name, const char *comment,
771                           void *state)
772 {
773         d_printf("%-21.21s %s\n", group_name, comment);
774 }
775
776 static void group_fn(const char *group_name, void *state)
777 {
778         d_printf("%-21.21s\n", group_name);
779 }
780
781 static int rap_group_delete(struct net_context *c, int argc, const char **argv)
782 {
783         struct cli_state *cli;
784         int ret;
785         if (argc == 0) {
786                 d_printf("\n\nGroup name not specified\n");
787                 return net_rap_group_usage(c, argc, argv);
788         }
789
790         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
791                 return -1;
792
793         ret = cli_NetGroupDelete(cli, argv[0]);
794         cli_shutdown(cli);
795         return ret;
796 }
797
798 static int rap_group_add(struct net_context *c, int argc, const char **argv)
799 {
800         struct cli_state *cli;
801         int ret;
802         RAP_GROUP_INFO_1 grinfo;
803
804         if (argc == 0) {
805                 d_printf("\n\nGroup name not specified\n");
806                 return net_rap_group_usage(c, argc, argv);
807         }
808
809         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
810                 return -1;
811
812         /* BB check for length 21 or smaller explicitly ? BB */
813         safe_strcpy(grinfo.group_name, argv[0], sizeof(grinfo.group_name)-1);
814         grinfo.reserved1 = '\0';
815         grinfo.comment = smb_xstrdup(c->opt_comment);
816
817         ret = cli_NetGroupAdd(cli, &grinfo);
818         cli_shutdown(cli);
819         return ret;
820 }
821
822 int net_rap_group(struct net_context *c, int argc, const char **argv)
823 {
824         struct functable func[] = {
825                 {"ADD", rap_group_add},
826                 {"DELETE", rap_group_delete},
827                 {NULL, NULL}
828         };
829
830         if (argc == 0) {
831                 struct cli_state *cli;
832                 int ret;
833                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
834                         return -1;
835                 if (c->opt_long_list_entries) {
836                         d_printf("Group name            Comment\n");
837                         d_printf("-----------------------------\n");
838                         ret = cli_RNetGroupEnum(cli, long_group_fn, NULL);
839                         cli_shutdown(cli);
840                         return ret;
841                 }
842                 ret = cli_RNetGroupEnum0(cli, group_fn, NULL);
843                 cli_shutdown(cli);
844                 return ret;
845         }
846
847         return net_run_function(c, argc, argv, func, net_rap_group_usage);
848 }
849
850 int net_rap_groupmember_usage(struct net_context *c, int argc, const char **argv)
851 {
852         d_printf(
853          "net rap groupmember LIST <group> [misc. options] [targets]"\
854          "\n\t Enumerate users in a group\n"\
855          "\nnet rap groupmember DELETE <group> <user> [misc. options] "\
856          "[targets]\n\t Delete sepcified user from specified group\n"\
857          "\nnet rap groupmember ADD <group> <user> [misc. options] [targets]"\
858          "\n\t Add specified user to specified group\n");
859
860         net_common_flags_usage(c, argc, argv);
861         return -1;
862 }
863
864
865 static int rap_groupmember_add(struct net_context *c, int argc, const char **argv)
866 {
867         struct cli_state *cli;
868         int ret;
869         if (argc != 2) {
870                 d_printf("\n\nGroup or user name not specified\n");
871                 return net_rap_groupmember_usage(c, argc, argv);
872         }
873
874         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
875                 return -1;
876
877         ret = cli_NetGroupAddUser(cli, argv[0], argv[1]);
878         cli_shutdown(cli);
879         return ret;
880 }
881
882 static int rap_groupmember_delete(struct net_context *c, int argc, const char **argv)
883 {
884         struct cli_state *cli;
885         int ret;
886         if (argc != 2) {
887                 d_printf("\n\nGroup or user name not specified\n");
888                 return net_rap_groupmember_usage(c, argc, argv);
889         }
890
891         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
892                 return -1;
893
894         ret = cli_NetGroupDelUser(cli, argv[0], argv[1]);
895         cli_shutdown(cli);
896         return ret;
897 }
898
899 static int rap_groupmember_list(struct net_context *c, int argc, const char **argv)
900 {
901         struct cli_state *cli;
902         int ret;
903         if (argc == 0) {
904                 d_printf("\n\nGroup name not specified\n");
905                 return net_rap_groupmember_usage(c, argc, argv);
906         }
907
908         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
909                 return -1;
910
911         ret = cli_NetGroupGetUsers(cli, argv[0], group_member_fn, NULL );
912         cli_shutdown(cli);
913         return ret;
914 }
915
916 int net_rap_groupmember(struct net_context *c, int argc, const char **argv)
917 {
918         struct functable func[] = {
919                 {"ADD", rap_groupmember_add},
920                 {"LIST", rap_groupmember_list},
921                 {"DELETE", rap_groupmember_delete},
922                 {NULL, NULL}
923         };
924
925         return net_run_function(c, argc, argv, func, net_rap_groupmember_usage);
926 }
927
928 int net_rap_validate_usage(struct net_context *c, int argc, const char **argv)
929 {
930         d_printf("net rap validate <username> [password]\n"\
931                  "\tValidate user and password to check whether they"\
932                  " can access target server or domain\n");
933
934         net_common_flags_usage(c, argc, argv);
935         return -1;
936 }
937
938 int net_rap_validate(struct net_context *c, int argc, const char **argv)
939 {
940         return errmsg_not_implemented();
941 }
942
943 int net_rap_service_usage(struct net_context *c, int argc, const char **argv)
944 {
945         d_printf("net rap service [misc. options] [targets] \n"\
946                  "\tlists all running service daemons on target server\n");
947         d_printf("\nnet rap service START <name> [service startup arguments]"\
948                  " [misc. options] [targets]"\
949                  "\n\tStart named service on remote server\n");
950         d_printf("\nnet rap service STOP <name> [misc. options] [targets]\n"\
951                  "\n\tStop named service on remote server\n");
952
953         net_common_flags_usage(c, argc, argv);
954         return -1;
955 }
956
957 static int rap_service_start(struct net_context *c, int argc, const char **argv)
958 {
959         return errmsg_not_implemented();
960 }
961
962 static int rap_service_stop(struct net_context *c, int argc, const char **argv)
963 {
964         return errmsg_not_implemented();
965 }
966
967 static void service_fn(const char *service_name, const char *dummy,
968                        void *state)
969 {
970         d_printf("%-21.21s\n", service_name);
971 }
972
973 int net_rap_service(struct net_context *c, int argc, const char **argv)
974 {
975         struct functable func[] = {
976                 {"START", rap_service_start},
977                 {"STOP", rap_service_stop},
978                 {NULL, NULL}
979         };
980
981         if (argc == 0) {
982                 struct cli_state *cli;
983                 int ret;
984                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
985                         return -1;
986
987                 if (c->opt_long_list_entries) {
988                         d_printf("Service name          Comment\n");
989                         d_printf("-----------------------------\n");
990                         ret = cli_RNetServiceEnum(cli, long_group_fn, NULL);
991                 }
992                 ret = cli_RNetServiceEnum(cli, service_fn, NULL);
993                 cli_shutdown(cli);
994                 return ret;
995         }
996
997         return net_run_function(c, argc, argv, func, net_rap_service_usage);
998 }
999
1000 int net_rap_password_usage(struct net_context *c, int argc, const char **argv)
1001 {
1002         d_printf(
1003          "net rap password <user> <oldpwo> <newpw> [misc. options] [target]\n"\
1004          "\tchanges the password for the specified user at target\n");
1005
1006         return -1;
1007 }
1008
1009
1010 int net_rap_password(struct net_context *c, int argc, const char **argv)
1011 {
1012         struct cli_state *cli;
1013         int ret;
1014
1015         if (argc < 3)
1016                 return net_rap_password_usage(c, argc, argv);
1017
1018         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1019                 return -1;
1020
1021         /* BB Add check for password lengths? */
1022         ret = cli_oem_change_password(cli, argv[0], argv[2], argv[1]);
1023         cli_shutdown(cli);
1024         return ret;
1025 }
1026
1027 int net_rap_admin_usage(struct net_context *c, int argc, const char **argv)
1028 {
1029         d_printf(
1030    "net rap admin <remote command> [cmd args [env]] [misc. options] [targets]"\
1031    "\n\texecutes a remote command on an os/2 target server\n");
1032
1033         return -1;
1034 }
1035
1036
1037 int net_rap_admin(struct net_context *c, int argc, const char **argv)
1038 {
1039         return errmsg_not_implemented();
1040 }
1041
1042 /* The help subsystem for the RAP subcommand */
1043
1044 int net_rap_help(struct net_context *c, int argc, const char **argv)
1045 {
1046         d_printf("  net rap domain \tto list domains \n"\
1047                  "  net rap file \t\tto list open files on a server \n"\
1048                  "  net rap group \tto list user groups  \n"\
1049                  "  net rap groupmember \tto list users in a group \n"\
1050                  "  net rap password \tto change the password of a user\n"\
1051                  "  net rap printq \tto list the print queues on a server\n"\
1052                  "  net rap server \tto list servers in a domain\n"\
1053                  "  net rap session \tto list clients with open sessions to a server\n"\
1054                  "  net rap share \tto list shares exported by a server\n"\
1055                  "  net rap user \t\tto list users\n"\
1056                  "  net rap validate \tto check whether a user and the corresponding password are valid\n"\
1057                  "  net rap help\n"\
1058                  "\nType \"net help <option>\" to get more information on that option\n\n");
1059
1060         net_common_flags_usage(c, argc, argv);
1061         return -1;
1062 }
1063
1064 /*
1065   handle "net rap help *" subcommands
1066 */
1067 int net_rap_usage(struct net_context *c, int argc, const char **argv)
1068 {
1069         struct functable func[] = {
1070                 {"FILE", net_rap_file_usage},
1071                 {"SHARE", net_rap_share_usage},
1072                 {"SESSION", net_rap_session_usage},
1073                 {"SERVER", net_rap_server_usage},
1074                 {"DOMAIN", net_rap_domain_usage},
1075                 {"PRINTQ", net_rap_printq_usage},
1076                 {"USER", net_rap_user_usage},
1077                 {"GROUP", net_rap_group_usage},
1078                 {"VALIDATE", net_rap_validate_usage},
1079                 {"GROUPMEMBER", net_rap_groupmember_usage},
1080                 {"ADMIN", net_rap_admin_usage},
1081                 {"SERVICE", net_rap_service_usage},
1082                 {"PASSWORD", net_rap_password_usage},
1083                 {NULL, NULL}};
1084
1085         return net_run_function(c, argc, argv, func, net_rap_help);
1086 }
1087
1088 /* Entry-point for all the RAP functions. */
1089
1090 int net_rap(struct net_context *c, int argc, const char **argv)
1091 {
1092         struct functable func[] = {
1093                 {"FILE", net_rap_file},
1094                 {"SHARE", net_rap_share},
1095                 {"SESSION", net_rap_session},
1096                 {"SERVER", net_rap_server},
1097                 {"DOMAIN", net_rap_domain},
1098                 {"PRINTQ", net_rap_printq},
1099                 {"USER", net_rap_user},
1100                 {"GROUP", net_rap_group},
1101                 {"VALIDATE", net_rap_validate},
1102                 {"GROUPMEMBER", net_rap_groupmember},
1103                 {"ADMIN", net_rap_admin},
1104                 {"SERVICE", net_rap_service},
1105                 {"PASSWORD", net_rap_password},
1106                 {"HELP", net_rap_usage},
1107                 {NULL, NULL}
1108         };
1109
1110         return net_run_function(c, argc, argv, func, net_rap_help);
1111 }
1112