net: more whitespace cleanup
[ira/wip.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_help_file(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_help_share(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                 return net_rap_share_usage(c, argc, argv);
225         }
226         *p = 0;
227         strlcpy(sinfo.share_name, sharename, sizeof(sinfo.share_name));
228         sinfo.reserved1 = '\0';
229         sinfo.share_type = 0;
230         sinfo.comment = smb_xstrdup(c->opt_comment);
231         sinfo.perms = 0;
232         sinfo.maximum_users = c->opt_maxusers;
233         sinfo.active_users = 0;
234         sinfo.path = p+1;
235         memset(sinfo.password, '\0', sizeof(sinfo.password));
236         sinfo.reserved2 = '\0';
237
238         ret = cli_NetShareAdd(cli, &sinfo);
239         cli_shutdown(cli);
240         return ret;
241 }
242
243
244 int net_rap_share(struct net_context *c, int argc, const char **argv)
245 {
246         struct functable func[] = {
247                 {"DELETE", rap_share_delete},
248                 {"CLOSE", rap_share_delete},
249                 {"ADD", rap_share_add},
250                 {NULL, NULL}
251         };
252
253         if (argc == 0) {
254                 struct cli_state *cli;
255                 int ret;
256
257                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
258                         return -1;
259
260                 if (c->opt_long_list_entries) {
261                         d_printf(
262         "\nEnumerating shared resources (exports) on remote server:\n\n"\
263         "\nShare name   Type     Description\n"\
264         "----------   ----     -----------\n");
265                         ret = cli_RNetShareEnum(cli, long_share_fn, NULL);
266                 } else {
267                         ret = cli_RNetShareEnum(cli, share_fn, NULL);
268                 }
269                 cli_shutdown(cli);
270                 return ret;
271         }
272
273         return net_run_function(c, argc, argv, func, net_rap_share_usage);
274 }
275
276 int net_rap_session_usage(struct net_context *c, int argc, const char **argv)
277 {
278         d_printf(
279          "\nnet rap session [misc. options] [targets]"\
280          "\n\tenumerates all active SMB/CIFS sessions on target server\n");
281         d_printf(
282          "\nnet rap session DELETE <client_name> [misc. options] [targets] \n"\
283          "\tor"\
284          "\nnet rap session CLOSE <client_name> [misc. options] [targets]"\
285          "\n\tDeletes (closes) a session from specified client to server\n");
286         d_printf(
287         "\nnet rap session INFO <client_name>"\
288         "\n\tEnumerates all open files in specified session\n");
289
290         net_common_flags_usage(c, argc, argv);
291         return -1;
292 }
293
294 static void list_sessions_func(char *wsname, char *username, uint16 conns,
295                         uint16 opens, uint16 users, uint32 sess_time,
296                         uint32 idle_time, uint32 user_flags, char *clitype)
297 {
298         int hrs = idle_time / 3600;
299         int min = (idle_time / 60) % 60;
300         int sec = idle_time % 60;
301
302         d_printf("\\\\%-18.18s %-20.20s %-18.18s %5d %2.2d:%2.2d:%2.2d\n",
303                  wsname, username, clitype, opens, hrs, min, sec);
304 }
305
306 static void display_session_func(const char *wsname, const char *username,
307                                  uint16 conns, uint16 opens, uint16 users,
308                                  uint32 sess_time, uint32 idle_time,
309                                  uint32 user_flags, const char *clitype)
310 {
311         int ihrs = idle_time / 3600;
312         int imin = (idle_time / 60) % 60;
313         int isec = idle_time % 60;
314         int shrs = sess_time / 3600;
315         int smin = (sess_time / 60) % 60;
316         int ssec = sess_time % 60;
317         d_printf("User name       %-20.20s\n"\
318                  "Computer        %-20.20s\n"\
319                  "Guest logon     %-20.20s\n"\
320                  "Client Type     %-40.40s\n"\
321                  "Sess time       %2.2d:%2.2d:%2.2d\n"\
322                  "Idle time       %2.2d:%2.2d:%2.2d\n",
323                  username, wsname,
324                  (user_flags&0x0)?"yes":"no", clitype,
325                  shrs, smin, ssec, ihrs, imin, isec);
326 }
327
328 static void display_conns_func(uint16 conn_id, uint16 conn_type, uint16 opens,
329                                uint16 users, uint32 conn_time,
330                                const char *username, const char *netname)
331 {
332         d_printf("%-14.14s %-8.8s %5d\n",
333                  netname, share_type[conn_type], opens);
334 }
335
336 static int rap_session_info(struct net_context *c, int argc, const char **argv)
337 {
338         const char *sessname;
339         struct cli_state *cli;
340         int ret;
341
342         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
343                 return -1;
344
345         if (argc == 0)
346                 return net_rap_session_usage(c, argc, argv);
347
348         sessname = argv[0];
349
350         ret = cli_NetSessionGetInfo(cli, sessname, display_session_func);
351         if (ret < 0) {
352                 cli_shutdown(cli);
353                 return ret;
354         }
355
356         d_printf("Share name     Type     # Opens\n-------------------------"\
357                  "-----------------------------------------------------\n");
358         ret = cli_NetConnectionEnum(cli, sessname, display_conns_func);
359         cli_shutdown(cli);
360         return ret;
361 }
362
363 static int rap_session_delete(struct net_context *c, int argc, const char **argv)
364 {
365         struct cli_state *cli;
366         int ret;
367
368         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
369                 return -1;
370
371         if (argc == 0)
372                 return net_rap_session_usage(c, argc, argv);
373
374         ret = cli_NetSessionDel(cli, argv[0]);
375         cli_shutdown(cli);
376         return ret;
377 }
378
379 int net_rap_session(struct net_context *c, int argc, const char **argv)
380 {
381         struct functable func[] = {
382                 {"INFO", rap_session_info},
383                 {"DELETE", rap_session_delete},
384                 {"CLOSE", rap_session_delete},
385                 {NULL, NULL}
386         };
387
388         if (argc == 0) {
389                 struct cli_state *cli;
390                 int ret;
391
392                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
393                         return -1;
394
395                 d_printf("Computer             User name            "\
396                          "Client Type        Opens Idle time\n"\
397                          "------------------------------------------"\
398                          "------------------------------------\n");
399                 ret = cli_NetSessionEnum(cli, list_sessions_func);
400
401                 cli_shutdown(cli);
402                 return ret;
403         }
404
405         return net_run_function(c, argc, argv, func, net_rap_session_usage);
406 }
407
408 /****************************************************************************
409 list a server name
410 ****************************************************************************/
411 static void display_server_func(const char *name, uint32 m,
412                                 const char *comment, void * reserved)
413 {
414         d_printf("\t%-16.16s     %s\n", name, comment);
415 }
416
417
418 int net_rap_server_usage(struct net_context *c, int argc, const char **argv)
419 {
420         d_printf("net rap server [misc. options] [target]\n\t"\
421                  "lists the servers in the specified domain or workgroup.\n");
422         d_printf("\n\tIf domain is not specified, it uses the current"\
423                  " domain or workgroup as\n\tthe default.\n");
424
425         net_common_flags_usage(c, argc, argv);
426         return -1;
427 }
428
429 static int net_rap_server_name(struct net_context *c, int argc, const char *argv[])
430 {
431         struct cli_state *cli;
432         char *name;
433
434         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
435                 return -1;
436
437         if (!cli_get_server_name(NULL, cli, &name)) {
438                 d_fprintf(stderr, "cli_get_server_name failed\n");
439                 cli_shutdown(cli);
440                 return -1;
441         }
442
443         d_printf("Server name = %s\n", name);
444
445         TALLOC_FREE(name);
446         cli_shutdown(cli);
447         return 0;
448 }
449
450 int net_rap_server(struct net_context *c, int argc, const char **argv)
451 {
452         struct cli_state *cli;
453         int ret;
454
455         if (argc > 0) {
456                 if (strequal(argv[0], "name")) {
457                         return net_rap_server_name(c, argc, argv);
458                 }
459                 /* smb4k uses 'net [rap|rpc] server domain' to query servers in a domain */
460                 /* Fall through for 'domain', any other forms will cause to show usage message */
461                 if (!strequal(argv[0], "domain")) {
462                         return net_rap_server_usage(c, argc-1, argv+1);
463                 }
464         }
465
466         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
467                 return -1;
468
469         d_printf("\nEnumerating servers in this domain or workgroup: \n\n"\
470                  "\tServer name          Server description\n"\
471                  "\t-------------        ----------------------------\n");
472
473         ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL,
474                                 display_server_func,NULL);
475         cli_shutdown(cli);
476         return ret;
477 }
478
479 int net_rap_domain_usage(struct net_context *c, int argc, const char **argv)
480 {
481         d_printf("net rap domain [misc. options] [target]\n\tlists the"\
482                  " domains or workgroups visible on the current network\n");
483
484         net_common_flags_usage(c, argc, argv);
485         return -1;
486 }
487
488 int net_rap_domain(struct net_context *c, int argc, const char **argv)
489 {
490         struct cli_state *cli;
491         int ret;
492
493         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
494                 return -1;
495
496         d_printf("\nEnumerating domains:\n\n"\
497                  "\tDomain name          Server name of Browse Master\n"\
498                  "\t-------------        ----------------------------\n");
499
500         ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
501                                 display_server_func,NULL);
502         cli_shutdown(cli);
503         return ret;
504 }
505
506 int net_rap_printq_usage(struct net_context *c, int argc, const char **argv)
507 {
508         d_printf(
509          "net rap printq [misc. options] [targets]\n"\
510          "\tor\n"\
511          "net rap printq list [<queue_name>] [misc. options] [targets]\n"\
512          "\tlists the specified queue and jobs on the target server.\n"\
513          "\tIf the queue name is not specified, all queues are listed.\n\n");
514         d_printf(
515          "net rap printq delete [<queue name>] [misc. options] [targets]\n"\
516          "\tdeletes the specified job number on the target server, or the\n"\
517          "\tprinter queue if no job number is specified\n");
518
519         net_common_flags_usage(c, argc, argv);
520
521         return -1;
522 }
523
524 static void enum_queue(const char *queuename, uint16 pri, uint16 start,
525                        uint16 until, const char *sep, const char *pproc,
526                        const char *dest, const char *qparms,
527                        const char *qcomment, uint16 status, uint16 jobcount)
528 {
529         d_printf("%-17.17s Queue %5d jobs                      ",
530                  queuename, jobcount);
531
532         switch (status) {
533         case 0:
534                 d_printf("*Printer Active*\n");
535                 break;
536         case 1:
537                 d_printf("*Printer Paused*\n");
538                 break;
539         case 2:
540                 d_printf("*Printer error*\n");
541                 break;
542         case 3:
543                 d_printf("*Delete Pending*\n");
544                 break;
545         default:
546                 d_printf("**UNKNOWN STATUS**\n");
547         }
548 }
549
550 static void enum_jobs(uint16 jobid, const char *ownername,
551                       const char *notifyname, const char *datatype,
552                       const char *jparms, uint16 pos, uint16 status,
553                       const char *jstatus, unsigned int submitted, unsigned int jobsize,
554                       const char *comment)
555 {
556         d_printf("     %-23.23s %5d %9d            ",
557                  ownername, jobid, jobsize);
558         switch (status) {
559         case 0:
560                 d_printf("Waiting\n");
561                 break;
562         case 1:
563                 d_printf("Held in queue\n");
564                 break;
565         case 2:
566                 d_printf("Spooling\n");
567                 break;
568         case 3:
569                 d_printf("Printing\n");
570                 break;
571         default:
572                 d_printf("**UNKNOWN STATUS**\n");
573         }
574 }
575
576 #define PRINTQ_ENUM_DISPLAY \
577     "Print queues at \\\\%s\n\n"\
578     "Name                         Job #      Size            Status\n\n"\
579     "------------------------------------------------------------------"\
580     "-------------\n"
581
582 static int rap_printq_info(struct net_context *c, int argc, const char **argv)
583 {
584         struct cli_state *cli;
585         int ret;
586
587         if (argc == 0)
588                 return net_rap_printq_usage(c, argc, argv);
589
590         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
591                 return -1;
592
593         d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
594         ret = cli_NetPrintQGetInfo(cli, argv[0], enum_queue, enum_jobs);
595         cli_shutdown(cli);
596         return ret;
597 }
598
599 static int rap_printq_delete(struct net_context *c, int argc, const char **argv)
600 {
601         struct cli_state *cli;
602         int ret;
603
604         if (argc == 0)
605                 return net_rap_printq_usage(c, argc, argv);
606
607         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
608                 return -1;
609
610         ret = cli_printjob_del(cli, atoi(argv[0]));
611         cli_shutdown(cli);
612         return ret;
613 }
614
615 int net_rap_printq(struct net_context *c, int argc, const char **argv)
616 {
617         struct cli_state *cli;
618         int ret;
619
620         struct functable func[] = {
621                 {"INFO", rap_printq_info},
622                 {"DELETE", rap_printq_delete},
623                 {NULL, NULL}
624         };
625
626         if (argc == 0) {
627                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
628                         return -1;
629
630                 d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
631                 ret = cli_NetPrintQEnum(cli, enum_queue, enum_jobs);
632                 cli_shutdown(cli);
633                 return ret;
634         }
635
636         return net_run_function(c, argc, argv, func, net_rap_printq_usage);
637 }
638
639 static int net_rap_user_usage(struct net_context *c, int argc, const char **argv)
640 {
641         return net_help_user(c, argc, argv);
642 }
643
644 static void user_fn(const char *user_name, void *state)
645 {
646         d_printf("%-21.21s\n", user_name);
647 }
648
649 static void long_user_fn(const char *user_name, const char *comment,
650                          const char * home_dir, const char * logon_script,
651                          void *state)
652 {
653         d_printf("%-21.21s %s\n",
654                  user_name, comment);
655 }
656
657 static void group_member_fn(const char *user_name, void *state)
658 {
659         d_printf("%-21.21s\n", user_name);
660 }
661
662 static int rap_user_delete(struct net_context *c, int argc, const char **argv)
663 {
664         struct cli_state *cli;
665         int ret;
666
667         if (argc == 0) {
668                 d_printf("\n\nUser name not specified\n");
669                 return net_rap_user_usage(c, argc, argv);
670         }
671
672         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
673                 return -1;
674
675         ret = cli_NetUserDelete(cli, argv[0]);
676         cli_shutdown(cli);
677         return ret;
678 }
679
680 static int rap_user_add(struct net_context *c, int argc, const char **argv)
681 {
682         struct cli_state *cli;
683         int ret;
684         RAP_USER_INFO_1 userinfo;
685
686         if (argc == 0) {
687                 d_printf("\n\nUser name not specified\n");
688                 return net_rap_user_usage(c, argc, argv);
689         }
690
691         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
692                 return -1;
693
694         safe_strcpy(userinfo.user_name, argv[0], sizeof(userinfo.user_name)-1);
695         if (c->opt_flags == -1)
696                 c->opt_flags = 0x21;
697
698         userinfo.userflags = c->opt_flags;
699         userinfo.reserved1 = '\0';
700         userinfo.comment = smb_xstrdup(c->opt_comment);
701         userinfo.priv = 1;
702         userinfo.home_dir = NULL;
703         userinfo.logon_script = NULL;
704
705         ret = cli_NetUserAdd(cli, &userinfo);
706
707         cli_shutdown(cli);
708         return ret;
709 }
710
711 static int rap_user_info(struct net_context *c, int argc, const char **argv)
712 {
713         struct cli_state *cli;
714         int ret;
715         if (argc == 0) {
716                 d_printf("\n\nUser name not specified\n");
717                 return net_rap_user_usage(c, argc, argv);
718         }
719
720         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
721                 return -1;
722
723         ret = cli_NetUserGetGroups(cli, argv[0], group_member_fn, NULL);
724         cli_shutdown(cli);
725         return ret;
726 }
727
728 int net_rap_user(struct net_context *c, int argc, const char **argv)
729 {
730         int ret = -1;
731         struct functable func[] = {
732                 {"ADD", rap_user_add},
733                 {"INFO", rap_user_info},
734                 {"DELETE", rap_user_delete},
735                 {NULL, NULL}
736         };
737
738         if (argc == 0) {
739                 struct cli_state *cli;
740                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
741                         goto done;
742                 if (c->opt_long_list_entries) {
743                         d_printf("\nUser name             Comment"\
744                                  "\n-----------------------------\n");
745                         ret = cli_RNetUserEnum(cli, long_user_fn, NULL);
746                         cli_shutdown(cli);
747                         goto done;
748                 }
749                 ret = cli_RNetUserEnum0(cli, user_fn, NULL);
750                 cli_shutdown(cli);
751                 goto done;
752         }
753
754         ret = net_run_function(c, argc, argv, func, net_rap_user_usage);
755  done:
756         if (ret != 0) {
757                 DEBUG(1, ("Net user returned: %d\n", ret));
758         }
759         return ret;
760 }
761
762
763 int net_rap_group_usage(struct net_context *c, int argc, const char **argv)
764 {
765         return net_help_group(c, argc, argv);
766 }
767
768 static void long_group_fn(const char *group_name, const char *comment,
769                           void *state)
770 {
771         d_printf("%-21.21s %s\n", group_name, comment);
772 }
773
774 static void group_fn(const char *group_name, void *state)
775 {
776         d_printf("%-21.21s\n", group_name);
777 }
778
779 static int rap_group_delete(struct net_context *c, int argc, const char **argv)
780 {
781         struct cli_state *cli;
782         int ret;
783         if (argc == 0) {
784                 d_printf("\n\nGroup name not specified\n");
785                 return net_rap_group_usage(c, argc, argv);
786         }
787
788         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
789                 return -1;
790
791         ret = cli_NetGroupDelete(cli, argv[0]);
792         cli_shutdown(cli);
793         return ret;
794 }
795
796 static int rap_group_add(struct net_context *c, int argc, const char **argv)
797 {
798         struct cli_state *cli;
799         int ret;
800         RAP_GROUP_INFO_1 grinfo;
801
802         if (argc == 0) {
803                 d_printf("\n\nGroup name not specified\n");
804                 return net_rap_group_usage(c, argc, argv);
805         }
806
807         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
808                 return -1;
809
810         /* BB check for length 21 or smaller explicitly ? BB */
811         safe_strcpy(grinfo.group_name, argv[0], sizeof(grinfo.group_name)-1);
812         grinfo.reserved1 = '\0';
813         grinfo.comment = smb_xstrdup(c->opt_comment);
814
815         ret = cli_NetGroupAdd(cli, &grinfo);
816         cli_shutdown(cli);
817         return ret;
818 }
819
820 int net_rap_group(struct net_context *c, int argc, const char **argv)
821 {
822         struct functable func[] = {
823                 {"ADD", rap_group_add},
824                 {"DELETE", rap_group_delete},
825                 {NULL, NULL}
826         };
827
828         if (argc == 0) {
829                 struct cli_state *cli;
830                 int ret;
831                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
832                         return -1;
833                 if (c->opt_long_list_entries) {
834                         d_printf("Group name            Comment\n");
835                         d_printf("-----------------------------\n");
836                         ret = cli_RNetGroupEnum(cli, long_group_fn, NULL);
837                         cli_shutdown(cli);
838                         return ret;
839                 }
840                 ret = cli_RNetGroupEnum0(cli, group_fn, NULL);
841                 cli_shutdown(cli);
842                 return ret;
843         }
844
845         return net_run_function(c, argc, argv, func, net_rap_group_usage);
846 }
847
848 int net_rap_groupmember_usage(struct net_context *c, int argc, const char **argv)
849 {
850         d_printf(
851          "net rap groupmember LIST <group> [misc. options] [targets]"\
852          "\n\t Enumerate users in a group\n"\
853          "\nnet rap groupmember DELETE <group> <user> [misc. options] "\
854          "[targets]\n\t Delete sepcified user from specified group\n"\
855          "\nnet rap groupmember ADD <group> <user> [misc. options] [targets]"\
856          "\n\t Add specified user to specified group\n");
857
858         net_common_flags_usage(c, argc, argv);
859         return -1;
860 }
861
862
863 static int rap_groupmember_add(struct net_context *c, int argc, const char **argv)
864 {
865         struct cli_state *cli;
866         int ret;
867         if (argc != 2) {
868                 d_printf("\n\nGroup or user name not specified\n");
869                 return net_rap_groupmember_usage(c, argc, argv);
870         }
871
872         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
873                 return -1;
874
875         ret = cli_NetGroupAddUser(cli, argv[0], argv[1]);
876         cli_shutdown(cli);
877         return ret;
878 }
879
880 static int rap_groupmember_delete(struct net_context *c, int argc, const char **argv)
881 {
882         struct cli_state *cli;
883         int ret;
884         if (argc != 2) {
885                 d_printf("\n\nGroup or user name not specified\n");
886                 return net_rap_groupmember_usage(c, argc, argv);
887         }
888
889         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
890                 return -1;
891
892         ret = cli_NetGroupDelUser(cli, argv[0], argv[1]);
893         cli_shutdown(cli);
894         return ret;
895 }
896
897 static int rap_groupmember_list(struct net_context *c, int argc, const char **argv)
898 {
899         struct cli_state *cli;
900         int ret;
901         if (argc == 0) {
902                 d_printf("\n\nGroup name not specified\n");
903                 return net_rap_groupmember_usage(c, argc, argv);
904         }
905
906         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
907                 return -1;
908
909         ret = cli_NetGroupGetUsers(cli, argv[0], group_member_fn, NULL );
910         cli_shutdown(cli);
911         return ret;
912 }
913
914 int net_rap_groupmember(struct net_context *c, int argc, const char **argv)
915 {
916         struct functable func[] = {
917                 {"ADD", rap_groupmember_add},
918                 {"LIST", rap_groupmember_list},
919                 {"DELETE", rap_groupmember_delete},
920                 {NULL, NULL}
921         };
922
923         return net_run_function(c, argc, argv, func, net_rap_groupmember_usage);
924 }
925
926 int net_rap_validate_usage(struct net_context *c, int argc, const char **argv)
927 {
928         d_printf("net rap validate <username> [password]\n"\
929                  "\tValidate user and password to check whether they"\
930                  " can access target server or domain\n");
931
932         net_common_flags_usage(c, argc, argv);
933         return -1;
934 }
935
936 int net_rap_validate(struct net_context *c, int argc, const char **argv)
937 {
938         return errmsg_not_implemented();
939 }
940
941 int net_rap_service_usage(struct net_context *c, int argc, const char **argv)
942 {
943         d_printf("net rap service [misc. options] [targets] \n"\
944                  "\tlists all running service daemons on target server\n");
945         d_printf("\nnet rap service START <name> [service startup arguments]"\
946                  " [misc. options] [targets]"\
947                  "\n\tStart named service on remote server\n");
948         d_printf("\nnet rap service STOP <name> [misc. options] [targets]\n"\
949                  "\n\tStop named service on remote server\n");
950
951         net_common_flags_usage(c, argc, argv);
952         return -1;
953 }
954
955 static int rap_service_start(struct net_context *c, int argc, const char **argv)
956 {
957         return errmsg_not_implemented();
958 }
959
960 static int rap_service_stop(struct net_context *c, int argc, const char **argv)
961 {
962         return errmsg_not_implemented();
963 }
964
965 static void service_fn(const char *service_name, const char *dummy,
966                        void *state)
967 {
968         d_printf("%-21.21s\n", service_name);
969 }
970
971 int net_rap_service(struct net_context *c, int argc, const char **argv)
972 {
973         struct functable func[] = {
974                 {"START", rap_service_start},
975                 {"STOP", rap_service_stop},
976                 {NULL, NULL}
977         };
978
979         if (argc == 0) {
980                 struct cli_state *cli;
981                 int ret;
982                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
983                         return -1;
984
985                 if (c->opt_long_list_entries) {
986                         d_printf("Service name          Comment\n");
987                         d_printf("-----------------------------\n");
988                         ret = cli_RNetServiceEnum(cli, long_group_fn, NULL);
989                 }
990                 ret = cli_RNetServiceEnum(cli, service_fn, NULL);
991                 cli_shutdown(cli);
992                 return ret;
993         }
994
995         return net_run_function(c, argc, argv, func, net_rap_service_usage);
996 }
997
998 int net_rap_password_usage(struct net_context *c, int argc, const char **argv)
999 {
1000         d_printf(
1001          "net rap password <user> <oldpwo> <newpw> [misc. options] [target]\n"\
1002          "\tchanges the password for the specified user at target\n");
1003
1004         return -1;
1005 }
1006
1007
1008 int net_rap_password(struct net_context *c, int argc, const char **argv)
1009 {
1010         struct cli_state *cli;
1011         int ret;
1012
1013         if (argc < 3)
1014                 return net_rap_password_usage(c, argc, argv);
1015
1016         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1017                 return -1;
1018
1019         /* BB Add check for password lengths? */
1020         ret = cli_oem_change_password(cli, argv[0], argv[2], argv[1]);
1021         cli_shutdown(cli);
1022         return ret;
1023 }
1024
1025 int net_rap_admin_usage(struct net_context *c, int argc, const char **argv)
1026 {
1027         d_printf(
1028    "net rap admin <remote command> [cmd args [env]] [misc. options] [targets]"\
1029    "\n\texecutes a remote command on an os/2 target server\n");
1030
1031         return -1;
1032 }
1033
1034
1035 int net_rap_admin(struct net_context *c, int argc, const char **argv)
1036 {
1037         return errmsg_not_implemented();
1038 }
1039
1040 /* The help subsystem for the RAP subcommand */
1041
1042 int net_rap_usage(struct net_context *c, int argc, const char **argv)
1043 {
1044         d_printf("  net rap domain \tto list domains \n"\
1045                  "  net rap file \t\tto list open files on a server \n"\
1046                  "  net rap group \tto list user groups  \n"\
1047                  "  net rap groupmember \tto list users in a group \n"\
1048                  "  net rap password \tto change the password of a user\n"\
1049                  "  net rap printq \tto list the print queues on a server\n"\
1050                  "  net rap server \tto list servers in a domain\n"\
1051                  "  net rap session \tto list clients with open sessions to a server\n"\
1052                  "  net rap share \tto list shares exported by a server\n"\
1053                  "  net rap user \t\tto list users\n"\
1054                  "  net rap validate \tto check whether a user and the corresponding password are valid\n"\
1055                  "  net rap help\n"\
1056                  "\nType \"net help <option>\" to get more information on that option\n\n");
1057
1058         net_common_flags_usage(c, argc, argv);
1059         return -1;
1060 }
1061
1062 /*
1063   handle "net rap help *" subcommands
1064 */
1065 int net_rap_help(struct net_context *c, int argc, const char **argv)
1066 {
1067         struct functable func[] = {
1068                 {"FILE", net_rap_file_usage},
1069                 {"SHARE", net_rap_share_usage},
1070                 {"SESSION", net_rap_session_usage},
1071                 {"SERVER", net_rap_server_usage},
1072                 {"DOMAIN", net_rap_domain_usage},
1073                 {"PRINTQ", net_rap_printq_usage},
1074                 {"USER", net_rap_user_usage},
1075                 {"GROUP", net_rap_group_usage},
1076                 {"VALIDATE", net_rap_validate_usage},
1077                 {"GROUPMEMBER", net_rap_groupmember_usage},
1078                 {"ADMIN", net_rap_admin_usage},
1079                 {"SERVICE", net_rap_service_usage},
1080                 {"PASSWORD", net_rap_password_usage},
1081                 {NULL, NULL}};
1082
1083         return net_run_function(c, argc, argv, func, net_rap_usage);
1084 }
1085
1086 /* Entry-point for all the RAP functions. */
1087
1088 int net_rap(struct net_context *c, int argc, const char **argv)
1089 {
1090         struct functable func[] = {
1091                 {"FILE", net_rap_file},
1092                 {"SHARE", net_rap_share},
1093                 {"SESSION", net_rap_session},
1094                 {"SERVER", net_rap_server},
1095                 {"DOMAIN", net_rap_domain},
1096                 {"PRINTQ", net_rap_printq},
1097                 {"USER", net_rap_user},
1098                 {"GROUP", net_rap_group},
1099                 {"VALIDATE", net_rap_validate},
1100                 {"GROUPMEMBER", net_rap_groupmember},
1101                 {"ADMIN", net_rap_admin},
1102                 {"SERVICE", net_rap_service},
1103                 {"PASSWORD", net_rap_password},
1104                 {"HELP", net_rap_help},
1105                 {NULL, NULL}
1106         };
1107
1108         return net_run_function(c, argc, argv, func, net_rap_usage);
1109 }
1110