net: use netapi function to list users.
[samba.git] / source3 / utils / pdbedit.c
index b87e88e406ec9f9b8053070c31824aa6bb49a0c1..e1d6709073aee32bdca32469e8f776ddd0f9166f 100644 (file)
@@ -61,69 +61,85 @@ static int export_database (struct pdb_methods *in,
                             struct pdb_methods *out, 
                             const char *username) 
 {
-       struct samu *user = NULL;
        NTSTATUS status;
+       struct pdb_search *u_search;
+       struct samr_displayentry userentry;
 
        DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
 
-       status = in->setsampwent(in, 0, 0);
-       if ( NT_STATUS_IS_ERR(status) ) {
-               fprintf(stderr, "Unable to set account database iterator for %s!\n", 
-                       in->name);
+       u_search = pdb_search_init(PDB_USER_SEARCH);
+       if (u_search == NULL) {
+               DEBUG(0, ("pdb_search_init failed\n"));
                return 1;
        }
 
-       if ( ( user = samu_new( NULL ) ) == NULL ) {
-               fprintf(stderr, "export_database: Memory allocation failure!\n");
+       if (!in->search_users(in, u_search, 0)) {
+               DEBUG(0, ("Could not start searching users\n"));
+               pdb_search_destroy(u_search);
                return 1;
        }
 
-       while ( NT_STATUS_IS_OK(in->getsampwent(in, user)) ) 
-       {
-               DEBUG(4, ("Processing account %s\n", user->username));
+       while (u_search->next_entry(u_search, &userentry)) {
+               struct samu *user;
+               struct samu *account;
+               DOM_SID user_sid;
 
-               /* If we don't have a specific user or if we do and 
-                  the login name matches */
+               DEBUG(4, ("Processing account %s\n", userentry.account_name));
 
-               if ( !username || (strcmp(username, user->username) == 0)) {
-                       struct samu *account;
+               if ((username != NULL)
+                   && (strcmp(username, userentry.account_name) != 0)) {
+                       /*
+                        * ignore unwanted users
+                        */
+                       continue;
+               }
 
-                       if ( (account = samu_new( NULL )) == NULL ) {
-                               fprintf(stderr, "export_database: Memory allocation failure!\n");
-                               TALLOC_FREE( user );
-                               in->endsampwent( in );
-                               return 1;
-                       }
+               user = samu_new(talloc_tos());
+               if (user == NULL) {
+                       DEBUG(0, ("talloc failed\n"));
+                       break;
+               }
 
-                       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 {
-                               status = out->update_sam_account( out, user );
-                       }
+               sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
 
-                       if ( NT_STATUS_IS_OK(status) ) {
-                               printf( "ok\n");
-                       } else {
-                               printf( "failed\n");
-                       }
+               status = in->getsampwsid(in, user, &user_sid);
 
-                       TALLOC_FREE( account );
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(2, ("getsampwsid failed: %s\n",
+                                 nt_errstr(status)));
+                       TALLOC_FREE(user);
+                       continue;
                }
 
-               /* clean up and get ready for another run */
+               account = samu_new(NULL);
+               if (account == NULL) {
+                       fprintf(stderr, "export_database: Memory allocation "
+                               "failure!\n");
+                       TALLOC_FREE( user );
+                       pdb_search_destroy(u_search);
+                       return 1;
+               }
 
-               TALLOC_FREE( user );
+               printf("Importing account for %s...", user->username);
+               status = out->getsampwnam(out, account, user->username);
 
-               if ( ( user = samu_new( NULL ) ) == NULL ) {
-                       fprintf(stderr, "export_database: Memory allocation failure!\n");
-                       return 1;
+               if (NT_STATUS_IS_OK(status)) {
+                       status = out->update_sam_account( out, user );
+               } else {
+                       status = out->add_sam_account(out, user);
                }
-       }
 
-       TALLOC_FREE( user );
+               if ( NT_STATUS_IS_OK(status) ) {
+                       printf( "ok\n");
+               } else {
+                       printf( "failed\n");
+               }
+
+               TALLOC_FREE( account );
+               TALLOC_FREE( user );
+       }
 
-       in->endsampwent(in);
+       pdb_search_destroy(u_search);
 
        return 0;
 }
@@ -221,18 +237,18 @@ static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdst
 
        /* TODO: chaeck if entry is a user or a workstation */
        if (!sam_pwent) return -1;
-       
+
        if (verbosity) {
-               pstring temp;
+               char temp[44];
                const uint8 *hours;
-               
+
                printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
                printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
                printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
                printf ("User SID:             %s\n",
-                       sid_string_static(pdb_get_user_sid(sam_pwent)));
+                       sid_string_tos(pdb_get_user_sid(sam_pwent)));
                printf ("Primary Group SID:    %s\n",
-                       sid_string_static(pdb_get_group_sid(sam_pwent)));
+                       sid_string_tos(pdb_get_group_sid(sam_pwent)));
                printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
                printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
                printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
@@ -242,34 +258,34 @@ static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdst
                printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
                printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
                printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
-               
+
                tmp = pdb_get_logon_time(sam_pwent);
                printf ("Logon time:           %s\n", tmp ? http_timestring(tmp) : "0");
-               
+
                tmp = pdb_get_logoff_time(sam_pwent);
                printf ("Logoff time:          %s\n", tmp ? http_timestring(tmp) : "0");
-               
+
                tmp = pdb_get_kickoff_time(sam_pwent);
                printf ("Kickoff time:         %s\n", tmp ? http_timestring(tmp) : "0");
-               
+
                tmp = pdb_get_pass_last_set_time(sam_pwent);
                printf ("Password last set:    %s\n", tmp ? http_timestring(tmp) : "0");
-               
+
                tmp = pdb_get_pass_can_change_time(sam_pwent);
                printf ("Password can change:  %s\n", tmp ? http_timestring(tmp) : "0");
-               
+
                tmp = pdb_get_pass_must_change_time(sam_pwent);
                printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
 
                tmp = pdb_get_bad_password_time(sam_pwent);
                printf ("Last bad password   : %s\n", tmp ? http_timestring(tmp) : "0");
-               printf ("Bad password count  : %d\n", 
+               printf ("Bad password count  : %d\n",
                        pdb_get_bad_password_count(sam_pwent));
-               
+
                hours = pdb_get_hours(sam_pwent);
                pdb_sethexhours(temp, hours);
                printf ("Logon hours         : %s\n", temp);
-               
+
        } else if (smbpwdstyle) {
                char lm_passwd[33];
                char nt_passwd[33];
@@ -277,7 +293,7 @@ static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdst
                uid = nametouid(pdb_get_username(sam_pwent));
                pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
                pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
-                       
+
                printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
                       pdb_get_username(sam_pwent),
                       (unsigned long)uid,
@@ -326,33 +342,50 @@ static int print_user_info (struct pdb_methods *in, const char *username, bool v
 **********************************************************/
 static int print_users_list (struct pdb_methods *in, bool verbosity, bool smbpwdstyle)
 {
-       struct samu *sam_pwent=NULL;
-       bool check;
-       
-       check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
-       if (!check) {
+       struct pdb_search *u_search;
+       struct samr_displayentry userentry;
+
+       u_search = pdb_search_init(PDB_USER_SEARCH);
+       if (u_search == NULL) {
+               DEBUG(0, ("pdb_search_init failed\n"));
                return 1;
        }
 
-       check = True;
-       if ( (sam_pwent = samu_new( NULL )) == NULL ) {
+       if (!in->search_users(in, u_search, 0)) {
+               DEBUG(0, ("Could not start searching users\n"));
+               pdb_search_destroy(u_search);
                return 1;
        }
 
-       while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
+       while (u_search->next_entry(u_search, &userentry)) {
+               struct samu *sam_pwent;
+               DOM_SID user_sid;
+               NTSTATUS status;
+
+               sam_pwent = samu_new(talloc_tos());
+               if (sam_pwent == NULL) {
+                       DEBUG(0, ("talloc failed\n"));
+                       break;
+               }
+
+               sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
+
+               status = in->getsampwsid(in, sam_pwent, &user_sid);
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(2, ("getsampwsid failed: %s\n",
+                                 nt_errstr(status)));
+                       TALLOC_FREE(sam_pwent);
+                       continue;
+               }
+
                if (verbosity)
                        printf ("---------------\n");
                print_sam_info (sam_pwent, verbosity, smbpwdstyle);
                TALLOC_FREE(sam_pwent);
-               
-               if ( (sam_pwent = samu_new( NULL )) == NULL ) {
-                       check = False;
-               }
        }
-       if (check) 
-               TALLOC_FREE(sam_pwent);
-       
-       in->endsampwent(in);
+       pdb_search_destroy(u_search);
+
        return 0;
 }
 
@@ -361,38 +394,50 @@ static int print_users_list (struct pdb_methods *in, bool verbosity, bool smbpwd
 **********************************************************/
 static int fix_users_list (struct pdb_methods *in)
 {
-       struct samu *sam_pwent=NULL;
-       bool check;
-       
-       check = NT_STATUS_IS_OK(in->setsampwent(in, False, 0));
-       if (!check) {
+       struct pdb_search *u_search;
+       struct samr_displayentry userentry;
+
+       u_search = pdb_search_init(PDB_USER_SEARCH);
+       if (u_search == NULL) {
+               DEBUG(0, ("pdb_search_init failed\n"));
                return 1;
        }
 
-       check = True;
-       if ( (sam_pwent = samu_new( NULL )) == NULL ) {
+       if (!in->search_users(in, u_search, 0)) {
+               DEBUG(0, ("Could not start searching users\n"));
+               pdb_search_destroy(u_search);
                return 1;
        }
 
-       while (check && NT_STATUS_IS_OK(in->getsampwent (in, sam_pwent))) {
-               printf("Updating record for user %s\n", pdb_get_username(sam_pwent));
-       
-               if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
-                       printf("Update of user %s failed!\n", pdb_get_username(sam_pwent));
+       while (u_search->next_entry(u_search, &userentry)) {
+               struct samu *sam_pwent;
+               DOM_SID user_sid;
+               NTSTATUS status;
+
+               sam_pwent = samu_new(talloc_tos());
+               if (sam_pwent == NULL) {
+                       DEBUG(0, ("talloc failed\n"));
+                       break;
                }
-               TALLOC_FREE(sam_pwent);
-               if ( (sam_pwent = samu_new( NULL )) == NULL ) {
-                       check = False;
+
+               sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
+
+               status = in->getsampwsid(in, sam_pwent, &user_sid);
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(2, ("getsampwsid failed: %s\n",
+                                 nt_errstr(status)));
+                       TALLOC_FREE(sam_pwent);
+                       continue;
                }
-               if (!check) {
-                       fprintf(stderr, "Failed to initialise new struct samu structure (out of memory?)\n");
+
+               if (!NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
+                       printf("Update of user %s failed!\n",
+                              pdb_get_username(sam_pwent));
                }
-                       
-       }
-       if (check) 
                TALLOC_FREE(sam_pwent);
-       
-       in->endsampwent(in);
+       }
+       pdb_search_destroy(u_search);
        return 0;
 }
 
@@ -723,13 +768,13 @@ static int delete_machine_entry (struct pdb_methods *in, const char *machinename
 
 int main (int argc, char **argv)
 {
-       static bool list_users = False;
-       static bool verbose = False;
-       static bool spstyle = False;
-       static bool machine = False;
-       static bool add_user = False;
-       static bool delete_user = False;
-       static bool modify_user = False;
+       static int list_users = False;
+       static int verbose = False;
+       static int spstyle = False;
+       static int machine = False;
+       static int add_user = False;
+       static int delete_user = False;
+       static int modify_user = False;
        uint32  setparms, checkparms;
        int opt;
        static char *full_name = NULL;
@@ -740,10 +785,10 @@ int main (int argc, char **argv)
        static char *backend = NULL;
        static char *backend_in = NULL;
        static char *backend_out = NULL;
-       static bool transfer_groups = False;
-       static bool transfer_account_policies = False;
-       static bool reset_account_policies = False;
-       static bool  force_initialised_password = False;
+       static int transfer_groups = False;
+       static int transfer_account_policies = False;
+       static int reset_account_policies = False;
+       static int  force_initialised_password = False;
        static char *logon_script = NULL;
        static char *profile_path = NULL;
        static char *user_domain = NULL;
@@ -752,10 +797,10 @@ int main (int argc, char **argv)
        static char *user_sid = NULL;
        static long int account_policy_value = 0;
        bool account_policy_value_set = False;
-       static bool badpw_reset = False;
-       static bool hours_reset = False;
+       static int badpw_reset = False;
+       static int hours_reset = False;
        static char *pwd_time_format = NULL;
-       static bool pw_from_stdin = False;
+       static int pw_from_stdin = False;
        struct pdb_methods *bin, *bout, *bdef;
        char *configfile = NULL;
        TALLOC_CTX *frame = talloc_stackframe();
@@ -828,8 +873,8 @@ int main (int argc, char **argv)
        if (user_name == NULL)
                user_name = poptGetArg(pc);
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False,True)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
+       if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True)) {
+               fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
                exit(1);
        }