Initial commit for Tizen
[profile/extras/shadow-utils.git] / libmisc / sub.c
1 /*
2  * Copyright (c) 1989 - 1991, Julianne Frances Haugh
3  * Copyright (c) 1996 - 1999, Marek Michałkiewicz
4  * Copyright (c) 2003 - 2006, Tomasz Kłoczko
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holders or contributors may not be used to
16  *    endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <config.h>
33
34 #ident "$Id: sub.c 2849 2009-04-30 21:08:49Z nekral-guest $"
35
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include "prototypes.h"
40 #include "defines.h"
41 #define BAD_SUBROOT2    "invalid root `%s' for user `%s'\n"
42 #define NO_SUBROOT2     "no subsystem root `%s' for user `%s'\n"
43 /*
44  * subsystem - change to subsystem root
45  *
46  *      A subsystem login is indicated by the presense of a "*" as
47  *      the first character of the login shell.  The given home
48  *      directory will be used as the root of a new filesystem which
49  *      the user is actually logged into.
50  */
51 void subsystem (const struct passwd *pw)
52 {
53         /*
54          * The new root directory must begin with a "/" character.
55          */
56
57         if (pw->pw_dir[0] != '/') {
58                 printf (_("Invalid root directory '%s'\n"), pw->pw_dir);
59                 SYSLOG ((LOG_WARN, BAD_SUBROOT2, pw->pw_dir, pw->pw_name));
60                 closelog ();
61                 exit (EXIT_FAILURE);
62         }
63
64         /*
65          * The directory must be accessible and the current process
66          * must be able to change into it.
67          */
68
69         if (chdir (pw->pw_dir) || chroot (pw->pw_dir)) {
70                 printf (_("Can't change root directory to '%s'\n"),
71                         pw->pw_dir);
72                 SYSLOG ((LOG_WARN, NO_SUBROOT2, pw->pw_dir, pw->pw_name));
73                 closelog ();
74                 exit (EXIT_FAILURE);
75         }
76 }