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