Whamcloud - gitweb
8b55d454223ba94ce593bdd5feb4eb658eec09c7
[fs/lustre-release.git] / lustre / utils / lsd_upcall.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <pwd.h>
31 #include <grp.h>
32
33 #include <liblustre.h>
34 #include <linux/lustre_idl.h>
35 #include <linux/obd.h>
36 #include <linux/lustre_mds.h>
37
38 /*
39  * return:
40  *  0:      fail to insert (found identical)
41  *  1:      inserted
42  */
43 int insert_sort(gid_t *groups, int size, gid_t grp)
44 {
45         int i;
46         gid_t save;
47
48         for (i = 0; i < size; i++) {
49                 if (groups[i] == grp)
50                         return 0;
51                 if (groups[i] > grp)
52                         break;
53         }
54
55         for (; i <= size; i++) {
56                 save = groups[i];
57                 groups[i] = grp;
58                 grp = save;
59         }
60         return 1;
61 }
62
63 int get_groups_local(uid_t uid, gid_t *gid, int *ngroups, gid_t **groups)
64 {
65         int     maxgroups;
66         int     i, size = 0;
67         struct passwd *pw;
68         struct group  *gr;
69
70         *ngroups = 0;
71         *groups = NULL;
72         maxgroups = sysconf(_SC_NGROUPS_MAX);
73         *groups = malloc(maxgroups * sizeof(gid_t));
74         if (!*groups)
75                 return -ENOMEM;
76
77         pw = getpwuid(uid);
78         if (!pw)
79                 return -errno;
80
81         *gid = pw->pw_gid;
82
83         while ((gr = getgrent())) {
84                 if (!gr->gr_mem)
85                         continue;
86                 for (i = 0; gr->gr_mem[i]; i++) {
87                         if (strcmp(gr->gr_mem[i], pw->pw_name))
88                                 continue;
89                         size += insert_sort(*groups, size, gr->gr_gid);
90                         break;
91                 }
92                 if (size == maxgroups)
93                         break;
94         }
95         endgrent();
96         *ngroups = size;
97         return 0;
98 }
99
100 int main (int argc, char **argv)
101 {
102         char   *pathname = "/proc/fs/lustre/mds/lsd_downcall";
103         int     fd, rc;
104         struct lsd_downcall_args ioc_data;
105
106         if (argc != 2) {
107                 printf("bad parameter\n");
108                 return -EINVAL;
109         }
110
111         ioc_data.uid = atoi(argv[1]);
112
113         fd = open(pathname, O_WRONLY);
114         if (fd < 0) {
115                 rc = -errno;
116                 printf("can't open device %s\n", pathname);
117                 return rc;
118         }
119
120         ioc_data.err = get_groups_local(ioc_data.uid, &ioc_data.gid,
121                                         &ioc_data.ngroups, &ioc_data.groups);
122
123         /* FIXME get these from config file */
124         ioc_data.allow_setuid = 1;
125         ioc_data.allow_setgid = 1;
126         ioc_data.allow_setgrp = 1;
127
128         rc = write(fd, &ioc_data, sizeof(ioc_data));
129         return (rc != sizeof(ioc_data));
130 }