Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | #include <fcntl.h> |
| 3 | #include <signal.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <syslog.h> |
| 8 | #include <termios.h> |
| 9 | #include <unistd.h> |
| 10 | #include <utmp.h> |
| 11 | #include <sys/resource.h> |
| 12 | #include <sys/stat.h> |
| 13 | #include <sys/time.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <ctype.h> |
| 16 | #include <time.h> |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 17 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 18 | #include "busybox.h" |
| 19 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 20 | |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_FEATURE_U_W_TMP |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 22 | // import from utmp.c |
| 23 | static void checkutmp(int picky); |
| 24 | static void setutmp(const char *name, const char *line); |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 25 | /* Stuff global to this file */ |
| 26 | struct utmp utent; |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 27 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 28 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 29 | // login defines |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 30 | #define TIMEOUT 60 |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 31 | #define EMPTY_USERNAME_COUNT 10 |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 32 | #define USERNAME_SIZE 32 |
| 33 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 34 | |
| 35 | static int check_nologin ( int amroot ); |
| 36 | |
| 37 | #if defined CONFIG_FEATURE_SECURETTY |
| 38 | static int check_tty ( const char *tty ); |
| 39 | |
| 40 | #else |
| 41 | static inline int check_tty ( const char *tty ) { return 1; } |
| 42 | |
| 43 | #endif |
| 44 | |
| 45 | static int is_my_tty ( const char *tty ); |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 46 | static int login_prompt ( char *buf_name ); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 47 | static void motd ( void ); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | static void alarm_handler ( int sig ) |
| 51 | { |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 52 | fprintf (stderr, "\nLogin timed out after %d seconds.\n", TIMEOUT ); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 53 | exit ( EXIT_SUCCESS ); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | extern int login_main(int argc, char **argv) |
| 58 | { |
| 59 | char tty[BUFSIZ]; |
| 60 | char full_tty[200]; |
| 61 | char fromhost[512]; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 62 | char username[USERNAME_SIZE]; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 63 | char *tmp; |
| 64 | int amroot; |
| 65 | int flag; |
| 66 | int failed; |
| 67 | int count=0; |
| 68 | struct passwd *pw, pw_copy; |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 69 | #ifdef CONFIG_WHEEL_GROUP |
| 70 | struct group *grp; |
| 71 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 72 | int opt_preserve = 0; |
| 73 | int opt_fflag = 0; |
| 74 | char *opt_host = 0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 75 | int alarmstarted = 0; |
| 76 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 77 | username[0]=0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 78 | amroot = ( getuid ( ) == 0 ); |
| 79 | signal ( SIGALRM, alarm_handler ); |
| 80 | |
| 81 | if (( argc > 1 ) && ( TIMEOUT > 0 )) { |
| 82 | alarm ( TIMEOUT ); |
| 83 | alarmstarted = 1; |
| 84 | } |
| 85 | |
| 86 | while (( flag = getopt(argc, argv, "f:h:p")) != EOF ) { |
| 87 | switch ( flag ) { |
| 88 | case 'p': |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 89 | opt_preserve = 1; |
| 90 | break; |
| 91 | case 'f': |
| 92 | /* |
| 93 | * username must be a seperate token |
| 94 | * (-f root, *NOT* -froot). --marekm |
| 95 | */ |
| 96 | if ( optarg != argv[optind-1] ) |
| 97 | show_usage ( ); |
| 98 | |
| 99 | if ( !amroot ) /* Auth bypass only if real UID is zero */ |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 100 | error_msg_and_die ( "-f permission denied" ); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 102 | safe_strncpy(username, optarg, USERNAME_SIZE); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 103 | opt_fflag = 1; |
| 104 | break; |
| 105 | case 'h': |
| 106 | opt_host = optarg; |
| 107 | break; |
| 108 | default: |
| 109 | show_usage ( ); |
| 110 | } |
| 111 | } |
| 112 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 113 | if (optind < argc) // user from command line (getty) |
| 114 | safe_strncpy(username, argv[optind], USERNAME_SIZE); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 115 | |
| 116 | if ( !isatty ( 0 ) || !isatty ( 1 ) || !isatty ( 2 )) |
| 117 | return EXIT_FAILURE; /* Must be a terminal */ |
| 118 | |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 119 | #ifdef CONFIG_FEATURE_U_W_TMP |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 120 | checkutmp ( !amroot ); |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 121 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 122 | |
| 123 | tmp = ttyname ( 0 ); |
| 124 | if ( tmp && ( strncmp ( tmp, "/dev/", 5 ) == 0 )) |
| 125 | safe_strncpy ( tty, tmp + 5, sizeof( tty )); |
| 126 | else |
| 127 | safe_strncpy ( tty, "UNKNOWN", sizeof( tty )); |
| 128 | |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 129 | #ifdef CONFIG_FEATURE_U_W_TMP |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 130 | if ( amroot ) |
| 131 | memset ( utent.ut_host, 0, sizeof utent.ut_host ); |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 132 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 133 | |
| 134 | if ( opt_host ) { |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 135 | #ifdef CONFIG_FEATURE_U_W_TMP |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 136 | safe_strncpy ( utent.ut_host, opt_host, sizeof( utent. ut_host )); |
Eric Andersen | 71ae64b | 2002-10-10 04:20:21 +0000 | [diff] [blame] | 137 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 138 | snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s' from `%.200s'", tty, opt_host ); |
| 139 | } |
| 140 | else |
| 141 | snprintf ( fromhost, sizeof( fromhost ) - 1, " on `%.100s'", tty ); |
| 142 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 143 | setpgrp(); |
| 144 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 145 | openlog ( "login", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH ); |
| 146 | |
| 147 | while ( 1 ) { |
| 148 | failed = 0; |
| 149 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 150 | if ( !username[0] ) |
| 151 | if(!login_prompt ( username )) |
| 152 | return EXIT_FAILURE; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 153 | |
| 154 | if ( !alarmstarted && ( TIMEOUT > 0 )) { |
| 155 | alarm ( TIMEOUT ); |
| 156 | alarmstarted = 1; |
| 157 | } |
| 158 | |
| 159 | if (!( pw = getpwnam ( username ))) { |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 160 | pw_copy.pw_name = "UNKNOWN"; |
| 161 | pw_copy.pw_passwd = "!"; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 162 | opt_fflag = 0; |
| 163 | failed = 1; |
| 164 | } else |
| 165 | pw_copy = *pw; |
| 166 | |
| 167 | pw = &pw_copy; |
| 168 | |
| 169 | if (( pw-> pw_passwd [0] == '!' ) || ( pw-> pw_passwd[0] == '*' )) |
| 170 | failed = 1; |
| 171 | |
| 172 | if ( opt_fflag ) { |
| 173 | opt_fflag = 0; |
| 174 | goto auth_ok; |
| 175 | } |
| 176 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 177 | if (!failed && ( pw-> pw_uid == 0 ) && ( !check_tty ( tty ))) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 178 | failed = 1; |
| 179 | |
| 180 | /* Don't check the password if password entry is empty (!) */ |
| 181 | if ( !pw-> pw_passwd[0] ) |
| 182 | goto auth_ok; |
| 183 | |
| 184 | /* authorization takes place here */ |
| 185 | if ( correct_password ( pw )) |
| 186 | goto auth_ok; |
| 187 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 188 | failed = 1; |
| 189 | |
| 190 | auth_ok: |
| 191 | if ( !failed) |
| 192 | break; |
| 193 | |
| 194 | { // delay next try |
| 195 | time_t start, now; |
| 196 | |
| 197 | time ( &start ); |
| 198 | now = start; |
| 199 | while ( difftime ( now, start ) < FAIL_DELAY) { |
| 200 | sleep ( FAIL_DELAY ); |
| 201 | time ( &now ); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | puts("Login incorrect"); |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 206 | username[0] = 0; |
| 207 | if ( ++count == 3 ) { |
| 208 | syslog ( LOG_WARNING, "invalid password for `%s'%s\n", pw->pw_name, fromhost); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 209 | return EXIT_FAILURE; |
| 210 | } |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 211 | } |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 212 | |
| 213 | alarm ( 0 ); |
| 214 | if ( check_nologin ( pw-> pw_uid == 0 )) |
| 215 | return EXIT_FAILURE; |
| 216 | |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 217 | #ifdef CONFIG_FEATURE_U_W_TMP |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 218 | setutmp ( username, tty ); |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 219 | #endif |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 220 | if ( *tty != '/' ) |
| 221 | snprintf ( full_tty, sizeof( full_tty ) - 1, "/dev/%s", tty); |
| 222 | else |
| 223 | safe_strncpy ( full_tty, tty, sizeof( full_tty ) - 1 ); |
| 224 | |
| 225 | if ( !is_my_tty ( full_tty )) |
| 226 | syslog ( LOG_ERR, "unable to determine TTY name, got %s\n", full_tty ); |
| 227 | |
| 228 | /* Try these, but don't complain if they fail |
| 229 | * (for example when the root fs is read only) */ |
| 230 | chown ( full_tty, pw-> pw_uid, pw-> pw_gid ); |
| 231 | chmod ( full_tty, 0600 ); |
| 232 | |
| 233 | change_identity ( pw ); |
| 234 | setup_environment ( pw-> pw_shell, 1, !opt_preserve, pw ); |
| 235 | |
| 236 | motd ( ); |
| 237 | signal ( SIGALRM, SIG_DFL ); /* default alarm signal */ |
| 238 | |
| 239 | if ( pw-> pw_uid == 0 ) |
| 240 | syslog ( LOG_INFO, "root login %s\n", fromhost ); |
| 241 | |
| 242 | run_shell ( pw-> pw_shell, 1, 0, 0 ); /* exec the shell finally. */ |
| 243 | |
| 244 | return EXIT_FAILURE; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 249 | static int login_prompt ( char *buf_name ) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 250 | { |
| 251 | char buf [1024]; |
| 252 | char *sp, *ep; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 253 | int i; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 254 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 255 | for(i=0; i<EMPTY_USERNAME_COUNT; i++) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 256 | gethostname ( buf, sizeof( buf )); |
Eric Andersen | b26674b | 2002-11-07 02:45:55 +0000 | [diff] [blame] | 257 | printf ( "\n%s login: ", buf ); |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 258 | fflush ( stdout ); |
| 259 | |
| 260 | if ( !fgets ( buf, sizeof( buf ) - 1, stdin )) |
| 261 | return 0; |
| 262 | |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 263 | if ( !strchr ( buf, '\n' )) |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | |
| 266 | for ( sp = buf; isspace ( *sp ); sp++ ) { } |
| 267 | for ( ep = sp; isgraph ( *ep ); ep++ ) { } |
| 268 | |
| 269 | *ep = 0; |
Eric Andersen | 0fbff13 | 2002-06-22 17:49:29 +0000 | [diff] [blame] | 270 | safe_strncpy(buf_name, sp, USERNAME_SIZE); |
| 271 | if(buf_name[0]) |
| 272 | return 1; |
| 273 | } |
| 274 | return 0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | |
| 278 | static int check_nologin ( int amroot ) |
| 279 | { |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 280 | if ( access ( nologin_file, F_OK ) == 0 ) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 281 | FILE *fp; |
| 282 | int c; |
| 283 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 284 | if (( fp = fopen ( nologin_file, "r" ))) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 285 | while (( c = getc ( fp )) != EOF ) |
| 286 | putchar (( c == '\n' ) ? '\r' : c ); |
| 287 | |
| 288 | fflush ( stdout ); |
| 289 | fclose ( fp ); |
| 290 | } else { |
| 291 | puts ( "\r\nSystem closed for routine maintenance.\r" ); |
| 292 | } |
| 293 | if ( !amroot ) |
| 294 | return 1; |
| 295 | |
| 296 | puts ( "\r\n[Disconnect bypassed -- root login allowed.]\r" ); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | #ifdef CONFIG_FEATURE_SECURETTY |
| 302 | |
| 303 | static int check_tty ( const char *tty ) |
| 304 | { |
| 305 | FILE *fp; |
| 306 | int i; |
| 307 | char buf[BUFSIZ]; |
| 308 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 309 | if (( fp = fopen ( securetty_file, "r" ))) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 310 | while ( fgets ( buf, sizeof( buf ) - 1, fp )) { |
| 311 | for ( i = xstrlen( buf ) - 1; i >= 0; --i ) { |
| 312 | if ( !isspace ( buf[i] )) |
| 313 | break; |
| 314 | } |
| 315 | buf[++i] = '\0'; |
| 316 | if (( buf [0] == '\0' ) || ( buf [0] == '#' )) |
| 317 | continue; |
| 318 | |
| 319 | if ( strcmp ( buf, tty ) == 0 ) { |
| 320 | fclose ( fp ); |
| 321 | return 1; |
| 322 | } |
| 323 | } |
| 324 | fclose(fp); |
| 325 | return 0; |
| 326 | } |
Eric Andersen | f870148 | 2002-11-14 10:58:17 +0000 | [diff] [blame] | 327 | /* A missing securetty file is not an error. */ |
| 328 | return 0; |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | #endif |
| 332 | |
| 333 | /* returns 1 if true */ |
| 334 | static int is_my_tty ( const char *tty ) |
| 335 | { |
| 336 | struct stat by_name, by_fd; |
| 337 | |
| 338 | if ( stat ( tty, &by_name ) || fstat ( 0, &by_fd )) |
| 339 | return 0; |
| 340 | |
| 341 | if ( by_name. st_rdev != by_fd. st_rdev ) |
| 342 | return 0; |
| 343 | else |
| 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static void motd ( ) |
| 349 | { |
| 350 | FILE *fp; |
| 351 | register int c; |
| 352 | |
Eric Andersen | 27f64e1 | 2002-06-23 04:24:25 +0000 | [diff] [blame] | 353 | if (( fp = fopen ( motd_file, "r" ))) { |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 354 | while (( c = getc ( fp )) != EOF ) |
| 355 | putchar ( c ); |
| 356 | fclose ( fp ); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 361 | #ifdef CONFIG_FEATURE_U_W_TMP |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 362 | // vv Taken from tinylogin utmp.c vv |
| 363 | |
| 364 | #define _WTMP_FILE "/var/log/wtmp" |
| 365 | |
| 366 | #define NO_UTENT \ |
| 367 | "No utmp entry. You must exec \"login\" from the lowest level \"sh\"" |
| 368 | #define NO_TTY \ |
| 369 | "Unable to determine your tty name." |
| 370 | |
| 371 | /* |
| 372 | * checkutmp - see if utmp file is correct for this process |
| 373 | * |
| 374 | * System V is very picky about the contents of the utmp file |
| 375 | * and requires that a slot for the current process exist. |
| 376 | * The utmp file is scanned for an entry with the same process |
| 377 | * ID. If no entry exists the process exits with a message. |
| 378 | * |
| 379 | * The "picky" flag is for network and other logins that may |
| 380 | * use special flags. It allows the pid checks to be overridden. |
| 381 | * This means that getty should never invoke login with any |
| 382 | * command line flags. |
| 383 | */ |
| 384 | |
| 385 | static void checkutmp(int picky) |
| 386 | { |
| 387 | char *line; |
| 388 | struct utmp *ut; |
| 389 | pid_t pid = getpid(); |
| 390 | |
| 391 | setutent(); |
| 392 | |
| 393 | /* First, try to find a valid utmp entry for this process. */ |
| 394 | while ((ut = getutent())) |
| 395 | if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0] && |
| 396 | (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS)) |
| 397 | break; |
| 398 | |
| 399 | /* If there is one, just use it, otherwise create a new one. */ |
| 400 | if (ut) { |
| 401 | utent = *ut; |
| 402 | } else { |
| 403 | if (picky) { |
| 404 | puts(NO_UTENT); |
| 405 | exit(1); |
| 406 | } |
| 407 | line = ttyname(0); |
| 408 | if (!line) { |
| 409 | puts(NO_TTY); |
| 410 | exit(1); |
| 411 | } |
| 412 | if (strncmp(line, "/dev/", 5) == 0) |
| 413 | line += 5; |
| 414 | memset((void *) &utent, 0, sizeof utent); |
| 415 | utent.ut_type = LOGIN_PROCESS; |
| 416 | utent.ut_pid = pid; |
| 417 | strncpy(utent.ut_line, line, sizeof utent.ut_line); |
| 418 | /* XXX - assumes /dev/tty?? */ |
| 419 | strncpy(utent.ut_id, utent.ut_line + 3, sizeof utent.ut_id); |
| 420 | strncpy(utent.ut_user, "LOGIN", sizeof utent.ut_user); |
| 421 | time(&utent.ut_time); |
| 422 | } |
| 423 | } |
| 424 | |
Robert Griebl | 1fca558 | 2002-06-04 20:45:46 +0000 | [diff] [blame] | 425 | /* |
| 426 | * setutmp - put a USER_PROCESS entry in the utmp file |
| 427 | * |
| 428 | * setutmp changes the type of the current utmp entry to |
| 429 | * USER_PROCESS. the wtmp file will be updated as well. |
| 430 | */ |
| 431 | |
| 432 | static void setutmp(const char *name, const char *line) |
| 433 | { |
| 434 | utent.ut_type = USER_PROCESS; |
| 435 | strncpy(utent.ut_user, name, sizeof utent.ut_user); |
| 436 | time(&utent.ut_time); |
| 437 | /* other fields already filled in by checkutmp above */ |
| 438 | setutent(); |
| 439 | pututline(&utent); |
| 440 | endutent(); |
| 441 | updwtmp(_WTMP_FILE, &utent); |
| 442 | } |
Eric Andersen | fdfe298 | 2002-10-10 03:55:09 +0000 | [diff] [blame] | 443 | #endif /* CONFIG_FEATURE_U_W_TMP */ |