VERSION: Bump version number up to 3.5.21.
[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 static int errmsg_not_implemented(void)
38 {
39         d_printf(_("\nNot implemented\n"));
40         return 0;
41 }
42
43 int net_rap_file_usage(struct net_context *c, int argc, const char **argv)
44 {
45         return net_file_usage(c, argc, argv);
46 }
47
48 /***************************************************************************
49   list info on an open file
50 ***************************************************************************/
51 static void file_fn(const char * pPath, const char * pUser, uint16 perms,
52                     uint16 locks, uint32 id)
53 {
54         d_printf("%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
55                  id, pUser, perms, locks, pPath);
56 }
57
58 static void one_file_fn(const char *pPath, const char *pUser, uint16 perms,
59                         uint16 locks, uint32 id)
60 {
61         d_printf(_("File ID          %d\n"
62                    "User name        %s\n"
63                    "Locks            0x%-4.2x\n"
64                    "Path             %s\n"
65                    "Permissions      0x%x\n"),
66                  id, pUser, locks, pPath, perms);
67 }
68
69
70 static int rap_file_close(struct net_context *c, int argc, const char **argv)
71 {
72         struct cli_state *cli;
73         int ret;
74         if (argc == 0 || c->display_usage) {
75                 return net_rap_file_usage(c, argc, argv);
76         }
77
78         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
79                 return -1;
80
81         ret = cli_NetFileClose(cli, atoi(argv[0]));
82         cli_shutdown(cli);
83         return ret;
84 }
85
86 static int rap_file_info(struct net_context *c, int argc, const char **argv)
87 {
88         struct cli_state *cli;
89         int ret;
90         if (argc == 0 || c->display_usage)
91                 return net_rap_file_usage(c, argc, argv);
92
93         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
94                 return -1;
95
96         ret = cli_NetFileGetInfo(cli, atoi(argv[0]), one_file_fn);
97         cli_shutdown(cli);
98         return ret;
99 }
100
101 static int rap_file_user(struct net_context *c, int argc, const char **argv)
102 {
103         struct cli_state *cli;
104         int ret;
105
106         if (argc == 0 || c->display_usage)
107                 return net_rap_file_usage(c, argc, argv);
108
109         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
110                 return -1;
111
112         /* list open files */
113
114         d_printf(_("\nEnumerating open files on remote server:\n\n"
115                    "\nFileId  Opened by            Perms  Locks  Path \n"
116                    "------  ---------            -----  -----  ---- \n"));
117         ret = cli_NetFileEnum(cli, argv[0], NULL, file_fn);
118
119         if (ret == -1)
120                 d_printf(_("\nOperation not supported by server!\n\n"));
121
122         cli_shutdown(cli);
123         return ret;
124 }
125
126 int net_rap_file(struct net_context *c, int argc, const char **argv)
127 {
128         struct functable func[] = {
129                 {
130                         "close",
131                         rap_file_close,
132                         NET_TRANSPORT_RAP,
133                         N_("Close specified file on server"),
134                         N_("net rap file close\n"
135                            "    Close specified file on server")
136                 },
137                 {
138                         "user",
139                         rap_file_user,
140                         NET_TRANSPORT_RAP,
141                         N_("List all files opened by username"),
142                         N_("net rap file user\n"
143                            "    List all files opened by username")
144                 },
145                 {
146                         "info",
147                         rap_file_info,
148                         NET_TRANSPORT_RAP,
149                         N_("Display info about an opened file"),
150                         N_("net rap file info\n"
151                            "    Display info about an opened file")
152                 },
153                 {NULL, NULL, 0, NULL, NULL}
154         };
155
156         if (argc == 0) {
157                 struct cli_state *cli;
158                 int ret;
159
160                 if (c->display_usage) {
161                         d_printf(_("Usage:\n"));
162                         d_printf(_("net rap file\n"
163                                    "    List all open files on rempte "
164                                    "server\n"));
165                         net_display_usage_from_functable(func);
166                         return 0;
167                 }
168
169                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
170                         return -1;
171
172                 /* list open files */
173
174                 d_printf(_("\nEnumerating open files on remote server:\n\n"
175                            "\nFileId  Opened by            Perms  Locks  Path\n"
176                            "------  ---------            -----  -----  ----\n"));
177                 ret = cli_NetFileEnum(cli, NULL, NULL, file_fn);
178
179                 if (ret == -1)
180                         d_printf(_("\nOperation not supported by server!\n\n"));
181
182                 cli_shutdown(cli);
183                 return ret;
184         }
185
186         return net_run_function(c, argc, argv, "net rap file", func);
187 }
188
189 int net_rap_share_usage(struct net_context *c, int argc, const char **argv)
190 {
191         return net_share_usage(c, argc, argv);
192 }
193
194 static void long_share_fn(const char *share_name, uint32 type,
195                           const char *comment, void *state)
196 {
197         d_printf("%-12s %-8.8s %-50s\n",
198                  share_name, net_share_type_str(type), comment);
199 }
200
201 static void share_fn(const char *share_name, uint32 type,
202                      const char *comment, void *state)
203 {
204         d_printf("%s\n", share_name);
205 }
206
207 static int rap_share_delete(struct net_context *c, int argc, const char **argv)
208 {
209         struct cli_state *cli;
210         int ret;
211
212         if (argc == 0 || c->display_usage) {
213                 return net_rap_share_usage(c, argc, argv);
214         }
215
216         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
217                 return -1;
218
219         ret = cli_NetShareDelete(cli, argv[0]);
220         cli_shutdown(cli);
221         return ret;
222 }
223
224 static int rap_share_add(struct net_context *c, int argc, const char **argv)
225 {
226         struct cli_state *cli;
227         int ret;
228
229         RAP_SHARE_INFO_2 sinfo;
230         char *p;
231         char *sharename;
232
233         if (argc == 0 || c->display_usage) {
234                 return net_rap_share_usage(c, argc, argv);
235         }
236
237         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
238                 return -1;
239
240         sharename = SMB_STRDUP(argv[0]);
241         p = strchr(sharename, '=');
242         if (p == NULL) {
243                 d_printf(_("Server path not specified\n"));
244                 SAFE_FREE(sharename);
245                 return net_rap_share_usage(c, argc, argv);
246         }
247         *p = 0;
248         strlcpy(sinfo.share_name, sharename, sizeof(sinfo.share_name));
249         sinfo.reserved1 = '\0';
250         sinfo.share_type = 0;
251         sinfo.comment = smb_xstrdup(c->opt_comment);
252         sinfo.perms = 0;
253         sinfo.maximum_users = c->opt_maxusers;
254         sinfo.active_users = 0;
255         sinfo.path = p+1;
256         memset(sinfo.password, '\0', sizeof(sinfo.password));
257         sinfo.reserved2 = '\0';
258
259         ret = cli_NetShareAdd(cli, &sinfo);
260         cli_shutdown(cli);
261         SAFE_FREE(sharename);
262         return ret;
263 }
264
265
266 int net_rap_share(struct net_context *c, int argc, const char **argv)
267 {
268         struct functable func[] = {
269                 {
270                         "delete",
271                         rap_share_delete,
272                         NET_TRANSPORT_RAP,
273                         N_("Delete a share from server"),
274                         N_("net rap share delete\n"
275                            "    Delete a share from server")
276                 },
277                 {
278                         "close",
279                         rap_share_delete,
280                         NET_TRANSPORT_RAP,
281                         N_("Delete a share from server"),
282                         N_("net rap share close\n"
283                            "    Delete a share from server\n"
284                            "    Alias for net rap share delete")
285                 },
286                 {
287                         "add",
288                         rap_share_add,
289                         NET_TRANSPORT_RAP,
290                         N_("Add a share to server"),
291                         N_("net rap share add\n"
292                            "    Add a share to server")
293                 },
294                 {NULL, NULL, 0, NULL, NULL}
295         };
296
297         if (argc == 0) {
298                 struct cli_state *cli;
299                 int ret;
300
301                 if (c->display_usage) {
302                         d_printf(_("Usage:\n"));
303                         d_printf(_("net rap share\n"
304                                    "    List all shares on remote server\n"));
305                         net_display_usage_from_functable(func);
306                         return 0;
307                 }
308
309                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
310                         return -1;
311
312                 if (c->opt_long_list_entries) {
313                         d_printf(_(
314         "\nEnumerating shared resources (exports) on remote server:\n\n"
315         "\nShare name   Type     Description\n"
316         "----------   ----     -----------\n"));
317                         ret = cli_RNetShareEnum(cli, long_share_fn, NULL);
318                 } else {
319                         ret = cli_RNetShareEnum(cli, share_fn, NULL);
320                 }
321                 cli_shutdown(cli);
322                 return ret;
323         }
324
325         return net_run_function(c, argc, argv, "net rap share", func);
326 }
327
328 int net_rap_session_usage(struct net_context *c, int argc, const char **argv)
329 {
330         d_printf(_(
331          "\nnet rap session [misc. options] [targets]"
332          "\n\tenumerates all active SMB/CIFS sessions on target server\n"));
333         d_printf(_(
334          "\nnet rap session DELETE <client_name> [misc. options] [targets] \n"
335          "\tor"
336          "\nnet rap session CLOSE <client_name> [misc. options] [targets]"
337          "\n\tDeletes (closes) a session from specified client to server\n"));
338         d_printf(_(
339         "\nnet rap session INFO <client_name>"
340         "\n\tEnumerates all open files in specified session\n"));
341
342         net_common_flags_usage(c, argc, argv);
343         return -1;
344 }
345
346 static void list_sessions_func(char *wsname, char *username, uint16 conns,
347                         uint16 opens, uint16 users, uint32 sess_time,
348                         uint32 idle_time, uint32 user_flags, char *clitype)
349 {
350         int hrs = idle_time / 3600;
351         int min = (idle_time / 60) % 60;
352         int sec = idle_time % 60;
353
354         d_printf("\\\\%-18.18s %-20.20s %-18.18s %5d %2.2d:%2.2d:%2.2d\n",
355                  wsname, username, clitype, opens, hrs, min, sec);
356 }
357
358 static void display_session_func(const char *wsname, const char *username,
359                                  uint16 conns, uint16 opens, uint16 users,
360                                  uint32 sess_time, uint32 idle_time,
361                                  uint32 user_flags, const char *clitype)
362 {
363         int ihrs = idle_time / 3600;
364         int imin = (idle_time / 60) % 60;
365         int isec = idle_time % 60;
366         int shrs = sess_time / 3600;
367         int smin = (sess_time / 60) % 60;
368         int ssec = sess_time % 60;
369         d_printf(_("User name       %-20.20s\n"
370                    "Computer        %-20.20s\n"
371                    "Guest logon     %-20.20s\n"
372                    "Client Type     %-40.40s\n"
373                    "Sess time       %2.2d:%2.2d:%2.2d\n"
374                    "Idle time       %2.2d:%2.2d:%2.2d\n"),
375                  username, wsname,
376                  (user_flags&0x0)?_("yes"):_("no"), clitype,
377                  shrs, smin, ssec, ihrs, imin, isec);
378 }
379
380 static void display_conns_func(uint16 conn_id, uint16 conn_type, uint16 opens,
381                                uint16 users, uint32 conn_time,
382                                const char *username, const char *netname)
383 {
384         d_printf("%-14.14s %-8.8s %5d\n",
385                  netname, net_share_type_str(conn_type), opens);
386 }
387
388 static int rap_session_info(struct net_context *c, int argc, const char **argv)
389 {
390         const char *sessname;
391         struct cli_state *cli;
392         int ret;
393
394         if (argc == 0 || c->display_usage)
395                 return net_rap_session_usage(c, argc, argv);
396
397         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
398                 return -1;
399
400         sessname = argv[0];
401
402         ret = cli_NetSessionGetInfo(cli, sessname, display_session_func);
403         if (ret < 0) {
404                 cli_shutdown(cli);
405                 return ret;
406         }
407
408         d_printf(_("Share name     Type     # Opens\n-------------------------"
409                    "-----------------------------------------------------\n"));
410         ret = cli_NetConnectionEnum(cli, sessname, display_conns_func);
411         cli_shutdown(cli);
412         return ret;
413 }
414
415 static int rap_session_delete(struct net_context *c, int argc, const char **argv)
416 {
417         struct cli_state *cli;
418         int ret;
419
420         if (argc == 0 || c->display_usage)
421                 return net_rap_session_usage(c, argc, argv);
422
423         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
424                 return -1;
425
426         ret = cli_NetSessionDel(cli, argv[0]);
427         cli_shutdown(cli);
428         return ret;
429 }
430
431 int net_rap_session(struct net_context *c, int argc, const char **argv)
432 {
433         struct functable func[] = {
434                 {
435                         "info",
436                         rap_session_info,
437                         NET_TRANSPORT_RAP,
438                         N_("Display information about session"),
439                         N_("net rap session info\n"
440                            "    Display information about session")
441                 },
442                 {
443                         "delete",
444                         rap_session_delete,
445                         NET_TRANSPORT_RAP,
446                         N_("Close specified session"),
447                         N_("net rap session delete\n"
448                            "    Close specified session\n"
449                            "    Alias for net rap session close")
450                 },
451                 {
452                         "close",
453                         rap_session_delete,
454                         NET_TRANSPORT_RAP,
455                         N_("Close specified session"),
456                         N_("net rap session close\n"
457                            "    Close specified session")
458                 },
459                 {NULL, NULL, 0, NULL, NULL}
460         };
461
462         if (argc == 0) {
463                 struct cli_state *cli;
464                 int ret;
465
466                 if (c->display_usage) {
467                         d_printf(_("Usage:\n"));
468                         d_printf(_("net rap session\n"
469                                    "    List all open sessions on remote "
470                                    "server\n"));
471                         net_display_usage_from_functable(func);
472                         return 0;
473                 }
474
475                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
476                         return -1;
477
478                 d_printf(_("Computer             User name            "
479                            "Client Type        Opens Idle time\n"
480                            "------------------------------------------"
481                            "------------------------------------\n"));
482                 ret = cli_NetSessionEnum(cli, list_sessions_func);
483
484                 cli_shutdown(cli);
485                 return ret;
486         }
487
488         return net_run_function(c, argc, argv, "net rap session", func);
489 }
490
491 /****************************************************************************
492 list a server name
493 ****************************************************************************/
494 static void display_server_func(const char *name, uint32 m,
495                                 const char *comment, void * reserved)
496 {
497         d_printf("\t%-16.16s     %s\n", name, comment);
498 }
499
500 static int net_rap_server_name(struct net_context *c, int argc, const char *argv[])
501 {
502         struct cli_state *cli;
503         char *name;
504
505         if (c->display_usage) {
506                 d_printf("%s\n%s",
507                          _("Usage:"),
508                          _("net rap server name\n"
509                            "    Get the name of the server\n"));
510                 return 0;
511         }
512
513         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
514                 return -1;
515
516         if (!cli_get_server_name(NULL, cli, &name)) {
517                 d_fprintf(stderr, _("cli_get_server_name failed\n"));
518                 cli_shutdown(cli);
519                 return -1;
520         }
521
522         d_printf(_("Server name = %s\n"), name);
523
524         TALLOC_FREE(name);
525         cli_shutdown(cli);
526         return 0;
527 }
528
529 static int net_rap_server_domain(struct net_context *c, int argc,
530                                  const char **argv)
531 {
532         struct cli_state *cli;
533         int ret;
534
535         if (c->display_usage) {
536                 d_printf("%s\n%s",
537                          _("Usage:"),
538                          _("net rap server domain\n"
539                            "    Enumerate servers in this domain/workgroup\n"));
540                 return 0;
541         }
542
543         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
544                 return -1;
545
546         d_printf(_("\nEnumerating servers in this domain or workgroup: \n\n"
547                    "\tServer name          Server description\n"
548                    "\t-------------        ----------------------------\n"));
549
550         ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL,
551                                 display_server_func,NULL);
552         cli_shutdown(cli);
553         return ret;
554 }
555
556 int net_rap_server(struct net_context *c, int argc, const char **argv)
557 {
558         struct functable func[] = {
559                 {
560                         "name",
561                         net_rap_server_name,
562                         NET_TRANSPORT_RAP,
563                         N_("Get the name of the server"),
564                         N_("net rap server name\n"
565                            "    Get the name of the server")
566                 },
567                 {
568                         "domain",
569                         net_rap_server_domain,
570                         NET_TRANSPORT_RAP,
571                         N_("Get the servers in this domain/workgroup"),
572                         N_("net rap server domain\n"
573                            "    Get the servers in this domain/workgroup")
574                 },
575                 {NULL, NULL, 0, NULL, NULL}
576         };
577
578         /* smb4k uses 'net [rap|rpc] server domain' to query servers in a domain */
579         /* Fall through for 'domain', any other forms will cause to show usage message */
580         return net_run_function(c, argc, argv, "net rap server", func);
581
582 }
583
584 int net_rap_domain_usage(struct net_context *c, int argc, const char **argv)
585 {
586         d_printf(_("net rap domain [misc. options] [target]\n\tlists the"
587                    " domains or workgroups visible on the current network\n"));
588
589         net_common_flags_usage(c, argc, argv);
590         return -1;
591 }
592
593 int net_rap_domain(struct net_context *c, int argc, const char **argv)
594 {
595         struct cli_state *cli;
596         int ret;
597
598         if (c->display_usage)
599                 return net_rap_domain_usage(c, argc, argv);
600
601         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
602                 return -1;
603
604         d_printf(_("\nEnumerating domains:\n\n"
605                    "\tDomain name          Server name of Browse Master\n"
606                    "\t-------------        ----------------------------\n"));
607
608         ret = cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM,
609                                 display_server_func,NULL);
610         cli_shutdown(cli);
611         return ret;
612 }
613
614 int net_rap_printq_usage(struct net_context *c, int argc, const char **argv)
615 {
616         d_printf(_(
617          "net rap printq [misc. options] [targets]\n"
618          "\tor\n"
619          "net rap printq info [<queue_name>] [misc. options] [targets]\n"
620          "\tlists the specified queue and jobs on the target server.\n"
621          "\tIf the queue name is not specified, all queues are listed.\n\n"));
622         d_printf(_(
623          "net rap printq delete [<queue name>] [misc. options] [targets]\n"
624          "\tdeletes the specified job number on the target server, or the\n"
625          "\tprinter queue if no job number is specified\n"));
626
627         net_common_flags_usage(c, argc, argv);
628
629         return -1;
630 }
631
632 static void enum_queue(const char *queuename, uint16 pri, uint16 start,
633                        uint16 until, const char *sep, const char *pproc,
634                        const char *dest, const char *qparms,
635                        const char *qcomment, uint16 status, uint16 jobcount)
636 {
637         d_printf(_("%-17.17s Queue %5d jobs                      "),
638                  queuename, jobcount);
639
640         switch (status) {
641         case 0:
642                 d_printf(_("*Printer Active*\n"));
643                 break;
644         case 1:
645                 d_printf(_("*Printer Paused*\n"));
646                 break;
647         case 2:
648                 d_printf(_("*Printer error*\n"));
649                 break;
650         case 3:
651                 d_printf(_("*Delete Pending*\n"));
652                 break;
653         default:
654                 d_printf(_("**UNKNOWN STATUS**\n"));
655         }
656 }
657
658 static void enum_jobs(uint16 jobid, const char *ownername,
659                       const char *notifyname, const char *datatype,
660                       const char *jparms, uint16 pos, uint16 status,
661                       const char *jstatus, unsigned int submitted, unsigned int jobsize,
662                       const char *comment)
663 {
664         d_printf("     %-23.23s %5d %9d            ",
665                  ownername, jobid, jobsize);
666         switch (status) {
667         case 0:
668                 d_printf(_("Waiting\n"));
669                 break;
670         case 1:
671                 d_printf(_("Held in queue\n"));
672                 break;
673         case 2:
674                 d_printf(_("Spooling\n"));
675                 break;
676         case 3:
677                 d_printf(_("Printing\n"));
678                 break;
679         default:
680                 d_printf(_("**UNKNOWN STATUS**\n"));
681         }
682 }
683
684 #define PRINTQ_ENUM_DISPLAY \
685     _("Print queues at \\\\%s\n\n"\
686       "Name                         Job #      Size            Status\n\n"\
687       "------------------------------------------------------------------"\
688       "-------------\n")
689
690 static int rap_printq_info(struct net_context *c, int argc, const char **argv)
691 {
692         struct cli_state *cli;
693         int ret;
694
695         if (argc == 0 || c->display_usage)
696                 return net_rap_printq_usage(c, argc, argv);
697
698         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
699                 return -1;
700
701         d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
702         ret = cli_NetPrintQGetInfo(cli, argv[0], enum_queue, enum_jobs);
703         cli_shutdown(cli);
704         return ret;
705 }
706
707 static int rap_printq_delete(struct net_context *c, int argc, const char **argv)
708 {
709         struct cli_state *cli;
710         int ret;
711
712         if (argc == 0 || c->display_usage)
713                 return net_rap_printq_usage(c, argc, argv);
714
715         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
716                 return -1;
717
718         ret = cli_printjob_del(cli, atoi(argv[0]));
719         cli_shutdown(cli);
720         return ret;
721 }
722
723 int net_rap_printq(struct net_context *c, int argc, const char **argv)
724 {
725         struct cli_state *cli;
726         int ret;
727
728         struct functable func[] = {
729                 {
730                         "info",
731                         rap_printq_info,
732                         NET_TRANSPORT_RAP,
733                         N_("Display info about print queues and jobs"),
734                         N_("net rap printq info [queue]\n"
735                            "    Display info about print jobs in queue.\n"
736                            "    If queue is not specified, all queues are "
737                            "listed")
738                 },
739                 {
740                         "delete",
741                         rap_printq_delete,
742                         NET_TRANSPORT_RAP,
743                         N_("Delete print job(s)"),
744                         N_("net rap printq delete\n"
745                            "    Delete print job(s)")
746                 },
747                 {NULL, NULL, 0, NULL, NULL}
748         };
749
750         if (argc == 0) {
751                 if (c->display_usage) {
752                         d_printf(_("Usage:\n"));
753                         d_printf(_("net rap printq\n"
754                                    "    List the print queue\n"));
755                         net_display_usage_from_functable(func);
756                         return 0;
757                 }
758
759                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
760                         return -1;
761
762                 d_printf(PRINTQ_ENUM_DISPLAY, cli->desthost); /* list header */
763                 ret = cli_NetPrintQEnum(cli, enum_queue, enum_jobs);
764                 cli_shutdown(cli);
765                 return ret;
766         }
767
768         return net_run_function(c, argc, argv, "net rap printq", func);
769 }
770
771 static int net_rap_user_usage(struct net_context *c, int argc, const char **argv)
772 {
773         return net_user_usage(c, argc, argv);
774 }
775
776 static void user_fn(const char *user_name, void *state)
777 {
778         d_printf("%-21.21s\n", user_name);
779 }
780
781 static void long_user_fn(const char *user_name, const char *comment,
782                          const char * home_dir, const char * logon_script,
783                          void *state)
784 {
785         d_printf("%-21.21s %s\n",
786                  user_name, comment);
787 }
788
789 static void group_member_fn(const char *user_name, void *state)
790 {
791         d_printf("%-21.21s\n", user_name);
792 }
793
794 static int rap_user_delete(struct net_context *c, int argc, const char **argv)
795 {
796         struct cli_state *cli;
797         int ret;
798
799         if (argc == 0 || c->display_usage) {
800                 return net_rap_user_usage(c, argc, argv);
801         }
802
803         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
804                 return -1;
805
806         ret = cli_NetUserDelete(cli, argv[0]);
807         cli_shutdown(cli);
808         return ret;
809 }
810
811 static int rap_user_add(struct net_context *c, int argc, const char **argv)
812 {
813         struct cli_state *cli;
814         int ret;
815         RAP_USER_INFO_1 userinfo;
816
817         if (argc == 0 || c->display_usage) {
818                 return net_rap_user_usage(c, argc, argv);
819         }
820
821         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
822                 return -1;
823
824         safe_strcpy(userinfo.user_name, argv[0], sizeof(userinfo.user_name)-1);
825         if (c->opt_flags == -1)
826                 c->opt_flags = 0x21;
827
828         userinfo.userflags = c->opt_flags;
829         userinfo.reserved1 = '\0';
830         userinfo.comment = smb_xstrdup(c->opt_comment ? c->opt_comment : "");
831         userinfo.priv = 1;
832         userinfo.home_dir = NULL;
833         userinfo.logon_script = NULL;
834         userinfo.passwrd[0] = '\0';
835
836         ret = cli_NetUserAdd(cli, &userinfo);
837
838         cli_shutdown(cli);
839         return ret;
840 }
841
842 static int rap_user_info(struct net_context *c, int argc, const char **argv)
843 {
844         struct cli_state *cli;
845         int ret;
846         if (argc == 0 || c->display_usage) {
847                 return net_rap_user_usage(c, argc, argv);
848         }
849
850         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
851                 return -1;
852
853         ret = cli_NetUserGetGroups(cli, argv[0], group_member_fn, NULL);
854         cli_shutdown(cli);
855         return ret;
856 }
857
858 int net_rap_user(struct net_context *c, int argc, const char **argv)
859 {
860         int ret = -1;
861         struct functable func[] = {
862                 {
863                         "add",
864                         rap_user_add,
865                         NET_TRANSPORT_RAP,
866                         N_("Add specified user"),
867                         N_("net rap user add\n"
868                            "    Add specified user")
869                 },
870                 {
871                         "info",
872                         rap_user_info,
873                         NET_TRANSPORT_RAP,
874                         N_("List domain groups of specified user"),
875                         N_("net rap user info\n"
876                            "    List domain groups of specified user")
877
878                 },
879                 {
880                         "delete",
881                         rap_user_delete,
882                         NET_TRANSPORT_RAP,
883                         N_("Remove specified user"),
884                         N_("net rap user delete\n"
885                            "    Remove specified user")
886                 },
887                 {NULL, NULL, 0, NULL, NULL}
888         };
889
890         if (argc == 0) {
891                 struct cli_state *cli;
892                 if (c->display_usage) {
893                         d_printf(_("Usage:\n"));
894                         d_printf(_("net rap user\n"
895                                    "    List all users\n"));
896                         net_display_usage_from_functable(func);
897                         return 0;
898                 }
899
900                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
901                         goto done;
902                 if (c->opt_long_list_entries) {
903                         d_printf(_("\nUser name             Comment"
904                                    "\n-----------------------------\n"));
905                         ret = cli_RNetUserEnum(cli, long_user_fn, NULL);
906                         cli_shutdown(cli);
907                         goto done;
908                 }
909                 ret = cli_RNetUserEnum0(cli, user_fn, NULL);
910                 cli_shutdown(cli);
911                 goto done;
912         }
913
914         ret = net_run_function(c, argc, argv, "net rap user", func);
915  done:
916         if (ret != 0) {
917                 DEBUG(1, (_("Net user returned: %d\n"), ret));
918         }
919         return ret;
920 }
921
922
923 int net_rap_group_usage(struct net_context *c, int argc, const char **argv)
924 {
925         return net_group_usage(c, argc, argv);
926 }
927
928 static void long_group_fn(const char *group_name, const char *comment,
929                           void *state)
930 {
931         d_printf("%-21.21s %s\n", group_name, comment);
932 }
933
934 static void group_fn(const char *group_name, void *state)
935 {
936         d_printf("%-21.21s\n", group_name);
937 }
938
939 static int rap_group_delete(struct net_context *c, int argc, const char **argv)
940 {
941         struct cli_state *cli;
942         int ret;
943         if (argc == 0 || c->display_usage) {
944                 return net_rap_group_usage(c, argc, argv);
945         }
946
947         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
948                 return -1;
949
950         ret = cli_NetGroupDelete(cli, argv[0]);
951         cli_shutdown(cli);
952         return ret;
953 }
954
955 static int rap_group_add(struct net_context *c, int argc, const char **argv)
956 {
957         struct cli_state *cli;
958         int ret;
959         RAP_GROUP_INFO_1 grinfo;
960
961         if (argc == 0 || c->display_usage) {
962                 return net_rap_group_usage(c, argc, argv);
963         }
964
965         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
966                 return -1;
967
968         /* BB check for length 21 or smaller explicitly ? BB */
969         safe_strcpy(grinfo.group_name, argv[0], sizeof(grinfo.group_name)-1);
970         grinfo.reserved1 = '\0';
971         grinfo.comment = smb_xstrdup(c->opt_comment ? c->opt_comment : "");
972
973         ret = cli_NetGroupAdd(cli, &grinfo);
974         cli_shutdown(cli);
975         return ret;
976 }
977
978 int net_rap_group(struct net_context *c, int argc, const char **argv)
979 {
980         struct functable func[] = {
981                 {
982                         "add",
983                         rap_group_add,
984                         NET_TRANSPORT_RAP,
985                         N_("Add specified group"),
986                         N_("net rap group add\n"
987                            "    Add specified group")
988                 },
989                 {
990                         "delete",
991                         rap_group_delete,
992                         NET_TRANSPORT_RAP,
993                         N_("Delete specified group"),
994                         N_("net rap group delete\n"
995                            "    Delete specified group")
996                 },
997                 {NULL, NULL, 0, NULL, NULL}
998         };
999
1000         if (argc == 0) {
1001                 struct cli_state *cli;
1002                 int ret;
1003                 if (c->display_usage) {
1004                         d_printf(_("Usage:\n"));
1005                         d_printf(_("net rap group\n"
1006                                    "    List all groups\n"));
1007                         net_display_usage_from_functable(func);
1008                         return 0;
1009                 }
1010
1011                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1012                         return -1;
1013                 if (c->opt_long_list_entries) {
1014                         d_printf(_("Group name            Comment\n"
1015                                    "-----------------------------\n"));
1016                         ret = cli_RNetGroupEnum(cli, long_group_fn, NULL);
1017                         cli_shutdown(cli);
1018                         return ret;
1019                 }
1020                 ret = cli_RNetGroupEnum0(cli, group_fn, NULL);
1021                 cli_shutdown(cli);
1022                 return ret;
1023         }
1024
1025         return net_run_function(c, argc, argv, "net rap group", func);
1026 }
1027
1028 int net_rap_groupmember_usage(struct net_context *c, int argc, const char **argv)
1029 {
1030         d_printf(_(
1031          "net rap groupmember LIST <group> [misc. options] [targets]"
1032          "\n\t Enumerate users in a group\n"
1033          "\nnet rap groupmember DELETE <group> <user> [misc. options] "
1034          "[targets]\n\t Delete specified user from specified group\n"
1035          "\nnet rap groupmember ADD <group> <user> [misc. options] [targets]"
1036          "\n\t Add specified user to specified group\n"));
1037
1038         net_common_flags_usage(c, argc, argv);
1039         return -1;
1040 }
1041
1042
1043 static int rap_groupmember_add(struct net_context *c, int argc, const char **argv)
1044 {
1045         struct cli_state *cli;
1046         int ret;
1047         if (argc != 2 || c->display_usage) {
1048                 return net_rap_groupmember_usage(c, argc, argv);
1049         }
1050
1051         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1052                 return -1;
1053
1054         ret = cli_NetGroupAddUser(cli, argv[0], argv[1]);
1055         cli_shutdown(cli);
1056         return ret;
1057 }
1058
1059 static int rap_groupmember_delete(struct net_context *c, int argc, const char **argv)
1060 {
1061         struct cli_state *cli;
1062         int ret;
1063         if (argc != 2 || c->display_usage) {
1064                 return net_rap_groupmember_usage(c, argc, argv);
1065         }
1066
1067         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1068                 return -1;
1069
1070         ret = cli_NetGroupDelUser(cli, argv[0], argv[1]);
1071         cli_shutdown(cli);
1072         return ret;
1073 }
1074
1075 static int rap_groupmember_list(struct net_context *c, int argc, const char **argv)
1076 {
1077         struct cli_state *cli;
1078         int ret;
1079         if (argc == 0 || c->display_usage) {
1080                 return net_rap_groupmember_usage(c, argc, argv);
1081         }
1082
1083         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1084                 return -1;
1085
1086         ret = cli_NetGroupGetUsers(cli, argv[0], group_member_fn, NULL );
1087         cli_shutdown(cli);
1088         return ret;
1089 }
1090
1091 int net_rap_groupmember(struct net_context *c, int argc, const char **argv)
1092 {
1093         struct functable func[] = {
1094                 {
1095                         "add",
1096                         rap_groupmember_add,
1097                         NET_TRANSPORT_RAP,
1098                         N_("Add specified user to group"),
1099                         N_("net rap groupmember add\n"
1100                            "    Add specified user to group")
1101                 },
1102                 {
1103                         "list",
1104                         rap_groupmember_list,
1105                         NET_TRANSPORT_RAP,
1106                         N_("List users in group"),
1107                         N_("net rap groupmember list\n"
1108                            "    List users in group")
1109                 },
1110                 {
1111                         "delete",
1112                         rap_groupmember_delete,
1113                         NET_TRANSPORT_RAP,
1114                         N_("Remove user from group"),
1115                         N_("net rap groupmember delete\n"
1116                            "    Remove user from group")
1117                 },
1118                 {NULL, NULL, 0, NULL, NULL}
1119         };
1120
1121         return net_run_function(c, argc, argv, "net rap groupmember", func);
1122 }
1123
1124 int net_rap_validate_usage(struct net_context *c, int argc, const char **argv)
1125 {
1126         d_printf(_("net rap validate <username> [password]\n"
1127                    "\tValidate user and password to check whether they"
1128                    " can access target server or domain\n"));
1129
1130         net_common_flags_usage(c, argc, argv);
1131         return -1;
1132 }
1133
1134 int net_rap_validate(struct net_context *c, int argc, const char **argv)
1135 {
1136         return errmsg_not_implemented();
1137 }
1138
1139 int net_rap_service_usage(struct net_context *c, int argc, const char **argv)
1140 {
1141         d_printf(_("net rap service [misc. options] [targets] \n"
1142                  "\tlists all running service daemons on target server\n"));
1143         d_printf(_("\nnet rap service START <name> [service startup arguments]"
1144                  " [misc. options] [targets]"
1145                  "\n\tStart named service on remote server\n"));
1146         d_printf(_("\nnet rap service STOP <name> [misc. options] [targets]\n"
1147                  "\n\tStop named service on remote server\n"));
1148
1149         net_common_flags_usage(c, argc, argv);
1150         return -1;
1151 }
1152
1153 static int rap_service_start(struct net_context *c, int argc, const char **argv)
1154 {
1155         return errmsg_not_implemented();
1156 }
1157
1158 static int rap_service_stop(struct net_context *c, int argc, const char **argv)
1159 {
1160         return errmsg_not_implemented();
1161 }
1162
1163 static void service_fn(const char *service_name, const char *dummy,
1164                        void *state)
1165 {
1166         d_printf("%-21.21s\n", service_name);
1167 }
1168
1169 int net_rap_service(struct net_context *c, int argc, const char **argv)
1170 {
1171         struct functable func[] = {
1172                 {
1173                         "start",
1174                         rap_service_start,
1175                         NET_TRANSPORT_RAP,
1176                         N_("Start service on remote server"),
1177                         N_("net rap service start\n"
1178                            "    Start service on remote server")
1179                 },
1180                 {
1181                         "stop",
1182                         rap_service_stop,
1183                         NET_TRANSPORT_RAP,
1184                         N_("Stop named serve on remote server"),
1185                         N_("net rap service stop\n"
1186                            "    Stop named serve on remote server")
1187                 },
1188                 {NULL, NULL, 0, NULL, NULL}
1189         };
1190
1191         if (argc == 0) {
1192                 struct cli_state *cli;
1193                 int ret;
1194                 if (c->display_usage) {
1195                         d_printf(_("Usage:\n"));
1196                         d_printf(_("net rap service\n"
1197                                    "    List services on remote server\n"));
1198                         net_display_usage_from_functable(func);
1199                         return 0;
1200                 }
1201
1202                 if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1203                         return -1;
1204
1205                 if (c->opt_long_list_entries) {
1206                         d_printf(_("Service name          Comment\n"
1207                                    "-----------------------------\n"));
1208                         ret = cli_RNetServiceEnum(cli, long_group_fn, NULL);
1209                 }
1210                 ret = cli_RNetServiceEnum(cli, service_fn, NULL);
1211                 cli_shutdown(cli);
1212                 return ret;
1213         }
1214
1215         return net_run_function(c, argc, argv, "net rap service", func);
1216 }
1217
1218 int net_rap_password_usage(struct net_context *c, int argc, const char **argv)
1219 {
1220         d_printf(_(
1221          "net rap password <user> <oldpwo> <newpw> [misc. options] [target]\n"
1222          "\tchanges the password for the specified user at target\n"));
1223
1224         return -1;
1225 }
1226
1227
1228 int net_rap_password(struct net_context *c, int argc, const char **argv)
1229 {
1230         struct cli_state *cli;
1231         int ret;
1232
1233         if (argc < 3 || c->display_usage)
1234                 return net_rap_password_usage(c, argc, argv);
1235
1236         if (!NT_STATUS_IS_OK(net_make_ipc_connection(c, 0, &cli)))
1237                 return -1;
1238
1239         /* BB Add check for password lengths? */
1240         ret = cli_oem_change_password(cli, argv[0], argv[2], argv[1]);
1241         cli_shutdown(cli);
1242         return ret;
1243 }
1244
1245 int net_rap_admin_usage(struct net_context *c, int argc, const char **argv)
1246 {
1247         d_printf(_(
1248    "net rap admin <remote command> [cmd args [env]] [misc. options] [targets]"
1249    "\n\texecutes a remote command on an os/2 target server\n"));
1250
1251         return -1;
1252 }
1253
1254
1255 int net_rap_admin(struct net_context *c, int argc, const char **argv)
1256 {
1257         return errmsg_not_implemented();
1258 }
1259
1260 /* Entry-point for all the RAP functions. */
1261
1262 int net_rap(struct net_context *c, int argc, const char **argv)
1263 {
1264         struct functable func[] = {
1265                 {
1266                         "file",
1267                         net_rap_file,
1268                         NET_TRANSPORT_RAP,
1269                         N_("List open files"),
1270                         N_("net rap file\n"
1271                            "    List open files")
1272                 },
1273                 {
1274                         "share",
1275                         net_rap_share,
1276                         NET_TRANSPORT_RAP,
1277                         N_("List shares exported by server"),
1278                         N_("net rap share\n"
1279                            "    List shares exported by server")
1280                 },
1281                 {
1282                         "session",
1283                         net_rap_session,
1284                         NET_TRANSPORT_RAP,
1285                         N_("List open sessions"),
1286                         N_("net rap session\n"
1287                            "    List open sessions")
1288                 },
1289                 {
1290                         "server",
1291                         net_rap_server,
1292                         NET_TRANSPORT_RAP,
1293                         N_("List servers in workgroup"),
1294                         N_("net rap server\n"
1295                            "    List servers in domain/workgroup")
1296                 },
1297                 {
1298                         "domain",
1299                         net_rap_domain,
1300                         NET_TRANSPORT_RAP,
1301                         N_("List domains in network"),
1302                         N_("net rap domain\n"
1303                            "    List domains in network")
1304                 },
1305                 {
1306                         "printq",
1307                         net_rap_printq,
1308                         NET_TRANSPORT_RAP,
1309                         N_("List printer queues on server"),
1310                         N_("net rap printq\n"
1311                            "    List printer queues on server")
1312                 },
1313                 {
1314                         "user",
1315                         net_rap_user,
1316                         NET_TRANSPORT_RAP,
1317                         N_("List users"),
1318                         N_("net rap user\n"
1319                            "    List users")
1320                 },
1321                 {
1322                         "group",
1323                         net_rap_group,
1324                         NET_TRANSPORT_RAP,
1325                         N_("List user groups"),
1326                         N_("net rap group\n"
1327                            "    List user groups")
1328                 },
1329                 {
1330                         "validate",
1331                         net_rap_validate,
1332                         NET_TRANSPORT_RAP,
1333                         N_("Check username/password"),
1334                         N_("net rap validate\n"
1335                            "    Check username/password")
1336                 },
1337                 {
1338                         "groupmember",
1339                         net_rap_groupmember,
1340                         NET_TRANSPORT_RAP,
1341                         N_("List/modify group memberships"),
1342                         N_("net rap groupmember\n"
1343                            "    List/modify group memberships")
1344                 },
1345                 {
1346                         "admin",
1347                         net_rap_admin,
1348                         NET_TRANSPORT_RAP,
1349                         N_("Execute commands on remote OS/2"),
1350                         N_("net rap admin\n"
1351                            "    Execute commands on remote OS/2")
1352                 },
1353                 {
1354                         "service",
1355                         net_rap_service,
1356                         NET_TRANSPORT_RAP,
1357                         N_("Start/stop remote service"),
1358                         N_("net rap service\n"
1359                            "    Start/stop remote service")
1360                 },
1361                 {
1362                         "password",
1363                         net_rap_password,
1364                         NET_TRANSPORT_RAP,
1365                         N_("Change user password"),
1366                         N_("net rap password\n"
1367                            "    Change user password")
1368                 },
1369                 {NULL, NULL, 0, NULL, NULL}
1370         };
1371
1372         return net_run_function(c, argc, argv, "net rap", func);
1373 }
1374