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"
47 extern bool AllowDebugChange;
49 #ifdef WITH_FAKE_KASERVER
50 #include "utils/net_afs.h"
53 /***********************************************************************/
54 /* end of internationalization section */
55 /***********************************************************************/
57 enum netr_SchannelType get_sec_channel_type(const char *param)
59 if (!(param && *param)) {
60 return get_default_sec_channel();
62 if (strequal(param, "PDC")) {
64 } else if (strequal(param, "BDC")) {
66 } else if (strequal(param, "MEMBER")) {
67 return SEC_CHAN_WKSTA;
69 } else if (strequal(param, "DOMAIN")) {
70 return SEC_CHAN_DOMAIN;
73 return get_default_sec_channel();
78 static int net_changetrustpw(struct net_context *c, int argc, const char **argv)
80 if (net_ads_check_our_domain(c) == 0)
81 return net_ads_changetrustpw(c, argc, argv);
83 return net_rpc_changetrustpw(c, argc, argv);
86 static void set_line_buffering(FILE *f)
88 setvbuf(f, NULL, _IOLBF, 0);
91 static int net_changesecretpw(struct net_context *c, int argc,
95 enum netr_SchannelType sec_channel_type = SEC_CHAN_WKSTA;
99 set_line_buffering(stdin);
100 set_line_buffering(stdout);
101 set_line_buffering(stderr);
104 trust_pw = get_pass(_("Enter machine password: "), c->opt_stdin);
106 if (!secrets_store_machine_password(trust_pw, lp_workgroup(), sec_channel_type)) {
108 _("Unable to write the machine account password in the secrets database"));
112 d_printf(_("Modified trust account password in secrets database\n"));
116 d_printf(_("Machine account password change requires the -f flag.\n"
117 "Do NOT use this function unless you know what it does!\n"
118 "This function will change the ADS Domain member "
119 "machine account password in the secrets.tdb file!\n"));
126 * @brief Set the authorised user for winbindd access in secrets.tdb
128 static int net_setauthuser(struct net_context *c, int argc, const char **argv)
130 const char *password = NULL;
132 if (!secrets_init()) {
133 d_fprintf(stderr, _("Failed to open secrets.tdb.\n"));
137 /* Delete the settings. */
139 if (strncmp(argv[0], "delete", 6) != 0) {
140 d_fprintf(stderr,_("Usage:\n"));
142 _(" net setauthuser -U user[%%password] \n"
143 " Set the auth user account to user"
144 "password. Prompt for password if not "
147 _(" net setauthuser delete\n"
148 " Delete the auth user setting.\n"));
151 secrets_delete(SECRETS_AUTH_USER);
152 secrets_delete(SECRETS_AUTH_DOMAIN);
153 secrets_delete(SECRETS_AUTH_PASSWORD);
157 if (!c->opt_user_specified) {
158 d_fprintf(stderr, _("Usage:\n"));
160 _(" net setauthuser -U user[%%password]\n"
161 " Set the auth user account to user"
162 "password. Prompt for password if not "
165 _(" net setauthuser delete\n"
166 " Delete the auth user setting.\n"));
170 password = net_prompt_pass(c, _("the auth user"));
171 if (password == NULL) {
172 d_fprintf(stderr,_("Failed to get the auth users password.\n"));
176 if (!secrets_store(SECRETS_AUTH_USER, c->opt_user_name,
177 strlen(c->opt_user_name) + 1)) {
178 d_fprintf(stderr, _("error storing auth user name\n"));
182 if (!secrets_store(SECRETS_AUTH_DOMAIN, c->opt_workgroup,
183 strlen(c->opt_workgroup) + 1)) {
184 d_fprintf(stderr, _("error storing auth user domain\n"));
188 if (!secrets_store(SECRETS_AUTH_PASSWORD, password,
189 strlen(password) + 1)) {
190 d_fprintf(stderr, _("error storing auth user password\n"));
198 * @brief Get the auth user settings
200 static int net_getauthuser(struct net_context *c, int argc, const char **argv)
202 char *user, *domain, *password;
204 /* Lift data from secrets file */
206 secrets_fetch_ipc_userpass(&user, &domain, &password);
208 if ((!user || !*user) && (!domain || !*domain ) &&
209 (!password || !*password)){
214 d_printf(_("No authorised user configured\n"));
218 /* Pretty print authorised user info */
220 d_printf("%s%s%s%s%s\n", domain ? domain : "",
221 domain ? lp_winbind_separator(): "", user,
222 password ? "%" : "", password ? password : "");
231 Retrieve our local SID or the SID for the specified name
233 static int net_getlocalsid(struct net_context *c, int argc, const char **argv)
243 name = global_myname();
246 if(!initialize_password_db(false, NULL)) {
247 DEBUG(0, ("WARNING: Could not open passdb - local sid may not reflect passdb\n"
248 "backend knowledge (such as the sid stored in LDAP)\n"));
251 /* first check to see if we can even access secrets, so we don't
252 panic when we can't. */
254 if (!secrets_init()) {
256 _("Unable to open secrets.tdb. Can't fetch domain "
257 "SID for name: %s\n"), name);
261 /* Generate one, if it doesn't exist */
262 get_global_sam_sid();
264 if (!secrets_fetch_domain_sid(name, &sid)) {
265 DEBUG(0, ("Can't fetch domain SID for name: %s\n", name));
268 sid_to_fstring(sid_str, &sid);
269 d_printf(_("SID for domain %s is: %s\n"), name, sid_str);
273 static int net_setlocalsid(struct net_context *c, int argc, const char **argv)
278 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
279 || (!string_to_sid(&sid, argv[0]))
280 || (sid.num_auths != 4)) {
281 d_printf(_("Usage:"));
282 d_printf(" net setlocalsid S-1-5-21-x-y-z\n");
286 if (!secrets_store_domain_sid(global_myname(), &sid)) {
287 DEBUG(0,("Can't store domain SID as a pdc/bdc.\n"));
294 static int net_setdomainsid(struct net_context *c, int argc, const char **argv)
299 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
300 || (!string_to_sid(&sid, argv[0]))
301 || (sid.num_auths != 4)) {
302 d_printf(_("Usage:"));
303 d_printf(" net setdomainsid S-1-5-21-x-y-z\n");
307 if (!secrets_store_domain_sid(lp_workgroup(), &sid)) {
308 DEBUG(0,("Can't store domain SID.\n"));
315 static int net_getdomainsid(struct net_context *c, int argc, const char **argv)
317 struct dom_sid domain_sid;
321 d_printf(_("Usage:"));
322 d_printf(" net getdomainsid\n");
326 if(!initialize_password_db(false, NULL)) {
327 DEBUG(0, ("WARNING: Could not open passdb - domain SID may "
328 "not reflect passdb\n"
329 "backend knowledge (such as the SID stored in "
333 /* first check to see if we can even access secrets, so we don't
334 panic when we can't. */
336 if (!secrets_init()) {
337 d_fprintf(stderr, _("Unable to open secrets.tdb. Can't fetch "
338 "domain SID for name: %s\n"),
339 get_global_sam_name());
343 /* Generate one, if it doesn't exist */
344 get_global_sam_sid();
346 if (!secrets_fetch_domain_sid(global_myname(), &domain_sid)) {
347 d_fprintf(stderr, _("Could not fetch local SID\n"));
350 sid_to_fstring(sid_str, &domain_sid);
351 d_printf(_("SID for local machine %s is: %s\n"),
352 global_myname(), sid_str);
354 if (!secrets_fetch_domain_sid(c->opt_workgroup, &domain_sid)) {
355 d_fprintf(stderr, _("Could not fetch domain SID\n"));
359 sid_to_fstring(sid_str, &domain_sid);
360 d_printf(_("SID for domain %s is: %s\n"), c->opt_workgroup, sid_str);
365 static bool search_maxrid(struct pdb_search *search, const char *type,
368 struct samr_displayentry *entries;
369 uint32 i, num_entries;
371 if (search == NULL) {
372 d_fprintf(stderr, _("get_maxrid: Could not search %s\n"), type);
376 num_entries = pdb_search_entries(search, 0, 0xffffffff, &entries);
377 for (i=0; i<num_entries; i++)
378 *max_rid = MAX(*max_rid, entries[i].rid);
383 static uint32 get_maxrid(void)
387 if (!search_maxrid(pdb_search_users(talloc_tos(), 0), "users", &max_rid))
390 if (!search_maxrid(pdb_search_groups(talloc_tos()), "groups", &max_rid))
393 if (!search_maxrid(pdb_search_aliases(talloc_tos(),
394 get_global_sam_sid()),
395 "aliases", &max_rid))
401 static int net_maxrid(struct net_context *c, int argc, const char **argv)
406 d_fprintf(stderr, "%s net maxrid\n", _("Usage:"));
410 if ((rid = get_maxrid()) == 0) {
411 d_fprintf(stderr, _("can't get current maximum rid\n"));
415 d_printf(_("Currently used maximum rid: %d\n"), rid);
420 /* main function table */
421 static struct functable net_func[] = {
426 N_("Run functions using RPC transport"),
427 N_(" Use 'net help rpc' to get more extensive information "
428 "about 'net rpc' commands.")
434 N_("Run functions using RAP transport"),
435 N_(" Use 'net help rap' to get more extensive information "
436 "about 'net rap' commands.")
442 N_("Run functions using ADS transport"),
443 N_(" Use 'net help ads' to get more extensive information "
444 "about 'net ads' commands.")
447 /* eventually these should auto-choose the transport ... */
451 NET_TRANSPORT_RPC | NET_TRANSPORT_RAP,
452 N_("Functions on remote opened files"),
453 N_(" Use 'net help file' to get more information about 'net "
459 NET_TRANSPORT_RPC | NET_TRANSPORT_RAP,
460 N_("Functions on shares"),
461 N_(" Use 'net help share' to get more information about 'net "
468 N_("Manage sessions"),
469 N_(" Use 'net help session' to get more information about "
470 "'net session' commands.")
476 N_("List servers in workgroup"),
477 N_(" Use 'net help server' to get more information about 'net "
484 N_("List domains/workgroups on network"),
485 N_(" Use 'net help domain' to get more information about 'net "
492 N_("Modify printer queue"),
493 N_(" Use 'net help printq' to get more information about 'net "
499 NET_TRANSPORT_ADS | NET_TRANSPORT_RPC | NET_TRANSPORT_RAP,
501 N_(" Use 'net help user' to get more information about 'net "
507 NET_TRANSPORT_ADS | NET_TRANSPORT_RPC | NET_TRANSPORT_RAP,
509 N_(" Use 'net help group' to get more information about 'net "
516 N_("Manage group mappings"),
517 N_(" Use 'net help groupmap' to get more information about "
518 "'net groupmap' commands.")
524 N_("Functions on the SAM database"),
525 N_(" Use 'net help sam' to get more information about 'net "
532 N_("Validate username and password"),
533 N_(" Use 'net help validate' to get more information about "
534 "'net validate' commands.")
540 N_("Modify group memberships"),
541 N_(" Use 'net help groupmember' to get more information about "
542 "'net groupmember' commands.")
547 N_("Execute remote command on a remote OS/2 server"),
548 N_(" Use 'net help admin' to get more information about 'net "
554 N_("List/modify running services"),
555 N_(" Use 'net help service' to get more information about "
556 "'net service' commands.")
562 N_("Change user password on target server"),
563 N_(" Use 'net help password' to get more information about "
564 "'net password' commands.")
568 NET_TRANSPORT_ADS | NET_TRANSPORT_RPC,
569 N_("Change the trust password"),
570 N_(" Use 'net help changetrustpw' to get more information "
571 "about 'net changetrustpw'.")
576 N_("Change the secret password"),
577 N_(" net [options] changesecretpw\n"
578 " Change the ADS domain member machine account password "
580 " Do NOT use this function unless you know what it does.\n"
581 " Requires the -f flag to work.")
587 N_("Set the winbind auth user"),
588 N_(" net -U user[%%password] [-W domain] setauthuser\n"
589 " Set the auth user, password (and optionally domain\n"
590 " Will prompt for password if not given.\n"
591 " net setauthuser delete\n"
592 " Delete the existing auth user settings.")
598 N_("Get the winbind auth user settings"),
599 N_(" net getauthuser\n"
600 " Get the current winbind auth user settings.")
606 N_(" Use 'net help time' to get more information about 'net "
612 N_("Look up host names/IP addresses"),
613 N_(" Use 'net help lookup' to get more information about 'net "
619 N_("Manipulate the global lock table"),
620 N_(" Use 'net help g_lock' to get more information about "
621 "'net g_lock' commands.")
625 NET_TRANSPORT_ADS | NET_TRANSPORT_RPC,
626 N_("Join a domain/AD"),
627 N_(" Use 'net help join' to get more information about 'net "
633 N_("Join/unjoin (remote) machines to/from a domain/AD"),
634 N_(" Use 'net help dom' to get more information about 'net "
640 N_("Operate on the cache tdb file"),
641 N_(" Use 'net help cache' to get more information about 'net "
647 N_("Get the SID for the local domain"),
648 N_(" net getlocalsid")
653 N_("Set the SID for the local domain"),
654 N_(" net setlocalsid S-1-5-21-x-y-z")
659 N_("Set domain SID on member servers"),
660 N_(" net setdomainsid S-1-5-21-x-y-z")
665 N_("Get domain SID on member servers"),
666 N_(" net getdomainsid")
671 N_("Display the maximul RID currently used"),
677 N_("IDmap functions"),
678 N_(" Use 'net help idmap to get more information about 'net "
684 N_("Display server status"),
685 N_(" Use 'net help status' to get more information about 'net "
691 N_("Manage user-modifiable shares"),
692 N_(" Use 'net help usershare to get more information about "
693 "'net usershare' commands.")
698 N_("Display list of all users with SID"),
699 N_(" Use 'net help usersidlist' to get more information about "
700 "'net usersidlist'.")
705 N_("Manage Samba registry based configuration"),
706 N_(" Use 'net help conf' to get more information about 'net "
712 N_("Manage the Samba registry"),
713 N_(" Use 'net help registry' to get more information about "
714 "'net registry' commands.")
719 N_("Process Win32 *.evt eventlog files"),
720 N_(" Use 'net help eventlog' to get more information about "
721 "'net eventlog' commands.")
726 N_("Process tdb printer files"),
727 N_(" Use 'net help printing' to get more information about "
728 "'net printing' commands.")
734 N_("Manage the serverid tdb"),
735 N_(" Use 'net help serverid' to get more information about "
736 "'net serverid' commands.")
739 #ifdef WITH_FAKE_KASERVER
743 N_("Manage AFS tokens"),
744 N_(" Use 'net help afs' to get more information about 'net "
752 N_("Print usage information"),
753 N_(" Use 'net help help' to list usage information for 'net' "
756 {NULL, NULL, 0, NULL, NULL}
760 /****************************************************************************
762 ****************************************************************************/
763 int main(int argc, const char **argv)
769 const char ** argv_new;
771 TALLOC_CTX *frame = talloc_stackframe();
772 struct net_context *c = talloc_zero(frame, struct net_context);
774 struct poptOption long_options[] = {
775 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
776 {"workgroup", 'w', POPT_ARG_STRING, &c->opt_target_workgroup},
777 {"user", 'U', POPT_ARG_STRING, &c->opt_user_name, 'U'},
778 {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'},
779 {"port", 'p', POPT_ARG_INT, &c->opt_port},
780 {"myname", 'n', POPT_ARG_STRING, &c->opt_requester_name},
781 {"server", 'S', POPT_ARG_STRING, &c->opt_host},
782 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', N_("Encrypt SMB transport (UNIX extended servers only)") },
783 {"container", 'c', POPT_ARG_STRING, &c->opt_container},
784 {"comment", 'C', POPT_ARG_STRING, &c->opt_comment},
785 {"maxusers", 'M', POPT_ARG_INT, &c->opt_maxusers},
786 {"flags", 'F', POPT_ARG_INT, &c->opt_flags},
787 {"long", 'l', POPT_ARG_NONE, &c->opt_long_list_entries},
788 {"reboot", 'r', POPT_ARG_NONE, &c->opt_reboot},
789 {"force", 'f', POPT_ARG_NONE, &c->opt_force},
790 {"stdin", 'i', POPT_ARG_NONE, &c->opt_stdin},
791 {"timeout", 't', POPT_ARG_INT, &c->opt_timeout},
792 {"request-timeout",0,POPT_ARG_INT, &c->opt_request_timeout},
793 {"machine-pass",'P', POPT_ARG_NONE, &c->opt_machine_pass},
794 {"kerberos", 'k', POPT_ARG_NONE, &c->opt_kerberos},
795 {"myworkgroup", 'W', POPT_ARG_STRING, &c->opt_workgroup},
796 {"use-ccache", 0, POPT_ARG_NONE, &c->opt_ccache},
797 {"verbose", 'v', POPT_ARG_NONE, &c->opt_verbose},
798 {"test", 'T', POPT_ARG_NONE, &c->opt_testmode},
799 /* Options for 'net groupmap set' */
800 {"local", 'L', POPT_ARG_NONE, &c->opt_localgroup},
801 {"domain", 'D', POPT_ARG_NONE, &c->opt_domaingroup},
802 {"ntname", 'N', POPT_ARG_STRING, &c->opt_newntname},
803 {"rid", 'R', POPT_ARG_INT, &c->opt_rid},
804 /* Options for 'net rpc share migrate' */
805 {"acls", 0, POPT_ARG_NONE, &c->opt_acls},
806 {"attrs", 0, POPT_ARG_NONE, &c->opt_attrs},
807 {"timestamps", 0, POPT_ARG_NONE, &c->opt_timestamps},
808 {"exclude", 'X', POPT_ARG_STRING, &c->opt_exclude},
809 {"destination", 0, POPT_ARG_STRING, &c->opt_destination},
810 {"tallocreport", 0, POPT_ARG_NONE, &c->do_talloc_report},
811 /* Options for 'net rpc vampire (keytab)' */
812 {"force-full-repl", 0, POPT_ARG_NONE, &c->opt_force_full_repl},
813 {"single-obj-repl", 0, POPT_ARG_NONE, &c->opt_single_obj_repl},
814 {"clean-old-entries", 0, POPT_ARG_NONE, &c->opt_clean_old_entries},
820 zero_sockaddr(&c->opt_dest_ip);
824 setlocale(LC_ALL, "");
825 #if defined(HAVE_BINDTEXTDOMAIN)
826 bindtextdomain(MODULE_NAME, dyn_LOCALEDIR);
828 #if defined(HAVE_TEXTDOMAIN)
829 textdomain(MODULE_NAME);
832 /* set default debug level to 0 regardless of what smb.conf sets */
833 DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
835 c->private_data = net_func;
837 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
838 POPT_CONTEXT_KEEP_FIRST);
840 while((opt = poptGetNextOpt(pc)) != -1) {
843 c->display_usage = true;
846 c->smb_encrypt = true;
849 if (!interpret_string_addr(&c->opt_dest_ip,
850 poptGetOptArg(pc), 0)) {
851 d_fprintf(stderr, _("\nInvalid ip address specified\n"));
853 c->opt_have_ip = true;
857 c->opt_user_specified = true;
858 c->opt_user_name = SMB_STRDUP(c->opt_user_name);
859 p = strchr(c->opt_user_name,'%');
862 c->opt_password = p+1;
866 d_fprintf(stderr, _("\nInvalid option %s: %s\n"),
867 poptBadOption(pc, 0), poptStrerror(opt));
868 net_help(c, argc, argv);
874 * Don't load debug level from smb.conf. It should be
875 * set by cmdline arg or remain default (0)
877 AllowDebugChange = false;
878 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
880 argv_new = (const char **)poptGetArgs(pc);
883 for (i=0; i<argc; i++) {
884 if (argv_new[i] == NULL) {
890 if (c->do_talloc_report) {
891 talloc_enable_leak_report();
894 if (c->opt_requester_name) {
895 set_global_myname(c->opt_requester_name);
898 if (!c->opt_user_name && getenv("LOGNAME")) {
899 c->opt_user_name = getenv("LOGNAME");
902 if (!c->opt_workgroup) {
903 c->opt_workgroup = smb_xstrdup(lp_workgroup());
906 if (!c->opt_target_workgroup) {
907 c->opt_target_workgroup = smb_xstrdup(lp_workgroup());
915 /* this makes sure that when we do things like call scripts,
916 that it won't assert because we are not root */
919 if (c->opt_machine_pass) {
920 /* it is very useful to be able to make ads queries as the
921 machine account for testing purposes and for domain leave */
923 net_use_krb_machine_account(c);
926 if (!c->opt_password) {
927 c->opt_password = getenv("PASSWD");
930 rc = net_run_function(c, argc_new-1, argv_new+1, "net", net_func);
932 DEBUG(2,("return code = %d\n", rc));
934 gencache_stabilize();
936 libnetapi_free(c->netapi_ctx);