strchr and strrchr are macros when compiling with optimisation in gcc, so we can...
[ira/wip.git] / source3 / utils / smbpasswd.c
1 /*
2  * Unix SMB/Netbios implementation. 
3  * Version 1.9. 
4  * smbpasswd module. 
5  * Copyright (C) Jeremy Allison 1995-1998
6  * Copyright (C) Tim Potter     2001
7  * 
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  * 
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc., 675
20  * Mass Ave, Cambridge, MA 02139, USA.  */
21
22 #include "includes.h"
23
24 extern pstring global_myname;
25 extern int DEBUGLEVEL;
26
27 /*
28  * Next two lines needed for SunOS and don't
29  * hurt anything else...
30  */
31 extern char *optarg;
32 extern int optind;
33
34 /* forced running in root-mode */
35 static BOOL local_mode;
36
37 /*********************************************************
38 a strdup with exit
39 **********************************************************/
40 static char *xstrdup(char *s)
41 {
42         s = strdup(s);
43         if (!s) {
44                 fprintf(stderr,"out of memory\n");
45                 exit(1);
46         }
47         return s;
48 }
49
50
51 /*********************************************************
52  Print command usage on stderr and die.
53 **********************************************************/
54 static void usage(void)
55 {
56         if (getuid() == 0) {
57                 printf("smbpasswd [options] [username] [password]\n");
58         } else {
59                 printf("smbpasswd [options] [password]\n");
60         }
61         printf("options:\n");
62         printf("  -s                   use stdin for password prompt\n");
63         printf("  -D LEVEL             debug level\n");
64         printf("  -U USER              remote username\n");
65         printf("  -r MACHINE           remote machine\n");
66
67         if (getuid() == 0 || local_mode) {
68                 printf("  -L                   local mode (must be first option)\n");
69                 printf("  -R ORDER             name resolve order\n");
70                 printf("  -j DOMAIN            join domain name\n");
71                 printf("  -a                   add user\n");
72                 printf("  -x                   delete user\n");
73                 printf("  -d                   disable user\n");
74                 printf("  -e                   enable user\n");
75                 printf("  -n                   set no password\n");
76                 printf("  -m                   machine trust account\n");
77         }
78         exit(1);
79 }
80
81 /*********************************************************
82 Join a domain.
83 **********************************************************/
84 static int join_domain(char *domain, char *remote)
85 {
86         pstring remote_machine;
87         fstring trust_passwd;
88         unsigned char orig_trust_passwd_hash[16];
89         BOOL ret;
90
91         pstrcpy(remote_machine, remote ? remote : "");
92         fstrcpy(trust_passwd, global_myname);
93         strlower(trust_passwd);
94         E_md4hash( (uchar *)trust_passwd, orig_trust_passwd_hash);
95
96         /* Ensure that we are not trying to join a
97            domain if we are locally set up as a domain
98            controller. */
99
100         if(strequal(remote, global_myname)) {
101                 fprintf(stderr, "Cannot join domain %s as the domain controller name is our own. We cannot be a domain controller for a domain and also be a domain member.\n", domain);
102                 return 1;
103         }
104
105         /*
106          * Write the old machine account password.
107          */
108         
109         if(!secrets_store_trust_account_password(domain,  orig_trust_passwd_hash)) {              
110                 fprintf(stderr, "Unable to write the machine account password for \
111 machine %s in domain %s.\n", global_myname, domain);
112                 return 1;
113         }
114         
115         /*
116          * If we are given a remote machine assume this is the PDC.
117          */
118         
119         if(remote == NULL) {
120                 pstrcpy(remote_machine, lp_passwordserver());
121         }
122
123         if(!*remote_machine) {
124                 fprintf(stderr, "No password server list given in smb.conf - \
125 unable to join domain.\n");
126                 return 1;
127         }
128
129         ret = change_trust_account_password( domain, remote_machine);
130         
131         if(!ret) {
132                 trust_password_delete(domain);
133                 fprintf(stderr,"Unable to join domain %s.\n",domain);
134         } else {
135                 printf("Joined domain %s.\n",domain);
136         }
137         
138         return (int)ret;
139 }
140
141 /* Initialise client credentials for authenticated pipe access */
142
143 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
144                           char* domain, char* password)
145 {
146         ZERO_STRUCTP(creds);
147         
148         if (lp_encrypted_passwords()) {
149                 pwd_make_lm_nt_16(&creds->pwd, password);
150         } else {
151                 pwd_set_cleartext(&creds->pwd, password);
152         }
153
154         fstrcpy(creds->user_name, username);
155         fstrcpy(creds->domain, domain);
156 }
157
158 /*********************************************************
159 Join a domain using the administrator username and password
160 **********************************************************/
161
162 /* Macro for checking RPC error codes to make things more readable */
163
164 #define CHECK_RPC_ERR(rpc, msg) \
165         if ((result = rpc) != NT_STATUS_NO_PROBLEMO) { \
166                 DEBUG(0, (msg ": %s\n", get_nt_error_msg(result))); \
167                 goto done; \
168         }
169
170 #define CHECK_RPC_ERR_DEBUG(rpc, debug_args) \
171         if ((result = rpc) != NT_STATUS_NO_PROBLEMO) { \
172                 DEBUG(0, debug_args); \
173                 goto done; \
174         }
175
176 static int join_domain_byuser(char *domain, char *remote_machine,
177                               char *username, char *password)
178 {
179         /* libsmb variables */
180
181         struct nmb_name calling, called;
182         struct ntuser_creds creds;
183         struct cli_state cli;
184         fstring dest_host, acct_name;
185         struct in_addr dest_ip;
186         TALLOC_CTX *mem_ctx;
187
188         /* rpc variables */
189
190         POLICY_HND lsa_pol, sam_pol, domain_pol, user_pol;
191         DOM_SID domain_sid;
192         uint32 user_rid;
193
194         /* Password stuff */
195
196         char *machine_pwd;
197         int plen = 0;
198         uchar pwbuf[516], ntpw[16], sess_key[16];
199         SAM_USERINFO_CTR ctr;
200         SAM_USER_INFO_24 p24;
201         SAM_USER_INFO_10 p10;
202
203         /* Misc */
204
205         uint32 result;
206         int retval = 1;
207
208         /* Connect to remote machine */
209
210         ZERO_STRUCT(cli);
211         ZERO_STRUCT(creds);
212
213         if (!(mem_ctx = talloc_init())) {
214                 DEBUG(0, ("Could not initialise talloc context\n"));
215                 goto done;
216         }
217
218         if (!cli_initialise(&cli)) {
219                 DEBUG(0, ("Could not initialise client structure\n"));
220                 goto done;
221         }
222
223         init_rpcclient_creds(&creds, username, domain, password);
224         cli_init_creds(&cli, &creds);
225
226         if (!resolve_srv_name(remote_machine, dest_host, &dest_ip)) {
227                 DEBUG(0, ("Could not resolve name %s\n", remote_machine));
228                 goto done;
229         }
230
231         make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
232         make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
233
234         if (!cli_establish_connection(&cli, dest_host, &dest_ip, &calling, 
235                                       &called, "IPC$", "IPC", False, True)) {
236                 DEBUG(0, ("Error connecting to %s\n", dest_host));
237                 goto done;
238         }
239
240         /* Fetch domain sid */
241
242         if (!cli_nt_session_open(&cli, PIPE_LSARPC)) {
243                 DEBUG(0, ("Error connecting to SAM pipe\n"));
244                 goto done;
245         }
246
247
248         CHECK_RPC_ERR(cli_lsa_open_policy(&cli, mem_ctx, True,
249                                           SEC_RIGHTS_MAXIMUM_ALLOWED,
250                                           &lsa_pol),
251                       "error opening lsa policy handle");
252
253         CHECK_RPC_ERR(cli_lsa_query_info_policy(&cli, mem_ctx, &lsa_pol,
254                                                 5, domain, &domain_sid),
255                       "error querying info policy");
256
257         cli_lsa_close(&cli, mem_ctx, &lsa_pol);
258
259         cli_nt_session_close(&cli); /* Done with this pipe */
260
261         /* Create domain user */
262
263         if (!cli_nt_session_open(&cli, PIPE_SAMR)) {
264                 DEBUG(0, ("Error connecting to SAM pipe\n"));
265                 goto done;
266         }
267
268         CHECK_RPC_ERR(cli_samr_connect(&cli, mem_ctx, 
269                                        SEC_RIGHTS_MAXIMUM_ALLOWED,
270                                        &sam_pol),
271                       "could not connect to SAM database");
272
273         
274         CHECK_RPC_ERR(cli_samr_open_domain(&cli, mem_ctx, &sam_pol,
275                                            SEC_RIGHTS_MAXIMUM_ALLOWED,
276                                            &domain_sid, &domain_pol),
277                       "could not open domain");
278
279         /* Create domain user */
280
281         fstrcpy(acct_name, global_myname);
282         fstrcat(acct_name, "$");
283
284         strlower(acct_name);
285
286         {
287                 uint32 unknown = 0xe005000b;
288
289                 result = cli_samr_create_dom_user(&cli, mem_ctx, &domain_pol,
290                                                   acct_name, ACB_WSTRUST,
291                                                   unknown, &user_pol, 
292                                                   &user_rid);
293         }
294
295         if (result == NT_STATUS_USER_EXISTS) {
296                 uint32 num_rids, *name_types, *user_rids;
297                 uint32 flags = 0x3e8;
298                 char *names;
299                 
300                 /* Look up existing rid */
301                 
302                 names = (char *)&acct_name[0];
303
304                 CHECK_RPC_ERR_DEBUG(
305                         cli_samr_lookup_names(&cli, mem_ctx,
306                                               &domain_pol, flags,
307                                               1, &names, &num_rids,
308                                               &user_rids, &name_types),
309                         ("error looking up rid for user %s: %s\n",
310                          acct_name, get_nt_error_msg(result)));
311
312                 if (name_types[0] != SID_NAME_USER) {
313                         DEBUG(0, ("%s is not a user account\n", acct_name));
314                         goto done;
315                 }
316
317                 user_rid = user_rids[0];
318                 
319                 /* Open handle on user */
320
321                 CHECK_RPC_ERR_DEBUG(
322                         cli_samr_open_user(&cli, mem_ctx, &domain_pol,
323                                            SEC_RIGHTS_MAXIMUM_ALLOWED,
324                                            user_rid, &user_pol),
325                         ("could not re-open existing user %s: %s\n",
326                          acct_name, get_nt_error_msg(result)));
327                 
328         } else if (result != NT_STATUS_NOPROBLEMO) {
329                 DEBUG(0, ("error creating domain user: %s\n",
330                           get_nt_error_msg(result)));
331                 goto done;
332         }
333
334         /* Create a random machine account password */
335
336         {
337                 UNISTR2 upw;    /* Unicode password */
338
339                 upw.buffer = (uint16 *)talloc_zero(mem_ctx, 0xc * 
340                                                    sizeof(uint16));
341
342                 upw.uni_str_len = 0xc;
343                 upw.uni_max_len = 0xc;
344
345                 machine_pwd = (char *)upw.buffer;
346                 plen = upw.uni_str_len * 2;
347                 generate_random_buffer(machine_pwd, plen, True);
348
349                 encode_pw_buffer(pwbuf, machine_pwd, plen, False);
350
351                 nt_owf_genW(&upw, ntpw);
352         }
353
354         /* Set password on machine account */
355
356         ZERO_STRUCT(ctr);
357         ZERO_STRUCT(p24);
358
359         init_sam_user_info24(&p24, pwbuf);
360
361         ctr.switch_value = 24;
362         ctr.info.id24 = &p24;
363
364         /* I don't think this is quite the right place for this
365            calculation.  It should be moved somewhere where the credentials
366            are calculated. )-: */
367
368         mdfour(sess_key, cli.pwd.smb_nt_pwd, 16);
369
370         CHECK_RPC_ERR(cli_samr_set_userinfo(&cli, mem_ctx, &user_pol, 24, 
371                                             sess_key, &ctr),
372                       "error setting trust account password");
373
374         /* Why do we have to try to (re-)set the ACB to be the same as what
375            we passed in the samr_create_dom_user() call?  When a NT
376            workstation is joined to a domain by an administrator the
377            acb_info is set to 0x80.  For a normal user with "Add
378            workstations to the domain" rights the acb_info is 0x84.  I'm
379            not sure whether it is supposed to make a difference or not.  NT
380            seems to cope with either value so don't bomb out if the set
381            userinfo2 level 0x10 fails.  -tpot */
382
383         ctr.switch_value = 0x10;
384         ctr.info.id10 = &p10;
385
386         init_sam_user_info10(&p10, ACB_WSTRUST);
387
388         /* Ignoring the return value is necessary for joining a domain
389            as a normal user with "Add workstation to domain" privilege. */
390
391         result = cli_samr_set_userinfo2(&cli, mem_ctx, &user_pol, 0x10, 
392                                         sess_key, &ctr);
393
394         /* Now store the secret in the secrets database */
395
396         strupper(domain);
397
398         if (!secrets_store_domain_sid(domain, &domain_sid) ||
399             !secrets_store_trust_account_password(domain, ntpw)) {
400                 DEBUG(0, ("error storing domain secrets\n"));
401                 goto done;
402         }
403
404         retval = 0;             /* Success! */
405
406  done:
407         /* Close down pipe - this will clean up open policy handles */
408
409         if (cli.nt_pipe_fnum)
410                 cli_nt_session_close(&cli);
411
412         /* Display success or failure */
413
414         if (retval != 0) {
415                 trust_password_delete(domain);
416                 fprintf(stderr,"Unable to join domain %s.\n",domain);
417         } else {
418                 printf("Joined domain %s.\n",domain);
419         }
420         
421         return retval;
422 }
423
424 static void set_line_buffering(FILE *f)
425 {
426         setvbuf(f, NULL, _IOLBF, 0);
427 }
428
429 /*************************************************************
430  Utility function to prompt for passwords from stdin. Each
431  password entered must end with a newline.
432 *************************************************************/
433 static char *stdin_new_passwd(void)
434 {
435         static fstring new_passwd;
436         size_t len;
437
438         ZERO_ARRAY(new_passwd);
439
440         /*
441          * if no error is reported from fgets() and string at least contains
442          * the newline that ends the password, then replace the newline with
443          * a null terminator.
444          */
445         if ( fgets(new_passwd, sizeof(new_passwd), stdin) != NULL) {
446                 if ((len = strlen(new_passwd)) > 0) {
447                         if(new_passwd[len-1] == '\n')
448                                 new_passwd[len - 1] = 0; 
449                 }
450         }
451         return(new_passwd);
452 }
453
454
455 /*************************************************************
456  Utility function to get passwords via tty or stdin
457  Used if the '-s' option is set to silently get passwords
458  to enable scripting.
459 *************************************************************/
460 static char *get_pass( char *prompt, BOOL stdin_get)
461 {
462         char *p;
463         if (stdin_get) {
464                 p = stdin_new_passwd();
465         } else {
466                 p = getpass(prompt);
467         }
468         return xstrdup(p);
469 }
470
471 /*************************************************************
472  Utility function to prompt for new password.
473 *************************************************************/
474 static char *prompt_for_new_password(BOOL stdin_get)
475 {
476         char *p;
477         fstring new_passwd;
478
479         ZERO_ARRAY(new_passwd);
480  
481         p = get_pass("New SMB password:", stdin_get);
482
483         fstrcpy(new_passwd, p);
484         safe_free(p);
485
486         p = get_pass("Retype new SMB password:", stdin_get);
487
488         if (strcmp(p, new_passwd)) {
489                 fprintf(stderr, "Mismatch - password unchanged.\n");
490                 ZERO_ARRAY(new_passwd);
491                 safe_free(p);
492                 return NULL;
493         }
494
495         return p;
496 }
497
498
499 /*************************************************************
500  Change a password either locally or remotely.
501 *************************************************************/
502
503 static BOOL password_change(const char *remote_machine, char *user_name, 
504                             char *old_passwd, char *new_passwd, int local_flags)
505 {
506         BOOL ret;
507         pstring err_str;
508         pstring msg_str;
509
510         if (remote_machine != NULL) {
511                 if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
512                                                         LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
513                         /* these things can't be done remotely yet */
514                         return False;
515                 }
516                 ret = remote_password_change(remote_machine, user_name, 
517                                                                          old_passwd, new_passwd, err_str, sizeof(err_str));
518                 if(*err_str)
519                         fprintf(stderr, err_str);
520                 return ret;
521         }
522         
523         ret = local_password_change(user_name, local_flags, new_passwd, 
524                                      err_str, sizeof(err_str), msg_str, sizeof(msg_str));
525
526         if(*msg_str)
527                 printf(msg_str);
528         if(*err_str)
529                 fprintf(stderr, err_str);
530
531         return ret;
532 }
533
534
535 /*************************************************************
536  Handle password changing for root.
537 *************************************************************/
538
539 static int process_root(int argc, char *argv[])
540 {
541         struct passwd  *pwd;
542         int result = 0, ch;
543         BOOL joining_domain = False, got_pass = False;
544         int local_flags = 0;
545         BOOL stdin_passwd_get = False;
546         fstring user_name, user_password;
547         char *new_domain = NULL;
548         char *new_passwd = NULL;
549         char *old_passwd = NULL;
550         char *remote_machine = NULL;
551
552         while ((ch = getopt(argc, argv, "ax:d:e:mnj:r:sR:D:U:L")) != EOF) {
553                 switch(ch) {
554                 case 'L':
555                         local_mode = True;
556                         break;
557                 case 'a':
558                         local_flags |= LOCAL_ADD_USER;
559                         break;
560                 case 'x':
561                         local_flags |= LOCAL_DELETE_USER;
562                         fstrcpy(user_name, optarg);
563                         new_passwd = xstrdup("XXXXXX");
564                         break;
565                 case 'd':
566                         local_flags |= LOCAL_DISABLE_USER;
567                         fstrcpy(user_name, optarg);
568                         new_passwd = xstrdup("XXXXXX");
569                         break;
570                 case 'e':
571                         local_flags |= LOCAL_ENABLE_USER;
572                         fstrcpy(user_name, optarg);
573                         break;
574                 case 'm':
575                         local_flags |= LOCAL_TRUST_ACCOUNT;
576                         break;
577                 case 'n':
578                         local_flags |= LOCAL_SET_NO_PASSWORD;
579                         new_passwd = xstrdup("NO PASSWORD");
580                         break;
581                 case 'j':
582                         new_domain = optarg;
583                         strupper(new_domain);
584                         joining_domain = True;
585                         break;
586                 case 'r':
587                         remote_machine = optarg;
588                         break;
589                 case 's':
590                         set_line_buffering(stdin);
591                         set_line_buffering(stdout);
592                         set_line_buffering(stderr);
593                         stdin_passwd_get = True;
594                         break;
595                 case 'R':
596                         lp_set_name_resolve_order(optarg);
597                         break;
598                 case 'D':
599                         DEBUGLEVEL = atoi(optarg);
600                         break;
601                 case 'U': {
602                         char *lp;
603
604                         fstrcpy(user_name, optarg);
605
606                         if ((lp = strchr_m(user_name, '%'))) {
607                                 *lp = 0;
608                                 fstrcpy(user_password, lp + 1);
609                                 got_pass = True;
610                                 memset(strchr_m(optarg, '%') + 1, 'X',
611                                        strlen(user_password));
612                         }
613
614                         break;
615                 }
616                 default:
617                         usage();
618                 }
619         }
620         
621         argc -= optind;
622         argv += optind;
623
624         /*
625          * Ensure both add/delete user are not set
626          * Ensure add/delete user and either remote machine or join domain are
627          * not both set.
628          */     
629         if(((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) == (LOCAL_ADD_USER|LOCAL_DELETE_USER)) || 
630            ((local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER)) && 
631                 ((remote_machine != NULL) || joining_domain))) {
632                 usage();
633         }
634         
635         /* Only load interfaces if we are doing network operations. */
636
637         if (joining_domain || remote_machine) {
638                 load_interfaces();
639         }
640
641         /* Join a domain */
642
643         if (joining_domain) {
644
645                 if (argc != 0)
646                         usage();
647
648                 /* Are we joining by specifing an admin username and
649                    password? */
650
651                 if (user_name[0]) {
652
653                         /* Get administrator password if not specified */
654
655                         if (!got_pass) {
656                                 char *pass = getpass("Password: ");
657
658                                 if (pass)
659                                         pstrcpy(user_password, pass);
660                         }
661                                 
662                         return join_domain_byuser(new_domain, remote_machine,
663                                                   user_name, user_password);
664                 } else {
665
666                         /* Or just with the server manager? */
667
668                         return join_domain(new_domain, remote_machine);
669                 }
670         }
671
672         /*
673          * Deal with root - can add a user, but only locally.
674          */
675
676         switch(argc) {
677         case 0:
678                 break;
679         case 1:
680                 fstrcpy(user_name, argv[0]);
681                 break;
682         case 2:
683                 fstrcpy(user_name, argv[0]);
684                 new_passwd = xstrdup(argv[1]);
685                 break;
686         default:
687                 usage();
688         }
689
690         if (!user_name[0] && (pwd = sys_getpwuid(0))) {
691                 fstrcpy(user_name, pwd->pw_name);
692         } 
693
694         if (!user_name[0]) {
695                 fprintf(stderr,"You must specify a username\n");
696                 exit(1);
697         }
698
699         if (local_flags & LOCAL_TRUST_ACCOUNT) {
700                 /* add the $ automatically */
701                 static fstring buf;
702
703                 /*
704                  * Remove any trailing '$' before we
705                  * generate the initial machine password.
706                  */
707
708                 if (user_name[strlen(user_name)-1] == '$') {
709                         user_name[strlen(user_name)-1] = 0;
710                 }
711
712                 if (local_flags & LOCAL_ADD_USER) {
713                         safe_free(new_passwd);
714                         new_passwd = xstrdup(user_name);
715                         strlower(new_passwd);
716                 }
717
718                 /*
719                  * Now ensure the username ends in '$' for
720                  * the machine add.
721                  */
722
723                 slprintf(buf, sizeof(buf)-1, "%s$", user_name);
724                 fstrcpy(user_name, buf);
725         }
726
727         if (remote_machine != NULL) {
728                 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
729         }
730         
731         if (!new_passwd) {
732
733                 /*
734                  * If we are trying to enable a user, first we need to find out
735                  * if they are using a modern version of the smbpasswd file that
736                  * disables a user by just writing a flag into the file. If so
737                  * then we can re-enable a user without prompting for a new
738                  * password. If not (ie. they have a no stored password in the
739                  * smbpasswd file) then we need to prompt for a new password.
740                  */
741
742                 if(local_flags & LOCAL_ENABLE_USER) {
743                         SAM_ACCOUNT *sampass = NULL;
744                         BOOL ret;
745                         
746                         pdb_init_sam(&sampass);
747                         ret = pdb_getsampwnam(sampass, user_name);
748                         if((sampass != False) && (pdb_get_lanman_passwd(sampass) != NULL)) {
749                                 new_passwd = xstrdup("XXXX"); /* Don't care. */
750                         }
751                         pdb_free_sam(sampass);
752                 }
753
754                 if(!new_passwd)
755                         new_passwd = prompt_for_new_password(stdin_passwd_get);
756
757                 if(!new_passwd) {
758                         fprintf(stderr, "Unable to get new password.\n");
759                         exit(1);
760                 }
761         }
762         
763         if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
764                 fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
765                 result = 1;
766                 goto done;
767         } 
768
769         if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD))) {
770                 SAM_ACCOUNT *sampass = NULL;
771                 BOOL ret;
772                 
773                 pdb_init_sam(&sampass);
774                 ret = pdb_getsampwnam(sampass, user_name);
775
776                 printf("Password changed for user %s.", user_name );
777                 if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
778                         printf(" User has disabled flag set.");
779                 if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
780                         printf(" User has no password flag set.");
781                 printf("\n");
782                 pdb_free_sam(sampass);
783         }
784
785  done:
786         safe_free(new_passwd);
787         return result;
788 }
789
790
791 /*************************************************************
792 handle password changing for non-root
793 *************************************************************/
794 static int process_nonroot(int argc, char *argv[])
795 {
796         struct passwd  *pwd = NULL;
797         int result = 0, ch;
798         BOOL stdin_passwd_get = False;
799         char *old_passwd = NULL;
800         char *remote_machine = NULL;
801         char *user_name = NULL;
802         char *new_passwd = NULL;
803
804         while ((ch = getopt(argc, argv, "hD:r:sU:")) != EOF) {
805                 switch(ch) {
806                 case 'D':
807                         DEBUGLEVEL = atoi(optarg);
808                         break;
809                 case 'r':
810                         remote_machine = optarg;
811                         break;
812                 case 's':
813                         set_line_buffering(stdin);
814                         set_line_buffering(stdout);
815                         set_line_buffering(stderr);
816                         stdin_passwd_get = True;
817                         break;
818                 case 'U':
819                         user_name = optarg;
820                         break;
821                 default:
822                         usage();
823                 }
824         }
825         
826         argc -= optind;
827         argv += optind;
828
829         if(argc > 1) {
830                 usage();
831         }
832         
833         if (argc == 1) {
834                 new_passwd = argv[0];
835         }
836         
837         if (!user_name) {
838                 pwd = sys_getpwuid(getuid());
839                 if (pwd) {
840                         user_name = xstrdup(pwd->pw_name);
841                 } else {
842                         fprintf(stderr,"you don't exist - go away\n");
843                         exit(1);
844                 }
845         }
846         
847         /*
848          * A non-root user is always setting a password
849          * via a remote machine (even if that machine is
850          * localhost).
851          */     
852
853         load_interfaces(); /* Delayed from main() */
854
855         if (remote_machine == NULL) {
856                 remote_machine = "127.0.0.1";
857         }
858
859         if (remote_machine != NULL) {
860                 old_passwd = get_pass("Old SMB password:",stdin_passwd_get);
861         }
862         
863         if (!new_passwd) {
864                 new_passwd = prompt_for_new_password(stdin_passwd_get);
865         }
866         
867         if (!new_passwd) {
868                 fprintf(stderr, "Unable to get new password.\n");
869                 exit(1);
870         }
871
872         if (!password_change(remote_machine, user_name, old_passwd, new_passwd, 0)) {
873                 fprintf(stderr,"Failed to change password for %s\n", user_name);
874                 result = 1;
875                 goto done;
876         }
877
878         printf("Password changed for user %s\n", user_name);
879
880  done:
881         safe_free(old_passwd);
882         safe_free(new_passwd);
883
884         return result;
885 }
886
887
888
889 /*********************************************************
890  Start here.
891 **********************************************************/
892 int main(int argc, char **argv)
893 {       
894         static pstring servicesf = CONFIGFILE;
895
896 #if defined(HAVE_SET_AUTH_PARAMETERS)
897         set_auth_parameters(argc, argv);
898 #endif /* HAVE_SET_AUTH_PARAMETERS */
899
900         TimeInit();
901         
902         setup_logging("smbpasswd", True);
903         
904         if(!initialize_password_db(True)) {
905                 fprintf(stderr, "Can't setup password database vectors.\n");
906                 exit(1);
907         }
908
909         if (!lp_load(servicesf,True,False,False)) {
910                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
911                         servicesf);
912                 exit(1);
913         }
914
915         /*
916          * Set the machine NETBIOS name if not already
917          * set from the config file. 
918          */ 
919     
920         if (!*global_myname) {   
921                 char *p;
922                 fstrcpy(global_myname, myhostname());
923                 p = strchr_m(global_myname, '.' );
924                 if (p) *p = 0;
925         }           
926         strupper(global_myname);
927
928         secrets_init();
929
930         /* Check the effective uid - make sure we are not setuid */
931         if ((geteuid() == (uid_t)0) && (getuid() != (uid_t)0)) {
932                 fprintf(stderr, "smbpasswd must *NOT* be setuid root.\n");
933                 exit(1);
934         }
935
936         /* pre-check for local mode option as first option. We can't
937            do this via normal getopt as getopt can't be called
938            twice. */
939         if (argc > 1 && strcmp(argv[1], "-L") == 0) {
940                 local_mode = True;
941         }
942
943         if (local_mode || getuid() == 0) {
944                 return process_root(argc, argv);
945         } 
946
947         return process_nonroot(argc, argv);
948 }