s3:utils: Use C99 initializer for poptOption in mvxattr
[garming/samba-autobuild/.git] / source3 / utils / smbpasswd.c
1 /*
2  * Unix SMB/CIFS implementation. 
3  * Copyright (C) Jeremy Allison 1995-1998
4  * Copyright (C) Tim Potter     2001
5  * 
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 3 of the License, or (at your
9  * option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with
17  * this program; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "includes.h"
20 #include "system/passwd.h"
21 #include "secrets.h"
22 #include "../librpc/gen_ndr/samr.h"
23 #include "../lib/util/util_pw.h"
24 #include "libsmb/proto.h"
25 #include "passdb.h"
26 #include "cmdline_contexts.h"
27
28 /*
29  * Next two lines needed for SunOS and don't
30  * hurt anything else...
31  */
32 extern char *optarg;
33 extern int optind;
34
35 /* forced running in root-mode */
36 static bool got_username = False;
37 static bool stdin_passwd_get = False;
38 static fstring user_name;
39 static char *new_passwd = NULL;
40 static const char *remote_machine = NULL;
41
42 static fstring ldap_secret;
43
44
45 /*********************************************************
46  Print command usage on stderr and die.
47 **********************************************************/
48 static void usage(void)
49 {
50         printf("When run by root:\n");
51         printf("    smbpasswd [options] [username]\n");
52         printf("otherwise:\n");
53         printf("    smbpasswd [options]\n\n");
54
55         printf("options:\n");
56         printf("  -L                   local mode (must be first option)\n");
57         printf("  -h                   print this usage message\n");
58         printf("  -s                   use stdin for password prompt\n");
59         printf("  -c smb.conf file     Use the given path to the smb.conf file\n");
60         printf("  -D LEVEL             debug level\n");
61         printf("  -r MACHINE           remote machine\n");
62         printf("  -U USER              remote username (e.g. SAM/user)\n");
63
64         printf("extra options when run by root or in local mode:\n");
65         printf("  -a                   add user\n");
66         printf("  -d                   disable user\n");
67         printf("  -e                   enable user\n");
68         printf("  -i                   interdomain trust account\n");
69         printf("  -m                   machine trust account\n");
70         printf("  -n                   set no password\n");
71         printf("  -W                   use stdin ldap admin password\n");
72         printf("  -w PASSWORD          ldap admin password\n");
73         printf("  -x                   delete user\n");
74         printf("  -R ORDER             name resolve order\n");
75
76         exit(1);
77 }
78
79 static void set_line_buffering(FILE *f)
80 {
81         setvbuf(f, NULL, _IOLBF, 0);
82 }
83
84 /*******************************************************************
85  Process command line options
86  ******************************************************************/
87
88 static int process_options(int argc, char **argv, int local_flags)
89 {
90         int ch;
91         const char *configfile = get_dyn_CONFIGFILE();
92
93         local_flags |= LOCAL_SET_PASSWORD;
94
95         ZERO_STRUCT(user_name);
96
97         user_name[0] = '\0';
98
99         while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LWS:")) != EOF) {
100                 switch(ch) {
101                 case 'L':
102                         if (getuid() != 0) {
103                                 fprintf(stderr, "smbpasswd -L can only be used by root.\n");
104                                 exit(1);
105                         }
106                         local_flags |= LOCAL_AM_ROOT;
107                         break;
108                 case 'c':
109                         configfile = optarg;
110                         set_dyn_CONFIGFILE(optarg);
111                         break;
112                 case 'a':
113                         local_flags |= LOCAL_ADD_USER;
114                         break;
115                 case 'x':
116                         local_flags |= LOCAL_DELETE_USER;
117                         local_flags &= ~LOCAL_SET_PASSWORD;
118                         break;
119                 case 'd':
120                         local_flags |= LOCAL_DISABLE_USER;
121                         local_flags &= ~LOCAL_SET_PASSWORD;
122                         break;
123                 case 'e':
124                         local_flags |= LOCAL_ENABLE_USER;
125                         local_flags &= ~LOCAL_SET_PASSWORD;
126                         break;
127                 case 'm':
128                         local_flags |= LOCAL_TRUST_ACCOUNT;
129                         break;
130                 case 'i':
131                         local_flags |= LOCAL_INTERDOM_ACCOUNT;
132                         break;
133                 case 'j':
134                         d_printf("See 'net join' for this functionality\n");
135                         exit(1);
136                         break;
137                 case 'n':
138                         local_flags |= LOCAL_SET_NO_PASSWORD;
139                         local_flags &= ~LOCAL_SET_PASSWORD;
140                         SAFE_FREE(new_passwd);
141                         new_passwd = smb_xstrdup("NO PASSWORD");
142                         break;
143                 case 'r':
144                         remote_machine = optarg;
145                         break;
146                 case 's':
147                         set_line_buffering(stdin);
148                         set_line_buffering(stdout);
149                         set_line_buffering(stderr);
150                         stdin_passwd_get = True;
151                         break;
152                 case 'w':
153                         local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
154                         fstrcpy(ldap_secret, optarg);
155                         break;
156                 case 'R':
157                         lp_set_cmdline("name resolve order", optarg);
158                         break;
159                 case 'D':
160                         lp_set_cmdline("log level", optarg);
161                         break;
162                 case 'U': {
163                         got_username = True;
164                         fstrcpy(user_name, optarg);
165                         break;
166                 case 'W':
167                         local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
168                         *ldap_secret = '\0';
169                         break;
170                 }
171                 case 'h':
172                 default:
173                         usage();
174                 }
175         }
176
177         argc -= optind;
178         argv += optind;
179
180         switch(argc) {
181         case 0:
182                 if (!got_username)
183                         fstrcpy(user_name, "");
184                 break;
185         case 1:
186                 if (!(local_flags & LOCAL_AM_ROOT)) {
187                         usage();
188                 } else {
189                         if (got_username) {
190                                 usage();
191                         } else {
192                                 fstrcpy(user_name, argv[0]);
193                         }
194                 }
195                 break;
196         default:
197                 usage();
198         }
199
200         cmdline_messaging_context(configfile);
201
202         if (!lp_load_global(configfile)) {
203                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
204                         configfile);
205                 exit(1);
206         }
207
208         return local_flags;
209 }
210
211 /*************************************************************
212  Utility function to prompt for new password.
213 *************************************************************/
214 static char *prompt_for_new_password(bool stdin_get)
215 {
216         char *p;
217         fstring new_pw;
218
219         ZERO_ARRAY(new_pw);
220
221         p = get_pass("New SMB password:", stdin_get);
222         if (p == NULL) {
223                 return NULL;
224         }
225
226         fstrcpy(new_pw, p);
227         SAFE_FREE(p);
228
229         p = get_pass("Retype new SMB password:", stdin_get);
230         if (p == NULL) {
231                 return NULL;
232         }
233
234         if (strcmp(p, new_pw)) {
235                 fprintf(stderr, "Mismatch - password unchanged.\n");
236                 ZERO_ARRAY(new_pw);
237                 SAFE_FREE(p);
238                 return NULL;
239         }
240
241         return p;
242 }
243
244
245 /*************************************************************
246  Change a password either locally or remotely.
247 *************************************************************/
248
249 static NTSTATUS password_change(const char *remote_mach,
250                                 const char *domain, const char *username,
251                                 const char *old_passwd, const char *new_pw,
252                                 int local_flags)
253 {
254         NTSTATUS ret;
255         char *err_str = NULL;
256         char *msg_str = NULL;
257
258         if (remote_mach != NULL) {
259                 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|
260                                    LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
261                                    LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
262                         /* these things can't be done remotely yet */
263                         fprintf(stderr, "Invalid remote operation!\n");
264                         return NT_STATUS_UNSUCCESSFUL;
265                 }
266                 ret = remote_password_change(remote_mach,
267                                              domain, username,
268                                              old_passwd, new_pw, &err_str);
269         } else {
270                 ret = local_password_change(username, local_flags, new_pw,
271                                             &err_str, &msg_str);
272         }
273
274         if (msg_str) {
275                 printf("%s", msg_str);
276         }
277         if (err_str) {
278                 fprintf(stderr, "%s", err_str);
279         }
280         if (!NT_STATUS_IS_OK(ret) && !err_str) {
281                 fprintf(stderr, "Failed to change password!\n");
282         }
283
284         SAFE_FREE(msg_str);
285         SAFE_FREE(err_str);
286         return ret;
287 }
288
289 /*******************************************************************
290  Store the LDAP admin password in secrets.tdb
291  ******************************************************************/
292 static bool store_ldap_admin_pw (char* pw)
293 {       
294         if (!pw) 
295                 return False;
296
297         if (!secrets_init())
298                 return False;
299
300         return secrets_store_ldap_pw(lp_ldap_admin_dn(talloc_tos()), pw);
301 }
302
303
304 /*************************************************************
305  Handle password changing for root.
306 *************************************************************/
307
308 static int process_root(int local_flags)
309 {
310         struct passwd  *pwd;
311         int result = 0;
312         char *old_passwd = NULL;
313
314         if (local_flags & LOCAL_SET_LDAP_ADMIN_PW) {
315                 char *ldap_admin_dn = lp_ldap_admin_dn(talloc_tos());
316                 if ( ! *ldap_admin_dn ) {
317                         DEBUG(0,("ERROR: 'ldap admin dn' not defined! Please check your smb.conf\n"));
318                         goto done;
319                 }
320
321                 printf("Setting stored password for \"%s\" in secrets.tdb\n", ldap_admin_dn);
322                 if ( ! *ldap_secret ) {
323                         new_passwd = prompt_for_new_password(stdin_passwd_get);
324                         if (new_passwd == NULL) {
325                                 fprintf(stderr, "Failed to read new password!\n");
326                                 exit(1);
327                         }
328                         fstrcpy(ldap_secret, new_passwd);
329                 }
330                 if (!store_ldap_admin_pw(ldap_secret)) {
331                         DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
332                 }
333                 goto done;
334         }
335
336         /* Ensure passdb startup(). */
337         if(!initialize_password_db(False, NULL)) {
338                 DEBUG(0, ("Failed to open passdb!\n"));
339                 exit(1);
340         }
341
342         /* Ensure we have a SAM sid. */
343         get_global_sam_sid();
344
345         /*
346          * Ensure both add/delete user are not set
347          * Ensure add/delete user and either remote machine or join domain are
348          * not both set.
349          */     
350         if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) || 
351            ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) && 
352                 (remote_machine != NULL))) {
353                 usage();
354         }
355
356         /* Only load interfaces if we are doing network operations. */
357
358         if (remote_machine) {
359                 load_interfaces();
360         }
361
362         if (!user_name[0] && (pwd = getpwuid_alloc(talloc_tos(), geteuid()))) {
363                 fstrcpy(user_name, pwd->pw_name);
364                 TALLOC_FREE(pwd);
365         } 
366
367         if (!user_name[0]) {
368                 fprintf(stderr,"You must specify a username\n");
369                 exit(1);
370         }
371
372         if (local_flags & LOCAL_TRUST_ACCOUNT) {
373                 /* add the $ automatically */
374                 size_t user_name_len = strlen(user_name);
375
376                 if (user_name[user_name_len - 1] == '$') {
377                         user_name_len--;
378                 } else {
379                         if (user_name_len + 2 > sizeof(user_name)) {
380                                 fprintf(stderr, "machine name too long\n");
381                                 exit(1);
382                         }
383                         user_name[user_name_len] = '$';
384                         user_name[user_name_len + 1] = '\0';
385                 }
386
387                 if (local_flags & LOCAL_ADD_USER) {
388                         SAFE_FREE(new_passwd);
389
390                         /*
391                          * Remove any trailing '$' before we
392                          * generate the initial machine password.
393                          */
394                         new_passwd = smb_xstrndup(user_name, user_name_len);
395                         if (!strlower_m(new_passwd)) {
396                                 fprintf(stderr, "strlower_m %s failed\n",
397                                         new_passwd);
398                                 exit(1);
399                         }
400                 }
401         } else if (local_flags & LOCAL_INTERDOM_ACCOUNT) {
402                 size_t user_name_len = strlen(user_name);
403
404                 if (user_name[user_name_len - 1] != '$') {
405                         if (user_name_len + 2 > sizeof(user_name)) {
406                                 fprintf(stderr, "machine name too long\n");
407                                 exit(1);
408                         }
409                         user_name[user_name_len] = '$';
410                         user_name[user_name_len + 1] = '\0';
411                 }
412
413                 if ((local_flags & LOCAL_ADD_USER) && (new_passwd == NULL)) {
414                         /*
415                          * Prompt for trusting domain's account password
416                          */
417                         new_passwd = prompt_for_new_password(stdin_passwd_get);
418                         if(!new_passwd) {
419                                 fprintf(stderr, "Unable to get newpassword.\n");
420                                 exit(1);
421                         }
422                 }
423         } else {
424
425                 if (remote_machine != NULL) {
426                         old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
427                         if(!old_passwd) {
428                                 fprintf(stderr, "Unable to get old password.\n");
429                                 exit(1);
430                         }
431                 }
432
433                 if (!(local_flags & LOCAL_SET_PASSWORD)) {
434
435                         /*
436                          * If we are trying to enable a user, first we need to find out
437                          * if they are using a modern version of the smbpasswd file that
438                          * disables a user by just writing a flag into the file. If so
439                          * then we can re-enable a user without prompting for a new
440                          * password. If not (ie. they have a no stored password in the
441                          * smbpasswd file) then we need to prompt for a new password.
442                          */
443
444                         if(local_flags & LOCAL_ENABLE_USER) {
445                                 struct samu *sampass = NULL;
446
447                                 sampass = samu_new( NULL );
448                                 if (!sampass) {
449                                         fprintf(stderr, "talloc fail for struct samu.\n");
450                                         exit(1);
451                                 }
452                                 if (!pdb_getsampwnam(sampass, user_name)) {
453                                         fprintf(stderr, "Failed to find user %s in passdb backend.\n",
454                                                 user_name );
455                                         exit(1);
456                                 }
457
458                                 if(pdb_get_nt_passwd(sampass) == NULL) {
459                                         local_flags |= LOCAL_SET_PASSWORD;
460                                 }
461                                 TALLOC_FREE(sampass);
462                         }
463                 }
464
465                 if((local_flags & LOCAL_SET_PASSWORD) && (new_passwd == NULL)) {
466
467                         new_passwd = prompt_for_new_password(stdin_passwd_get);
468                         if(!new_passwd) {
469                                 fprintf(stderr, "Unable to get new password.\n");
470                                 exit(1);
471                         }
472                 }
473         }
474
475         if (!NT_STATUS_IS_OK(password_change(remote_machine,
476                                              NULL, user_name,
477                                              old_passwd, new_passwd,
478                                              local_flags))) {
479                 result = 1;
480                 goto done;
481         } 
482
483         if(remote_machine) {
484                 printf("Password changed for user %s on %s.\n", user_name, remote_machine );
485         } else if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
486                 struct samu *sampass = NULL;
487
488                 sampass = samu_new( NULL );
489                 if (!sampass) {
490                         fprintf(stderr, "talloc fail for struct samu.\n");
491                         exit(1);
492                 }
493
494                 if (!pdb_getsampwnam(sampass, user_name)) {
495                         fprintf(stderr, "Failed to find user %s in passdb backend.\n",
496                                 user_name );
497                         exit(1);
498                 }
499
500                 printf("Password changed for user %s.", user_name );
501                 if(pdb_get_acct_ctrl(sampass)&ACB_DISABLED) {
502                         printf(" User has disabled flag set.");
503                 }
504                 if(pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) {
505                         printf(" User has no password flag set.");
506                 }
507                 printf("\n");
508                 TALLOC_FREE(sampass);
509         }
510
511  done:
512         SAFE_FREE(old_passwd);
513         SAFE_FREE(new_passwd);
514         return result;
515 }
516
517
518 /*************************************************************
519  Handle password changing for non-root.
520 *************************************************************/
521
522 static int process_nonroot(int local_flags)
523 {
524         struct passwd  *pwd = NULL;
525         int result = 0;
526         char *old_pw = NULL;
527         char *new_pw = NULL;
528         const char *username = user_name;
529         const char *domain = NULL;
530         char *p = NULL;
531
532         if (local_flags & ~(LOCAL_AM_ROOT | LOCAL_SET_PASSWORD)) {
533                 /* Extra flags that we can't honor non-root */
534                 usage();
535         }
536
537         if (!user_name[0]) {
538                 pwd = getpwuid_alloc(talloc_tos(), getuid());
539                 if (pwd) {
540                         fstrcpy(user_name,pwd->pw_name);
541                         TALLOC_FREE(pwd);
542                 } else {
543                         fprintf(stderr, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
544                         exit(1);
545                 }
546         }
547
548         /* Allow domain as part of the username */
549         if ((p = strchr_m(user_name, '\\')) ||
550             (p = strchr_m(user_name, '/')) ||
551             (p = strchr_m(user_name, *lp_winbind_separator()))) {
552                 *p = '\0';
553                 username = p + 1;
554                 domain = user_name;
555         }
556
557         /*
558          * A non-root user is always setting a password
559          * via a remote machine (even if that machine is
560          * localhost).
561          */     
562
563         load_interfaces(); /* Delayed from main() */
564
565         if (remote_machine != NULL) {
566                 if (!is_ipaddress(remote_machine)) {
567                         domain = remote_machine;
568                 }
569         } else {
570                 remote_machine = "127.0.0.1";
571
572                 /*
573                  * If we deal with a local user, change the password for the
574                  * user in our SAM.
575                  */
576                 domain = get_global_sam_name();
577         }
578
579         old_pw = get_pass("Old SMB password:",stdin_passwd_get);
580         if (old_pw == NULL) {
581                 fprintf(stderr, "Unable to get old password.\n");
582                 exit(1);
583         }
584
585         if (!new_passwd) {
586                 new_pw = prompt_for_new_password(stdin_passwd_get);
587         }
588         else
589                 new_pw = smb_xstrdup(new_passwd);
590
591         if (!new_pw) {
592                 fprintf(stderr, "Unable to get new password.\n");
593                 exit(1);
594         }
595
596         if (!NT_STATUS_IS_OK(password_change(remote_machine,
597                                              domain, username,
598                                              old_pw, new_pw, 0))) {
599                 result = 1;
600                 goto done;
601         }
602
603         printf("Password changed for user %s\n", username);
604
605  done:
606         SAFE_FREE(old_pw);
607         SAFE_FREE(new_pw);
608
609         return result;
610 }
611
612
613
614 /*********************************************************
615  Start here.
616 **********************************************************/
617 int main(int argc, char **argv)
618 {       
619         TALLOC_CTX *frame = talloc_stackframe();
620         int local_flags = 0;
621         int ret;
622
623 #if defined(HAVE_SET_AUTH_PARAMETERS)
624         set_auth_parameters(argc, argv);
625 #endif /* HAVE_SET_AUTH_PARAMETERS */
626
627         if (getuid() == 0) {
628                 local_flags = LOCAL_AM_ROOT;
629         }
630
631         smb_init_locale();
632
633         local_flags = process_options(argc, argv, local_flags);
634
635         setup_logging("smbpasswd", DEBUG_STDERR);
636
637         /*
638          * Set the machine NETBIOS name if not already
639          * set from the config file. 
640          */ 
641
642         if (!init_names())
643                 return 1;
644
645         /* Check the effective uid - make sure we are not setuid */
646         if (is_setuid_root()) {
647                 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
648                 exit(1);
649         }
650
651         if (local_flags & LOCAL_AM_ROOT) {
652                 bool ok;
653
654                 ok = secrets_init();
655                 if (!ok) {
656                         return 1;
657                 }
658                 ret = process_root(local_flags);
659         } else {
660                 ret = process_nonroot(local_flags);
661         }
662         TALLOC_FREE(frame);
663         return ret;
664 }