This patch modifies 'net rpc vampire' to add new and existing users to both
[Samba.git] / source / utils / net.c
blobd8f3264840f435c53051da3e6b767704a1e33556
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_port = 0;
69 int opt_maxusers = -1;
70 const char *opt_comment = "";
71 const char *opt_container = "cn=Users";
72 int opt_flags = -1;
73 int opt_timeout = 0;
74 const char *opt_target_workgroup = NULL;
75 static int opt_machine_pass = 0;
77 BOOL opt_have_ip = False;
78 struct in_addr opt_dest_ip;
80 uint32 get_sec_channel_type(const char *param)
82 if (!(param && *param)) {
83 return get_default_sec_channel();
84 } else {
85 if (strcasecmp(param, "PDC")==0) {
86 return SEC_CHAN_BDC;
87 } else if (strcasecmp(param, "BDC")==0) {
88 return SEC_CHAN_BDC;
89 } else if (strcasecmp(param, "MEMBER")==0) {
90 return SEC_CHAN_WKSTA;
91 #if 0
92 } else if (strcasecmp(param, "DOMAIN")==0) {
93 return SEC_CHAN_DOMAIN;
94 #endif
95 } else {
96 return get_default_sec_channel();
102 run a function from a function table. If not found then
103 call the specified usage function
105 int net_run_function(int argc, const char **argv, struct functable *table,
106 int (*usage_fn)(int argc, const char **argv))
108 int i;
110 if (argc < 1) {
111 d_printf("\nUsage: \n");
112 return usage_fn(argc, argv);
114 for (i=0; table[i].funcname; i++) {
115 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
116 return table[i].fn(argc-1, argv+1);
118 d_printf("No command: %s\n", argv[0]);
119 return usage_fn(argc, argv);
123 /****************************************************************************
124 connect to \\server\ipc$
125 ****************************************************************************/
126 NTSTATUS connect_to_ipc(struct cli_state **c, struct in_addr *server_ip,
127 const char *server_name)
129 NTSTATUS nt_status;
131 if (!opt_password) {
132 char *pass = getpass("Password:");
133 if (pass) {
134 opt_password = strdup(pass);
138 nt_status = cli_full_connection(c, opt_requester_name, server_name,
139 server_ip, opt_port,
140 "IPC$", "IPC",
141 opt_user_name, opt_workgroup,
142 opt_password, 0, NULL);
144 if (NT_STATUS_IS_OK(nt_status)) {
145 return nt_status;
146 } else {
147 DEBUG(1,("Cannot connect to server. Error was %s\n",
148 nt_errstr(nt_status)));
150 /* Display a nicer message depending on the result */
152 if (NT_STATUS_V(nt_status) ==
153 NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
154 d_printf("The username or password was not correct.\n");
156 return nt_status;
160 /****************************************************************************
161 connect to \\server\ipc$ anonymously
162 ****************************************************************************/
163 NTSTATUS connect_to_ipc_anonymous(struct cli_state **c,
164 struct in_addr *server_ip, const char *server_name)
166 NTSTATUS nt_status;
168 nt_status = cli_full_connection(c, opt_requester_name, server_name,
169 server_ip, opt_port,
170 "IPC$", "IPC",
171 "", "",
172 "", 0, NULL);
174 if (NT_STATUS_IS_OK(nt_status)) {
175 return nt_status;
176 } else {
177 DEBUG(1,("Cannot connect to server (anonymously). Error was %s\n", nt_errstr(nt_status)));
178 return nt_status;
182 BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
185 if (opt_host) {
186 *server_name = strdup(opt_host);
189 if (opt_have_ip) {
190 *server_ip = opt_dest_ip;
191 if (!*server_name) {
192 *server_name = strdup(inet_ntoa(opt_dest_ip));
194 } else if (*server_name) {
195 /* resolve the IP address */
196 if (!resolve_name(*server_name, server_ip, 0x20)) {
197 DEBUG(1,("Unable to resolve server name\n"));
198 return False;
200 } else if (flags & NET_FLAGS_PDC) {
201 struct in_addr pdc_ip;
203 if (get_pdc_ip(opt_target_workgroup, &pdc_ip)) {
204 fstring dc_name;
206 if (is_zero_ip(pdc_ip))
207 return False;
209 if (!lookup_dc_name(global_myname(), opt_target_workgroup, &pdc_ip, dc_name))
210 return False;
212 *server_name = strdup(dc_name);
213 *server_ip = pdc_ip;
216 } else if (flags & NET_FLAGS_DMB) {
217 struct in_addr msbrow_ip;
218 /* if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
219 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B)) {
220 DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
221 return False;
222 } else {
223 *server_ip = msbrow_ip;
225 *server_name = strdup(inet_ntoa(opt_dest_ip));
226 } else if (flags & NET_FLAGS_MASTER) {
227 struct in_addr brow_ips;
228 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D)) {
229 /* go looking for workgroups */
230 DEBUG(1,("Unable to resolve master browser via name lookup\n"));
231 return False;
232 } else {
233 *server_ip = brow_ips;
235 *server_name = strdup(inet_ntoa(opt_dest_ip));
236 } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
237 extern struct in_addr loopback_ip;
238 *server_ip = loopback_ip;
239 *server_name = strdup("127.0.0.1");
242 if (!server_name || !*server_name) {
243 DEBUG(1,("no server to connect to\n"));
244 return False;
247 return True;
251 BOOL net_find_dc(struct in_addr *server_ip, fstring server_name, const char *domain_name)
253 if (get_pdc_ip(domain_name, server_ip)) {
254 fstring dc_name;
256 if (is_zero_ip(*server_ip))
257 return False;
259 if (!lookup_dc_name(global_myname(), domain_name, server_ip, dc_name))
260 return False;
262 fstrcpy(server_name, dc_name);
263 return True;
264 } else
265 return False;
269 struct cli_state *net_make_ipc_connection(unsigned flags)
271 char *server_name = NULL;
272 struct in_addr server_ip;
273 struct cli_state *cli = NULL;
274 NTSTATUS nt_status;
276 if (!net_find_server(flags, &server_ip, &server_name)) {
277 d_printf("\nUnable to find a suitable server\n");
278 return NULL;
281 if (flags & NET_FLAGS_ANONYMOUS) {
282 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, server_name);
283 } else {
284 nt_status = connect_to_ipc(&cli, &server_ip, server_name);
287 SAFE_FREE(server_name);
288 if (NT_STATUS_IS_OK(nt_status)) {
289 return cli;
290 } else {
291 return NULL;
295 static int net_user(int argc, const char **argv)
297 if (net_ads_check() == 0)
298 return net_ads_user(argc, argv);
300 /* if server is not specified, default to PDC? */
301 if (net_rpc_check(NET_FLAGS_PDC))
302 return net_rpc_user(argc, argv);
304 return net_rap_user(argc, argv);
307 static int net_group(int argc, const char **argv)
309 if (net_ads_check() == 0)
310 return net_ads_group(argc, argv);
312 if (argc == 0 && net_rpc_check(NET_FLAGS_PDC))
313 return net_rpc_group(argc, argv);
315 return net_rap_group(argc, argv);
318 static int net_join(int argc, const char **argv)
320 if (net_ads_check() == 0) {
321 if (net_ads_join(argc, argv) == 0)
322 return 0;
323 else
324 d_printf("ADS join did not work, trying RPC...\n");
326 return net_rpc_join(argc, argv);
329 static int net_changetrustpw(int argc, const char **argv)
331 if (net_ads_check() == 0)
332 return net_ads_changetrustpw(argc, argv);
334 return net_rpc_changetrustpw(argc, argv);
337 static int net_share(int argc, const char **argv)
339 if (net_rpc_check(0))
340 return net_rpc_share(argc, argv);
341 return net_rap_share(argc, argv);
344 static int net_file(int argc, const char **argv)
346 if (net_rpc_check(0))
347 return net_rpc_file(argc, argv);
348 return net_rap_file(argc, argv);
351 /***********************************************************
352 migrated functionality from smbgroupedit
353 **********************************************************/
354 static int net_groupmap(int argc, const char **argv)
356 if ( 0 == argc )
357 return net_help_groupmap( argc, argv );
359 if ( !StrCaseCmp( argv[0], "add" ) )
360 return net_groupmap_add(argc-1, argv+1);
361 else if ( !StrCaseCmp( argv[0], "modify" ) )
362 return net_groupmap_modify(argc-1, argv+1);
363 else if ( !StrCaseCmp( argv[0], "delete" ) )
364 return net_groupmap_delete(argc-1, argv+1);
365 else if ( !StrCaseCmp( argv[0], "list" ) )
366 return net_groupmap_list(argc-1, argv+1);
368 return net_help_groupmap( argc, argv );
371 /***********************************************************
372 Helper function for net_idmap_dump. Dump one entry.
373 **********************************************************/
374 static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
375 TDB_DATA key,
376 TDB_DATA data,
377 void *unused)
379 if (strncmp(key.dptr, "S-", 2) != 0)
380 return 0;
382 printf("%s %s\n", data.dptr, key.dptr);
383 return 0;
386 /***********************************************************
387 Dump the current idmap
388 **********************************************************/
389 static int net_idmap_dump(int argc, const char **argv)
391 TDB_CONTEXT *idmap_tdb;
393 if ( argc != 1 )
394 return net_help_idmap( argc, argv );
396 idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
398 if (idmap_tdb == NULL) {
399 d_printf("Could not open idmap: %s\n", argv[0]);
400 return -1;
403 tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
405 tdb_close(idmap_tdb);
407 return 0;
410 /***********************************************************
411 Look at the current idmap
412 **********************************************************/
413 static int net_idmap(int argc, const char **argv)
415 if ( 0 == argc )
416 return net_help_idmap( argc, argv );
418 if ( !StrCaseCmp( argv[0], "dump" ) )
419 return net_idmap_dump(argc-1, argv+1);
421 return net_help_idmap( argc, argv );
426 Retrieve our local SID or the SID for the specified name
428 static int net_getlocalsid(int argc, const char **argv)
430 DOM_SID sid;
431 const char *name;
432 fstring sid_str;
434 if (argc >= 1) {
435 name = argv[0];
437 else {
438 name = global_myname();
441 if (!secrets_fetch_domain_sid(name, &sid)) {
442 DEBUG(0, ("Can't fetch domain SID for name: %s\n", name));
443 return 1;
445 sid_to_string(sid_str, &sid);
446 d_printf("SID for domain %s is: %s\n", name, sid_str);
447 return 0;
450 static int net_setlocalsid(int argc, const char **argv)
452 DOM_SID sid;
454 if ( (argc != 1)
455 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
456 || (!string_to_sid(&sid, argv[0]))
457 || (sid.num_auths != 4)) {
458 d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n");
459 return 1;
462 if (!secrets_store_domain_sid(global_myname(), &sid)) {
463 DEBUG(0,("Can't store domain SID as a pdc/bdc.\n"));
464 return 1;
467 return 0;
470 static int net_getdomainsid(int argc, const char **argv)
472 DOM_SID domain_sid;
473 fstring sid_str;
475 if (!secrets_fetch_domain_sid(global_myname(), &domain_sid)) {
476 d_printf("Could not fetch local SID\n");
477 return 1;
479 sid_to_string(sid_str, &domain_sid);
480 d_printf("SID for domain %s is: %s\n", global_myname(), sid_str);
482 if (!secrets_fetch_domain_sid(opt_workgroup, &domain_sid)) {
483 d_printf("Could not fetch domain SID\n");
484 return 1;
487 sid_to_string(sid_str, &domain_sid);
488 d_printf("SID for domain %s is: %s\n", opt_workgroup, sid_str);
490 return 0;
493 static uint32 get_maxrid(void)
495 SAM_ACCOUNT *pwd = NULL;
496 uint32 max_rid = 0;
497 GROUP_MAP *map = NULL;
498 int num_entries = 0;
499 int i;
501 if (!pdb_setsampwent(False)) {
502 DEBUG(0, ("load_sampwd_entries: Unable to open passdb.\n"));
503 return 0;
506 for (; (NT_STATUS_IS_OK(pdb_init_sam(&pwd)))
507 && pdb_getsampwent(pwd) == True; pwd=NULL) {
508 uint32 rid;
510 if (!sid_peek_rid(pdb_get_user_sid(pwd), &rid)) {
511 DEBUG(0, ("can't get RID for user '%s'\n",
512 pdb_get_username(pwd)));
513 pdb_free_sam(&pwd);
514 continue;
517 if (rid > max_rid)
518 max_rid = rid;
520 DEBUG(1,("%d is user '%s'\n", rid, pdb_get_username(pwd)));
521 pdb_free_sam(&pwd);
524 pdb_endsampwent();
525 pdb_free_sam(&pwd);
527 if (!pdb_enum_group_mapping(SID_NAME_UNKNOWN, &map, &num_entries,
528 ENUM_ONLY_MAPPED, MAPPING_WITHOUT_PRIV))
529 return max_rid;
531 for (i = 0; i < num_entries; i++) {
532 uint32 rid;
534 if (!sid_peek_check_rid(get_global_sam_sid(), &map[i].sid,
535 &rid)) {
536 DEBUG(3, ("skipping map for group '%s', SID %s\n",
537 map[i].nt_name,
538 sid_string_static(&map[i].sid)));
539 continue;
541 DEBUG(1,("%d is group '%s'\n", rid, map[i].nt_name));
543 if (rid > max_rid)
544 max_rid = rid;
547 SAFE_FREE(map);
549 return max_rid;
552 static int net_maxrid(int argc, const char **argv)
554 uint32 rid;
556 if (argc != 0) {
557 DEBUG(0, ("usage: net maxrid\n"));
558 return 1;
561 if ((rid = get_maxrid()) == 0) {
562 DEBUG(0, ("can't get current maximum rid\n"));
563 return 1;
566 d_printf("Currently used maximum rid: %d\n", rid);
568 return 0;
571 /* main function table */
572 static struct functable net_func[] = {
573 {"RPC", net_rpc},
574 {"RAP", net_rap},
575 {"ADS", net_ads},
577 /* eventually these should auto-choose the transport ... */
578 {"FILE", net_file},
579 {"SHARE", net_share},
580 {"SESSION", net_rap_session},
581 {"SERVER", net_rap_server},
582 {"DOMAIN", net_rap_domain},
583 {"PRINTQ", net_rap_printq},
584 {"USER", net_user},
585 {"GROUP", net_group},
586 {"GROUPMAP", net_groupmap},
587 {"VALIDATE", net_rap_validate},
588 {"GROUPMEMBER", net_rap_groupmember},
589 {"ADMIN", net_rap_admin},
590 {"SERVICE", net_rap_service},
591 {"PASSWORD", net_rap_password},
592 {"CHANGETRUSTPW", net_changetrustpw},
593 {"TIME", net_time},
594 {"LOOKUP", net_lookup},
595 {"JOIN", net_join},
596 {"CACHE", net_cache},
597 {"GETLOCALSID", net_getlocalsid},
598 {"SETLOCALSID", net_setlocalsid},
599 {"GETDOMAINSID", net_getdomainsid},
600 {"MAXRID", net_maxrid},
601 {"IDMAP", net_idmap},
603 {"HELP", net_help},
604 {NULL, NULL}
608 /****************************************************************************
609 main program
610 ****************************************************************************/
611 int main(int argc, const char **argv)
613 int opt,i;
614 char *p;
615 int rc = 0;
616 int argc_new = 0;
617 const char ** argv_new;
618 poptContext pc;
620 struct poptOption long_options[] = {
621 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
622 {"workgroup", 'w', POPT_ARG_STRING, &opt_target_workgroup},
623 {"user", 'U', POPT_ARG_STRING, &opt_user_name, 'U'},
624 {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'},
625 {"port", 'p', POPT_ARG_INT, &opt_port},
626 {"myname", 'n', POPT_ARG_STRING, &opt_requester_name},
627 {"server", 'S', POPT_ARG_STRING, &opt_host},
628 {"container", 'c', POPT_ARG_STRING, &opt_container},
629 {"comment", 'C', POPT_ARG_STRING, &opt_comment},
630 {"maxusers", 'M', POPT_ARG_INT, &opt_maxusers},
631 {"flags", 'F', POPT_ARG_INT, &opt_flags},
632 {"long", 'l', POPT_ARG_NONE, &opt_long_list_entries},
633 {"reboot", 'r', POPT_ARG_NONE, &opt_reboot},
634 {"force", 'f', POPT_ARG_NONE, &opt_force},
635 {"timeout", 't', POPT_ARG_INT, &opt_timeout},
636 {"machine-pass",'P', POPT_ARG_NONE, &opt_machine_pass},
637 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
638 POPT_COMMON_SAMBA
639 { 0, 0, 0, 0}
642 zero_ip(&opt_dest_ip);
644 dbf = x_stderr;
646 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
647 POPT_CONTEXT_KEEP_FIRST);
649 while((opt = poptGetNextOpt(pc)) != -1) {
650 switch (opt) {
651 case 'h':
652 net_help(argc, argv);
653 exit(0);
654 break;
655 case 'I':
656 opt_dest_ip = *interpret_addr2(poptGetOptArg(pc));
657 if (is_zero_ip(opt_dest_ip))
658 d_printf("\nInvalid ip address specified\n");
659 else
660 opt_have_ip = True;
661 break;
662 case 'U':
663 opt_user_specified = True;
664 opt_user_name = strdup(opt_user_name);
665 p = strchr(opt_user_name,'%');
666 if (p) {
667 *p = 0;
668 opt_password = p+1;
670 break;
671 default:
672 d_printf("\nInvalid option %s: %s\n",
673 poptBadOption(pc, 0), poptStrerror(opt));
674 net_help(argc, argv);
675 exit(1);
679 lp_load(dyn_CONFIGFILE,True,False,False);
681 argv_new = (const char **)poptGetArgs(pc);
683 argc_new = argc;
684 for (i=0; i<argc; i++) {
685 if (argv_new[i] == NULL) {
686 argc_new = i;
687 break;
691 if (!opt_requester_name) {
692 static fstring myname;
693 get_myname(myname);
694 opt_requester_name = myname;
697 if (!opt_user_name && getenv("LOGNAME")) {
698 opt_user_name = getenv("LOGNAME");
701 if (!opt_workgroup) {
702 opt_workgroup = smb_xstrdup(lp_workgroup());
705 if (!opt_target_workgroup) {
706 opt_target_workgroup = smb_xstrdup(lp_workgroup());
709 if (!init_names())
710 exit(1);
712 load_interfaces();
714 /* this makes sure that when we do things like call scripts,
715 that it won't assert becouse we are not root */
716 sec_init();
718 if (opt_machine_pass) {
719 char *user = NULL;
720 /* it is very useful to be able to make ads queries as the
721 machine account for testing purposes and for domain leave */
723 if (!secrets_init()) {
724 d_printf("ERROR: Unable to open secrets database\n");
725 exit(1);
728 opt_password = secrets_fetch_machine_password(opt_workgroup, NULL, NULL);
730 asprintf(&user,"%s$", global_myname());
731 opt_user_name = user;
732 if (!opt_password) {
733 d_printf("ERROR: Unable to fetch machine password\n");
734 exit(1);
738 if (!opt_password) {
739 opt_password = getenv("PASSWD");
742 rc = net_run_function(argc_new-1, argv_new+1, net_func, net_help);
744 DEBUG(2,("return code = %d\n", rc));
745 return rc;