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