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