diff options
-rw-r--r-- | loginutils/login.c | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/loginutils/login.c b/loginutils/login.c index 4d93ece49..8ccc5bc8a 100644 --- a/loginutils/login.c +++ b/loginutils/login.c | |||
@@ -34,15 +34,15 @@ extern char *pw_encrypt(const char *clear, const char *salt); | |||
34 | 34 | ||
35 | 35 | ||
36 | // login defines | 36 | // login defines |
37 | #define DEFAULT_USER "UNKNOWN" | ||
38 | #define DEFAULT_PWD "!" | ||
39 | #define DEFAULT_SHELL "/bin/sh" | ||
40 | #define TIMEOUT 60 | 37 | #define TIMEOUT 60 |
41 | #define FAIL_DELAY 3 | 38 | #define FAIL_DELAY 3 |
39 | #define EMPTY_USERNAME_COUNT 10 | ||
42 | #define MOTD_FILE "/etc/motd" | 40 | #define MOTD_FILE "/etc/motd" |
43 | #define NOLOGIN_FILE "/etc/nologin" | 41 | #define NOLOGIN_FILE "/etc/nologin" |
44 | #define SECURETTY_FILE "/etc/securetty" | 42 | #define SECURETTY_FILE "/etc/securetty" |
45 | 43 | ||
44 | #define USERNAME_SIZE 32 | ||
45 | |||
46 | /* Stuff global to this file */ | 46 | /* Stuff global to this file */ |
47 | struct utmp utent; | 47 | struct utmp utent; |
48 | 48 | ||
@@ -58,14 +58,13 @@ static inline int check_tty ( const char *tty ) { return 1; } | |||
58 | #endif | 58 | #endif |
59 | 59 | ||
60 | static int is_my_tty ( const char *tty ); | 60 | static int is_my_tty ( const char *tty ); |
61 | static const char *login_prompt ( void ); | 61 | static int login_prompt ( char *buf_name ); |
62 | static void motd ( void ); | 62 | static void motd ( void ); |
63 | static void set_env(int argc, char *const *argv); | ||
64 | 63 | ||
65 | 64 | ||
66 | static void alarm_handler ( int sig ) | 65 | static void alarm_handler ( int sig ) |
67 | { | 66 | { |
68 | error_msg ( "\nLogin timed out after %d seconds.\n", TIMEOUT ); | 67 | fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT ); |
69 | exit ( EXIT_SUCCESS ); | 68 | exit ( EXIT_SUCCESS ); |
70 | } | 69 | } |
71 | 70 | ||
@@ -75,6 +74,7 @@ extern int login_main(int argc, char **argv) | |||
75 | char tty[BUFSIZ]; | 74 | char tty[BUFSIZ]; |
76 | char full_tty[200]; | 75 | char full_tty[200]; |
77 | char fromhost[512]; | 76 | char fromhost[512]; |
77 | char username[USERNAME_SIZE]; | ||
78 | char *tmp; | 78 | char *tmp; |
79 | int amroot; | 79 | int amroot; |
80 | int flag; | 80 | int flag; |
@@ -85,9 +85,9 @@ extern int login_main(int argc, char **argv) | |||
85 | int opt_preserve = 0; | 85 | int opt_preserve = 0; |
86 | int opt_fflag = 0; | 86 | int opt_fflag = 0; |
87 | char *opt_host = 0; | 87 | char *opt_host = 0; |
88 | const char *username = 0; | ||
89 | int alarmstarted = 0; | 88 | int alarmstarted = 0; |
90 | 89 | ||
90 | username[0]=0; | ||
91 | amroot = ( getuid ( ) == 0 ); | 91 | amroot = ( getuid ( ) == 0 ); |
92 | signal ( SIGALRM, alarm_handler ); | 92 | signal ( SIGALRM, alarm_handler ); |
93 | 93 | ||
@@ -99,7 +99,6 @@ extern int login_main(int argc, char **argv) | |||
99 | while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) { | 99 | while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) { |
100 | switch ( flag ) { | 100 | switch ( flag ) { |
101 | case 'p': | 101 | case 'p': |
102 | printf ( "PRESERVE\n" ); | ||
103 | opt_preserve = 1; | 102 | opt_preserve = 1; |
104 | break; | 103 | break; |
105 | case 'f': | 104 | case 'f': |
@@ -111,11 +110,9 @@ extern int login_main(int argc, char **argv) | |||
111 | show_usage ( ); | 110 | show_usage ( ); |
112 | 111 | ||
113 | if ( !amroot ) /* Auth bypass only if real UID is zero */ | 112 | if ( !amroot ) /* Auth bypass only if real UID is zero */ |
114 | error_msg_and_die ( "login: -f permission denied\n" ); | 113 | error_msg_and_die ( "-f permission denied" ); |
115 | |||
116 | printf ( "USERNAME: %s\n", optarg ); | ||
117 | 114 | ||
118 | username = optarg; | 115 | safe_strncpy(username, optarg, USERNAME_SIZE); |
119 | opt_fflag = 1; | 116 | opt_fflag = 1; |
120 | break; | 117 | break; |
121 | case 'h': | 118 | case 'h': |
@@ -126,8 +123,8 @@ extern int login_main(int argc, char **argv) | |||
126 | } | 123 | } |
127 | } | 124 | } |
128 | 125 | ||
129 | if ( optind < argc ) // got a username | 126 | if (optind < argc) // user from command line (getty) |
130 | username = argv [optind++]; | 127 | safe_strncpy(username, argv[optind], USERNAME_SIZE); |
131 | 128 | ||
132 | if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 )) | 129 | if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 )) |
133 | return EXIT_FAILURE; /* Must be a terminal */ | 130 | return EXIT_FAILURE; /* Must be a terminal */ |
@@ -151,13 +148,16 @@ extern int login_main(int argc, char **argv) | |||
151 | else | 148 | else |
152 | snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty ); | 149 | snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty ); |
153 | 150 | ||
151 | setpgrp(); | ||
152 | |||
154 | openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH ); | 153 | openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH ); |
155 | 154 | ||
156 | while ( 1 ) { | 155 | while ( 1 ) { |
157 | failed = 0; | 156 | failed = 0; |
158 | 157 | ||
159 | if ( !username || !username[0] ) | 158 | if ( !username[0] ) |
160 | username = login_prompt ( ); | 159 | if(!login_prompt ( username )) |
160 | return EXIT_FAILURE; | ||
161 | 161 | ||
162 | if ( !alarmstarted && ( TIMEOUT > 0 )) { | 162 | if ( !alarmstarted && ( TIMEOUT > 0 )) { |
163 | alarm ( TIMEOUT ); | 163 | alarm ( TIMEOUT ); |
@@ -165,9 +165,8 @@ extern int login_main(int argc, char **argv) | |||
165 | } | 165 | } |
166 | 166 | ||
167 | if (!( pw = getpwnam ( username ))) { | 167 | if (!( pw = getpwnam ( username ))) { |
168 | pw_copy. pw_name = DEFAULT_USER; | 168 | pw_copy.pw_name = "UNKNOWN"; |
169 | pw_copy. pw_passwd = DEFAULT_PWD; | 169 | pw_copy.pw_passwd = "!"; |
170 | pw_copy. pw_shell = DEFAULT_SHELL; | ||
171 | opt_fflag = 0; | 170 | opt_fflag = 0; |
172 | failed = 1; | 171 | failed = 1; |
173 | } else | 172 | } else |
@@ -183,7 +182,7 @@ extern int login_main(int argc, char **argv) | |||
183 | goto auth_ok; | 182 | goto auth_ok; |
184 | } | 183 | } |
185 | 184 | ||
186 | if (( pw-> pw_uid == 0 ) && ( !check_tty ( tty ))) | 185 | if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty ))) |
187 | failed = 1; | 186 | failed = 1; |
188 | 187 | ||
189 | /* Don't check the password if password entry is empty (!) */ | 188 | /* Don't check the password if password entry is empty (!) */ |
@@ -194,7 +193,6 @@ extern int login_main(int argc, char **argv) | |||
194 | if ( correct_password ( pw )) | 193 | if ( correct_password ( pw )) |
195 | goto auth_ok; | 194 | goto auth_ok; |
196 | 195 | ||
197 | syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw-> pw_name, fromhost); | ||
198 | failed = 1; | 196 | failed = 1; |
199 | 197 | ||
200 | auth_ok: | 198 | auth_ok: |
@@ -213,9 +211,12 @@ auth_ok: | |||
213 | } | 211 | } |
214 | 212 | ||
215 | puts("Login incorrect"); | 213 | puts("Login incorrect"); |
216 | if ( ++count == 3 ) | 214 | username[0] = 0; |
215 | if ( ++count == 3 ) { | ||
216 | syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost); | ||
217 | return EXIT_FAILURE; | 217 | return EXIT_FAILURE; |
218 | } | 218 | } |
219 | } | ||
219 | 220 | ||
220 | alarm ( 0 ); | 221 | alarm ( 0 ); |
221 | if ( check_nologin ( pw-> pw_uid == 0 )) | 222 | if ( check_nologin ( pw-> pw_uid == 0 )) |
@@ -251,11 +252,13 @@ auth_ok: | |||
251 | 252 | ||
252 | 253 | ||
253 | 254 | ||
254 | static const char *login_prompt ( void ) | 255 | static int login_prompt ( char *buf_name ) |
255 | { | 256 | { |
256 | char buf [1024]; | 257 | char buf [1024]; |
257 | char *sp, *ep; | 258 | char *sp, *ep; |
259 | int i; | ||
258 | 260 | ||
261 | for(i=0; i<EMPTY_USERNAME_COUNT; i++) { | ||
259 | gethostname ( buf, sizeof( buf )); | 262 | gethostname ( buf, sizeof( buf )); |
260 | printf ( "\nBusyBox on %s login: ", buf ); | 263 | printf ( "\nBusyBox on %s login: ", buf ); |
261 | fflush ( stdout ); | 264 | fflush ( stdout ); |
@@ -263,14 +266,18 @@ static const char *login_prompt ( void ) | |||
263 | if ( !fgets ( buf, sizeof( buf ) - 1, stdin )) | 266 | if ( !fgets ( buf, sizeof( buf ) - 1, stdin )) |
264 | return 0; | 267 | return 0; |
265 | 268 | ||
266 | if ( !strchr ( buf, '\n' )); | 269 | if ( !strchr ( buf, '\n' )) |
267 | return 0; | 270 | return 0; |
268 | 271 | ||
269 | for ( sp = buf; isspace ( *sp ); sp++ ) { } | 272 | for ( sp = buf; isspace ( *sp ); sp++ ) { } |
270 | for ( ep = sp; isgraph ( *ep ); ep++ ) { } | 273 | for ( ep = sp; isgraph ( *ep ); ep++ ) { } |
271 | 274 | ||
272 | *ep = 0; | 275 | *ep = 0; |
273 | return sp; | 276 | safe_strncpy(buf_name, sp, USERNAME_SIZE); |
277 | if(buf_name[0]) | ||
278 | return 1; | ||
279 | } | ||
280 | return 0; | ||
274 | } | 281 | } |
275 | 282 | ||
276 | 283 | ||