Cleanups to make auto-detection of method on net user smoother.
[kai/samba.git] / source3 / utils / pdbedit.c
index 5202d8d3fe8e0317bbb802d220ada31f8f36462f..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   
@@ -58,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);
 }
 
@@ -131,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);
 
@@ -153,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;
 }
 
@@ -282,7 +286,9 @@ static int new_machine (char *machinename)
        char name[16];
        char *password = NULL;
        
-       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';
@@ -299,6 +305,8 @@ static int new_machine (char *machinename)
        
        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 {
@@ -316,9 +324,11 @@ static int new_machine (char *machinename)
 
 static int delete_user_entry (char *username)
 {
-       SAM_ACCOUNT *samaccount;
+       SAM_ACCOUNT *samaccount = NULL;
 
-       pdb_init_sam(&samaccount);
+       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);
@@ -335,13 +345,15 @@ static int delete_user_entry (char *username)
 static int delete_machine_entry (char *machinename)
 {
        char name[16];
-       SAM_ACCOUNT *samaccount;
+       SAM_ACCOUNT *samaccount = NULL;
        
        safe_strcpy (name, machinename, 16);
        if (name[strlen(name)] != '$')
                safe_strcat (name, "$", 16);
 
-       pdb_init_sam(&samaccount);
+       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);
@@ -572,7 +584,7 @@ int main (int argc, char **argv)
                exit(1);
        }
        
-       while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwx")) != EOF) {
+       while ((ch = getopt(argc, argv, "ad:f:h:i:lmp:s:u:vwxD:")) != EOF) {
                switch(ch) {
                case 'a':
                        add_user = True;
@@ -619,6 +631,9 @@ int main (int argc, char **argv)
                        import = True;
                        smbpasswd = optarg;
                        break;
+               case 'D':
+                       DEBUGLEVEL = atoi(optarg);
+                       break;
                default:
                        usage();
                }