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 /***********************************************************************/
47 /* Beginning of internationalization section. Translatable constants */
48 /* should be kept in this area and referenced in the rest of the code. */
50 /* No functions, outside of Samba or LSB (Linux Standards Base) should */
51 /* be used (if possible). */
52 /***********************************************************************/
54 #define YES_STRING "Yes"
55 #define NO_STRING "No"
57 /***********************************************************************/
58 /* end of internationalization section */
59 /***********************************************************************/
61 uint32 get_sec_channel_type(const char *param)
63 if (!(param && *param)) {
64 return get_default_sec_channel();
66 if (strequal(param, "PDC")) {
68 } else if (strequal(param, "BDC")) {
70 } else if (strequal(param, "MEMBER")) {
71 return SEC_CHAN_WKSTA;
73 } else if (strequal(param, "DOMAIN")) {
74 return SEC_CHAN_DOMAIN;
77 return get_default_sec_channel();
83 run a function from a function table. If not found then
84 call the specified usage function
86 int net_run_function(struct net_context *c, int argc, const char **argv,
87 struct functable *table,
88 int (*usage_fn)(struct net_context *c,
89 int argc, const char **argv))
94 d_printf("\nUsage: \n");
95 return usage_fn(c, argc, argv);
97 for (i=0; table[i].funcname; i++) {
98 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
99 return table[i].fn(c, argc-1, argv+1);
101 d_fprintf(stderr, "No command: %s\n", argv[0]);
102 return usage_fn(c, argc, argv);
106 * run a function from a function table.
108 int net_run_function2(struct net_context *c, int argc, const char **argv,
109 const char *whoami, struct functable2 *table)
114 for (i=0; table[i].funcname; i++) {
115 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
116 return table[i].fn(c, argc-1, argv+1);
120 for (i=0; table[i].funcname != NULL; i++) {
121 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
128 static int net_changetrustpw(struct net_context *c, int argc, const char **argv)
130 if (net_ads_check_our_domain(c) == 0)
131 return net_ads_changetrustpw(c, argc, argv);
133 return net_rpc_changetrustpw(c, argc, argv);
136 static void set_line_buffering(FILE *f)
138 setvbuf(f, NULL, _IOLBF, 0);
141 static int net_changesecretpw(struct net_context *c, int argc,
145 uint32 sec_channel_type = SEC_CHAN_WKSTA;
149 set_line_buffering(stdin);
150 set_line_buffering(stdout);
151 set_line_buffering(stderr);
154 trust_pw = get_pass("Enter machine password: ", c->opt_stdin);
156 if (!secrets_store_machine_password(trust_pw, lp_workgroup(), sec_channel_type)) {
157 d_fprintf(stderr, "Unable to write the machine account password in the secrets database");
161 d_printf("Modified trust account password in secrets database\n");
165 d_printf("Machine account password change requires the -f flag.\n");
166 d_printf("Do NOT use this function unless you know what it does!\n");
167 d_printf("This function will change the ADS Domain member machine account password in the secrets.tdb file!\n");
173 static int net_share(struct net_context *c, int argc, const char **argv)
175 if (net_rpc_check(c, 0))
176 return net_rpc_share(c, argc, argv);
177 return net_rap_share(c, argc, argv);
181 Retrieve our local SID or the SID for the specified name
183 static int net_getlocalsid(struct net_context *c, int argc, const char **argv)
193 name = global_myname();
196 if(!initialize_password_db(false, NULL)) {
197 DEBUG(0, ("WARNING: Could not open passdb - local sid may not reflect passdb\n"
198 "backend knowledge (such as the sid stored in LDAP)\n"));
201 /* first check to see if we can even access secrets, so we don't
202 panic when we can't. */
204 if (!secrets_init()) {
205 d_fprintf(stderr, "Unable to open secrets.tdb. Can't fetch domain SID for name: %s\n", name);
209 /* Generate one, if it doesn't exist */
210 get_global_sam_sid();
212 if (!secrets_fetch_domain_sid(name, &sid)) {
213 DEBUG(0, ("Can't fetch domain SID for name: %s\n", name));
216 sid_to_fstring(sid_str, &sid);
217 d_printf("SID for domain %s is: %s\n", name, sid_str);
221 static int net_setlocalsid(struct net_context *c, int argc, const char **argv)
226 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
227 || (!string_to_sid(&sid, argv[0]))
228 || (sid.num_auths != 4)) {
229 d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n");
233 if (!secrets_store_domain_sid(global_myname(), &sid)) {
234 DEBUG(0,("Can't store domain SID as a pdc/bdc.\n"));
241 static int net_setdomainsid(struct net_context *c, int argc, const char **argv)
246 || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
247 || (!string_to_sid(&sid, argv[0]))
248 || (sid.num_auths != 4)) {
249 d_printf("usage: net setdomainsid S-1-5-21-x-y-z\n");
253 if (!secrets_store_domain_sid(lp_workgroup(), &sid)) {
254 DEBUG(0,("Can't store domain SID.\n"));
261 static int net_getdomainsid(struct net_context *c, int argc, const char **argv)
267 d_printf("usage: net getdomainsid\n");
271 if(!initialize_password_db(false, NULL)) {
272 DEBUG(0, ("WARNING: Could not open passdb - domain SID may "
273 "not reflect passdb\n"
274 "backend knowledge (such as the SID stored in "
278 /* first check to see if we can even access secrets, so we don't
279 panic when we can't. */
281 if (!secrets_init()) {
282 d_fprintf(stderr, "Unable to open secrets.tdb. Can't fetch domain"
283 "SID for name: %s\n", get_global_sam_name());
287 /* Generate one, if it doesn't exist */
288 get_global_sam_sid();
290 if (!secrets_fetch_domain_sid(global_myname(), &domain_sid)) {
291 d_fprintf(stderr, "Could not fetch local SID\n");
294 sid_to_fstring(sid_str, &domain_sid);
295 d_printf("SID for local machine %s is: %s\n", global_myname(), sid_str);
297 if (!secrets_fetch_domain_sid(c->opt_workgroup, &domain_sid)) {
298 d_fprintf(stderr, "Could not fetch domain SID\n");
302 sid_to_fstring(sid_str, &domain_sid);
303 d_printf("SID for domain %s is: %s\n", c->opt_workgroup, sid_str);
308 #ifdef WITH_FAKE_KASERVER
310 int net_help_afs(struct net_context *c, int argc, const char **argv)
312 d_printf(" net afs key filename\n"
313 "\tImports a OpenAFS KeyFile into our secrets.tdb\n\n");
314 d_printf(" net afs impersonate <user> <cell>\n"
315 "\tCreates a token for user@cell\n\n");
319 static int net_afs_key(struct net_context *c, int argc, const char **argv)
322 struct afs_keyfile keyfile;
325 d_printf("usage: 'net afs key <keyfile> cell'\n");
329 if (!secrets_init()) {
330 d_fprintf(stderr, "Could not open secrets.tdb\n");
334 if ((fd = open(argv[0], O_RDONLY, 0)) < 0) {
335 d_fprintf(stderr, "Could not open %s\n", argv[0]);
339 if (read(fd, &keyfile, sizeof(keyfile)) != sizeof(keyfile)) {
340 d_fprintf(stderr, "Could not read keyfile\n");
344 if (!secrets_store_afs_keyfile(argv[1], &keyfile)) {
345 d_fprintf(stderr, "Could not write keyfile to secrets.tdb\n");
352 static int net_afs_impersonate(struct net_context *c, int argc,
358 fprintf(stderr, "Usage: net afs impersonate <user> <cell>\n");
362 token = afs_createtoken_str(argv[0], argv[1]);
365 fprintf(stderr, "Could not create token\n");
369 if (!afs_settoken_str(token)) {
370 fprintf(stderr, "Could not set token into kernel\n");
374 printf("Success: %s@%s\n", argv[0], argv[1]);
378 static int net_afs(struct net_context *c, int argc, const char **argv)
380 struct functable func[] = {
381 {"key", net_afs_key},
382 {"impersonate", net_afs_impersonate},
383 {"help", net_help_afs},
386 return net_run_function(c, argc, argv, func, net_help_afs);
389 #endif /* WITH_FAKE_KASERVER */
391 static bool search_maxrid(struct pdb_search *search, const char *type,
394 struct samr_displayentry *entries;
395 uint32 i, num_entries;
397 if (search == NULL) {
398 d_fprintf(stderr, "get_maxrid: Could not search %s\n", type);
402 num_entries = pdb_search_entries(search, 0, 0xffffffff, &entries);
403 for (i=0; i<num_entries; i++)
404 *max_rid = MAX(*max_rid, entries[i].rid);
405 pdb_search_destroy(search);
409 static uint32 get_maxrid(void)
413 if (!search_maxrid(pdb_search_users(0), "users", &max_rid))
416 if (!search_maxrid(pdb_search_groups(), "groups", &max_rid))
419 if (!search_maxrid(pdb_search_aliases(get_global_sam_sid()),
420 "aliases", &max_rid))
426 static int net_maxrid(struct net_context *c, int argc, const char **argv)
431 DEBUG(0, ("usage: net maxrid\n"));
435 if ((rid = get_maxrid()) == 0) {
436 DEBUG(0, ("can't get current maximum rid\n"));
440 d_printf("Currently used maximum rid: %d\n", rid);
445 /* main function table */
446 static struct functable net_func[] = {
451 /* eventually these should auto-choose the transport ... */
453 {"SHARE", net_share},
454 {"SESSION", net_rap_session},
455 {"SERVER", net_rap_server},
456 {"DOMAIN", net_rap_domain},
457 {"PRINTQ", net_rap_printq},
459 {"GROUP", net_group},
460 {"GROUPMAP", net_groupmap},
462 {"VALIDATE", net_rap_validate},
463 {"GROUPMEMBER", net_rap_groupmember},
464 {"ADMIN", net_rap_admin},
465 {"SERVICE", net_rap_service},
466 {"PASSWORD", net_rap_password},
467 {"CHANGETRUSTPW", net_changetrustpw},
468 {"CHANGESECRETPW", net_changesecretpw},
470 {"LOOKUP", net_lookup},
473 {"CACHE", net_cache},
474 {"GETLOCALSID", net_getlocalsid},
475 {"SETLOCALSID", net_setlocalsid},
476 {"SETDOMAINSID", net_setdomainsid},
477 {"GETDOMAINSID", net_getdomainsid},
478 {"MAXRID", net_maxrid},
479 {"IDMAP", net_idmap},
480 {"STATUS", net_status},
481 {"USERSHARE", net_usershare},
482 {"USERSIDLIST", net_usersidlist},
484 {"REGISTRY", net_registry},
485 #ifdef WITH_FAKE_KASERVER
494 /****************************************************************************
496 ****************************************************************************/
497 int main(int argc, const char **argv)
503 const char ** argv_new;
505 TALLOC_CTX *frame = talloc_stackframe();
506 struct net_context *c = talloc_zero(frame, struct net_context);
508 struct poptOption long_options[] = {
509 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
510 {"workgroup", 'w', POPT_ARG_STRING, &c->opt_target_workgroup},
511 {"user", 'U', POPT_ARG_STRING, &c->opt_user_name, 'U'},
512 {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'},
513 {"port", 'p', POPT_ARG_INT, &c->opt_port},
514 {"myname", 'n', POPT_ARG_STRING, &c->opt_requester_name},
515 {"server", 'S', POPT_ARG_STRING, &c->opt_host},
516 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
517 {"container", 'c', POPT_ARG_STRING, &c->opt_container},
518 {"comment", 'C', POPT_ARG_STRING, &c->opt_comment},
519 {"maxusers", 'M', POPT_ARG_INT, &c->opt_maxusers},
520 {"flags", 'F', POPT_ARG_INT, &c->opt_flags},
521 {"long", 'l', POPT_ARG_NONE, &c->opt_long_list_entries},
522 {"reboot", 'r', POPT_ARG_NONE, &c->opt_reboot},
523 {"force", 'f', POPT_ARG_NONE, &c->opt_force},
524 {"stdin", 'i', POPT_ARG_NONE, &c->opt_stdin},
525 {"timeout", 't', POPT_ARG_INT, &c->opt_timeout},
526 {"machine-pass",'P', POPT_ARG_NONE, &c->opt_machine_pass},
527 {"myworkgroup", 'W', POPT_ARG_STRING, &c->opt_workgroup},
528 {"verbose", 'v', POPT_ARG_NONE, &c->opt_verbose},
529 {"test", 'T', POPT_ARG_NONE, &c->opt_testmode},
530 /* Options for 'net groupmap set' */
531 {"local", 'L', POPT_ARG_NONE, &c->opt_localgroup},
532 {"domain", 'D', POPT_ARG_NONE, &c->opt_domaingroup},
533 {"ntname", 'N', POPT_ARG_STRING, &c->opt_newntname},
534 {"rid", 'R', POPT_ARG_INT, &c->opt_rid},
535 /* Options for 'net rpc share migrate' */
536 {"acls", 0, POPT_ARG_NONE, &c->opt_acls},
537 {"attrs", 0, POPT_ARG_NONE, &c->opt_attrs},
538 {"timestamps", 0, POPT_ARG_NONE, &c->opt_timestamps},
539 {"exclude", 'X', POPT_ARG_STRING, &c->opt_exclude},
540 {"destination", 0, POPT_ARG_STRING, &c->opt_destination},
541 {"tallocreport", 0, POPT_ARG_NONE, &c->do_talloc_report},
548 zero_addr(&c->opt_dest_ip);
552 /* set default debug level to 0 regardless of what smb.conf sets */
553 DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
556 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
557 POPT_CONTEXT_KEEP_FIRST);
559 while((opt = poptGetNextOpt(pc)) != -1) {
562 net_help(c, argc, argv);
566 c->smb_encrypt = true;
569 if (!interpret_string_addr(&c->opt_dest_ip,
570 poptGetOptArg(pc), 0)) {
571 d_fprintf(stderr, "\nInvalid ip address specified\n");
573 c->opt_have_ip = true;
577 c->opt_user_specified = true;
578 c->opt_user_name = SMB_STRDUP(c->opt_user_name);
579 p = strchr(c->opt_user_name,'%');
582 c->opt_password = p+1;
586 d_fprintf(stderr, "\nInvalid option %s: %s\n",
587 poptBadOption(pc, 0), poptStrerror(opt));
588 net_help(c, argc, argv);
594 * Don't load debug level from smb.conf. It should be
595 * set by cmdline arg or remain default (0)
597 c->AllowDebugChange = false;
598 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
600 argv_new = (const char **)poptGetArgs(pc);
603 for (i=0; i<argc; i++) {
604 if (argv_new[i] == NULL) {
610 if (c->do_talloc_report) {
611 talloc_enable_leak_report();
614 if (c->opt_requester_name) {
615 set_global_myname(c->opt_requester_name);
618 if (!c->opt_user_name && getenv("LOGNAME")) {
619 c->opt_user_name = getenv("LOGNAME");
622 if (!c->opt_workgroup) {
623 c->opt_workgroup = smb_xstrdup(lp_workgroup());
626 if (!c->opt_target_workgroup) {
627 c->opt_target_workgroup = smb_xstrdup(lp_workgroup());
635 /* this makes sure that when we do things like call scripts,
636 that it won't assert becouse we are not root */
639 if (c->opt_machine_pass) {
640 /* it is very useful to be able to make ads queries as the
641 machine account for testing purposes and for domain leave */
643 net_use_krb_machine_account(c);
646 if (!c->opt_password) {
647 c->opt_password = getenv("PASSWD");
650 rc = net_run_function(c, argc_new-1, argv_new+1, net_func, net_help);
652 DEBUG(2,("return code = %d\n", rc));
654 libnetapi_free(c->netapi_ctx);