Moved getlimits and other support functions out to planetlab.[ch].
[util-vserver.git] / src / vsh.c
1 /*
2  * Marc E. Fiuczynski <mef@cs.princeton.edu>
3  *
4  * Copyright (c) 2004 The Trustees of Princeton University (Trustees).
5  *
6  * vsh is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * vsh is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Poptop; see the file COPYING.  If not, write to the Free
18  * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25 #include "compat.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <pwd.h>
32 #include <unistd.h>
33 #include <syscall.h>
34 #include <sys/syscall.h>
35 #include <asm/unistd.h>
36 #include <sys/mount.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/resource.h>
40 #include <fcntl.h>
41 #include <ctype.h>
42 #include <stdarg.h>
43
44 //--------------------------------------------------------------------
45 #include "vserver.h"
46 #include "planetlab.h"
47
48 #undef CONFIG_VSERVER_LEGACY
49
50 /* Base for all vserver roots for chroot */
51 #define VSERVER_ROOT_BASE       "/vservers"
52
53 /* Change to root:root (before entering new context) */
54 static int setuidgid_root()
55 {
56         if (setgid(0) < 0) {
57                 PERROR("setgid(0)");
58                 return -1;
59         }
60         if (setuid(0) < 0) {
61                 PERROR("setuid(0)");
62                 return -1;
63         }
64         return 0;
65 }
66
67 static void compute_new_root(char *base, char **root, uid_t uid)
68 {
69         int             root_len;
70         struct passwd   *pwd;
71
72         if ((pwd = getpwuid(uid)) == NULL) {
73                 PERROR("getpwuid(%d)", uid);
74                 exit(1);
75         }
76
77         root_len = 
78                 strlen(base) + strlen("/") +
79                 strlen(pwd->pw_name)      + NULLBYTE_SIZE;
80         (*root) = (char *)malloc(root_len);
81         if ((*root) == NULL) {
82                 PERROR("malloc(%d)", root_len);
83                 exit(1);
84         }
85     
86         sprintf((*root), "%s/%s", base, pwd->pw_name);
87         (*root)[root_len - 1] = '\0';
88 }
89
90 /* Example: sandbox_root = /vservers/bnc, relpath = /proc/1 */
91 static int sandbox_file_exists(char *sandbox_root, char *relpath)
92 {
93         struct stat stat_buf;
94         char   *file;
95         int    len, exists = 0;
96
97         len = strlen(sandbox_root) + strlen(relpath) + NULLBYTE_SIZE;
98         if ((file = (char *)malloc(len)) == NULL) {
99                 PERROR("malloc(%d)", len);
100                 exit(1);
101         }
102         sprintf(file, "%s%s", sandbox_root, relpath);
103         file[len - 1] = '\0';
104         if (stat(file, &stat_buf) == 0) {
105                 exists = 1;
106         }
107
108
109         free(file);
110         return exists;
111 }
112
113 static int proc_mounted(char *sandbox_root)
114 {
115         return sandbox_file_exists(sandbox_root, "/proc/1");
116 }
117
118 static int devpts_mounted(char *sandbox_root)
119 {
120         return sandbox_file_exists(sandbox_root, "/dev/pts/0");
121 }
122
123 static void mount_proc(char *sandbox_root)
124 {
125         char        *source = "/proc";
126         char        *target;
127         int         len;
128
129         len = strlen(sandbox_root) + strlen("/") + strlen("proc") + NULLBYTE_SIZE;
130         if ((target = (char *)malloc(len)) == NULL) {
131                 PERROR("malloc(%d)", len);
132                 exit(1);
133         }
134
135         sprintf(target, "%s/proc", sandbox_root);
136         target[len - 1] = '\0';
137         if (!proc_mounted(sandbox_root))
138                 mount(source, target, "proc", MS_BIND | MS_RDONLY, NULL);
139
140         free(target);
141 }
142
143 static void mount_devpts(char *sandbox_root)
144 {
145         char        *source = "/dev/pts";
146         char        *target;
147         int         len;
148     
149         len = strlen(sandbox_root) + strlen("/") + strlen("dev/pts") + NULLBYTE_SIZE;
150         if ((target = (char *)malloc(len)) == NULL) {
151                 PERROR("malloc(%d)", len);
152                 exit(1);
153         }
154
155         sprintf(target, "%s/dev/pts", sandbox_root);
156         target[len - 1] = '\0';
157         if (!devpts_mounted(sandbox_root))
158                 mount(source, target, "devpts", 0, NULL);
159
160         free(target);
161 }
162
163 static int sandbox_chroot(uid_t uid)
164 {
165         char *sandbox_root = NULL;
166
167         compute_new_root(VSERVER_ROOT_BASE,&sandbox_root, uid);
168         mount_proc(sandbox_root);
169         mount_devpts(sandbox_root);
170         if (chroot(sandbox_root) < 0) {
171                 PERROR("chroot(%s)", sandbox_root);
172                 exit(1);
173         }
174         if (chdir("/") < 0) {
175                 PERROR("chdir(/)");
176                 exit(1);
177         }
178         return 0;
179 }
180
181 static int sandbox_processes(xid_t ctx, char *context)
182 {
183 #ifdef CONFIG_VSERVER_LEGACY
184         int     flags;
185
186         flags = 0;
187         flags |= 1; /* VX_INFO_LOCK -- cannot request a new vx_id */
188         /* flags |= 4; VX_INFO_NPROC -- limit number of procs in a context */
189
190         (void) vc_new_s_context(ctx, 0, flags);
191
192         /* use legacy dirty hack for capremove */
193         if (vc_new_s_context(VC_SAMECTX, vc_get_insecurebcaps(), flags) == VC_NOCTX) {
194                 PERROR("vc_new_s_context(%u, 0x%16ullx, 0x%08x)",
195                        VC_SAMECTX, vc_get_insecurebcaps(), flags);
196                 exit(1);
197         }
198 #else
199         int  ctx_is_new;
200         struct sliver_resources slr;
201         pl_get_limits(context,&slr);
202
203         /* check whether the slice has been taken off of the whitelist */
204         if (slr.vs_whitelisted==0)
205           {
206             fprintf(stderr, "*** %s has not been allocated resources on this node ***\n", context);
207             exit(0);
208           }
209
210         /* check whether the slice has been suspended */
211         if (slr.vs_cpu==0)
212           {
213             fprintf(stderr, "*** %s has zero cpu resources and presumably it has been disabled/suspended ***\n");
214             exit(0);
215           }
216
217         (void) (sandbox_chroot(ctx));
218
219         if ((ctx_is_new = pl_chcontext(ctx, 0, ~vc_get_insecurebcaps(),&slr)) < 0)
220           {
221             PERROR("pl_chcontext(%u)", ctx);
222             exit(1);
223           }
224         if (ctx_is_new)
225           {
226             pl_set_limits(ctx,&slr);
227             pl_setup_done(ctx);
228           }
229 #endif
230         return 0;
231 }
232
233
234 void runas_slice_user(char *username)
235 {
236         struct passwd pwdd, *pwd = &pwdd, *result;
237         char          *pwdBuffer;
238         char          *home_env, *logname_env, *mail_env, *shell_env, *user_env;
239         int           home_len, logname_len, mail_len, shell_len, user_len;
240         long          pwdBuffer_len;
241         static char   *envp[10];
242
243
244         pwdBuffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
245         if (pwdBuffer_len == -1) {
246                 PERROR("sysconf(_SC_GETPW_R_SIZE_MAX)");
247                 exit(1);
248         }
249
250         pwdBuffer = (char*)malloc(pwdBuffer_len);
251         if (pwdBuffer == NULL) {
252                 PERROR("malloc(%d)", pwdBuffer_len);
253                 exit(1);
254         }
255
256         errno = 0;
257         if ((getpwnam_r(username,pwd,pwdBuffer,pwdBuffer_len, &result) != 0) || (errno != 0)) {
258                 PERROR("getpwnam_r(%s)", username);
259                 exit(1);
260         }
261
262         if (setgid(pwd->pw_gid) < 0) {
263                 PERROR("setgid(%d)", pwd->pw_gid);
264                 exit(1);
265         }
266
267         if (setuid(pwd->pw_uid) < 0) {
268                 PERROR("setuid(%d)", pwd->pw_uid);
269                 exit(1);
270         }
271
272         if (chdir(pwd->pw_dir) < 0) {
273                 PERROR("chdir(%s)", pwd->pw_dir);
274                 exit(1);
275         }
276
277         home_len    = strlen("HOME=") + strlen(pwd->pw_dir) + NULLBYTE_SIZE;
278         logname_len = strlen("LOGNAME=") + strlen(username) + NULLBYTE_SIZE;
279         mail_len    = strlen("MAIL=/var/spool/mail/") + strlen(username) 
280                 + NULLBYTE_SIZE;
281         shell_len   = strlen("SHELL=") + strlen(pwd->pw_shell) + NULLBYTE_SIZE;
282         user_len    = strlen("USER=") + strlen(username) + NULLBYTE_SIZE;
283
284         home_env    = (char *)malloc(home_len);
285         logname_env = (char *)malloc(logname_len);
286         mail_env    = (char *)malloc(mail_len);
287         shell_env   = (char *)malloc(shell_len);
288         user_env    = (char *)malloc(user_len);
289
290         if ((home_env    == NULL)  || 
291             (logname_env == NULL)  ||
292             (mail_env    == NULL)  ||
293             (shell_env   == NULL)  ||
294             (user_env    == NULL)) {
295                 PERROR("malloc");
296                 exit(1);
297         }
298
299         sprintf(home_env, "HOME=%s", pwd->pw_dir);
300         sprintf(logname_env, "LOGNAME=%s", username);
301         sprintf(mail_env, "MAIL=/var/spool/mail/%s", username);
302         sprintf(shell_env, "SHELL=%s", pwd->pw_shell);
303         sprintf(user_env, "USER=%s", username);
304     
305         home_env[home_len - 1]       = '\0';
306         logname_env[logname_len - 1] = '\0';
307         mail_env[mail_len - 1]       = '\0';
308         shell_env[shell_len - 1]     = '\0';
309         user_env[user_len - 1]       = '\0';
310
311         envp[0] = home_env;
312         envp[1] = logname_env;
313         envp[2] = mail_env;
314         envp[3] = shell_env;
315         envp[4] = user_env;
316         envp[5] = 0;
317
318         if ((putenv(home_env)    < 0) ||
319             (putenv(logname_env) < 0) ||
320             (putenv(mail_env)    < 0) ||
321             (putenv(shell_env)   < 0) ||
322             (putenv(user_env)    < 0)) {
323                 PERROR("vserver: putenv error ");
324                 exit(1);
325         }
326 }
327
328 void slice_enter(char *context)
329 {
330         struct passwd pwdd, *pwd = &pwdd, *result;
331         char          *pwdBuffer;
332         long          pwdBuffer_len;
333         uid_t uid;
334
335         pwdBuffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
336         if (pwdBuffer_len == -1) {
337                 PERROR("sysconf(_SC_GETPW_R_SIZE_MAX)");
338                 exit(1);
339         }
340
341         pwdBuffer = (char*)malloc(pwdBuffer_len);
342         if (pwdBuffer == NULL) {
343                 PERROR("malloc(%d)", pwdBuffer_len);
344                 exit(1);
345         }
346
347         errno = 0;
348         if ((getpwnam_r(context,pwd,pwdBuffer,pwdBuffer_len, &result) != 0) || (errno != 0)) {
349                 PERROR("getpwnam_r(%s)", context);
350                 exit(2);
351         }
352         uid = pwd->pw_uid;
353
354         if (setuidgid_root() < 0) { /* For chroot, new_s_context */
355                 fprintf(stderr, "vsh: Could not become root, check that SUID flag is set on binary\n");
356                 exit(2);
357         }
358
359 #ifdef CONFIG_VSERVER_LEGACY
360         (void) (sandbox_chroot(uid));
361 #endif
362
363         if (sandbox_processes((xid_t) uid, context) < 0) {
364                 fprintf(stderr, "vsh: Could not change context to %d\n", uid);
365                 exit(2);
366         }
367 }
368
369 //--------------------------------------------------------------------
370
371 #define DEFAULT_SHELL "/bin/sh"
372
373 /* Exit statuses for programs like 'env' that exec other programs.
374    EXIT_FAILURE might not be 1, so use EXIT_FAIL in such programs.  */
375 enum
376 {
377   EXIT_CANNOT_INVOKE = 126,
378   EXIT_ENOENT = 127
379 };
380
381 int main(int argc, char **argv)
382 {
383     struct passwd   pwdd, *pwd = &pwdd, *result;
384     char            *context, *username, *shell, *pwdBuffer;
385     long            pwdBuffer_len;
386     uid_t           uid;
387     int             index, i;
388
389     if (argv[0][0]=='-') 
390       index = 1;
391     else
392       index = 0;
393
394     uid = getuid();
395     if ((pwd = getpwuid(uid)) == NULL) {
396       PERROR("getpwuid(%d)", uid);
397       exit(1);
398     }
399
400     context = (char*)strdup(pwd->pw_name);
401     if (!context) {
402       PERROR("strdup");
403       exit(2);
404     }
405
406     /* enter vserver "context" */
407     slice_enter(context);
408
409     /* Now run as username in this context. Note that for PlanetLab's
410        vserver configuration the context name also happens to be the
411        "default" username within the vserver context.
412     */
413     username = context;
414     runas_slice_user(username);
415
416     /* With the uid/gid appropriately set. Let's figure out what the
417      * shell in the vserver's /etc/passwd is for the given username.
418      */
419
420     pwdBuffer_len = sysconf(_SC_GETPW_R_SIZE_MAX);
421     if (pwdBuffer_len == -1) {
422             PERROR("sysconf(_SC_GETPW_R_SIZE_MAX");
423             exit(1);
424     }
425     pwdBuffer = (char*)malloc(pwdBuffer_len);
426     if (pwdBuffer == NULL) {
427             PERROR("malloc(%d)", pwdBuffer_len);
428             exit(1);
429     }
430
431     errno = 0;
432     if ((getpwnam_r(username,pwd,pwdBuffer,pwdBuffer_len, &result) != 0) || (errno != 0)) {
433         PERROR("getpwnam_r(%s)", username);
434         exit(1);
435     }
436
437     /* Make sure pw->pw_shell is non-NULL.*/
438     if (pwd->pw_shell == NULL || pwd->pw_shell[0] == '\0') {
439       pwd->pw_shell = (char *) DEFAULT_SHELL;
440     }
441
442     shell = (char *)strdup(pwd->pw_shell);
443     if (!shell) {
444       PERROR("strdup");
445       exit(2);
446     }
447
448     /* Check whether 'su' or 'sshd' invoked us as a login shell or
449        not; did this above when testing argv[0]=='-'.
450     */
451     argv[0] = shell;
452     if (index == 1) {
453       char **args;
454       args = (char**)malloc(sizeof(char*)*(argc+2));
455       if (!args) {
456         PERROR("malloc(%d)", sizeof(char*)*(argc+2));
457         exit(1);
458       }
459       args[0] = argv[0];
460       args[1] = "-l";
461       for(i=1;i<argc+1;i++) {
462         args[i+1] = argv[i];
463       }
464       argv = args;
465     }
466     (void) execvp(shell,argv);
467     {
468       int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
469       exit (exit_status);
470     }
471
472     return 0; /* shutup compiler */
473 }