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 Copyright (C) 2008 Kai Blin (kai@samba.org)
10 Originally written by Steve and Jim. Largely rewritten by tridge in
13 Reworked again by abartlet in December 2001
15 Another overhaul, moving functionality into plug-ins loaded on demand by Kai
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 3 of the License, or
21 (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 /*****************************************************/
33 /* Distributed SMB/CIFS Server Management Utility */
35 /* The intent was to make the syntax similar */
36 /* to the NET utility (first developed in DOS */
37 /* with additional interesting & useful functions */
38 /* added in later SMB server network operating */
41 /*****************************************************/
44 #include "utils/net.h"
46 extern bool AllowDebugChange
;
48 #ifdef WITH_FAKE_KASERVER
49 #include "utils/net_afs.h"
52 /***********************************************************************/
53 /* Beginning of internationalization section. Translatable constants */
54 /* should be kept in this area and referenced in the rest of the code. */
56 /* No functions, outside of Samba or LSB (Linux Standards Base) should */
57 /* be used (if possible). */
58 /***********************************************************************/
60 #define YES_STRING "Yes"
61 #define NO_STRING "No"
63 /***********************************************************************/
64 /* end of internationalization section */
65 /***********************************************************************/
67 uint32
get_sec_channel_type(const char *param
)
69 if (!(param
&& *param
)) {
70 return get_default_sec_channel();
72 if (strequal(param
, "PDC")) {
74 } else if (strequal(param
, "BDC")) {
76 } else if (strequal(param
, "MEMBER")) {
77 return SEC_CHAN_WKSTA
;
79 } else if (strequal(param
, "DOMAIN")) {
80 return SEC_CHAN_DOMAIN
;
83 return get_default_sec_channel();
88 static int net_changetrustpw(struct net_context
*c
, int argc
, const char **argv
)
90 if (net_ads_check_our_domain(c
) == 0)
91 return net_ads_changetrustpw(c
, argc
, argv
);
93 return net_rpc_changetrustpw(c
, argc
, argv
);
96 static void set_line_buffering(FILE *f
)
98 setvbuf(f
, NULL
, _IOLBF
, 0);
101 static int net_changesecretpw(struct net_context
*c
, int argc
,
105 uint32 sec_channel_type
= SEC_CHAN_WKSTA
;
109 set_line_buffering(stdin
);
110 set_line_buffering(stdout
);
111 set_line_buffering(stderr
);
114 trust_pw
= get_pass("Enter machine password: ", c
->opt_stdin
);
116 if (!secrets_store_machine_password(trust_pw
, lp_workgroup(), sec_channel_type
)) {
117 d_fprintf(stderr
, "Unable to write the machine account password in the secrets database");
121 d_printf("Modified trust account password in secrets database\n");
125 d_printf("Machine account password change requires the -f flag.\n");
126 d_printf("Do NOT use this function unless you know what it does!\n");
127 d_printf("This function will change the ADS Domain member machine account password in the secrets.tdb file!\n");
134 Retrieve our local SID or the SID for the specified name
136 static int net_getlocalsid(struct net_context
*c
, int argc
, const char **argv
)
146 name
= global_myname();
149 if(!initialize_password_db(false, NULL
)) {
150 DEBUG(0, ("WARNING: Could not open passdb - local sid may not reflect passdb\n"
151 "backend knowledge (such as the sid stored in LDAP)\n"));
154 /* first check to see if we can even access secrets, so we don't
155 panic when we can't. */
157 if (!secrets_init()) {
158 d_fprintf(stderr
, "Unable to open secrets.tdb. Can't fetch domain SID for name: %s\n", name
);
162 /* Generate one, if it doesn't exist */
163 get_global_sam_sid();
165 if (!secrets_fetch_domain_sid(name
, &sid
)) {
166 DEBUG(0, ("Can't fetch domain SID for name: %s\n", name
));
169 sid_to_fstring(sid_str
, &sid
);
170 d_printf("SID for domain %s is: %s\n", name
, sid_str
);
174 static int net_setlocalsid(struct net_context
*c
, int argc
, const char **argv
)
179 || (strncmp(argv
[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
180 || (!string_to_sid(&sid
, argv
[0]))
181 || (sid
.num_auths
!= 4)) {
182 d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n");
186 if (!secrets_store_domain_sid(global_myname(), &sid
)) {
187 DEBUG(0,("Can't store domain SID as a pdc/bdc.\n"));
194 static int net_setdomainsid(struct net_context
*c
, int argc
, const char **argv
)
199 || (strncmp(argv
[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
200 || (!string_to_sid(&sid
, argv
[0]))
201 || (sid
.num_auths
!= 4)) {
202 d_printf("usage: net setdomainsid S-1-5-21-x-y-z\n");
206 if (!secrets_store_domain_sid(lp_workgroup(), &sid
)) {
207 DEBUG(0,("Can't store domain SID.\n"));
214 static int net_getdomainsid(struct net_context
*c
, int argc
, const char **argv
)
220 d_printf("usage: net getdomainsid\n");
224 if(!initialize_password_db(false, NULL
)) {
225 DEBUG(0, ("WARNING: Could not open passdb - domain SID may "
226 "not reflect passdb\n"
227 "backend knowledge (such as the SID stored in "
231 /* first check to see if we can even access secrets, so we don't
232 panic when we can't. */
234 if (!secrets_init()) {
235 d_fprintf(stderr
, "Unable to open secrets.tdb. Can't fetch domain"
236 "SID for name: %s\n", get_global_sam_name());
240 /* Generate one, if it doesn't exist */
241 get_global_sam_sid();
243 if (!secrets_fetch_domain_sid(global_myname(), &domain_sid
)) {
244 d_fprintf(stderr
, "Could not fetch local SID\n");
247 sid_to_fstring(sid_str
, &domain_sid
);
248 d_printf("SID for local machine %s is: %s\n", global_myname(), sid_str
);
250 if (!secrets_fetch_domain_sid(c
->opt_workgroup
, &domain_sid
)) {
251 d_fprintf(stderr
, "Could not fetch domain SID\n");
255 sid_to_fstring(sid_str
, &domain_sid
);
256 d_printf("SID for domain %s is: %s\n", c
->opt_workgroup
, sid_str
);
261 static bool search_maxrid(struct pdb_search
*search
, const char *type
,
264 struct samr_displayentry
*entries
;
265 uint32 i
, num_entries
;
267 if (search
== NULL
) {
268 d_fprintf(stderr
, "get_maxrid: Could not search %s\n", type
);
272 num_entries
= pdb_search_entries(search
, 0, 0xffffffff, &entries
);
273 for (i
=0; i
<num_entries
; i
++)
274 *max_rid
= MAX(*max_rid
, entries
[i
].rid
);
279 static uint32
get_maxrid(void)
283 if (!search_maxrid(pdb_search_users(talloc_tos(), 0), "users", &max_rid
))
286 if (!search_maxrid(pdb_search_groups(talloc_tos()), "groups", &max_rid
))
289 if (!search_maxrid(pdb_search_aliases(talloc_tos(),
290 get_global_sam_sid()),
291 "aliases", &max_rid
))
297 static int net_maxrid(struct net_context
*c
, int argc
, const char **argv
)
302 DEBUG(0, ("usage: net maxrid\n"));
306 if ((rid
= get_maxrid()) == 0) {
307 DEBUG(0, ("can't get current maximum rid\n"));
311 d_printf("Currently used maximum rid: %d\n", rid
);
316 /* main function table */
317 static struct functable net_func
[] = {
322 "Run functions using RPC transport",
323 " Use 'net help rpc' to get more extensive information about "
324 "'net rpc' commands."
330 "Run functions using RAP transport",
331 " Use 'net help rap' to get more extensive information about "
332 "'net rap' commands."
338 "Run functions using ADS transport",
339 " Use 'net help ads' to get more extensive information about "
340 "'net ads' commands."
343 /* eventually these should auto-choose the transport ... */
347 NET_TRANSPORT_RPC
| NET_TRANSPORT_RAP
,
348 "Functions on remote opened files",
349 " Use 'net help file' to get more information about 'net "
355 NET_TRANSPORT_RPC
| NET_TRANSPORT_RAP
,
356 "Functions on shares",
357 " Use 'net help share' to get more information about 'net "
365 " Use 'net help session' to get more information about 'net "
372 "List servers in workgroup",
373 " Use 'net help server' to get more information about 'net "
380 "List domains/workgroups on network",
381 " Use 'net help domain' to get more information about 'net "
388 "Modify printer queue",
389 " Use 'net help printq' to get more information about 'net "
395 NET_TRANSPORT_ADS
| NET_TRANSPORT_RPC
| NET_TRANSPORT_RAP
,
397 " Use 'net help user' to get more information about 'net "
403 NET_TRANSPORT_ADS
| NET_TRANSPORT_RPC
| NET_TRANSPORT_RAP
,
405 " Use 'net help group' to get more information about 'net "
412 "Manage group mappings",
413 " Use 'net help groupmap' to get more information about 'net "
414 "groupmap' commands."
420 "Functions on the SAM database",
421 " Use 'net help sam' to get more information about 'net sam' "
428 "Validate username and password",
429 " Use 'net help validate' to get more information about 'net "
430 "validate' commands."
436 "Modify group memberships",
437 " Use 'net help groupmember' to get more information about "
438 "'net groupmember' commands."
443 "Execute remote command on a remote OS/2 server",
444 " Use 'net help admin' to get more information about 'net "
450 "List/modify running services",
451 " Use 'net help service' to get more information about 'net "
458 "Change user password on target server",
459 " Use 'net help password' to get more information about 'net "
460 "password' commands."
464 NET_TRANSPORT_ADS
| NET_TRANSPORT_RPC
,
465 "Change the trust password",
466 " Use 'net help changetrustpw' to get more information about "
467 "'net changetrustpw'."
472 "Change the secret password",
473 " net [options] changesecretpw\n"
474 " Change the ADS domain member machine account password in "
476 " Do NOT use this function unless you know what it does.\n"
477 " Requires the -f flag to work."
483 " Use 'net help time' to get more information about 'net "
489 "Look up host names/IP addresses",
490 " Use 'net help lookup' to get more information about 'net "
495 NET_TRANSPORT_ADS
| NET_TRANSPORT_RPC
,
497 " Use 'net help join' to get more information about 'net "
503 "Join/unjoin (remote) machines to/from a domain/AD",
504 " Use 'net help dom' to get more information about 'net dom' "
510 "Operate on the cache tdb file",
511 " Use 'net help cache' to get more information about 'net "
517 "Get the SID for the local domain",
523 "Set the SID for the local domain",
524 " net setlocalsid S-1-5-21-x-y-z"
529 "Set domain SID on member servers",
530 " net setdomainsid S-1-5-21-x-y-z"
535 "Get domain SID on member servers",
541 "Display the maximul RID currently used",
548 " Use 'net help idmap to get more information about 'net "
554 "Display server status",
555 " Use 'net help status' to get more information about 'net "
561 "Manage user-modifiable shares",
562 " Use 'net help usershare to get more information about 'net "
563 "usershare' commands."
568 "Display list of all users with SID",
569 " Use 'net help usersidlist' to get more information about "
575 "Manage Samba registry based configuration",
576 " Use 'net help conf' to get more information about 'net "
582 "Manage the Samba registry",
583 " Use 'net help registry' to get more information about 'net "
584 "registry' commands."
589 "Open a lua interpreter",
590 " Use 'net help lua' to get more information about 'net "
596 "Dump Win32 *.evt eventlog files",
597 " Use 'net help eventlog' to get more information about 'net "
598 "eventlog' commands."
601 #ifdef WITH_FAKE_KASERVER
606 " Use 'net help afs' to get more information about 'net afs' "
614 "Print usage information",
615 " Use 'net help help' to list usage information for 'net' "
618 {NULL
, NULL
, 0, NULL
, NULL
}
622 /****************************************************************************
624 ****************************************************************************/
625 int main(int argc
, const char **argv
)
631 const char ** argv_new
;
633 TALLOC_CTX
*frame
= talloc_stackframe();
634 struct net_context
*c
= talloc_zero(frame
, struct net_context
);
636 struct poptOption long_options
[] = {
637 {"help", 'h', POPT_ARG_NONE
, 0, 'h'},
638 {"workgroup", 'w', POPT_ARG_STRING
, &c
->opt_target_workgroup
},
639 {"user", 'U', POPT_ARG_STRING
, &c
->opt_user_name
, 'U'},
640 {"ipaddress", 'I', POPT_ARG_STRING
, 0,'I'},
641 {"port", 'p', POPT_ARG_INT
, &c
->opt_port
},
642 {"myname", 'n', POPT_ARG_STRING
, &c
->opt_requester_name
},
643 {"server", 'S', POPT_ARG_STRING
, &c
->opt_host
},
644 {"encrypt", 'e', POPT_ARG_NONE
, NULL
, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
645 {"container", 'c', POPT_ARG_STRING
, &c
->opt_container
},
646 {"comment", 'C', POPT_ARG_STRING
, &c
->opt_comment
},
647 {"maxusers", 'M', POPT_ARG_INT
, &c
->opt_maxusers
},
648 {"flags", 'F', POPT_ARG_INT
, &c
->opt_flags
},
649 {"long", 'l', POPT_ARG_NONE
, &c
->opt_long_list_entries
},
650 {"reboot", 'r', POPT_ARG_NONE
, &c
->opt_reboot
},
651 {"force", 'f', POPT_ARG_NONE
, &c
->opt_force
},
652 {"stdin", 'i', POPT_ARG_NONE
, &c
->opt_stdin
},
653 {"timeout", 't', POPT_ARG_INT
, &c
->opt_timeout
},
654 {"machine-pass",'P', POPT_ARG_NONE
, &c
->opt_machine_pass
},
655 {"kerberos", 'k', POPT_ARG_NONE
, &c
->opt_kerberos
},
656 {"myworkgroup", 'W', POPT_ARG_STRING
, &c
->opt_workgroup
},
657 {"verbose", 'v', POPT_ARG_NONE
, &c
->opt_verbose
},
658 {"test", 'T', POPT_ARG_NONE
, &c
->opt_testmode
},
659 /* Options for 'net groupmap set' */
660 {"local", 'L', POPT_ARG_NONE
, &c
->opt_localgroup
},
661 {"domain", 'D', POPT_ARG_NONE
, &c
->opt_domaingroup
},
662 {"ntname", 'N', POPT_ARG_STRING
, &c
->opt_newntname
},
663 {"rid", 'R', POPT_ARG_INT
, &c
->opt_rid
},
664 /* Options for 'net rpc share migrate' */
665 {"acls", 0, POPT_ARG_NONE
, &c
->opt_acls
},
666 {"attrs", 0, POPT_ARG_NONE
, &c
->opt_attrs
},
667 {"timestamps", 0, POPT_ARG_NONE
, &c
->opt_timestamps
},
668 {"exclude", 'X', POPT_ARG_STRING
, &c
->opt_exclude
},
669 {"destination", 0, POPT_ARG_STRING
, &c
->opt_destination
},
670 {"tallocreport", 0, POPT_ARG_NONE
, &c
->do_talloc_report
},
671 /* Options for 'net rpc vampire (keytab)' */
672 {"force-full-repl", 0, POPT_ARG_NONE
, &c
->opt_force_full_repl
},
673 {"single-obj-repl", 0, POPT_ARG_NONE
, &c
->opt_single_obj_repl
},
674 {"clean-old-entries", 0, POPT_ARG_NONE
, &c
->opt_clean_old_entries
},
681 zero_sockaddr(&c
->opt_dest_ip
);
685 /* set default debug level to 0 regardless of what smb.conf sets */
686 DEBUGLEVEL_CLASS
[DBGC_ALL
] = 0;
688 c
->private_data
= net_func
;
690 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
,
691 POPT_CONTEXT_KEEP_FIRST
);
693 while((opt
= poptGetNextOpt(pc
)) != -1) {
696 c
->display_usage
= true;
699 c
->smb_encrypt
= true;
702 if (!interpret_string_addr(&c
->opt_dest_ip
,
703 poptGetOptArg(pc
), 0)) {
704 d_fprintf(stderr
, "\nInvalid ip address specified\n");
706 c
->opt_have_ip
= true;
710 c
->opt_user_specified
= true;
711 c
->opt_user_name
= SMB_STRDUP(c
->opt_user_name
);
712 p
= strchr(c
->opt_user_name
,'%');
715 c
->opt_password
= p
+1;
719 d_fprintf(stderr
, "\nInvalid option %s: %s\n",
720 poptBadOption(pc
, 0), poptStrerror(opt
));
721 net_help(c
, argc
, argv
);
727 * Don't load debug level from smb.conf. It should be
728 * set by cmdline arg or remain default (0)
730 AllowDebugChange
= false;
731 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
733 argv_new
= (const char **)poptGetArgs(pc
);
736 for (i
=0; i
<argc
; i
++) {
737 if (argv_new
[i
] == NULL
) {
743 if (c
->do_talloc_report
) {
744 talloc_enable_leak_report();
747 if (c
->opt_requester_name
) {
748 set_global_myname(c
->opt_requester_name
);
751 if (!c
->opt_user_name
&& getenv("LOGNAME")) {
752 c
->opt_user_name
= getenv("LOGNAME");
755 if (!c
->opt_workgroup
) {
756 c
->opt_workgroup
= smb_xstrdup(lp_workgroup());
759 if (!c
->opt_target_workgroup
) {
760 c
->opt_target_workgroup
= smb_xstrdup(lp_workgroup());
768 /* this makes sure that when we do things like call scripts,
769 that it won't assert becouse we are not root */
772 if (c
->opt_machine_pass
) {
773 /* it is very useful to be able to make ads queries as the
774 machine account for testing purposes and for domain leave */
776 net_use_krb_machine_account(c
);
779 if (!c
->opt_password
) {
780 c
->opt_password
= getenv("PASSWD");
783 rc
= net_run_function(c
, argc_new
-1, argv_new
+1, "net", net_func
);
785 DEBUG(2,("return code = %d\n", rc
));
787 libnetapi_free(c
->netapi_ctx
);