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