aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

login.c (3758B)


      1 /* see LICENSE file for copyright and license details */
      2 
      3 #ifndef _DEFAULT_SOURCE
      4 #define _DEFAULT_SOURCE
      5 #endif
      6 
      7 #include "config.h"
      8 #include "passwd.h"
      9 #include "session.h"
     10 #include "util.h"
     11 #include "wexec.h"
     12 
     13 #include <grp.h>
     14 #include <limits.h>
     15 #include <pwd.h>
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 #include <termios.h>
     20 #include <unistd.h>
     21 
     22 static void
     23 usage(void)
     24 {
     25   eprintf("usage: %s [-p name] [-f name]\n", argv0);
     26 }
     27 
     28 /* reads one line of input, echoing it or not, into a reusable static
     29  * buffer. returns null on eof so a hung-up line doesnt loop forever */
     30 static char *
     31 readline(const char *prompt, int echo)
     32 {
     33   static char    buf[LOGIN_NAME_MAX + 1];
     34   struct termios old, raw;
     35   int            have_old = 0;
     36   size_t         n;
     37 
     38   fputs(prompt, stdout);
     39   fflush(stdout);
     40 
     41   if (!echo && tcgetattr(STDIN_FILENO, &old) == 0) {
     42     have_old = 1;
     43     raw      = old;
     44     raw.c_lflag &= ~ECHO;
     45     tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
     46   }
     47 
     48   if (!fgets(buf, sizeof(buf), stdin)) {
     49     if (have_old)
     50       tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
     51     return NULL;
     52   }
     53 
     54   if (have_old) {
     55     tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
     56     fputc('\n', stdout);
     57   }
     58 
     59   n = strlen(buf);
     60   if (n && buf[n - 1] == '\n')
     61     buf[--n] = '\0';
     62 
     63   return buf;
     64 }
     65 
     66 // ?man login: authenticate a user and start a session
     67 // ?man arguments: [-p name] [-f name]
     68 // ?man prompt for a username (unless given) and password, verify against
     69 // ?man the system password database, then exec the user's login shell
     70 // ?man invoked by getty with -p name once a username has already been
     71 // ?man typed at the "login:" prompt
     72 int
     73 main(int argc, char *argv[])
     74 {
     75   struct passwd *pw;
     76   char           namebuf[LOGIN_NAME_MAX + 1];
     77   char           shellbuf[PATH_MAX];
     78   char          *preset = NULL, *trusted = NULL;
     79   char          *name, *pass, *line, *base;
     80   char          *shargv[2];
     81   int            tries;
     82 
     83   ARGBEGIN
     84   {
     85     // ?man -p:name preset the username; still prompts for a password
     86     case 'p':
     87       preset = EARGF(usage());
     88       break;
     89     // ?man -f:name log in as name without a password prompt: the
     90     // ?man caller has already authenticated this user out-of-band
     91     case 'f':
     92       trusted = EARGF(usage());
     93       break;
     94     default:
     95       usage();
     96   }
     97   ARGEND
     98 
     99   pw_init();
    100 
    101   if (trusted) {
    102     pw = getpwnam(trusted);
    103     if (!pw)
    104       eprintf("login: unknown user: %s\n", trusted);
    105   } else {
    106     pw = NULL;
    107     for (tries = 0; tries < 3 && !pw; tries++) {
    108       if (preset && tries == 0) {
    109         strlcpy(namebuf, preset, sizeof(namebuf));
    110       } else {
    111         line = readline("login: ", 1);
    112         if (!line)
    113           exit(1);
    114         strlcpy(namebuf, line, sizeof(namebuf));
    115       }
    116       name = namebuf;
    117 
    118       pass = readline("Password: ", 0);
    119       if (!pass)
    120         pass = "";
    121 
    122       pw = getpwnam(name);
    123       if (!pw || pw_check(pw, pass) != 1) {
    124         fputs("Login incorrect\n", stdout);
    125         pw = NULL;
    126       }
    127     }
    128     if (!pw)
    129       exit(1);
    130   }
    131 
    132   if (initgroups(pw->pw_name, pw->pw_gid) < 0)
    133     weprintf("initgroups:");
    134   if (setgid(pw->pw_gid) < 0)
    135     eprintf("setgid:");
    136   if (setuid(pw->pw_uid) < 0)
    137     eprintf("setuid:");
    138 
    139   session_record(pw->pw_name);
    140 
    141   if (chdir(pw->pw_dir) < 0)
    142     weprintf("chdir %s:", pw->pw_dir);
    143 
    144   setenv("HOME", pw->pw_dir, 1);
    145   setenv("USER", pw->pw_name, 1);
    146   setenv("LOGNAME", pw->pw_name, 1);
    147   strlcpy(shellbuf, pw->pw_shell[0] ? pw->pw_shell : "/bin/sh", sizeof(shellbuf));
    148   setenv("SHELL", shellbuf, 1);
    149 
    150   base = strrchr(shellbuf, '/');
    151   base = base ? base + 1 : shellbuf;
    152 
    153   shargv[0]    = emalloc(strlen(base) + 2);
    154   shargv[0][0] = '-';
    155   strcpy(shargv[0] + 1, base);
    156   shargv[1] = NULL;
    157 
    158   wexecv_self(shellbuf, shargv);
    159   return 1;
    160 }