net: Fix IPC connections with interactive password prompt.
[Samba.git] / source / utils / net.c
blob38ebaf6c26ea157547bf2cbbed6f48d0bd223e9a
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)
9 Originally written by Steve and Jim. Largely rewritten by tridge in
10 November 2001.
12 Reworked again by abartlet in December 2001
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 /*****************************************************/
29 /* */
30 /* Distributed SMB/CIFS Server Management Utility */
31 /* */
32 /* The intent was to make the syntax similar */
33 /* to the NET utility (first developed in DOS */
34 /* with additional interesting & useful functions */
35 /* added in later SMB server network operating */
36 /* systems). */
37 /* */
38 /*****************************************************/
40 #include "includes.h"
41 #include "utils/net.h"
43 /***********************************************************************/
44 /* Beginning of internationalization section. Translatable constants */
45 /* should be kept in this area and referenced in the rest of the code. */
46 /* */
47 /* No functions, outside of Samba or LSB (Linux Standards Base) should */
48 /* be used (if possible). */
49 /***********************************************************************/
51 #define YES_STRING "Yes"
52 #define NO_STRING "No"
54 /************************************************************************************/
55 /* end of internationalization section */
56 /************************************************************************************/
58 /* Yes, these buggers are globals.... */
59 const char *opt_requester_name = NULL;
60 const char *opt_host = NULL;
61 const char *opt_password = NULL;
62 const char *opt_user_name = NULL;
63 BOOL opt_user_specified = False;
64 const char *opt_workgroup = NULL;
65 int opt_long_list_entries = 0;
66 int opt_reboot = 0;
67 int opt_force = 0;
68 int opt_stdin = 0;
69 int opt_port = 0;
70 int opt_verbose = 0;
71 int opt_maxusers = -1;
72 const char *opt_comment = "";
73 const char *opt_container = NULL;
74 int opt_flags = -1;
75 int opt_timeout = 0;
76 const char *opt_target_workgroup = NULL;
77 int opt_machine_pass = 0;
78 BOOL opt_localgroup = False;
79 BOOL opt_domaingroup = False;
80 static BOOL do_talloc_report=False;
81 const char *opt_newntname = "";
82 int opt_rid = 0;
83 int opt_acls = 0;
84 int opt_attrs = 0;
85 int opt_timestamps = 0;
86 const char *opt_exclude = NULL;
87 const char *opt_destination = NULL;
89 BOOL opt_have_ip = False;
90 struct in_addr opt_dest_ip;
92 extern struct in_addr loopback_ip;
93 extern BOOL AllowDebugChange;
95 uint32 get_sec_channel_type(const char *param)
97 if (!(param && *param)) {
98 return get_default_sec_channel();
99 } else {
100 if (strequal(param, "PDC")) {
101 return SEC_CHAN_BDC;
102 } else if (strequal(param, "BDC")) {
103 return SEC_CHAN_BDC;
104 } else if (strequal(param, "MEMBER")) {
105 return SEC_CHAN_WKSTA;
106 #if 0
107 } else if (strequal(param, "DOMAIN")) {
108 return SEC_CHAN_DOMAIN;
109 #endif
110 } else {
111 return get_default_sec_channel();
117 run a function from a function table. If not found then
118 call the specified usage function
120 int net_run_function(int argc, const char **argv, struct functable *table,
121 int (*usage_fn)(int argc, const char **argv))
123 int i;
125 if (argc < 1) {
126 d_printf("\nUsage: \n");
127 return usage_fn(argc, argv);
129 for (i=0; table[i].funcname; i++) {
130 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
131 return table[i].fn(argc-1, argv+1);
133 d_fprintf(stderr, "No command: %s\n", argv[0]);
134 return usage_fn(argc, argv);
138 * run a function from a function table.
140 int net_run_function2(int argc, const char **argv, const char *whoami,
141 struct functable2 *table)
143 int i;
145 if (argc != 0) {
146 for (i=0; table[i].funcname; i++) {
147 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
148 return table[i].fn(argc-1, argv+1);
152 for (i=0; table[i].funcname != NULL; i++) {
153 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
154 table[i].helptext);
157 return -1;
160 /****************************************************************************
161 connect to \\server\service
162 ****************************************************************************/
164 NTSTATUS connect_to_service(struct cli_state **c, struct in_addr *server_ip,
165 const char *server_name,
166 const char *service_name,
167 const char *service_type)
169 NTSTATUS nt_status;
171 if (!opt_password && !opt_machine_pass) {
172 char *pass = getpass("Password:");
173 if (pass) {
174 opt_password = SMB_STRDUP(pass);
178 nt_status = cli_full_connection(c, NULL, server_name,
179 server_ip, opt_port,
180 service_name, service_type,
181 opt_user_name, opt_workgroup,
182 opt_password, 0, Undefined, NULL);
184 if (NT_STATUS_IS_OK(nt_status) ||
185 NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT) ||
186 NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT) ||
187 NT_STATUS_EQUAL(nt_status, NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT)) {
188 return nt_status;
191 d_fprintf(stderr, "Could not connect to server %s\n", server_name);
193 /* Display a nicer message depending on the result */
195 if (NT_STATUS_V(nt_status) ==
196 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
197 d_fprintf(stderr, "The username or password was not correct.\n");
199 if (NT_STATUS_V(nt_status) ==
200 NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
201 d_fprintf(stderr, "The account was locked out.\n");
203 if (NT_STATUS_V(nt_status) ==
204 NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
205 d_fprintf(stderr, "The account was disabled.\n");
207 return nt_status;
211 /****************************************************************************
212 connect to \\server\ipc$
213 ****************************************************************************/
214 NTSTATUS connect_to_ipc(struct cli_state **c, struct in_addr *server_ip,
215 const char *server_name)
217 return connect_to_service(c, server_ip, server_name, "IPC$", "IPC");
220 /****************************************************************************
221 connect to \\server\ipc$ anonymously
222 ****************************************************************************/
223 NTSTATUS connect_to_ipc_anonymous(struct cli_state **c,
224 struct in_addr *server_ip, const char *server_name)
226 NTSTATUS nt_status;
228 nt_status = cli_full_connection(c, opt_requester_name, server_name,
229 server_ip, opt_port,
230 "IPC$", "IPC",
231 "", "",
232 "", 0, Undefined, NULL);
234 if (NT_STATUS_IS_OK(nt_status)) {
235 return nt_status;
236 } else {
237 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
238 return nt_status;
242 /****************************************************************************
243 Return malloced user@realm for krb5 login.
244 ****************************************************************************/
246 static char *get_user_and_realm(const char *username)
248 char *user_and_realm = NULL;
250 if (!username) {
251 return NULL;
253 if (strchr_m(username, '@')) {
254 user_and_realm = SMB_STRDUP(username);
255 } else {
256 if (asprintf(&user_and_realm, "%s@%s", username, lp_realm()) == -1) {
257 user_and_realm = NULL;
260 return user_and_realm;
263 /****************************************************************************
264 connect to \\server\ipc$ using KRB5
265 ****************************************************************************/
267 NTSTATUS connect_to_ipc_krb5(struct cli_state **c,
268 struct in_addr *server_ip, const char *server_name)
270 NTSTATUS nt_status;
271 char *user_and_realm = NULL;
273 if (!opt_password && !opt_machine_pass) {
274 char *pass = getpass("Password:");
275 if (pass) {
276 opt_password = SMB_STRDUP(pass);
280 user_and_realm = get_user_and_realm(opt_user_name);
281 if (!user_and_realm) {
282 return NT_STATUS_NO_MEMORY;
285 nt_status = cli_full_connection(c, NULL, server_name,
286 server_ip, opt_port,
287 "IPC$", "IPC",
288 user_and_realm, opt_workgroup,
289 opt_password, CLI_FULL_CONNECTION_USE_KERBEROS,
290 Undefined, NULL);
292 SAFE_FREE(user_and_realm);
294 if (NT_STATUS_IS_OK(nt_status)) {
295 return nt_status;
296 } else {
297 DEBUG(1,("Cannot connect to server using kerberos. Error was %s\n", nt_errstr(nt_status)));
298 return nt_status;
303 * Connect a server and open a given pipe
305 * @param cli_dst A cli_state
306 * @param pipe The pipe to open
307 * @param got_pipe boolean that stores if we got a pipe
309 * @return Normal NTSTATUS return.
311 NTSTATUS connect_dst_pipe(struct cli_state **cli_dst, struct rpc_pipe_client **pp_pipe_hnd, int pipe_num)
313 NTSTATUS nt_status;
314 char *server_name = SMB_STRDUP("127.0.0.1");
315 struct cli_state *cli_tmp = NULL;
316 struct rpc_pipe_client *pipe_hnd = NULL;
318 if (server_name == NULL) {
319 return NT_STATUS_NO_MEMORY;
322 if (opt_destination) {
323 SAFE_FREE(server_name);
324 if ((server_name = SMB_STRDUP(opt_destination)) == NULL) {
325 return NT_STATUS_NO_MEMORY;
329 /* make a connection to a named pipe */
330 nt_status = connect_to_ipc(&cli_tmp, NULL, server_name);
331 if (!NT_STATUS_IS_OK(nt_status)) {
332 SAFE_FREE(server_name);
333 return nt_status;
336 pipe_hnd = cli_rpc_pipe_open_noauth(cli_tmp, pipe_num, &nt_status);
337 if (!pipe_hnd) {
338 DEBUG(0, ("couldn't not initialize pipe\n"));
339 cli_shutdown(cli_tmp);
340 SAFE_FREE(server_name);
341 return nt_status;
344 *cli_dst = cli_tmp;
345 *pp_pipe_hnd = pipe_hnd;
346 SAFE_FREE(server_name);
348 return nt_status;
351 /****************************************************************************
352 Use the local machine account (krb) and password for this session.
353 ****************************************************************************/
355 int net_use_krb_machine_account(void)
357 char *user_name = NULL;
359 if (!secrets_init()) {
360 d_fprintf(stderr, "ERROR: Unable to open secrets database\n");
361 exit(1);
364 opt_password = secrets_fetch_machine_password(opt_target_workgroup, NULL, NULL);
365 if (asprintf(&user_name, "%s$@%s", global_myname(), lp_realm()) == -1) {
366 return -1;
368 opt_user_name = user_name;
369 return 0;
372 /****************************************************************************
373 Use the machine account name and password for this session.
374 ****************************************************************************/
376 int net_use_machine_account(void)
378 char *user_name = NULL;
380 if (!secrets_init()) {
381 d_fprintf(stderr, "ERROR: Unable to open secrets database\n");
382 exit(1);
385 opt_password = secrets_fetch_machine_password(opt_target_workgroup, NULL, NULL);
386 if (asprintf(&user_name, "%s$", global_myname()) == -1) {
387 return -1;
389 opt_user_name = user_name;
390 return 0;
393 BOOL net_find_server(const char *domain, unsigned flags, struct in_addr *server_ip, char **server_name)
395 const char *d = domain ? domain : opt_target_workgroup;
397 if (opt_host) {
398 *server_name = SMB_STRDUP(opt_host);
401 if (opt_have_ip) {
402 *server_ip = opt_dest_ip;
403 if (!*server_name) {
404 *server_name = SMB_STRDUP(inet_ntoa(opt_dest_ip));
406 } else if (*server_name) {
407 /* resolve the IP address */
408 if (!resolve_name(*server_name, server_ip, 0x20)) {
409 DEBUG(1,("Unable to resolve server name\n"));
410 return False;
412 } else if (flags & NET_FLAGS_PDC) {
413 struct in_addr pdc_ip;
415 if (get_pdc_ip(d, &pdc_ip)) {
416 fstring dc_name;
418 if (is_zero_ip(pdc_ip))
419 return False;
421 if ( !name_status_find(d, 0x1b, 0x20, pdc_ip, dc_name) )
422 return False;
424 *server_name = SMB_STRDUP(dc_name);
425 *server_ip = pdc_ip;
427 } else if (flags & NET_FLAGS_DMB) {
428 struct in_addr msbrow_ip;
429 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
430 if (!resolve_name(d, &msbrow_ip, 0x1B)) {
431 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
432 return False;
433 } else {
434 *server_ip = msbrow_ip;
436 *server_name = SMB_STRDUP(inet_ntoa(opt_dest_ip));
437 } else if (flags & NET_FLAGS_MASTER) {
438 struct in_addr brow_ips;
439 if (!resolve_name(d, &brow_ips, 0x1D)) {
440 /* go looking for workgroups */
441 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
442 return False;
443 } else {
444 *server_ip = brow_ips;
446 *server_name = SMB_STRDUP(inet_ntoa(opt_dest_ip));
447 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
448 *server_ip = loopback_ip;
449 *server_name = SMB_STRDUP("127.0.0.1");
452 if (!server_name || !*server_name) {
453 DEBUG(1,("no server to connect to\n"));
454 return False;
457 return True;
461 BOOL net_find_pdc(struct in_addr *server_ip, fstring server_name, const char *domain_name)
463 if (get_pdc_ip(domain_name, server_ip)) {
464 if (is_zero_ip(*server_ip))
465 return False;
467 if (!name_status_find(domain_name, 0x1b, 0x20, *server_ip, server_name))
468 return False;
470 return True;
472 else
473 return False;
476 struct cli_state *net_make_ipc_connection( unsigned flags )
478 return net_make_ipc_connection_ex( NULL, NULL, NULL, flags );
481 struct cli_state *net_make_ipc_connection_ex( const char *domain, const char *server,
482 struct in_addr *ip, unsigned flags)
484 char *server_name = NULL;
485 struct in_addr server_ip;
486 struct cli_state *cli = NULL;
487 NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
489 if ( !server || !ip ) {
490 if (!net_find_server(domain, flags, &server_ip, &server_name)) {
491 d_fprintf(stderr, "Unable to find a suitable server\n");
492 return NULL;
494 } else {
495 server_name = SMB_STRDUP( server );
496 server_ip = *ip;
499 if (opt_user_name) {
500 nt_status = connect_to_ipc(&cli, &server_ip, server_name);
501 if (NT_STATUS_IS_OK(nt_status)) {
502 goto connected;
505 if (flags & NET_FLAGS_ANONYMOUS) {
506 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, server_name);
507 if (NT_STATUS_IS_OK(nt_status)) {
508 goto connected;
512 SAFE_FREE(server_name);
513 d_fprintf(stderr, "Connection failed: %s\n",
514 nt_errstr(nt_status));
515 return NULL;
517 connected:
518 /* store the server in the affinity cache if it was a PDC */
520 if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
521 saf_store( cli->server_domain, cli->desthost );
523 return cli;
526 static int net_user(int argc, const char **argv)
528 if (net_ads_check() == 0)
529 return net_ads_user(argc, argv);
531 /* if server is not specified, default to PDC? */
532 if (net_rpc_check(NET_FLAGS_PDC))
533 return net_rpc_user(argc, argv);
535 return net_rap_user(argc, argv);
538 static int net_group(int argc, const char **argv)
540 if (net_ads_check() == 0)
541 return net_ads_group(argc, argv);
543 if (argc == 0 && net_rpc_check(NET_FLAGS_PDC))
544 return net_rpc_group(argc, argv);
546 return net_rap_group(argc, argv);
549 static int net_join(int argc, const char **argv)
551 if (net_ads_check_our_domain() == 0) {
552 if (net_ads_join(argc, argv) == 0)
553 return 0;
554 else
555 d_fprintf(stderr, "ADS join did not work, falling back to RPC...\n");
557 return net_rpc_join(argc, argv);
560 static int net_changetrustpw(int argc, const char **argv)
562 if (net_ads_check_our_domain() == 0)
563 return net_ads_changetrustpw(argc, argv);
565 return net_rpc_changetrustpw(argc, argv);
568 static void set_line_buffering(FILE *f)
570 setvbuf(f, NULL, _IOLBF, 0);
573 static int net_changesecretpw(int argc, const char **argv)
575 char *trust_pw;
576 uint32 sec_channel_type = SEC_CHAN_WKSTA;
578 if(opt_force) {
579 if (opt_stdin) {
580 set_line_buffering(stdin);
581 set_line_buffering(stdout);
582 set_line_buffering(stderr);
585 trust_pw = get_pass("Enter machine password: ", opt_stdin);
587 if (!secrets_store_machine_password(trust_pw, lp_workgroup(), sec_channel_type)) {
588 d_fprintf(stderr, "Unable to write the machine account password in the secrets database");
589 return 1;
591 else {
592 d_printf("Modified trust account password in secrets database\n");
595 else {
596 d_printf("Machine account password change requires the -f flag.\n");
597 d_printf("Do NOT use this function unless you know what it does!\n");
598 d_printf("This function will change the ADS Domain member machine account password in the secrets.tdb file!\n");
601 return 0;
604 static int net_share(int argc, const char **argv)
606 if (net_rpc_check(0))
607 return net_rpc_share(argc, argv);
608 return net_rap_share(argc, argv);
611 static int net_file(int argc, const char **argv)
613 if (net_rpc_check(0))
614 return net_rpc_file(argc, argv);
615 return net_rap_file(argc, argv);
619 Retrieve our local SID or the SID for the specified name
621 static int net_getlocalsid(int argc, const char **argv)
623 DOM_SID sid;
624 const char *name;
625 fstring sid_str;
627 if (argc >= 1) {
628 name = argv[0];
630 else {
631 name = global_myname();
634 if(!initialize_password_db(False)) {
635 DEBUG(0, ("WARNING: Could not open passdb - local sid may not reflect passdb\n"
636 "backend knowlege (such as the sid stored in LDAP)\n"));
639 /* first check to see if we can even access secrets, so we don't
640 panic when we can't. */
642 if (!secrets_init()) {
643 d_fprintf(stderr, "Unable to open secrets.tdb. Can't fetch domain SID for name: %s\n", name);
644 return 1;
647 /* Generate one, if it doesn't exist */
648 get_global_sam_sid();
650 if (!secrets_fetch_domain_sid(name, &sid)) {
651 DEBUG(0, ("Can't fetch domain SID for name: %s\n", name));
652 return 1;
654 sid_to_string(sid_str, &sid);
655 d_printf("SID for domain %s is: %s\n", name, sid_str);
656 return 0;
659 static int net_setlocalsid(int argc, const char **argv)
661 DOM_SID sid;
663 if ( (argc != 1)
664 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
665 || (!string_to_sid(&sid, argv[0]))
666 || (sid.num_auths != 4)) {
667 d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n");
668 return 1;
671 if (!secrets_store_domain_sid(global_myname(), &sid)) {
672 DEBUG(0,("Can't store domain SID as a pdc/bdc.\n"));
673 return 1;
676 return 0;
679 static int net_setdomainsid(int argc, const char **argv)
681 DOM_SID sid;
683 if ( (argc != 1)
684 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
685 || (!string_to_sid(&sid, argv[0]))
686 || (sid.num_auths != 4)) {
687 d_printf("usage: net setdomainsid S-1-5-21-x-y-z\n");
688 return 1;
691 if (!secrets_store_domain_sid(lp_workgroup(), &sid)) {
692 DEBUG(0,("Can't store domain SID.\n"));
693 return 1;
696 return 0;
699 static int net_getdomainsid(int argc, const char **argv)
701 DOM_SID domain_sid;
702 fstring sid_str;
704 if(!initialize_password_db(False)) {
705 DEBUG(0, ("WARNING: Could not open passdb - domain sid may not reflect passdb\n"
706 "backend knowlege (such as the sid stored in LDAP)\n"));
709 /* first check to see if we can even access secrets, so we don't
710 panic when we can't. */
712 if (!secrets_init()) {
713 d_fprintf(stderr, "Unable to open secrets.tdb. "
714 "Can't fetch domainSID for name: %s\n",
715 get_global_sam_name());
716 return 1;
719 /* Generate one, if it doesn't exist */
720 get_global_sam_sid();
722 if (!secrets_fetch_domain_sid(global_myname(), &domain_sid)) {
723 d_fprintf(stderr, "Could not fetch local SID\n");
724 return 1;
726 sid_to_string(sid_str, &domain_sid);
727 d_printf("SID for domain %s is: %s\n", global_myname(), sid_str);
729 if (!secrets_fetch_domain_sid(opt_workgroup, &domain_sid)) {
730 d_fprintf(stderr, "Could not fetch domain SID\n");
731 return 1;
734 sid_to_string(sid_str, &domain_sid);
735 d_printf("SID for domain %s is: %s\n", opt_workgroup, sid_str);
737 return 0;
740 #ifdef WITH_FAKE_KASERVER
742 int net_help_afs(int argc, const char **argv)
744 d_printf(" net afs key filename\n"
745 "\tImports a OpenAFS KeyFile into our secrets.tdb\n\n");
746 d_printf(" net afs impersonate <user> <cell>\n"
747 "\tCreates a token for user@cell\n\n");
748 return -1;
751 static int net_afs_key(int argc, const char **argv)
753 int fd;
754 struct afs_keyfile keyfile;
756 if (argc != 2) {
757 d_printf("usage: 'net afs key <keyfile> cell'\n");
758 return -1;
761 if (!secrets_init()) {
762 d_fprintf(stderr, "Could not open secrets.tdb\n");
763 return -1;
766 if ((fd = open(argv[0], O_RDONLY, 0)) < 0) {
767 d_fprintf(stderr, "Could not open %s\n", argv[0]);
768 return -1;
771 if (read(fd, &keyfile, sizeof(keyfile)) != sizeof(keyfile)) {
772 d_fprintf(stderr, "Could not read keyfile\n");
773 return -1;
776 if (!secrets_store_afs_keyfile(argv[1], &keyfile)) {
777 d_fprintf(stderr, "Could not write keyfile to secrets.tdb\n");
778 return -1;
781 return 0;
784 static int net_afs_impersonate(int argc, const char **argv)
786 char *token;
788 if (argc != 2) {
789 fprintf(stderr, "Usage: net afs impersonate <user> <cell>\n");
790 exit(1);
793 token = afs_createtoken_str(argv[0], argv[1]);
795 if (token == NULL) {
796 fprintf(stderr, "Could not create token\n");
797 exit(1);
800 if (!afs_settoken_str(token)) {
801 fprintf(stderr, "Could not set token into kernel\n");
802 exit(1);
805 printf("Success: %s@%s\n", argv[0], argv[1]);
806 return 0;
809 static int net_afs(int argc, const char **argv)
811 struct functable func[] = {
812 {"key", net_afs_key},
813 {"impersonate", net_afs_impersonate},
814 {"help", net_help_afs},
815 {NULL, NULL}
817 return net_run_function(argc, argv, func, net_help_afs);
820 #endif /* WITH_FAKE_KASERVER */
822 static BOOL search_maxrid(struct pdb_search *search, const char *type,
823 uint32 *max_rid)
825 struct samr_displayentry *entries;
826 uint32 i, num_entries;
828 if (search == NULL) {
829 d_fprintf(stderr, "get_maxrid: Could not search %s\n", type);
830 return False;
833 num_entries = pdb_search_entries(search, 0, 0xffffffff, &entries);
834 for (i=0; i<num_entries; i++)
835 *max_rid = MAX(*max_rid, entries[i].rid);
836 pdb_search_destroy(search);
837 return True;
840 static uint32 get_maxrid(void)
842 uint32 max_rid = 0;
844 if (!search_maxrid(pdb_search_users(0), "users", &max_rid))
845 return 0;
847 if (!search_maxrid(pdb_search_groups(), "groups", &max_rid))
848 return 0;
850 if (!search_maxrid(pdb_search_aliases(get_global_sam_sid()),
851 "aliases", &max_rid))
852 return 0;
854 return max_rid;
857 static int net_maxrid(int argc, const char **argv)
859 uint32 rid;
861 if (argc != 0) {
862 DEBUG(0, ("usage: net maxrid\n"));
863 return 1;
866 if ((rid = get_maxrid()) == 0) {
867 DEBUG(0, ("can't get current maximum rid\n"));
868 return 1;
871 d_printf("Currently used maximum rid: %d\n", rid);
873 return 0;
876 /* main function table */
877 static struct functable net_func[] = {
878 {"RPC", net_rpc},
879 {"RAP", net_rap},
880 {"ADS", net_ads},
882 /* eventually these should auto-choose the transport ... */
883 {"FILE", net_file},
884 {"SHARE", net_share},
885 {"SESSION", net_rap_session},
886 {"SERVER", net_rap_server},
887 {"DOMAIN", net_rap_domain},
888 {"PRINTQ", net_rap_printq},
889 {"USER", net_user},
890 {"GROUP", net_group},
891 {"GROUPMAP", net_groupmap},
892 {"SAM", net_sam},
893 {"VALIDATE", net_rap_validate},
894 {"GROUPMEMBER", net_rap_groupmember},
895 {"ADMIN", net_rap_admin},
896 {"SERVICE", net_rap_service},
897 {"PASSWORD", net_rap_password},
898 {"CHANGETRUSTPW", net_changetrustpw},
899 {"CHANGESECRETPW", net_changesecretpw},
900 {"TIME", net_time},
901 {"LOOKUP", net_lookup},
902 {"JOIN", net_join},
903 {"CACHE", net_cache},
904 {"GETLOCALSID", net_getlocalsid},
905 {"SETLOCALSID", net_setlocalsid},
906 {"SETDOMAINSID", net_setdomainsid},
907 {"GETDOMAINSID", net_getdomainsid},
908 {"MAXRID", net_maxrid},
909 {"IDMAP", net_idmap},
910 {"STATUS", net_status},
911 {"USERSHARE", net_usershare},
912 {"USERSIDLIST", net_usersidlist},
913 #ifdef WITH_FAKE_KASERVER
914 {"AFS", net_afs},
915 #endif
917 {"HELP", net_help},
918 {NULL, NULL}
922 /****************************************************************************
923 main program
924 ****************************************************************************/
925 int main(int argc, const char **argv)
927 int opt,i;
928 char *p;
929 int rc = 0;
930 int argc_new = 0;
931 const char ** argv_new;
932 poptContext pc;
934 struct poptOption long_options[] = {
935 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
936 {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup},
937 {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'},
938 {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'},
939 {"port", 'p', POPT_ARG_INT, &opt_port},
940 {"myname", 'n', POPT_ARG_STRING, &opt_requester_name},
941 {"server", 'S', POPT_ARG_STRING, &opt_host},
942 {"container", 'c', POPT_ARG_STRING, &opt_container},
943 {"comment", 'C', POPT_ARG_STRING, &opt_comment},
944 {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers},
945 {"flags", 'F', POPT_ARG_INT, &opt_flags},
946 {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries},
947 {"reboot", 'r', POPT_ARG_NONE, &opt_reboot},
948 {"force", 'f', POPT_ARG_NONE, &opt_force},
949 {"stdin", 'i', POPT_ARG_NONE, &opt_stdin},
950 {"timeout", 't', POPT_ARG_INT, &opt_timeout},
951 {"machine-pass",'P', POPT_ARG_NONE, &opt_machine_pass},
952 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
953 {"verbose", 'v', POPT_ARG_NONE, &opt_verbose},
954 /* Options for 'net groupmap set' */
955 {"local", 'L', POPT_ARG_NONE, &opt_localgroup},
956 {"domain", 'D', POPT_ARG_NONE, &opt_domaingroup},
957 {"ntname", 'N', POPT_ARG_STRING, &opt_newntname},
958 {"rid", 'R', POPT_ARG_INT, &opt_rid},
959 /* Options for 'net rpc share migrate' */
960 {"acls", 0, POPT_ARG_NONE, &opt_acls},
961 {"attrs", 0, POPT_ARG_NONE, &opt_attrs},
962 {"timestamps", 0, POPT_ARG_NONE, &opt_timestamps},
963 {"exclude", 'e', POPT_ARG_STRING, &opt_exclude},
964 {"destination", 0, POPT_ARG_STRING, &opt_destination},
965 {"tallocreport", 0, POPT_ARG_NONE, &do_talloc_report},
967 POPT_COMMON_SAMBA
968 { 0, 0, 0, 0}
971 zero_ip(&opt_dest_ip);
973 load_case_tables();
975 /* set default debug level to 0 regardless of what smb.conf sets */
976 DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
977 dbf = x_stderr;
979 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
980 POPT_CONTEXT_KEEP_FIRST);
982 while((opt = poptGetNextOpt(pc)) != -1) {
983 switch (opt) {
984 case 'h':
985 net_help(argc, argv);
986 exit(0);
987 break;
988 case 'I':
989 opt_dest_ip = *interpret_addr2(poptGetOptArg(pc));
990 if (is_zero_ip(opt_dest_ip))
991 d_fprintf(stderr, "\nInvalid ip address specified\n");
992 else
993 opt_have_ip = True;
994 break;
995 case 'U':
996 opt_user_specified = True;
997 opt_user_name = SMB_STRDUP(opt_user_name);
998 p = strchr(opt_user_name,'%');
999 if (p) {
1000 *p = 0;
1001 opt_password = p+1;
1003 break;
1004 default:
1005 d_fprintf(stderr, "\nInvalid option %s: %s\n",
1006 poptBadOption(pc, 0), poptStrerror(opt));
1007 net_help(argc, argv);
1008 exit(1);
1013 * Don't load debug level from smb.conf. It should be
1014 * set by cmdline arg or remain default (0)
1016 AllowDebugChange = False;
1017 lp_load(dyn_CONFIGFILE,True,False,False,True);
1019 argv_new = (const char **)poptGetArgs(pc);
1021 argc_new = argc;
1022 for (i=0; i<argc; i++) {
1023 if (argv_new[i] == NULL) {
1024 argc_new = i;
1025 break;
1029 if (do_talloc_report) {
1030 talloc_enable_leak_report();
1033 if (opt_requester_name) {
1034 set_global_myname(opt_requester_name);
1037 if (!opt_user_name && getenv("LOGNAME")) {
1038 opt_user_name = getenv("LOGNAME");
1041 if (!opt_user_name) {
1042 opt_user_name = "";
1045 if (!opt_workgroup) {
1046 opt_workgroup = smb_xstrdup(lp_workgroup());
1049 if (!opt_target_workgroup) {
1050 opt_target_workgroup = smb_xstrdup(lp_workgroup());
1053 if (!init_names())
1054 exit(1);
1056 load_interfaces();
1058 /* this makes sure that when we do things like call scripts,
1059 that it won't assert becouse we are not root */
1060 sec_init();
1062 if (opt_machine_pass) {
1063 /* it is very useful to be able to make ads queries as the
1064 machine account for testing purposes and for domain leave */
1066 net_use_krb_machine_account();
1069 if (!opt_password) {
1070 opt_password = getenv("PASSWD");
1073 rc = net_run_function(argc_new-1, argv_new+1, net_func, net_help);
1075 DEBUG(2,("return code = %d\n", rc));
1076 return rc;