pts: Don't malloc(0) when there's nothing to do
[openafs.git] / src / ptserver / pts.c
index 2af6532..b6601d6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
-#include <stdio.h>
-#include <string.h>
-#ifdef AFS_AIX32_ENV
-#include <signal.h>
-#endif
 #include <ctype.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <afs/cmd.h>
+
 #ifdef AFS_NT40_ENV
-#include <winsock2.h>
 #include <WINNT/afsevent.h>
-#else
-#include <netinet/in.h>
+#include <WINNT/afsreg.h>
 #endif
+
 #include <afs/cellconfig.h>
+#include <afs/afsutil.h>
+#include <afs/com_err.h>
+#include <afs/cmd.h>
 #include <rx/rx.h>
 #include <rx/xdr.h>
+
 #include "ptclient.h"
 #include "ptuser.h"
 #include "pterror.h"
 #include "ptprototypes.h"
-#include <afs/afsutil.h>
-#include <afs/com_err.h>
 
 #undef FOREIGN
 
@@ -121,7 +116,7 @@ pts_Sleep(struct cmd_syndesc *as, void *arock)
 static int
 popsource(void)
 {
-    register struct sourcestack *sp;
+    struct sourcestack *sp;
     if (!(sp = shead))
        return 0;
     if (source != stdin)
@@ -142,6 +137,29 @@ osi_audit(void)
     return 0;
 }
 
+#ifdef AFS_NT40_ENV
+static DWORD
+win32_enableCrypt(void)
+{
+    HKEY parmKey;
+    DWORD dummyLen;
+    DWORD cryptall = 0;
+    DWORD code;
+
+    /* Look up configuration parameters in Registry */
+    code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
+                        0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &parmKey);
+    if (code != ERROR_SUCCESS) {
+        dummyLen = sizeof(cryptall);
+        RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
+                        (BYTE *) &cryptall, &dummyLen);
+    }
+    RegCloseKey (parmKey);
+
+    return cryptall;
+}
+#endif /* AFS_NT40_ENV */
+
 static int
 GetGlobals(struct cmd_syndesc *as, void *arock)
 {
@@ -182,7 +200,11 @@ GetGlobals(struct cmd_syndesc *as, void *arock)
        changed = 1;
        sec = 1;
     }
-    if (as->parms[22].items) { /* -encrypt */
+    if (as->parms[22].items    /* -encrypt */
+#ifdef AFS_NT40_ENV
+        || win32_enableCrypt()
+#endif /* AFS_NT40_ENV */
+        ) {
        changed = 1;
        sec = 3;
     }
@@ -195,6 +217,12 @@ GetGlobals(struct cmd_syndesc *as, void *arock)
        else
            confdir = AFSDIR_CLIENT_ETC_DIRPATH;
     }
+
+    if (as->parms[23].items) { /* -config */
+       changed = 1;
+       confdir = as->parms[23].items->data;
+    }
+
     if (changed) {
        CleanUp(as, arock);
        code = pr_Initialize(sec, confdir, cell);
@@ -233,7 +261,7 @@ CleanUp(struct cmd_syndesc *as, void *arock)
 static int
 CreateGroup(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 id;
     char *owner;
     struct cmd_item *namei;
@@ -254,17 +282,16 @@ CreateGroup(struct cmd_syndesc *as, void *arock)
                        idi->data);
                return code;
            }
-           if (id >= 0) {
+           if (id == 0) {
+               printf("0 isn't a valid group id; aborting\n");
+               return EINVAL;
+           }
+           if (id > 0) {
                code = PRBADARG;
                afs_com_err(whoami, code, "because group id %d was not negative",
                        id);
                return code;
            }
-           
-            if (id == 0) {
-               printf("0 isn't a valid user id; aborting\n");
-               return EINVAL;
-           }
 
            idi = idi->next;
        } else
@@ -293,7 +320,7 @@ CreateGroup(struct cmd_syndesc *as, void *arock)
 static int
 CreateUser(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 id;
     struct cmd_item *namei;
     struct cmd_item *idi;
@@ -312,6 +339,12 @@ CreateUser(struct cmd_syndesc *as, void *arock)
                printf("0 isn't a valid user id; aborting\n");
                return EINVAL;
            }
+           if (id < 0) {
+               code = PRBADARG;
+               afs_com_err(whoami, code, "because user id %d was not positive",
+                       id);
+               return code;
+           }
            idi = idi->next;
        } else
            id = 0;
@@ -339,7 +372,7 @@ CreateUser(struct cmd_syndesc *as, void *arock)
 static int
 GetNameOrId(struct cmd_syndesc *as, struct idlist *lids, struct namelist *lnames)
 {
-    register afs_int32 code = 0;
+    afs_int32 code = 0;
     int n = 0;
     struct cmd_item *i;
     int goodCount;
@@ -431,23 +464,33 @@ static int
 GetNameOrId(struct cmd_syndesc *as, struct idlist *lids,
            struct namelist *lnames)
 {
-    register afs_int32 code = 0;
+    afs_int32 code = 0;
     int n = 0, nd = 0, nm = 0, id, x;
     struct cmd_item *i;
     struct namelist names, tnames;     /* local copy, if not ret. names */
     struct idlist ids, tids;   /* local copy, if not ret. ids */
     int goodCount = 0;
 
+    /* Initialise our outputs */
+    memset(lids, 0, sizeof(struct idlist));
+    if (lnames)
+       memset(lnames, 0, sizeof(struct namelist));
+
     for (i = as->parms[0].items; i; i = i->next)
        n++;
-    lids->idlist_val = (afs_int32 *) malloc(n * sizeof(afs_int32));
+
+    /* Nothing to do, so bail */
+    if (n == 0)
+       return 0;
+
+    lids->idlist_val = malloc(n * sizeof(afs_int32));
     lids->idlist_len = n;
-    ids.idlist_val = (afs_int32 *) malloc(n * sizeof(afs_int32));
+    ids.idlist_val = malloc(n * sizeof(afs_int32));
     ids.idlist_len = n;
-    names.namelist_val = (prname *) malloc(n * PR_MAXNAMELEN);
+    names.namelist_val = malloc(n * PR_MAXNAMELEN);
     names.namelist_len = n;
     if (lnames) {
-       lnames->namelist_val = (prname *) malloc(n * PR_MAXNAMELEN);
+       lnames->namelist_val = malloc(n * PR_MAXNAMELEN);
        lnames->namelist_len = 0;
     }
     for (i = as->parms[0].items; i; i = i->next) {
@@ -475,7 +518,7 @@ GetNameOrId(struct cmd_syndesc *as, struct idlist *lids,
        afs_com_err(whoami, code, "so couldn't look up names");
     else {
        for (n = 0; n < tids.idlist_len; n++) {
-           if ((tids.idlist_val[n] == ANONYMOUSID)) {
+           if (tids.idlist_val[n] == ANONYMOUSID) {
                afs_com_err(whoami, PRNOENT, "so couldn't look up id for %s",
                        names.namelist_val[n]);
            } else
@@ -519,7 +562,7 @@ GetNameOrId(struct cmd_syndesc *as, struct idlist *lids,
 static int
 AddToGroup(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     struct cmd_item *u, *g;
 
     for (u = as->parms[0].items; u; u = u->next) {
@@ -540,7 +583,7 @@ AddToGroup(struct cmd_syndesc *as, void *arock)
 static int
 RemoveFromGroup(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     struct cmd_item *u, *g;
 
     for (u = as->parms[0].items; u; u = u->next) {
@@ -561,7 +604,7 @@ RemoveFromGroup(struct cmd_syndesc *as, void *arock)
 static int
 ListMembership(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     idlist ids;
     namelist names;
     int i;
@@ -580,7 +623,13 @@ ListMembership(struct cmd_syndesc *as, void *arock)
 
        list.namelist_val = 0;
        list.namelist_len = 0;
-       code = pr_IDListMembers(ids.idlist_val[i], &list);
+       if (as->parms[2].items) {       /* -expandgroups */
+           code = pr_IDListExpandedMembers(id, &list);
+           if (!code)
+               printf("Expanded ");
+       } else {
+           code = pr_IDListMembers(id, &list);
+       }
        if (code) {
            afs_com_err(whoami, code, "; unable to get membership of %s (id: %d)",
                    name, id);
@@ -595,6 +644,24 @@ ListMembership(struct cmd_syndesc *as, void *arock)
            printf("  %s\n", list.namelist_val[j]);
        if (list.namelist_val)
            free(list.namelist_val);
+       if (as->parms[1].items && id < 0) {     /* -supergroups */
+           list.namelist_val = 0;
+           list.namelist_len = 0;
+           code = pr_ListSuperGroups(ids.idlist_val[i], &list);
+           if (code == RXGEN_OPCODE) {
+               continue; /* server does not support supergroups */
+           } else if (code != 0) {
+               afs_com_err(whoami, code,
+                           "; unable to get supergroups of %s (id: %d)",
+                           name, id);
+               continue;
+           }
+           printf("Groups %s (id: %d) is a member of:\n", name, id);
+           for (j = 0; j < list.namelist_len; j++)
+               printf("  %s\n", list.namelist_val[j]);
+           if (list.namelist_val)
+               free(list.namelist_val);
+       }
     }
     if (ids.idlist_val)
        free(ids.idlist_val);
@@ -606,7 +673,7 @@ ListMembership(struct cmd_syndesc *as, void *arock)
 static int
 Delete(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     idlist ids;
     namelist names;
     int i;
@@ -645,7 +712,7 @@ int flags_shift[5] = { 2, 1, 2, 2, 1 };     /* bits for each */
 static int
 CheckEntry(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 rcode = 1;
     int i, flag = 0, admin = 0;
     namelist lnames, names;
@@ -781,7 +848,7 @@ ListEntries(struct cmd_syndesc *as, void *arock)
 static int
 ChownGroup(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     char *name;
     char *owner;
 
@@ -797,7 +864,7 @@ ChownGroup(struct cmd_syndesc *as, void *arock)
 static int
 ChangeName(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     char *oldname;
     char *newname;
 
@@ -813,7 +880,7 @@ ChangeName(struct cmd_syndesc *as, void *arock)
 static int
 ListMax(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 maxUser, maxGroup;
 
     code = pr_ListMaxUserId(&maxUser);
@@ -834,7 +901,7 @@ ListMax(struct cmd_syndesc *as, void *arock)
 static int
 SetMaxCommand(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     afs_int32 maxid;
 
     code = 0;
@@ -874,7 +941,7 @@ SetMaxCommand(struct cmd_syndesc *as, void *arock)
 static int
 SetFields(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     idlist ids;
     namelist names;
     int i;
@@ -969,7 +1036,7 @@ SetFields(struct cmd_syndesc *as, void *arock)
 static int
 ListOwned(struct cmd_syndesc *as, void *arock)
 {
-    register afs_int32 code;
+    afs_int32 code;
     idlist ids;
     namelist names;
     namelist list;
@@ -1035,11 +1102,12 @@ add_std_args(struct cmd_syndesc *ts)
                "use user's authentication (default)");
     cmd_AddParm(ts, "-encrypt", CMD_FLAG, CMD_OPTIONAL,
                "encrypt commands");
+    cmd_AddParm(ts, "-config", CMD_SINGLE, CMD_OPTIONAL, "config location");
 }
 
 /*
 static void add_NameOrId_args (ts)
-  register struct cmd_syndesc *ts;
+  struct cmd_syndesc *ts;
 {
     cmd_AddParm(ts,"-name",CMD_LIST,CMD_OPTIONAL,"user or group name");
     cmd_AddParm(ts,"-id",CMD_LIST,CMD_OPTIONAL,"user or group id");
@@ -1051,8 +1119,8 @@ static void add_NameOrId_args (ts)
 int
 main(int argc, char **argv)
 {
-    register afs_int32 code;
-    register struct cmd_syndesc *ts;
+    afs_int32 code;
+    struct cmd_syndesc *ts;
 
     char line[2048];
     char *cp, *lastp;
@@ -1071,8 +1139,8 @@ main(int argc, char **argv)
 
 #ifdef AFS_AIX32_ENV
     /*
-     * The following signal action for AIX is necessary so that in case of a 
-     * crash (i.e. core is generated) we can include the user's data section 
+     * The following signal action for AIX is necessary so that in case of a
+     * crash (i.e. core is generated) we can include the user's data section
      * in the core dump. Unfortunately, by default, only a partial core is
      * generated which, in many cases, isn't too useful.
      */
@@ -1116,6 +1184,8 @@ main(int argc, char **argv)
     ts = cmd_CreateSyntax("membership", ListMembership, NULL,
                          "list membership of a user or group");
     cmd_AddParm(ts, "-nameorid", CMD_LIST, 0, "user or group name or id");
+    cmd_AddParm(ts, "-supergroups", CMD_FLAG, CMD_OPTIONAL, "show supergroups");
+    cmd_AddParm(ts, "-expandgroups", CMD_FLAG, CMD_OPTIONAL, "expand super and sub group membership");
     add_std_args(ts);
     cmd_CreateAlias(ts, "groups");