7 * (c) 1999 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This program is free software; you can redistribute it and/or modify
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.
17 * This program is distributed in the hope that it will be useful,
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.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
34 #include <sys/types.h>
39 extern char **environ
;
41 /*----- Main code ---------------------------------------------------------*/
44 # define CHROOTSH_PATH "/usr/bin/chrootsh"
47 static const char *quis
= "chrootsh";
49 static void *xmalloc(size_t sz
)
54 fprintf(stderr
, "%s: not enough memory\n", quis
);
60 static char *xstrdup(const char *p
)
62 size_t sz
= strlen(p
) + 1;
63 char *q
= xmalloc(sz
);
68 int main(int argc
, char *argv
[])
76 /* --- Resolve the program name --- */
81 for (q
= argv
[0]; *q
; q
++) {
88 openlog(quis
, LOG_PID
| LOG_NDELAY
, LOG_DAEMON
);
91 /* --- Check the user is meant to be chrooted --- */
94 uid_t eff
= geteuid();
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
);
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
);
112 /* --- Chroot the user --- */
115 char *p
= xstrdup(pw
->pw_dir
);
116 char *q
= strstr(p
, "/./");
120 if (chdir(p
) || chroot(p
)) {
122 syslog(LOG_ERR
, "error entering chroot gaol: %m");
124 fprintf(stderr
, "%s: couldn't call chroot: %s\n", quis
, strerror(e
));
131 /* --- Read the new password block --- */
133 myname
= xstrdup(pw
->pw_name
);
134 pw
= getpwnam(myname
);
137 "configuration error: user `%s' not defined in gaol", myname
);
138 fprintf(stderr
, "%s: you don't exist in the gaol\n", quis
);
143 /* --- Now fiddle with environment strings and suchlike --- */
148 for (n
= 0; environ
[n
]; n
++)
150 env
= xmalloc((n
+ 1) * sizeof(char *));
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
);
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
);
167 /* --- Change directory (again) --- */
169 if (chdir(pw
->pw_dir
)) {
174 fprintf(stderr
, "No directory, logging in with HOME=/\n");
178 /* --- Finally, sort the argument list --- */
184 av
= xmalloc((argc
+ 1) * sizeof(char *));
186 for (q
= p
; *q
; q
++) {
190 if (argv
[0][0] == '-') {
191 q
= xmalloc(2 + strlen(p
));
198 for (i
= 1; i
<= argc
; i
++)
202 /* --- Run the real shell --- */
204 syslog(LOG_INFO
, "chroot user `%s' logged in ok", myname
);
206 execve(pw
->pw_shell
, av
, env
);
207 fprintf(stderr
, "%s: couldn't exec `%s': %s\n",
208 quis
, pw
->pw_shell
, strerror(errno
));
209 return (EXIT_FAILURE
);
212 /*----- That's all, folks -------------------------------------------------*/