[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / utils / pdbedit.c
index 013eab18ce502c22128b82689eefa49de42c510a..66a706d1ce99c990ffa4a5bd1e29358495ba8305 100644 (file)
@@ -8,7 +8,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
+   the 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,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -96,7 +95,7 @@ static int export_database (struct pdb_methods *in,
                                return 1;
                        }
 
-                       printf("Importing accout for %s...", user->username);
+                       printf("Importing account for %s...", user->username);
                        if ( !NT_STATUS_IS_OK(out->getsampwnam( out, account, user->username )) ) {
                                status = out->add_sam_account(out, user);
                        } else {
@@ -285,14 +284,14 @@ static int print_sam_info (struct samu *sam_pwent, BOOL verbosity, BOOL smbpwdst
                       lm_passwd,
                       nt_passwd,
                       pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
-                      (uint32)pdb_get_pass_last_set_time(sam_pwent));
+                      (uint32)convert_time_t_to_uint32(pdb_get_pass_last_set_time(sam_pwent)));
        } else {
                uid = nametouid(pdb_get_username(sam_pwent));
                printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid, 
                        pdb_get_fullname(sam_pwent));
        }
 
-       return 0;       
+       return 0;
 }
 
 /*********************************************************
@@ -573,6 +572,7 @@ static int new_user (struct pdb_methods *in, const char *username,
                        
                        if (sscanf(user_sid, "%d", &u_rid) != 1) {
                                fprintf(stderr, "Error passed string is not a complete user SID or RID!\n");
+                               TALLOC_FREE(sam_pwent);
                                return -1;
                        }
                        sid_copy(&u_sid, get_global_sam_sid());
@@ -623,28 +623,25 @@ static int new_machine (struct pdb_methods *in, const char *machine_in)
        fstrcpy(machineaccount, machinename);
        fstrcat(machineaccount, "$");
 
-       if ((pwd = getpwnam_alloc(NULL, machineaccount))) {
-
-               if ( (sam_pwent = samu_new( NULL )) == NULL ) {
-                       fprintf(stderr, "Memory allocation error!\n");
-                       TALLOC_FREE(pwd);
-                       return -1;
-               }
+       if ( !(pwd = getpwnam_alloc( NULL, machineaccount )) ) {
+               DEBUG(0,("Cannot locate Unix account for %s\n", machineaccount));
+               return -1;
+       }
 
-               if ( !NT_STATUS_IS_OK(samu_set_unix(sam_pwent, pwd )) ) {
-                       fprintf(stderr, "Could not init sam from pw\n");
-                       TALLOC_FREE(pwd);
-                       return -1;
-               }
+       if ( (sam_pwent = samu_new( NULL )) == NULL ) {
+               fprintf(stderr, "Memory allocation error!\n");
+               TALLOC_FREE(pwd);
+               return -1;
+       }
 
+       if ( !NT_STATUS_IS_OK(samu_alloc_rid_unix(sam_pwent, pwd )) ) {
+               fprintf(stderr, "Could not init sam from pw\n");
                TALLOC_FREE(pwd);
-       } else {
-               if ( (sam_pwent = samu_new( NULL )) == NULL ) {
-                       fprintf(stderr, "Could not init sam from pw\n");
-                       return -1;
-               }
+               return -1;
        }
 
+       TALLOC_FREE(pwd);
+
        pdb_set_plaintext_passwd (sam_pwent, machinename);
        pdb_set_username (sam_pwent, machineaccount, PDB_CHANGED);      
        pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST, PDB_CHANGED);
@@ -760,6 +757,8 @@ int main (int argc, char **argv)
        static char *pwd_time_format = NULL;
        static BOOL pw_from_stdin = False;
        struct pdb_methods *bin, *bout, *bdef;
+       char *configfile = NULL;
+       TALLOC_CTX *frame = talloc_stackframe();
        poptContext pc;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
@@ -797,6 +796,13 @@ int main (int argc, char **argv)
                POPT_TABLEEND
        };
        
+       /* we shouldn't have silly checks like this */
+       if (getuid() != 0) {
+               d_fprintf(stderr, "You must be root to use pdbedit\n");
+               TALLOC_FREE(frame);
+               return -1;
+       }
+       
        bin = bout = bdef = NULL;
 
        load_case_tables();
@@ -811,6 +817,9 @@ int main (int argc, char **argv)
                case 'C':
                        account_policy_value_set = True;
                        break;
+               case 's':
+                       configfile = optarg;
+                       break;
                }
        }
 
@@ -824,7 +833,7 @@ int main (int argc, char **argv)
                exit(1);
        }
 
-       if(!initialize_password_db(False))
+       if(!initialize_password_db(False, NULL))
                exit(1);
 
        if (!init_names())
@@ -879,12 +888,18 @@ int main (int argc, char **argv)
                uint32 value;
                int field = account_policy_name_to_fieldnum(account_policy);
                if (field == 0) {
-                       char *apn = account_policy_names_list();
-                       fprintf(stderr, "No account policy by that name\n");
-                       if (apn) {
-                               fprintf(stderr, "Account policy names are :\n%s\n", apn);
+                       const char **names;
+                       int count;
+                       int i;
+                       account_policy_names_list(&names, &count);
+                       fprintf(stderr, "No account policy by that name!\n");
+                       if (count !=0) {
+                               fprintf(stderr, "Account policy names are:\n");
+                               for (i = 0; i < count ; i++) {
+                                       d_fprintf(stderr, "%s\n", names[i]);
+                               }
                        }
-                       SAFE_FREE(apn);
+                       SAFE_FREE(names);
                        exit(1);
                }
                if (!pdb_get_account_policy(field, &value)) {
@@ -1036,5 +1051,6 @@ int main (int argc, char **argv)
        }
        poptPrintHelp(pc, stderr, 0);
 
+       TALLOC_FREE(frame);
        return 1;
 }