r19978: More "net sam policy" improvements. Thanks to Karolin Seeger <ks@sernet.de>
authorVolker Lendecke <vlendec@samba.org>
Fri, 1 Dec 2006 14:54:31 +0000 (14:54 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:16:17 +0000 (12:16 -0500)
Volker

source/lib/account_pol.c
source/utils/net_sam.c
source/utils/pdbedit.c

index 4cb0b77e74666a35b0c8c10fc9cce7c7fbc31784..f4008457ac13bf3c33ff92f6c4221fdde63ae0a5 100644 (file)
@@ -83,28 +83,24 @@ static const struct ap_table account_policy_names[] = {
        {0, NULL, 0, "", NULL}
 };
 
-char *account_policy_names_list(void)
-{
-       char *nl, *p;
-       int i;
-       size_t len = 0;
+void account_policy_names_list(const char ***names, int *num_names)
+{      
+       const char **nl;
+       int i, count;
 
-       for (i=0; account_policy_names[i].string; i++) {
-               len += strlen(account_policy_names[i].string) + 1;
+       for (count=0; account_policy_names[count].string; count++) {
        }
-       len++;
-       nl = (char *)SMB_MALLOC(len);
+       nl = SMB_MALLOC_ARRAY(const char *, count);
        if (!nl) {
-               return NULL;
+               *num_names = 0;
+               return;
        }
-       p = nl;
        for (i=0; account_policy_names[i].string; i++) {
-               memcpy(p, account_policy_names[i].string, strlen(account_policy_names[i].string) + 1);
-               p[strlen(account_policy_names[i].string)] = '\n';
-               p += strlen(account_policy_names[i].string) + 1;
+               nl[i] = account_policy_names[i].string;
        }
-       *p = '\0';
-       return nl;
+       *num_names = count;
+       *names = nl;
+       return;
 }
 
 /****************************************************************************
index d92bf1521cd38ab14264c73be1fe739232568a4c..2c9b9649f7a9b33399dc0f42cf443bba3b18bd91 100644 (file)
@@ -365,38 +365,57 @@ static int net_sam_policy_set(int argc, const char **argv)
        const char *account_policy = NULL;
        uint32 value, old_value;
        int field;
+       char *endptr;
 
         if (argc != 2) {
-                d_fprintf(stderr, "usage: net sam policy set" 
+                d_fprintf(stderr, "usage: net sam policy set 
                          "\"<account policy>\" <value> \n");
                 return -1;
         }
 
-       value = strtoul(argv[1], NULL, 10);
        account_policy = argv[0];
        field = account_policy_name_to_fieldnum(account_policy);
+       value = strtoul(argv[1], &endptr, 10);
 
-       printf("Account policy \"%s\" description: %s\n", account_policy,
-               account_policy_get_desc(field));
+       if (field == 0) {
+               const char **names;
+                int i, count;
+
+                account_policy_names_list(&names, &count);
+               d_fprintf(stderr, "No account policy \"%s\"!\n\n", argv[0]);
+               d_fprintf(stderr, "Valid account policies are:\n");
+
+               for (i=0; i<count; i++) {
+                       d_fprintf(stderr, "%s\n", names[i]);
+               }
+
+               SAFE_FREE(names);
+               return -1;
+       }
 
        if (!pdb_get_account_policy(field, &old_value)) {
-                fprintf(stderr, "Valid account policy, but unable to "
-                        "fetch value!\n");
-                return -1;
-        }
-       
-        printf("Account policy \"%s\" value was: %d\n", account_policy,
-               old_value);
+               d_fprintf(stderr, "Valid account policy, but unable to fetch "
+                         "value!\n");
+       }
 
-        if (!pdb_set_account_policy(field, value)) {
-                d_fprintf(stderr, "Setting account policy %s to %u failed \n",
-                          account_policy, value);
-        }
+       if ((endptr == argv[1]) || (endptr[0] != '\0')) {
+               d_printf("Unable to set policy \"%s\"! Invalid value %s.\n", 
+                         account_policy, argv[1]); 
+               return -1;
+       }
+               
+       if (!pdb_set_account_policy(field, value)) {
+               d_fprintf(stderr, "Valid account policy, but unable to "
+                         "set value!\n");
+               return -1;
+       }
 
-        printf("Account policy \"%s\" value is now: %d\n", account_policy,
-               value);
+       d_printf("Account policy \"%s\" value was: %d\n", account_policy,
+                old_value);
 
-        return 0;
+       d_printf("Account policy \"%s\" value is now: %d\n", account_policy,
+                value);
+       return 0;
 }
 
 static int net_sam_policy_show(int argc, const char **argv)
@@ -415,13 +434,19 @@ static int net_sam_policy_show(int argc, const char **argv)
         field = account_policy_name_to_fieldnum(account_policy);
 
         if (field == 0) {
-                char *apn = account_policy_names_list();
+               const char **names;
+               int count;
+               int i;
+                account_policy_names_list(&names, &count);
                 d_fprintf(stderr, "No account policy by that name!\n");
-                if (apn) {
+                if (count != 0) {
                         d_fprintf(stderr, "Valid account policies "
-                                  "are:\n%s\n", apn);
+                                  "are:\n");
+                       for (i=0; i<count; i++) {
+                               d_fprintf(stderr, "%s\n", names[i]);
+                       }
                 }
-                SAFE_FREE(apn);
+                SAFE_FREE(names);
                 return -1;
         }
 
@@ -440,12 +465,18 @@ static int net_sam_policy_show(int argc, const char **argv)
 
 static int net_sam_policy_list(int argc, const char **argv)
 {
-       char *apn = account_policy_names_list();
-        if (apn) {
+       const char **names;
+       int count;
+       int i;
+       account_policy_names_list(&names, &count);
+        if (count != 0) {
                d_fprintf(stderr, "Valid account policies "
-                         "are:\n\n%s\n", apn);
+                         "are:\n");
+               for (i = 0; i < count ; i++) {
+                       d_fprintf(stderr, "%s\n", names[i]);
+               }
        }
-        SAFE_FREE(apn);
+        SAFE_FREE(names);
         return -1;
 }
 
index ceb346d98744bcecdf1f7e8b694048441dd11c1b..d79ab187a3b6aff677704de616446857559251eb 100644 (file)
@@ -885,12 +885,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)) {