aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-06-23 04:24:25 +0000
committerEric Andersen <andersen@codepoet.org>2002-06-23 04:24:25 +0000
commit27f64e1f4eb4354844f6553e37501deffde8373e (patch)
tree632fbb26b13ad67f6efa335c33a22551b2707930 /libbb
parent0fbff134f400ea51540cfd6ef5eeaeab60f9a5de (diff)
downloadbusybox-w32-27f64e1f4eb4354844f6553e37501deffde8373e.tar.gz
busybox-w32-27f64e1f4eb4354844f6553e37501deffde8373e.tar.bz2
busybox-w32-27f64e1f4eb4354844f6553e37501deffde8373e.zip
Port over the last of the tinylogin applets
-Erik
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Makefile.in10
-rw-r--r--libbb/change_identity.c54
-rw-r--r--libbb/correct_password.c78
-rw-r--r--libbb/libc5.c18
-rw-r--r--libbb/messages.c36
-rw-r--r--libbb/obscure.c246
-rw-r--r--libbb/pw_encrypt.c48
-rw-r--r--libbb/pwd2spwd.c73
-rw-r--r--libbb/restricted_shell.c57
-rw-r--r--libbb/run_shell.c81
-rw-r--r--libbb/setup_environment.c93
11 files changed, 791 insertions, 3 deletions
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index c6493bfa6..2af70f8c7 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -34,17 +34,21 @@ LIBBB_SRC:= \
34 my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ 34 my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \
35 print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \ 35 print_file.c process_escape_sequence.c read_package_field.c recursive_action.c \
36 safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \ 36 safe_read.c safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c \
37 trim.c unzip.c uncompress.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c \ 37 trim.c unzip.c uncompress.c vdprintf.c verror_msg.c vperror_msg.c wfopen.c \
38 xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \ 38 xgetcwd.c xreadlink.c xregcomp.c interface.c remove_file.c last_char_is.c \
39 copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \ 39 copyfd.c vherror_msg.c herror_msg.c herror_msg_and_die.c xgethostbyname.c \
40 dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \ 40 dirname.c make_directory.c create_icmp_socket.c u_signal_names.c arith.c \
41 simplify_path.c inet_common.c inode_hash.c 41 simplify_path.c inet_common.c inode_hash.c obscure.c pwd2spwd.c xfuncs.c \
42 correct_password.c change_identity.c setup_environment.c run_shell.c \
43 pw_encrypt.c restricted_shell.c
42LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) 44LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
43 45
44LIBBB_MSRC:=$(LIBBB_DIR)messages.c 46LIBBB_MSRC:=$(LIBBB_DIR)messages.c
45LIBBB_MOBJ:=full_version.o name_too_long.o omitting_directory.o not_a_directory.o \ 47LIBBB_MOBJ:=full_version.o name_too_long.o omitting_directory.o not_a_directory.o \
46 memory_exhausted.o invalid_date.o invalid_option.o io_error.o dash_dash_help.o \ 48 memory_exhausted.o invalid_date.o invalid_option.o io_error.o dash_dash_help.o \
47 write_error.o too_few_args.o name_longer_than_foo.o unknown.o can_not_create_raw_socket.o 49 write_error.o too_few_args.o name_longer_than_foo.o unknown.o can_not_create_raw_socket.o \
50 shadow_file.o passwd_file.o group_file.o gshadow_file.o nologin_file.o securetty_file.o \
51 motd_file.o
48LIBBB_MOBJS=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ)) 52LIBBB_MOBJS=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ))
49 53
50libraries-y+=$(LIBBB_DIR)$(LIBBB_AR) 54libraries-y+=$(LIBBB_DIR)$(LIBBB_AR)
diff --git a/libbb/change_identity.c b/libbb/change_identity.c
new file mode 100644
index 000000000..819b216e0
--- /dev/null
+++ b/libbb/change_identity.c
@@ -0,0 +1,54 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1991, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <errno.h>
33#include <unistd.h>
34#include <string.h>
35#include <stdlib.h>
36#include <syslog.h>
37#include <ctype.h>
38
39#include "libbb.h"
40
41
42/* Become the user and group(s) specified by PW. */
43void change_identity ( const struct passwd *pw )
44{
45 if ( initgroups ( pw-> pw_name, pw-> pw_gid ) == -1 )
46 perror_msg_and_die ( "cannot set groups" );
47 endgrent ( );
48
49 if ( setgid ( pw-> pw_gid ))
50 perror_msg_and_die ( "cannot set group id" );
51 if ( setuid ( pw->pw_uid ))
52 perror_msg_and_die ( "cannot set user id" );
53}
54
diff --git a/libbb/correct_password.c b/libbb/correct_password.c
new file mode 100644
index 000000000..758b89eed
--- /dev/null
+++ b/libbb/correct_password.c
@@ -0,0 +1,78 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1991, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <errno.h>
33#include <unistd.h>
34#include <string.h>
35#include <stdlib.h>
36#include <syslog.h>
37#include <ctype.h>
38#include <crypt.h>
39
40#include "libbb.h"
41
42
43
44/* Ask the user for a password.
45 Return 1 if the user gives the correct password for entry PW,
46 0 if not. Return 1 without asking for a password if run by UID 0
47 or if PW has an empty password. */
48
49int correct_password ( const struct passwd *pw )
50{
51 char *unencrypted, *encrypted, *correct;
52
53#ifdef CONFIG_FEATURE_SHADOWPASSWDS
54 if (( strcmp ( pw-> pw_passwd, "x" ) == 0 ) || ( strcmp ( pw-> pw_passwd, "*" ) == 0 )) {
55 struct spwd *sp = getspnam ( pw-> pw_name );
56
57 if ( !sp )
58 error_msg_and_die ( "no valid shadow password" );
59
60 correct = sp-> sp_pwdp;
61 }
62 else
63#endif
64 correct = pw-> pw_passwd;
65
66 if ( correct == 0 || correct[0] == '\0' )
67 return 1;
68
69 unencrypted = getpass ( "Password: " );
70 if ( !unencrypted )
71 {
72 fputs ( "getpass: cannot open /dev/tty\n", stderr );
73 return 0;
74 }
75 encrypted = crypt ( unencrypted, correct );
76 memset ( unencrypted, 0, xstrlen ( unencrypted ));
77 return ( strcmp ( encrypted, correct ) == 0 ) ? 1 : 0;
78}
diff --git a/libbb/libc5.c b/libbb/libc5.c
index df622f65b..af15d1ac7 100644
--- a/libbb/libc5.c
+++ b/libbb/libc5.c
@@ -11,6 +11,24 @@
11 11
12#if __GNU_LIBRARY__ < 5 12#if __GNU_LIBRARY__ < 5
13 13
14/*
15 * Some systems already have updwtmp(). Some don't... This is
16 * the updwtmp() implementation from uClibc, Copyright 2002 by
17 * Erik Andersen <andersee@debian.org>
18 */
19extern void updwtmp(const char *wtmp_file, const struct utmp *lutmp)
20{
21 int fd;
22
23 fd = open(wtmp_file, O_APPEND | O_WRONLY, 0);
24 if (fd >= 0) {
25 if (lockf(fd, F_LOCK, 0)==0) {
26 write(fd, (const char *) lutmp, sizeof(struct utmp));
27 lockf(fd, F_ULOCK, 0);
28 close(fd);
29 }
30 }
31}
14 32
15/* Copyright (C) 1991 Free Software Foundation, Inc. 33/* Copyright (C) 1991 Free Software Foundation, Inc.
16This file is part of the GNU C Library. 34This file is part of the GNU C Library.
diff --git a/libbb/messages.c b/libbb/messages.c
index 895cfdc2b..185c1ee91 100644
--- a/libbb/messages.c
+++ b/libbb/messages.c
@@ -66,3 +66,39 @@
66#ifdef L_can_not_create_raw_socket 66#ifdef L_can_not_create_raw_socket
67 const char * const can_not_create_raw_socket = "can`t create raw socket"; 67 const char * const can_not_create_raw_socket = "can`t create raw socket";
68#endif 68#endif
69
70#ifdef L_passwd_file
71#define PASSWD_FILE "/etc/passwd"
72const char * const passwd_file = PASSWD_FILE;
73#endif
74
75#ifdef L_shadow_file
76#define SHADOW_FILE "/etc/shadow"
77const char * const shadow_file = SHADOW_FILE;
78#endif
79
80#ifdef L_group_file
81#define GROUP_FILE "/etc/group"
82const char * const group_file = GROUP_FILE;
83#endif
84
85#ifdef L_gshadow_file
86#define GSHADOW_FILE "/etc/gshadow"
87const char * const gshadow_file = GSHADOW_FILE;
88#endif
89
90#ifdef L_nologin_file
91#define NOLOGIN_FILE "/etc/nologin"
92const char * const nologin_file = NOLOGIN_FILE;
93#endif
94
95#ifdef L_securetty_file
96#define SECURETTY_FILE "/etc/securetty"
97const char * const securetty_file = SECURETTY_FILE;
98#endif
99
100#ifdef L_motd_file
101#define MOTD_FILE "/etc/motd"
102const char * const motd_file = MOTD_FILE;
103#endif
104
diff --git a/libbb/obscure.c b/libbb/obscure.c
new file mode 100644
index 000000000..dc7de751d
--- /dev/null
+++ b/libbb/obscure.c
@@ -0,0 +1,246 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1994, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/*
32 * This version of obscure.c contains modifications to support "cracklib"
33 * by Alec Muffet (alec.muffett@uk.sun.com). You must obtain the Cracklib
34 * library source code for this function to operate.
35 */
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <string.h>
40#include <ctype.h>
41#include "libbb.h"
42
43/*
44 * can't be a palindrome - like `R A D A R' or `M A D A M'
45 */
46
47static int palindrome(const char *old, const char *newval)
48{
49 int i, j;
50
51 i = strlen(newval);
52
53 for (j = 0; j < i; j++)
54 if (newval[i - j - 1] != newval[j])
55 return 0;
56
57 return 1;
58}
59
60/*
61 * more than half of the characters are different ones.
62 */
63
64static int similiar(const char *old, const char *newval)
65{
66 int i, j;
67
68 for (i = j = 0; newval[i] && old[i]; i++)
69 if (strchr(newval, old[i]))
70 j++;
71
72 if (i >= j * 2)
73 return 0;
74
75 return 1;
76}
77
78/*
79 * a nice mix of characters.
80 */
81
82static int simple(const char *old, const char *newval)
83{
84 int digits = 0;
85 int uppers = 0;
86 int lowers = 0;
87 int others = 0;
88 int size;
89 int i;
90
91 for (i = 0; newval[i]; i++) {
92 if (isdigit(newval[i]))
93 digits++;
94 else if (isupper(newval[i]))
95 uppers++;
96 else if (islower(newval[i]))
97 lowers++;
98 else
99 others++;
100 }
101
102 /*
103 * The scam is this - a password of only one character type
104 * must be 8 letters long. Two types, 7, and so on.
105 */
106
107 size = 9;
108 if (digits)
109 size--;
110 if (uppers)
111 size--;
112 if (lowers)
113 size--;
114 if (others)
115 size--;
116
117 if (size <= i)
118 return 0;
119
120 return 1;
121}
122
123static char *str_lower(char *string)
124{
125 char *cp;
126
127 for (cp = string; *cp; cp++)
128 *cp = tolower(*cp);
129 return string;
130}
131
132static char *password_check(const char *old, const char *newval, const struct passwd *pwdp)
133{
134 char *msg = NULL;
135 char *oldmono, *newmono, *wrapped;
136
137 if (strcmp(newval, old) == 0)
138 return "no change";
139
140 newmono = str_lower(xstrdup(newval));
141 oldmono = str_lower(xstrdup(old));
142 wrapped = (char *) xmalloc(strlen(oldmono) * 2 + 1);
143 strcpy(wrapped, oldmono);
144 strcat(wrapped, oldmono);
145
146 if (palindrome(oldmono, newmono))
147 msg = "a palindrome";
148
149 if (!msg && strcmp(oldmono, newmono) == 0)
150 msg = "case changes only";
151
152 if (!msg && similiar(oldmono, newmono))
153 msg = "too similiar";
154
155 if (!msg && simple(old, newval))
156 msg = "too simple";
157
158 if (!msg && strstr(wrapped, newmono))
159 msg = "rotated";
160
161 bzero(newmono, strlen(newmono));
162 bzero(oldmono, strlen(oldmono));
163 bzero(wrapped, strlen(wrapped));
164 free(newmono);
165 free(oldmono);
166 free(wrapped);
167
168 return msg;
169}
170
171static char *obscure_msg(const char *old, const char *newval, const struct passwd *pwdp)
172{
173 int maxlen, oldlen, newlen;
174 char *new1, *old1, *msg;
175
176 oldlen = strlen(old);
177 newlen = strlen(newval);
178
179#if 0 /* why not check the password when set for the first time? --marekm */
180 if (old[0] == '\0')
181 /* return (1); */
182 return NULL;
183#endif
184
185 if (newlen < 5)
186 return "too short";
187
188 /*
189 * Remaining checks are optional.
190 */
191 /* Not for us -- Sean
192 *if (!getdef_bool("OBSCURE_CHECKS_ENAB"))
193 * return NULL;
194 */
195 msg = password_check(old, newval, pwdp);
196 if (msg)
197 return msg;
198
199 /* The traditional crypt() truncates passwords to 8 chars. It is
200 possible to circumvent the above checks by choosing an easy
201 8-char password and adding some random characters to it...
202 Example: "password$%^&*123". So check it again, this time
203 truncated to the maximum length. Idea from npasswd. --marekm */
204
205 maxlen = 8;
206 if (oldlen <= maxlen && newlen <= maxlen)
207 return NULL;
208
209 new1 = (char *) xstrdup(newval);
210 old1 = (char *) xstrdup(old);
211 if (newlen > maxlen)
212 new1[maxlen] = '\0';
213 if (oldlen > maxlen)
214 old1[maxlen] = '\0';
215
216 msg = password_check(old1, new1, pwdp);
217
218 bzero(new1, newlen);
219 bzero(old1, oldlen);
220 free(new1);
221 free(old1);
222
223 return msg;
224}
225
226/*
227 * Obscure - see if password is obscure enough.
228 *
229 * The programmer is encouraged to add as much complexity to this
230 * routine as desired. Included are some of my favorite ways to
231 * check passwords.
232 */
233
234extern int obscure(const char *old, const char *newval, const struct passwd *pwdp)
235{
236 char *msg = obscure_msg(old, newval, pwdp);
237
238 /* if (msg) { */
239 if (msg != NULL) {
240 printf("Bad password: %s.\n", msg);
241 /* return 0; */
242 return 1;
243 }
244 /* return 1; */
245 return 0;
246}
diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c
new file mode 100644
index 000000000..0e4eb9f8a
--- /dev/null
+++ b/libbb/pw_encrypt.c
@@ -0,0 +1,48 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routine.
4 *
5 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <string.h>
24#include <crypt.h>
25#include "libbb.h"
26
27
28extern char *pw_encrypt(const char *clear, const char *salt)
29{
30 static char cipher[128];
31 char *cp;
32
33#ifdef CONFIG_FEATURE_SHA1_PASSWORDS
34 if (strncmp(salt, "$2$", 3) == 0) {
35 return sha1_crypt(clear);
36 }
37#endif
38 cp = (char *) crypt(clear, salt);
39 /* if crypt (a nonstandard crypt) returns a string too large,
40 truncate it so we don't overrun buffers and hope there is
41 enough security in what's left */
42 if (strlen(cp) > sizeof(cipher)-1) {
43 cp[sizeof(cipher)-1] = 0;
44 }
45 strcpy(cipher, cp);
46 return cipher;
47}
48
diff --git a/libbb/pwd2spwd.c b/libbb/pwd2spwd.c
new file mode 100644
index 000000000..95a2e4684
--- /dev/null
+++ b/libbb/pwd2spwd.c
@@ -0,0 +1,73 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1994, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <time.h>
32#include <sys/types.h>
33#include "libbb.h"
34
35/*
36 * pwd_to_spwd - create entries for new spwd structure
37 *
38 * pwd_to_spwd() creates a new (struct spwd) containing the
39 * information in the pointed-to (struct passwd).
40 */
41#define DAY (24L*3600L)
42#define WEEK (7*DAY)
43#define SCALE DAY
44struct spwd *pwd_to_spwd(const struct passwd *pw)
45{
46 static struct spwd sp;
47
48 /*
49 * Nice, easy parts first. The name and passwd map directly
50 * from the old password structure to the new one.
51 */
52 sp.sp_namp = pw->pw_name;
53 sp.sp_pwdp = pw->pw_passwd;
54
55 /*
56 * Defaults used if there is no pw_age information.
57 */
58 sp.sp_min = 0;
59 sp.sp_max = (10000L * DAY) / SCALE;
60 sp.sp_lstchg = time((time_t *) 0) / SCALE;
61
62 /*
63 * These fields have no corresponding information in the password
64 * file. They are set to uninitialized values.
65 */
66 sp.sp_warn = -1;
67 sp.sp_expire = -1;
68 sp.sp_inact = -1;
69 sp.sp_flag = -1;
70
71 return &sp;
72}
73
diff --git a/libbb/restricted_shell.c b/libbb/restricted_shell.c
new file mode 100644
index 000000000..74a64140f
--- /dev/null
+++ b/libbb/restricted_shell.c
@@ -0,0 +1,57 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1991, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <errno.h>
33#include <unistd.h>
34#include <string.h>
35#include <stdlib.h>
36#include <syslog.h>
37#include <ctype.h>
38#include "libbb.h"
39
40
41
42/* Return 1 if SHELL is a restricted shell (one not returned by
43 getusershell), else 0, meaning it is a standard shell. */
44
45int restricted_shell ( const char *shell )
46{
47 char *line;
48
49 setusershell ( );
50 while (( line = getusershell ( ))) {
51 if (( *line != '#' ) && ( strcmp ( line, shell ) == 0 ))
52 break;
53 }
54 endusershell ( );
55 return line ? 0 : 1;
56}
57
diff --git a/libbb/run_shell.c b/libbb/run_shell.c
new file mode 100644
index 000000000..30050fecb
--- /dev/null
+++ b/libbb/run_shell.c
@@ -0,0 +1,81 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1991, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <errno.h>
33#include <unistd.h>
34#include <string.h>
35#include <stdlib.h>
36#include <syslog.h>
37#include <ctype.h>
38#include "libbb.h"
39
40
41/* Run SHELL, or DEFAULT_SHELL if SHELL is empty.
42 If COMMAND is nonzero, pass it to the shell with the -c option.
43 If ADDITIONAL_ARGS is nonzero, pass it to the shell as more
44 arguments. */
45
46void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args )
47{
48 const char **args;
49 int argno = 1;
50 int additional_args_cnt = 0;
51
52 for ( args = additional_args; args && *args; args++ )
53 additional_args_cnt++;
54
55 if ( additional_args )
56 args = (const char **) xmalloc (sizeof (char *) * ( 4 + additional_args_cnt ));
57 else
58 args = (const char **) xmalloc (sizeof (char *) * 4 );
59
60 args [0] = get_last_path_component ( xstrdup ( shell ));
61
62 if ( loginshell ) {
63 char *args0 = xmalloc ( xstrlen ( args [0] ) + 2 );
64 args0 [0] = '-';
65 strcpy ( args0 + 1, args [0] );
66 args [0] = args0;
67 }
68
69 if ( command ) {
70 args [argno++] = "-c";
71 args [argno++] = command;
72 }
73 if ( additional_args ) {
74 for ( ; *additional_args; ++additional_args )
75 args [argno++] = *additional_args;
76 }
77 args [argno] = 0;
78 execv ( shell, (char **) args );
79 perror_msg_and_die ( "cannot run %s", shell );
80}
81
diff --git a/libbb/setup_environment.c b/libbb/setup_environment.c
new file mode 100644
index 000000000..dc171fa1f
--- /dev/null
+++ b/libbb/setup_environment.c
@@ -0,0 +1,93 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright 1989 - 1991, Julianne Frances Haugh <jockgrrl@austin.rr.com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <errno.h>
33#include <unistd.h>
34#include <string.h>
35#include <stdlib.h>
36#include <syslog.h>
37#include <ctype.h>
38#include "libbb.h"
39
40
41
42#define DEFAULT_LOGIN_PATH "/bin:/usr/bin"
43#define DEFAULT_ROOT_LOGIN_PATH "/usr/sbin:/bin:/usr/bin:/sbin"
44
45static void xsetenv ( const char *key, const char *value )
46{
47 if ( setenv ( key, value, 1 ))
48 error_msg_and_die ( "out of memory" );
49}
50
51void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw )
52{
53 if ( loginshell ) {
54 char *term;
55
56 /* Change the current working directory to be the home directory
57 * of the user. It is a fatal error for this process to be unable
58 * to change to that directory. There is no "default" home
59 * directory.
60 * Some systems default to HOME=/
61 */
62 if ( chdir ( pw-> pw_dir )) {
63 if ( chdir ( "/" )) {
64 syslog ( LOG_WARNING, "unable to cd to %s' for user %s'\n", pw-> pw_dir, pw-> pw_name );
65 error_msg_and_die ( "cannot cd to home directory or /" );
66 }
67 fputs ( "warning: cannot change to home directory\n", stderr );
68 }
69
70 /* Leave TERM unchanged. Set HOME, SHELL, USER, LOGNAME, PATH.
71 Unset all other environment variables. */
72 term = getenv ("TERM");
73 clearenv ( );
74 if ( term )
75 xsetenv ( "TERM", term );
76 xsetenv ( "HOME", pw-> pw_dir );
77 xsetenv ( "SHELL", shell );
78 xsetenv ( "USER", pw-> pw_name );
79 xsetenv ( "LOGNAME", pw-> pw_name );
80 xsetenv ( "PATH", ( pw-> pw_uid ? DEFAULT_LOGIN_PATH : DEFAULT_ROOT_LOGIN_PATH ));
81 }
82 else if ( changeenv ) {
83 /* Set HOME, SHELL, and if not becoming a super-user,
84 USER and LOGNAME. */
85 xsetenv ( "HOME", pw-> pw_dir );
86 xsetenv ( "SHELL", shell );
87 if ( pw-> pw_uid ) {
88 xsetenv ( "USER", pw-> pw_name );
89 xsetenv ( "LOGNAME", pw-> pw_name );
90 }
91 }
92}
93