aruu

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

passwd.c (4422B)


      1 /* see LICENSE file for copyright and license details */
      2 #ifndef _DEFAULT_SOURCE
      3 #define _DEFAULT_SOURCE
      4 #endif
      5 #ifndef _XOPEN_SOURCE
      6 #define _XOPEN_SOURCE 500
      7 #endif
      8 
      9 #include "passwd.h"
     10 #include "util.h"
     11 
     12 #include <pwd.h>
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <string.h>
     16 #include <termios.h>
     17 #include <unistd.h>
     18 
     19 static void
     20 usage(void)
     21 {
     22   eprintf("usage: %s [-l | -u | -d] [name]\n", argv0);
     23 }
     24 
     25 /* reads one line with echo off into a reusable static buffer */
     26 static char *
     27 readpass(const char *prompt)
     28 {
     29   static char    buf[128];
     30   struct termios old, raw;
     31   int            have_old;
     32   size_t         n;
     33 
     34   fputs(prompt, stdout);
     35   fflush(stdout);
     36 
     37   have_old = tcgetattr(STDIN_FILENO, &old) == 0;
     38   if (have_old) {
     39     raw = old;
     40     raw.c_lflag &= ~ECHO;
     41     tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
     42   }
     43 
     44   if (!fgets(buf, sizeof(buf), stdin)) {
     45     if (have_old)
     46       tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
     47     return NULL;
     48   }
     49 
     50   if (have_old) {
     51     tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
     52     fputc('\n', stdout);
     53   }
     54 
     55   n = strlen(buf);
     56   if (n && buf[n - 1] == '\n')
     57     buf[--n] = '\0';
     58 
     59   return buf;
     60 }
     61 
     62 // ?man passwd: change a user's password
     63 // ?man arguments: [-l | -u | -d] [name]
     64 // ?man change name's password (default: the caller's own), or with -l/-u/-d
     65 // ?man lock, unlock, or clear it instead. only root may target another
     66 // ?man user, or use -l/-u/-d at all
     67 int
     68 main(int argc, char *argv[])
     69 {
     70   struct pwdb_entry ent;
     71   struct passwd    *self, *target;
     72   char             *name, *cur, *new1, *new2, *hash;
     73   char              salt[PW_SALT_MAX];
     74   int               lflag = 0, uflag = 0, dflag = 0;
     75   uid_t             uid;
     76 
     77   ARGBEGIN
     78   {
     79     // ?man -l: lock the account by prefixing its hash with '!'
     80     case 'l':
     81       lflag = 1;
     82       break;
     83     // ?man -u: unlock an account previously locked with -l
     84     case 'u':
     85       uflag = 1;
     86       break;
     87     // ?man -d: clear the password, allowing a blank password login
     88     case 'd':
     89       dflag = 1;
     90       break;
     91     default:
     92       usage();
     93   }
     94   ARGEND
     95 
     96   if (lflag + uflag + dflag > 1)
     97     usage();
     98 
     99   uid  = getuid();
    100   self = getpwuid(uid);
    101   if (!self)
    102     eprintf("passwd: who are you?\n");
    103 
    104   /* copied: self is getpwuid's buffer, which getpwnam reads into, so a name
    105    * pointing into it would be written over while it is looked up */
    106   name = estrdup(argc > 0 ? argv[0] : self->pw_name);
    107   if (uid != 0 && (strcmp(name, self->pw_name) != 0 || lflag || uflag || dflag))
    108     eprintf("passwd: permission denied\n");
    109 
    110   target = getpwnam(name);
    111   if (!target)
    112     eprintf("passwd: unknown user: %s\n", name);
    113 
    114   pw_init();
    115 
    116   if (lflag || uflag || dflag) {
    117     if (pwdb_lookup(&ent, name) < 0)
    118       eprintf("passwd: cannot look up %s\n", name);
    119 
    120     if (lflag) {
    121       if (ent.hash[0] == '!') {
    122         hash = ent.hash;
    123       } else {
    124         hash    = emalloc(strlen(ent.hash) + 2);
    125         hash[0] = '!';
    126         strcpy(hash + 1, ent.hash);
    127       }
    128       if (pwdb_update(name, hash) < 0)
    129         eprintf("passwd: update failed\n");
    130       printf("passwd: password for %s locked\n", name);
    131     } else if (uflag) {
    132       hash = ent.hash[0] == '!' ? ent.hash + 1 : ent.hash;
    133       if (pwdb_update(name, hash) < 0)
    134         eprintf("passwd: update failed\n");
    135       printf("passwd: password for %s unlocked\n", name);
    136     } else {
    137       if (pwdb_update(name, "") < 0)
    138         eprintf("passwd: update failed\n");
    139       printf("passwd: password for %s cleared\n", name);
    140     }
    141     return 0;
    142   }
    143 
    144   if (uid != 0) {
    145     cur = readpass("Current password: ");
    146     /* target, not self: the lookup above wrote over self. for anyone but
    147      * root they are the same person, as checked above */
    148     if (!cur || pw_check(target, cur) != 1)
    149       eprintf("passwd: authentication failed\n");
    150   }
    151 
    152   new1 = readpass("New password: ");
    153   if (!new1)
    154     exit(1);
    155   new1 = estrdup(new1);
    156 
    157   new2 = readpass("Retype new password: ");
    158   if (!new2 || strcmp(new1, new2) != 0) {
    159     free(new1);
    160     eprintf("passwd: passwords do not match\n");
    161   }
    162   if (new1[0] == '\0')
    163     weprintf("passwd: warning: empty password\n");
    164 
    165   if (pw_gensalt(salt, sizeof(salt)) < 0)
    166     eprintf("passwd: cannot generate salt\n");
    167 
    168   hash = crypt(new1, salt);
    169   free(new1);
    170   if (!hash)
    171     eprintf("passwd: crypt:");
    172 
    173   if (pwdb_update(name, hash) < 0)
    174     eprintf("passwd: update failed\n");
    175 
    176   printf("passwd: password for %s updated\n", name);
    177   return 0;
    178 }