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