Whamcloud - gitweb
dec19f4fc7f8fb2826c96d92f2e06c025324ba2b
[fs/lustre-release.git] / lustre / utils / lacl_upcall.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2005 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 <sys/types.h>
24 #include <sys/wait.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <pwd.h>
33 #include <grp.h>
34
35 #include <liblustre.h>
36 #include <linux/lustre_idl.h>
37 #include <linux/obd.h>
38 #include <linux/lustre_mds.h>
39 #include <linux/obd_support.h>
40
41 #include <portals/ptlctl.h>
42 #include <portals/types.h>
43
44 int switch_user_identity(uid_t uid)
45 {
46         gid_t           gid;
47         struct passwd  *pw;
48         int             maxgroups, ngroups = 0;
49         gid_t          *groups;
50         struct group   *gr;
51         int             i;
52
53         /* originally must be root */
54         if (getuid() != 0 || geteuid() != 0)
55                 return -EPERM;
56
57         /* nothing more is needed for root */
58         if (uid == 0)
59                 return 0;
60
61         /* - groups
62          * - gid
63          * - uid
64          */
65         maxgroups = sysconf(_SC_NGROUPS_MAX);
66         groups = malloc(maxgroups * sizeof(gid_t));
67         if (!groups)
68                 return -ENOMEM;
69
70         pw = getpwuid(uid);
71         if (!pw)
72                 return -EPERM;
73
74         gid = pw->pw_gid;
75
76         while ((gr = getgrent())) {
77                 if (!gr->gr_mem)
78                         continue;
79                 for (i = 0; gr->gr_mem[i]; i++) {
80                         if (strcmp(gr->gr_mem[i], pw->pw_name))
81                                 continue;
82                         groups[ngroups++] = gr->gr_gid;
83                         break;
84                 }
85                 if (ngroups == maxgroups)
86                         break;
87         }
88         endgrent();
89
90         if (setgroups(ngroups, groups) == -1) {
91                 free(groups);
92                 return -EPERM;
93         }
94         free(groups);
95
96         if (setgid(gid) == -1) {
97                 return -EPERM;
98         }
99
100         if (setuid(uid) == -1) {
101                 return -EPERM;
102         }
103
104         return 0;
105 }
106
107 /*
108  * caller guarantee args not empty
109  */
110 int compose_command_line(char *cmdline, char *op, char *args)
111 {
112         char *p, *params, *file;
113
114         /* skip the white space at the tail */
115         p = args + strlen(args) - 1;
116
117         while (p >= args) {
118                 if (*p != ' ' && *p != '\t')
119                         break;
120                 p--;
121         }
122
123         /* not allow empty args */
124         if (p < args)
125                 return -1;
126
127         *(p + 1) = '\0';
128
129         /* find next space */
130         while (p >= args) {
131                 if (*p == ' ' || *p == '\t')
132                         break;
133                 p--;
134         }
135
136         if (p >= args) {
137                 *p = '\0';
138                 file = p + 1; /* file name */
139                 params = args;
140         } else {
141                 file = args;
142                 params = "";
143         }
144
145         /* backward path not allowed */
146         if (strstr(file, ".."))
147                 return -EPERM;
148
149         /* absolute path not allowed */
150         if (file[0] == '/')
151                 return -EPERM;
152
153         snprintf(cmdline, PATH_MAX, "%sfacl %s %s",
154                  op, params, file);
155         return 0;
156 }
157
158 void do_acl_command(uid_t uid, char *lroot, char *cmdline)
159 {
160         if (switch_user_identity(uid)) {
161                 printf("MDS: invalid user %u\n", uid);
162                 return;
163         }
164
165         if (chdir(lroot) < 0) {
166                 printf("MDS: can't change dir\n");
167                 return;
168         }
169
170         execl("/bin/sh", "sh", "-c", cmdline, NULL);
171         printf("MDS: can't execute\n");
172 }
173
174 #define ERRSTR_NO_CMDLINE       "No command line supplied\n"
175 #define ERRSTR_INVALID_ARGS     "Invalid arguments\n"
176 #define ERRSTR_MDS_PROCESS      "MDS procession error\n"
177
178 /*
179  * The args passed in are:
180  * 1. key (in hex)
181  * 2. uid (in uint)
182  * 3. lustre root
183  * 4. get/set
184  * 5. command line
185  */
186 #define OUTPUT_BUFSIZE          8192
187 int main (int argc, char **argv)
188 {
189         struct   rmtacl_downcall_args dc_args;
190         char    *dc_name = "/proc/fs/lustre/mds/lacl_downcall";
191         int      dc_fd;
192         int      uid;
193         char     output[OUTPUT_BUFSIZE];
194         char     cmdline[PATH_MAX];
195         int      pipeout[2], pipeerr[2], pid;
196         int      output_size, rd, childret;
197
198         if (argc != 6)
199                 return -1;
200
201         if (strcmp(argv[4], "get") && strcmp(argv[4], "set"))
202                 return -1;
203
204         dc_args.key = strtoull(argv[1], NULL, 16);
205         dc_args.res = output;
206         dc_args.reslen = 0;
207         dc_args.status = -1; /* default return error */
208
209         uid = atoi(argv[2]);
210
211         if (strlen(argv[5]) == 0) {
212                 dc_args.reslen = sizeof(ERRSTR_NO_CMDLINE);
213                 memcpy(output, ERRSTR_NO_CMDLINE, dc_args.reslen);
214                 goto downcall;
215         }
216
217         if (compose_command_line(cmdline, argv[4], argv[5])) {
218                 dc_args.reslen = sizeof(ERRSTR_INVALID_ARGS);
219                 memcpy(output, ERRSTR_INVALID_ARGS, dc_args.reslen);
220                 goto downcall;
221         }
222
223         /* create pipe */
224         if (pipe(pipeout) < 0 || pipe(pipeerr) < 0) {
225                 dc_args.reslen = sizeof(ERRSTR_MDS_PROCESS);
226                 memcpy(output, ERRSTR_MDS_PROCESS, dc_args.reslen);
227                 goto downcall;
228         }
229
230         if ((pid = fork()) < 0) {
231                 dc_args.reslen = sizeof(ERRSTR_MDS_PROCESS);
232                 memcpy(output, ERRSTR_MDS_PROCESS, dc_args.reslen);
233                 goto downcall;
234         } else if (pid == 0) {
235                 close(pipeout[0]);
236                 if (pipeout[1] != STDOUT_FILENO) {
237                         dup2(pipeout[1], STDOUT_FILENO);
238                         close(pipeout[1]);
239                 }
240
241                 close(pipeerr[0]);
242                 if (pipeerr[1] != STDERR_FILENO) {
243                         dup2(pipeerr[1], STDERR_FILENO);
244                         close(pipeerr[1]);
245                 }
246
247                 close(STDIN_FILENO);
248
249                 do_acl_command(uid, argv[3], cmdline);
250                 exit(-1);
251         }
252
253         /* parent process handling */
254         close(pipeout[1]);
255         close(pipeerr[1]);
256
257         output[0] = 0;
258         output_size = 0;
259         while (1) {
260                 rd = read(pipeout[0], output + output_size,
261                           OUTPUT_BUFSIZE - output_size);
262                 if (rd < 0) {
263                         output_size = sizeof(ERRSTR_MDS_PROCESS);
264                         memcpy(output, ERRSTR_MDS_PROCESS, dc_args.reslen);
265                         break;
266                 }
267                 if (rd == 0)
268                         break;
269                 output_size += rd;
270                 if (output_size >= OUTPUT_BUFSIZE)
271                         break;
272         }
273
274         /* if we got standard output, just leave; otherwise collect
275          * error output.
276          */
277         if (output_size != 0)
278                 goto wait_child;
279
280         while (1) {
281                 rd = read(pipeerr[0], output + output_size,
282                           OUTPUT_BUFSIZE - output_size);
283                 if (rd < 0) {
284                         output_size = sizeof(ERRSTR_MDS_PROCESS);
285                         memcpy(output, ERRSTR_MDS_PROCESS, dc_args.reslen);
286                         break;
287                 }
288                 if (rd == 0)
289                         break;
290                 output_size += rd;
291                 if (output_size >= OUTPUT_BUFSIZE)
292                         break;
293         }
294
295 wait_child:
296         wait(&childret);
297
298         dc_args.status = childret;
299         dc_args.reslen = output_size;
300
301 downcall:
302         dc_fd = open(dc_name, O_WRONLY);
303         if (dc_fd != -1) {
304                 write(dc_fd, &dc_args, sizeof(dc_args));
305                 close(dc_fd);
306         }
307
308         return 0;
309 }