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