ed36b0a2 |
1 | /* -*-c-*- |
2 | * |
4a9966ef |
3 | * $Id$ |
ed36b0a2 |
4 | * |
5 | * Chroot gaol shell |
6 | * |
ffe3ac31 |
7 | * (c) 1999 Mark Wooding |
ed36b0a2 |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
ffe3ac31 |
12 | * This program is free software; you can redistribute it and/or modify |
ed36b0a2 |
13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
16 | * |
ffe3ac31 |
17 | * This program is distributed in the hope that it will be useful, |
ed36b0a2 |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
21 | * |
22 | * You should have received a copy of the GNU General Public License |
ffe3ac31 |
23 | * along with this program; if not, write to the Free Software Foundation, |
ed36b0a2 |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ |
26 | |
ed36b0a2 |
27 | /*----- Header files ------------------------------------------------------*/ |
28 | |
29 | #include <errno.h> |
30 | #include <stdio.h> |
31 | #include <stdlib.h> |
32 | #include <string.h> |
33 | |
34 | #include <sys/types.h> |
35 | #include <unistd.h> |
cf60a621 |
36 | #include <syslog.h> |
ed36b0a2 |
37 | #include <pwd.h> |
38 | |
39 | extern char **environ; |
40 | |
41 | /*----- Main code ---------------------------------------------------------*/ |
42 | |
43 | #ifndef CHROOTSH_PATH |
44 | # define CHROOTSH_PATH "/usr/bin/chrootsh" |
45 | #endif |
46 | |
47 | static const char *quis = "chrootsh"; |
48 | |
49 | static void *xmalloc(size_t sz) |
50 | { |
51 | void *p = malloc(sz); |
52 | if (!p) { |
a079e30d |
53 | setuid(getuid()); |
ed36b0a2 |
54 | fprintf(stderr, "%s: not enough memory\n", quis); |
55 | exit(EXIT_FAILURE); |
56 | } |
57 | return (p); |
58 | } |
59 | |
60 | static char *xstrdup(const char *p) |
61 | { |
62 | size_t sz = strlen(p) + 1; |
63 | char *q = xmalloc(sz); |
64 | memcpy(q, p, sz); |
65 | return (q); |
66 | } |
67 | |
68 | int main(int argc, char *argv[]) |
69 | { |
70 | struct passwd *pw; |
71 | uid_t me = getuid(); |
cf60a621 |
72 | char *myname; |
ed36b0a2 |
73 | char **env; |
74 | char **av; |
75 | |
76 | /* --- Resolve the program name --- */ |
77 | |
78 | { |
79 | char *p, *q; |
80 | p = argv[0]; |
81 | for (q = argv[0]; *q; q++) { |
82 | if (*q == '/') |
83 | p = q + 1; |
84 | } |
cf60a621 |
85 | if (*p == '-') |
86 | p++; |
ed36b0a2 |
87 | quis = p; |
cf60a621 |
88 | openlog(quis, LOG_PID | LOG_NDELAY, LOG_DAEMON); |
ed36b0a2 |
89 | } |
90 | |
91 | /* --- Check the user is meant to be chrooted --- */ |
92 | |
a079e30d |
93 | { |
94 | uid_t eff = geteuid(); |
95 | |
96 | setreuid(eff, me); |
97 | pw = getpwuid(me); |
98 | if (!pw) { |
99 | syslog(LOG_ERR, "executed by non-existant user (uid = %i)", (int)me); |
100 | fprintf(stderr, "%s: you don't exist. Go away.\n", quis); |
101 | exit(EXIT_FAILURE); |
102 | } |
103 | if (strcmp(pw->pw_shell, CHROOTSH_PATH) != 0) { |
104 | syslog(LOG_ERR, "executed by non-chrooted user `%s'", pw->pw_name); |
105 | fprintf(stderr, "%s: you aren't a chrooted user\n", quis); |
106 | exit(EXIT_FAILURE); |
107 | } |
108 | endpwent(); |
109 | setreuid(me, eff); |
ed36b0a2 |
110 | } |
ed36b0a2 |
111 | |
112 | /* --- Chroot the user --- */ |
113 | |
114 | { |
115 | char *p = xstrdup(pw->pw_dir); |
116 | char *q = strstr(p, "/./"); |
117 | if (q) |
118 | *q = 0; |
119 | |
120 | if (chdir(p) || chroot(p)) { |
a079e30d |
121 | int e = errno; |
cf60a621 |
122 | syslog(LOG_ERR, "error entering chroot gaol: %m"); |
a079e30d |
123 | setuid(me); |
124 | fprintf(stderr, "%s: couldn't call chroot: %s\n", quis, strerror(e)); |
ed36b0a2 |
125 | exit(EXIT_FAILURE); |
126 | } |
127 | setuid(me); |
128 | free(p); |
129 | } |
130 | |
131 | /* --- Read the new password block --- */ |
132 | |
a079e30d |
133 | myname = xstrdup(pw->pw_name); |
134 | pw = getpwnam(myname); |
135 | if (!pw) { |
136 | syslog(LOG_ERR, |
137 | "configuration error: user `%s' not defined in gaol", myname); |
138 | fprintf(stderr, "%s: you don't exist in the gaol\n", quis); |
139 | exit(EXIT_FAILURE); |
ed36b0a2 |
140 | } |
a079e30d |
141 | endpwent(); |
ed36b0a2 |
142 | |
143 | /* --- Now fiddle with environment strings and suchlike --- */ |
144 | |
145 | { |
146 | size_t n; |
a079e30d |
147 | char **homevar = 0; |
ed36b0a2 |
148 | for (n = 0; environ[n]; n++) |
149 | ; |
150 | env = xmalloc((n + 1) * sizeof(char *)); |
151 | |
152 | for (n = 0; environ[n]; n++) { |
153 | if (strncmp(environ[n], "HOME=", 5) == 0) { |
154 | char *p = xmalloc(6 + strlen(pw->pw_dir)); |
155 | sprintf(p, "HOME=%s", pw->pw_dir); |
a079e30d |
156 | homevar = &env[n]; |
ed36b0a2 |
157 | env[n] = p; |
158 | } else if (strncmp(environ[n], "SHELL=", 6) == 0) { |
159 | char *p = xmalloc(7 + strlen(pw->pw_shell)); |
160 | sprintf(p, "SHELL=%s", pw->pw_shell); |
161 | env[n] = p; |
162 | } else |
163 | env[n] = environ[n]; |
164 | } |
165 | env[n] = 0; |
a079e30d |
166 | |
167 | /* --- Change directory (again) --- */ |
168 | |
169 | if (chdir(pw->pw_dir)) { |
170 | if (homevar) { |
171 | free(*homevar); |
172 | *homevar = "HOME=/"; |
173 | } |
174 | fprintf(stderr, "No directory, logging in with HOME=/\n"); |
175 | } |
ed36b0a2 |
176 | } |
177 | |
178 | /* --- Finally, sort the argument list --- */ |
179 | |
180 | { |
181 | char *p, *q; |
182 | int i; |
183 | |
184 | av = xmalloc((argc + 1) * sizeof(char *)); |
185 | p = pw->pw_shell; |
186 | for (q = p; *q; q++) { |
187 | if (*q == '/') |
188 | p = q + 1; |
189 | } |
190 | if (argv[0][0] == '-') { |
191 | q = xmalloc(2 + strlen(p)); |
192 | *q = '-'; |
193 | strcpy(q + 1, p); |
194 | av[0] = q; |
195 | } else |
196 | av[0] = p; |
197 | |
198 | for (i = 1; i <= argc; i++) |
199 | av[i] = argv[i]; |
200 | } |
201 | |
ed36b0a2 |
202 | /* --- Run the real shell --- */ |
203 | |
cf60a621 |
204 | syslog(LOG_INFO, "chroot user `%s' logged in ok", myname); |
205 | closelog(); |
ed36b0a2 |
206 | execve(pw->pw_shell, av, env); |
a079e30d |
207 | fprintf(stderr, "%s: couldn't exec `%s': %s\n", |
ed36b0a2 |
208 | quis, pw->pw_shell, strerror(errno)); |
209 | return (EXIT_FAILURE); |
210 | } |
a079e30d |
211 | |
ed36b0a2 |
212 | /*----- That's all, folks -------------------------------------------------*/ |