source: trunk/server/common/oursrc/accountadm/admof.c @ 1598

Last change on this file since 1598 was 1598, checked in by geofft, 14 years ago
admof: Always check system:scripts-root -c athena, not in the locker's cell Also rename us from OVERLORDS to SYSADMINS
File size: 7.5 KB
Line 
1/* admof
2 * Version 2.0, released 2007-12-30
3 * Anders Kaseorg <andersk@mit.edu>
4 * replacing Perl version by Jeff Arnold <jbarnold@mit.edu>
5 *
6 * Usage:
7 *   admof scripts andersk/root@ATHENA.MIT.EDU
8 * Outputs "yes" and exits with status 33 if the given principal is an
9 * administrator of the locker.
10 *
11 * Requires tokens (to authenticate/encrypt the connection to the
12 * ptserver) unless -noauth is given.
13 */
14
15#include <stdio.h>
16#include <limits.h>
17#include <string.h>
18#include <sys/types.h>
19#include <sys/stat.h>
20#include <pwd.h>
21#include <unistd.h>
22#include <netinet/in.h>
23#include <afs/vice.h>
24#include <afs/venus.h>
25#include <afs/ptclient.h>
26#include <afs/ptuser.h>
27#include <afs/prs_fs.h>
28#include <afs/ptint.h>
29#include <afs/cellconfig.h>
30#include <afs/afsutil.h>
31#include <krb5.h>
32#include <kerberosIV/krb.h>
33#include <stdbool.h>
34#include <syslog.h>
35
36extern int pioctl(char *, afs_int32, struct ViceIoctl *, afs_int32);
37
38#define die(args...) do { fprintf(stderr, args); pr_End(); exit(1); } while(0)
39#define _STR(x) #x
40#define STR(x) _STR(x)
41
42#define SYSADMINS "system:scripts-root"
43#define SYSADMIN_CELL "athena.mit.edu"
44
45static bool
46ismember(const char *user, const char *group)
47{
48    int flag;
49    if (pr_IsAMemberOf((char *)user, (char *)group, &flag) == 0)
50        return flag;
51    else
52        return 0;
53}
54
55/* Parse an ACL of n entries, returning the rights for user. */
56static int
57parse_rights(int n, const char **p, const char *user)
58{
59    int rights = 0, *trights = malloc(n * sizeof(int)), i;
60    namelist tnames = {.namelist_len = n,
61                       .namelist_val = malloc(n * PR_MAXNAMELEN)};
62    idlist tids = {.idlist_len = 0,
63                   .idlist_val = NULL};
64
65    if (trights == NULL || tnames.namelist_val == NULL)
66        die("internal error: malloc failed: %m");
67
68    for (i = 0; i < n; ++i) {
69        int off;
70        if (sscanf(*p, "%" STR(PR_MAXNAMELEN) "s %d\n%n",
71                   tnames.namelist_val[i], &trights[i], &off) < 2)
72            die("internal error: can't parse output from pioctl\n");
73        *p += off;
74    }
75
76    if (pr_NameToId(&tnames, &tids) != 0)
77        die("internal error: pr_NameToId failed");
78    if (tids.idlist_len < n)
79        die("internal error: pr_NameToId did not return enough ids");
80
81    for (i = 0; i < n; ++i) {
82        if (~rights & trights[i] &&
83            (strcasecmp(tnames.namelist_val[i], user) == 0 ||
84             (tids.idlist_val[i] < 0 && ismember(user, tnames.namelist_val[i]))))
85            rights |= trights[i];
86    }
87
88    /* Note: this first free probably should be xdr_free in OpenAFS 1.5.
89     * See commits b40b606 and f02f2e8 */
90    free(tids.idlist_val);
91    tids.idlist_val = NULL;
92    free(tnames.namelist_val);
93    free(trights);
94
95    return rights;
96}
97
98int
99main(int argc, const char *argv[])
100{
101    /* Get arguments. */
102    const char *locker, *name;
103    afs_int32 secLevel;
104
105    if (argc == 3) {
106        locker = argv[1];
107        name = argv[2];
108        secLevel = 3;
109    } else if (argc == 4 && strcmp("-noauth", argv[1]) == 0) {
110        locker = argv[2];
111        name = argv[3];
112        secLevel = 0;
113    } else {
114        die("Usage: %s [-noauth] LOCKER PRINCIPAL\n", argv[0]);
115    }
116
117    /* Convert the locker into a directory. */
118    char dir[PATH_MAX];
119    int n;
120    struct passwd *pwd = getpwnam(locker);
121    if (pwd != NULL)
122        n = snprintf(dir, sizeof dir, "%s", pwd->pw_dir);
123    else
124        n = snprintf(dir, sizeof dir, "/mit/%s", locker);
125    if (n < 0 || n >= sizeof dir)
126        die("internal error\n");
127
128    /* For non-AFS homedirs, read the .k5login file. */
129    if (strncmp(dir, "/afs/", 5) != 0 && strncmp(dir, "/mit/", 5) != 0) {
130        if (chdir(dir) != 0)
131            die("internal error: chdir: %m\n");
132        FILE *fp = fopen(".k5login", "r");
133        if (fp == NULL)
134            die("internal error: .k5login: %m\n");
135        struct stat st;
136        if (fstat(fileno(fp), &st) != 0)
137            die("internal error: fstat: %m\n");
138        if (st.st_uid != pwd->pw_uid && st.st_uid != 0) {
139            fclose(fp);
140            die("internal error: bad .k5login permissions\n");
141        }
142        bool found = false;
143        char *line = NULL;
144        size_t len = 0;
145        ssize_t read;
146        while ((read = getline(&line, &len, fp)) != -1) {
147            if (read > 0 && line[read - 1] == '\n')
148                line[read - 1] = '\0';
149            if (strcmp(name, line) == 0) {
150                found = true;
151                break;
152            }
153        }
154        if (line)
155            free(line);
156        fclose(fp);
157        if (found) {
158            printf("yes\n");
159            exit(33);
160        } else {
161            printf("no\n");
162            exit(1);
163        }
164    }
165
166    /* Get the locker's cell. */
167    char cell[MAXCELLCHARS];
168    struct ViceIoctl vi;
169    vi.in = NULL;
170    vi.in_size = 0;
171    vi.out = cell;
172    vi.out_size = sizeof cell;
173    if (pioctl(dir, VIOC_FILE_CELL_NAME, &vi, 1) != 0)
174        die("internal error: pioctl: %m\n");
175
176    if (pr_Initialize(secLevel, (char *)AFSDIR_CLIENT_ETC_DIRPATH, cell) != 0)
177        die("internal error: pr_Initialize failed\n");
178
179    /* Get the cell configuration. */
180    struct afsconf_dir *configdir = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH);
181    if (configdir == NULL)
182        die("internal error: afsconf_Open failed\n");
183    struct afsconf_cell cellconfig;
184    if (afsconf_GetCellInfo(configdir, cell, NULL, &cellconfig) != 0)
185        die("internal error: afsconf_GetCellInfo failed\n");
186    afsconf_Close(configdir);
187
188    /* Figure out the cell's realm. */
189    krb5_context context;
190    krb5_init_context(&context);
191
192    char **realm_list;
193    if (krb5_get_host_realm(context, cellconfig.hostName[0], &realm_list) != 0 ||
194        realm_list[0] == NULL)
195        die("internal error: krb5_get_host_realm failed");
196
197    /* Convert the Kerberos 5 principal into a (Kerberos IV-style) AFS
198       name, omitting the realm if it equals the cell's realm. */
199    krb5_principal principal;
200    if (krb5_parse_name(context, name, &principal) != 0)
201        die("internal error: krb5_parse_name failed");
202    char pname[ANAME_SZ], pinst[INST_SZ], prealm[REALM_SZ];
203    if (krb5_524_conv_principal(context, principal, pname, pinst, prealm) != 0)
204        die("internal error: krb5_524_conv_principal failed\n");
205    char user[MAX(PR_MAXNAMELEN, MAX_K_NAME_SZ)];
206    if (kname_unparse(user, pname, pinst,
207                      strcmp(prealm, realm_list[0]) == 0 ? NULL : prealm) != 0)
208        die("internal error: kname_unparse failed\n");
209
210    krb5_free_principal(context, principal);
211    krb5_free_host_realm(context, realm_list);
212    krb5_free_context(context);
213
214    /* Instead of canonicalizing the name as below, we just use
215       strcasecmp above. */
216#if 0
217    afs_int32 id;
218    if (pr_SNameToId((char *)user, &id) != 0)
219        die("bad principal\n");
220    if (id == ANONYMOUSID)
221        die("anonymous\n");
222    if (pr_SIdToName(id, user) != 0)
223        die("internal error: pr_SIdToName failed\n");
224#endif
225
226    /* Read the locker ACL. */
227    char acl[2048];
228    vi.in = NULL;
229    vi.in_size = 0;
230    vi.out = acl;
231    vi.out_size = sizeof acl;
232    if (pioctl(dir, VIOCGETAL, &vi, 1) != 0)
233        die("internal error: pioctl: %m\n");
234
235    /* Parse the locker ACL to compute the user's rights. */
236    const char *p = acl;
237
238    int nplus, nminus;
239    int off;
240    if (sscanf(p, "%d\n%d\n%n", &nplus, &nminus, &off) < 2)
241        die("internal error: can't parse output from pioctl\n");
242    p += off;
243
244    int rights = parse_rights(nplus, &p, user);
245    rights &= ~parse_rights(nminus, &p, user);
246    pr_End();
247
248#ifdef SYSADMINS
249    if (~rights & PRSFS_ADMINISTER) {
250        strncpy(cell, SYSADMIN_CELL, MAXCELLCHARS - 1);
251        if (pr_Initialize(secLevel, (char *)AFSDIR_CLIENT_ETC_DIRPATH, cell) == 0) {
252            if (ismember(user, SYSADMINS)) {
253                openlog("admof", 0, LOG_AUTHPRIV);
254                syslog(LOG_NOTICE, "giving %s admin rights on %s", user, locker);
255                closelog();
256                rights |= PRSFS_ADMINISTER;
257            }
258            pr_End();
259        }
260        /* If not, that's okay -- the normal codepath ran fine, so don't error */
261    }
262#endif
263
264    /* Output whether the user is an administrator. */
265    if (rights & PRSFS_ADMINISTER) {
266        printf("yes\n");
267        exit(33);
268    } else {
269        printf("no\n");
270        exit(1);
271    }
272}
Note: See TracBrowser for help on using the repository browser.