Cleanups to make auto-detection of method on net user smoother.
[kai/samba.git] / source3 / utils / pdbedit.c
index 90c50d7e9ff058641a8d514f305386813ac50271..1fb1f2355b6596faf6cbb7ee090cdf792b8e5bbf 100644 (file)
@@ -1,7 +1,6 @@
 /* 
-   Unix SMB/Netbios implementation.
+   Unix SMB/CIFS implementation.
    passdb editing frontend
-   Version 3.0
    
    Copyright (C) Simo Sorce      2000
    Copyright (C) Andrew Bartlett 2001   
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-/* base uid for trust accounts is set to 60000 ! 
- * May be we should add the defines in smb.h to make it possible having 
- * different values on different platforms?
- */
-
-#define BASE_MACHINE_UID 60000
-#define MAX_MACHINE_UID 65500 /* 5500 trust accounts aren't enough? */
-
 #include "includes.h"
 
 extern pstring global_myname;
+extern BOOL AllowDebugChange;
 
 /*
  * Next two lines needed for SunOS and don't
@@ -65,6 +57,7 @@ static void usage(void)
        printf("     -m                it is a machine trust\n");
        printf("  -x                   delete this user\n");
        printf("  -i file              import account from file (smbpasswd style)\n");
+       printf("  -D debuglevel        set DEBUGELEVEL (default = 1)\n");
        exit(1);
 }
 
@@ -87,8 +80,8 @@ static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdst
                        gid = pdb_get_gid(sam_pwent);
                        printf ("user ID/Group:  %d/%d\n", uid, gid);
                }
-               printf ("user RID/GRID:  %u/%u\n", (unsigned int)sam_pwent->user_rid,
-                       (unsigned int)sam_pwent->group_rid);
+               printf ("user RID/GRID:  %u/%u\n", (unsigned int)pdb_get_user_rid(sam_pwent),
+                       (unsigned int)pdb_get_group_rid(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_dirdrive(sam_pwent));
@@ -138,7 +131,9 @@ static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
        SAM_ACCOUNT *sam_pwent=NULL;
        BOOL ret;
        
-       pdb_init_sam(&sam_pwent);
+       if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
+               return -1;
+       }
        
        ret = pdb_getsampwnam (sam_pwent, username);
 
@@ -160,26 +155,28 @@ static int print_user_info (char *username, BOOL verbosity, BOOL smbpwdstyle)
 static int print_users_list (BOOL verbosity, BOOL smbpwdstyle)
 {
        SAM_ACCOUNT *sam_pwent=NULL;
-       BOOL ret;
+       BOOL check, ret;
        
-       pdb_init_sam(&sam_pwent);
        errno = 0; /* testing --simo */
-       ret = pdb_setsampwent(False);
-       if (ret && errno == ENOENT) {
+       check = pdb_setsampwent(False);
+       if (check && errno == ENOENT) {
                fprintf (stderr,"Password database not found!\n");
-               pdb_free_sam(&sam_pwent);
                exit(1);
        }
 
-       while ((ret = pdb_getsampwent (sam_pwent))) {
+       check = True;
+       if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
+
+       while (check && (ret = pdb_getsampwent (sam_pwent))) {
                if (verbosity)
                        printf ("---------------\n");
                print_sam_info (sam_pwent, verbosity, smbpwdstyle);
-               pdb_reset_sam(sam_pwent);
+               pdb_free_sam(&sam_pwent);
+               check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
        }
+       if (check) pdb_free_sam(&sam_pwent);
        
-       pdb_endsampwent ();
-       pdb_free_sam(&sam_pwent);
+       pdb_endsampwent();
        return 0;
 }
 
@@ -212,7 +209,7 @@ static int set_user_info (char *username, char *fullname, char *homedir, char *d
        if (profile)
                pdb_set_profile_path (sam_pwent, profile, True);
        
-       if (pdb_update_sam_account (sam_pwent, True))
+       if (pdb_update_sam_account (sam_pwent))
                print_user_info (username, True, False);
        else {
                fprintf (stderr, "Unable to modify entry!\n");
@@ -234,12 +231,16 @@ static int new_user (char *username, char *fullname, char *homedir, char *drive,
        
        ZERO_STRUCT(sam_pwent);
 
-       if (!(pwd = sys_getpwnam(username))) {
-               fprintf (stderr, "User %s does not exist in system passwd!\n", username);
-               return -1;
+       if ((pwd = getpwnam_alloc(username))) {
+               pdb_init_sam_pw (&sam_pwent, pwd);
+               passwd_free(&pwd);
+       } else {
+               fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
+               pdb_init_sam(&sam_pwent);
+               if (!pdb_set_username(sam_pwent, username)) {
+                       return False;
+               }
        }
-       
-       pdb_init_sam_pw (&sam_pwent, pwd);
 
        password1 = getpass("new password:");
        password2 = getpass("retype new password:");
@@ -251,7 +252,6 @@ static int new_user (char *username, char *fullname, char *homedir, char *drive,
 
        pdb_set_plaintext_passwd(sam_pwent, password1);
 
-       pdb_set_username(sam_pwent, username);
        if (fullname)
                pdb_set_fullname(sam_pwent, fullname);
        if (homedir)
@@ -283,12 +283,12 @@ static int new_user (char *username, char *fullname, char *homedir, char *drive,
 static int new_machine (char *machinename)
 {
        SAM_ACCOUNT *sam_pwent=NULL;
-       SAM_ACCOUNT *sam_trust=NULL;
        char name[16];
        char *password = NULL;
-       uid_t uid;
        
-       pdb_init_sam (&sam_pwent);
+       if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
+               return -1;
+       }
 
        if (machinename[strlen (machinename) -1] == '$')
                machinename[strlen (machinename) -1] = '\0';
@@ -303,25 +303,10 @@ static int new_machine (char *machinename)
 
        pdb_set_username (sam_pwent, name);
        
-       for (uid=BASE_MACHINE_UID; uid<=MAX_MACHINE_UID; uid++) {
-               pdb_init_sam (&sam_trust);
-               if (pdb_getsampwrid (sam_trust, pdb_uid_to_user_rid (uid))) {
-                       pdb_free_sam (&sam_trust);
-               } else {
-                       break;
-               }
-       }
-
-       if (uid>MAX_MACHINE_UID) {
-               fprintf (stderr, "No more free UIDs available to Machine accounts!\n");
-               pdb_free_sam(&sam_pwent);               
-               return -1;
-       }
-
-       pdb_set_user_rid (sam_pwent,pdb_uid_to_user_rid (uid));
-       pdb_set_group_rid (sam_pwent, pdb_gid_to_group_rid (BASE_MACHINE_UID));
        pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
        
+       pdb_set_group_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS);
+       
        if (pdb_add_sam_account (sam_pwent)) {
                print_user_info (name, True, False);
        } else {
@@ -339,7 +324,18 @@ static int new_machine (char *machinename)
 
 static int delete_user_entry (char *username)
 {
-       return pdb_delete_sam_account (username);
+       SAM_ACCOUNT *samaccount = NULL;
+
+       if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
+               return -1;
+       }
+
+       if (!pdb_getsampwnam(samaccount, username)) {
+               fprintf (stderr, "user %s does not exist in the passdb\n", username);
+               return -1;
+       }
+
+       return pdb_delete_sam_account (samaccount);
 }
 
 /*********************************************************
@@ -349,11 +345,22 @@ static int delete_user_entry (char *username)
 static int delete_machine_entry (char *machinename)
 {
        char name[16];
+       SAM_ACCOUNT *samaccount = NULL;
        
        safe_strcpy (name, machinename, 16);
        if (name[strlen(name)] != '$')
                safe_strcat (name, "$", 16);
-       return pdb_delete_sam_account (name);
+
+       if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
+               return -1;
+       }
+
+       if (!pdb_getsampwnam(samaccount, name)) {
+               fprintf (stderr, "user %s does not exist in the passdb\n", name);
+               return -1;
+       }
+
+       return pdb_delete_sam_account (samaccount);
 }
 
 /*********************************************************
@@ -442,7 +449,7 @@ account without a valid local system user.\n", user_name);
                        return False;
                }
 
-               if (!pdb_init_sam_pw(&sam_pwent, pwd)) {
+               if (!NT_STATUS_IS_OK(pdb_init_sam_pw(&sam_pwent, pwd))) {
                        fprintf(stderr, "Failed initialise SAM_ACCOUNT for user %s.\n", user_name);
                        return False;
                }
@@ -563,18 +570,21 @@ int main (int argc, char **argv)
                return 0;
        }
        
-       if(!initialize_password_db(True)) {
-               fprintf(stderr, "Can't setup password database vectors.\n");
-               exit(1);
-       }
-       
+       DEBUGLEVEL = 1;
+       AllowDebugChange = False;
+
        if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
                fprintf(stderr, "Can't load %s - run testparm to debug it\n", 
                        dyn_CONFIGFILE);
                exit(1);
        }
        
-       while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != EOF) {
+       if(!initialize_password_db(True)) {
+               fprintf(stderr, "Can't setup password database vectors.\n");
+               exit(1);
+       }
+       
+       while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwxD:")) != EOF) {
                switch(ch) {
                case 'a':
                        add_user = True;
@@ -621,6 +631,9 @@ int main (int argc, char **argv)
                        import = True;
                        smbpasswd = optarg;
                        break;
+               case 'D':
+                       DEBUGLEVEL = atoi(optarg);
+                       break;
                default:
                        usage();
                }