r23779: Change from v2 or later to v3 or later.
[ira/wip.git] / source3 / utils / smbpasswd.c
index eade5331af658f5d5ddb7333b56a2d0e7c011262..1c8b503b4d68522a6f35c93647e58c2463ffa030 100644 (file)
@@ -5,7 +5,7 @@
  * 
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
+ * Free Software Foundation; either version 3 of the License, or (at your
  * option) any later version.
  * 
  * This program is distributed in the hope that it will be useful, but WITHOUT
@@ -29,28 +29,14 @@ extern char *optarg;
 extern int optind;
 
 /* forced running in root-mode */
-static BOOL got_pass = False, got_username = False;
+static BOOL got_username = False;
 static BOOL stdin_passwd_get = False;
-static fstring user_name, user_password;
+static fstring user_name;
 static char *new_passwd = NULL;
 static const char *remote_machine = NULL;
 
 static fstring ldap_secret;
 
-/*****************************************************************************
- stubb functions
-****************************************************************************/
-
-void become_root( void )
-{
-        return;
-}
-
-void unbecome_root( void )
-{
-        return;
-}
-
 
 /*********************************************************
  Print command usage on stderr and die.
@@ -58,9 +44,9 @@ void unbecome_root( void )
 static void usage(void)
 {
        printf("When run by root:\n");
-       printf("    smbpasswd [options] [username] [password]\n");
+       printf("    smbpasswd [options] [username]\n");
        printf("otherwise:\n");
-       printf("    smbpasswd [options] [password]\n\n");
+       printf("    smbpasswd [options]\n\n");
 
        printf("options:\n");
        printf("  -L                   local mode (must be first option)\n");
@@ -78,7 +64,8 @@ static void usage(void)
        printf("  -i                   interdomain trust account\n");
        printf("  -m                   machine trust account\n");
        printf("  -n                   set no password\n");
-       printf("  -w                   ldap admin password\n");
+       printf("  -W                   use stdin ldap admin password\n");
+       printf("  -w PASSWORD          ldap admin password\n");
        printf("  -x                   delete user\n");
        printf("  -R ORDER             name resolve order\n");
 
@@ -93,6 +80,7 @@ static void set_line_buffering(FILE *f)
 /*******************************************************************
  Process command line options
  ******************************************************************/
+
 static int process_options(int argc, char **argv, int local_flags)
 {
        int ch;
@@ -102,11 +90,10 @@ static int process_options(int argc, char **argv, int local_flags)
        local_flags |= LOCAL_SET_PASSWORD;
 
        ZERO_STRUCT(user_name);
-       ZERO_STRUCT(user_password);
 
        user_name[0] = '\0';
 
-       while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:L")) != EOF) {
+       while ((ch = getopt(argc, argv, "c:axdehminjr:sw:R:D:U:LW")) != EOF) {
                switch(ch) {
                case 'L':
                        local_flags |= LOCAL_AM_ROOT;
@@ -164,19 +151,12 @@ static int process_options(int argc, char **argv, int local_flags)
                        DEBUGLEVEL = atoi(optarg);
                        break;
                case 'U': {
-                       char *lp;
-
                        got_username = True;
                        fstrcpy(user_name, optarg);
-
-                       if ((lp = strchr(user_name, '%'))) {
-                               *lp = 0;
-                               fstrcpy(user_password, lp + 1);
-                               got_pass = True;
-                               memset(strchr_m(optarg, '%') + 1, 'X',
-                                      strlen(user_password));
-                       }
-
+                       break;
+               case 'W':
+                       local_flags |= LOCAL_SET_LDAP_ADMIN_PW;
+                       *ldap_secret = '\0';
                        break;
                }
                case 'h':
@@ -195,7 +175,7 @@ static int process_options(int argc, char **argv, int local_flags)
                break;
        case 1:
                if (!(local_flags & LOCAL_AM_ROOT)) {
-                       new_passwd = argv[0];
+                       usage();
                } else {
                        if (got_username) {
                                usage();
@@ -204,69 +184,19 @@ static int process_options(int argc, char **argv, int local_flags)
                        }
                }
                break;
-       case 2:
-               if (!(local_flags & LOCAL_AM_ROOT) || got_username || got_pass) {
-                       usage();
-               }
-
-               fstrcpy(user_name, argv[0]);
-               new_passwd = smb_xstrdup(argv[1]);
-               break;
        default:
                usage();
        }
 
-       if (!lp_load(configfile,True,False,False)) {
+       if (!lp_load(configfile,True,False,False,True)) {
                fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
-                       dyn_CONFIGFILE);
+                       configfile);
                exit(1);
        }
 
        return local_flags;
 }
 
-/*************************************************************
- Utility function to prompt for passwords from stdin. Each
- password entered must end with a newline.
-*************************************************************/
-static char *stdin_new_passwd(void)
-{
-       static fstring new_pw;
-       size_t len;
-
-       ZERO_ARRAY(new_pw);
-
-       /*
-        * if no error is reported from fgets() and string at least contains
-        * the newline that ends the password, then replace the newline with
-        * a null terminator.
-        */
-       if ( fgets(new_pw, sizeof(new_pw), stdin) != NULL) {
-               if ((len = strlen(new_pw)) > 0) {
-                       if(new_pw[len-1] == '\n')
-                               new_pw[len - 1] = 0; 
-               }
-       }
-       return(new_pw);
-}
-
-
-/*************************************************************
- Utility function to get passwords via tty or stdin
- Used if the '-s' option is set to silently get passwords
- to enable scripting.
-*************************************************************/
-static char *get_pass( const char *prompt, BOOL stdin_get)
-{
-       char *p;
-       if (stdin_get) {
-               p = stdin_new_passwd();
-       } else {
-               p = getpass(prompt);
-       }
-       return smb_xstrdup(p);
-}
-
 /*************************************************************
  Utility function to prompt for new password.
 *************************************************************/
@@ -299,10 +229,11 @@ static char *prompt_for_new_password(BOOL stdin_get)
  Change a password either locally or remotely.
 *************************************************************/
 
-static BOOL password_change(const char *remote_mach, char *username, 
-                           char *old_passwd, char *new_pw, int local_flags)
+static NTSTATUS password_change(const char *remote_mach, char *username, 
+                               char *old_passwd, char *new_pw,
+                               int local_flags)
 {
-       BOOL ret;
+       NTSTATUS ret;
        pstring err_str;
        pstring msg_str;
 
@@ -310,12 +241,12 @@ static BOOL password_change(const char *remote_mach, char *username,
                if (local_flags & (LOCAL_ADD_USER|LOCAL_DELETE_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|
                                                        LOCAL_TRUST_ACCOUNT|LOCAL_SET_NO_PASSWORD)) {
                        /* these things can't be done remotely yet */
-                       return False;
+                       return NT_STATUS_UNSUCCESSFUL;
                }
                ret = remote_password_change(remote_mach, username, 
                                             old_passwd, new_pw, err_str, sizeof(err_str));
                if(*err_str)
-                       fprintf(stderr, err_str);
+                       fprintf(stderr, "%s", err_str);
                return ret;
        }
        
@@ -323,9 +254,9 @@ static BOOL password_change(const char *remote_mach, char *username,
                                     err_str, sizeof(err_str), msg_str, sizeof(msg_str));
 
        if(*msg_str)
-               printf(msg_str);
+               printf("%s", msg_str);
        if(*err_str)
-               fprintf(stderr, err_str);
+               fprintf(stderr, "%s", err_str);
 
        return ret;
 }
@@ -355,15 +286,33 @@ static int process_root(int local_flags)
        int result = 0;
        char *old_passwd = NULL;
 
-       if (local_flags & LOCAL_SET_LDAP_ADMIN_PW)
-       {
-               printf("Setting stored password for \"%s\" in secrets.tdb\n", 
-                       lp_ldap_admin_dn());
-               if (!store_ldap_admin_pw(ldap_secret))
+       if (local_flags & LOCAL_SET_LDAP_ADMIN_PW) {
+               char *ldap_admin_dn = lp_ldap_admin_dn();
+               if ( ! *ldap_admin_dn ) {
+                       DEBUG(0,("ERROR: 'ldap admin dn' not defined! Please check your smb.conf\n"));
+                       goto done;
+               }
+
+               printf("Setting stored password for \"%s\" in secrets.tdb\n", ldap_admin_dn);
+               if ( ! *ldap_secret ) {
+                       new_passwd = prompt_for_new_password(stdin_passwd_get);
+                       fstrcpy(ldap_secret, new_passwd);
+               }
+               if (!store_ldap_admin_pw(ldap_secret)) {
                        DEBUG(0,("ERROR: Failed to store the ldap admin password!\n"));
+               }
                goto done;
        }
 
+       /* Ensure passdb startup(). */
+       if(!initialize_password_db(False, NULL)) {
+               DEBUG(0, ("Failed to open passdb!\n"));
+               exit(1);
+       }
+               
+       /* Ensure we have a SAM sid. */
+       get_global_sam_sid();
+
        /*
         * Ensure both add/delete user are not set
         * Ensure add/delete user and either remote machine or join domain are
@@ -381,9 +330,9 @@ static int process_root(int local_flags)
                load_interfaces();
        }
 
-       if (!user_name[0] && (pwd = getpwuid_alloc(geteuid()))) {
+       if (!user_name[0] && (pwd = getpwuid_alloc(NULL, geteuid()))) {
                fstrcpy(user_name, pwd->pw_name);
-               passwd_free(&pwd);
+               TALLOC_FREE(pwd);
        } 
 
        if (!user_name[0]) {
@@ -453,15 +402,23 @@ static int process_root(int local_flags)
                         */
                        
                        if(local_flags & LOCAL_ENABLE_USER) {
-                               SAM_ACCOUNT *sampass = NULL;
-                               BOOL ret;
+                               struct samu *sampass = NULL;
                                
-                               pdb_init_sam(&sampass);
-                               ret = pdb_getsampwnam(sampass, user_name);
-                               if((sampass != False) && (pdb_get_lanman_passwd(sampass) == NULL)) {
+                               sampass = samu_new( NULL );
+                               if (!sampass) {
+                                       fprintf(stderr, "talloc fail for struct samu.\n");
+                                       exit(1);
+                               }
+                               if (!pdb_getsampwnam(sampass, user_name)) {
+                                       fprintf(stderr, "Failed to find user %s in passdb backend.\n",
+                                               user_name );
+                                       exit(1);
+                               }
+
+                               if(pdb_get_nt_passwd(sampass) == NULL) {
                                        local_flags |= LOCAL_SET_PASSWORD;
                                }
-                               pdb_free_sam(&sampass);
+                               TALLOC_FREE(sampass);
                        }
                }
                
@@ -475,7 +432,9 @@ static int process_root(int local_flags)
                }
        }
 
-       if (!password_change(remote_machine, user_name, old_passwd, new_passwd, local_flags)) {
+       if (!NT_STATUS_IS_OK(password_change(remote_machine, user_name,
+                                            old_passwd, new_passwd,
+                                            local_flags))) {
                fprintf(stderr,"Failed to modify password entry for user %s\n", user_name);
                result = 1;
                goto done;
@@ -484,19 +443,29 @@ static int process_root(int local_flags)
        if(remote_machine) {
                printf("Password changed for user %s on %s.\n", user_name, remote_machine );
        } else if(!(local_flags & (LOCAL_ADD_USER|LOCAL_DISABLE_USER|LOCAL_ENABLE_USER|LOCAL_DELETE_USER|LOCAL_SET_NO_PASSWORD|LOCAL_SET_PASSWORD))) {
-               SAM_ACCOUNT *sampass = NULL;
-               BOOL ret;
+               struct samu *sampass = NULL;
                
-               pdb_init_sam(&sampass);
-               ret = pdb_getsampwnam(sampass, user_name);
+               sampass = samu_new( NULL );
+               if (!sampass) {
+                       fprintf(stderr, "talloc fail for struct samu.\n");
+                       exit(1);
+               }
+
+               if (!pdb_getsampwnam(sampass, user_name)) {
+                       fprintf(stderr, "Failed to find user %s in passdb backend.\n",
+                               user_name );
+                       exit(1);
+               }
 
                printf("Password changed for user %s.", user_name );
-               if( (ret != False) && (pdb_get_acct_ctrl(sampass)&ACB_DISABLED) )
+               if(pdb_get_acct_ctrl(sampass)&ACB_DISABLED) {
                        printf(" User has disabled flag set.");
-               if((ret != False) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) )
+               }
+               if(pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ) {
                        printf(" User has no password flag set.");
+               }
                printf("\n");
-               pdb_free_sam(&sampass);
+               TALLOC_FREE(sampass);
        }
 
  done:
@@ -522,12 +491,12 @@ static int process_nonroot(int local_flags)
        }
 
        if (!user_name[0]) {
-               pwd = getpwuid_alloc(getuid());
+               pwd = getpwuid_alloc(NULL, getuid());
                if (pwd) {
                        fstrcpy(user_name,pwd->pw_name);
-                       passwd_free(&pwd);
+                       TALLOC_FREE(pwd);
                } else {
-                       fprintf(stderr, "smbpasswd: you don't exist - go away\n");
+                       fprintf(stderr, "smbpasswd: cannot lookup user name for uid %u\n", (unsigned int)getuid());
                        exit(1);
                }
        }
@@ -559,7 +528,8 @@ static int process_nonroot(int local_flags)
                exit(1);
        }
 
-       if (!password_change(remote_machine, user_name, old_pw, new_pw, 0)) {
+       if (!NT_STATUS_IS_OK(password_change(remote_machine, user_name, old_pw,
+                                            new_pw, 0))) {
                fprintf(stderr,"Failed to change password for %s\n", user_name);
                result = 1;
                goto done;
@@ -593,6 +563,8 @@ int main(int argc, char **argv)
                local_flags = LOCAL_AM_ROOT;
        }
 
+       load_case_tables();
+
        local_flags = process_options(argc, argv, local_flags);
 
        setup_logging("smbpasswd", True);