aruu

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

passwd.c (6924B)


      1 /* see LICENSE file for copyright and license details */
      2 #ifndef _XOPEN_SOURCE
      3 #define _XOPEN_SOURCE 700 /* crypt in unistd.h */
      4 #endif
      5 /* pw_update_file reads and writes entries with fgetpwent and putpwent,
      6  * which musl declares only under _GNU_SOURCE */
      7 #ifndef _GNU_SOURCE
      8 #define _GNU_SOURCE
      9 #endif
     10 #ifndef _DEFAULT_SOURCE
     11 #define _DEFAULT_SOURCE
     12 #endif
     13 #include "../passwd.h"
     14 #include "../paths.h"
     15 #include "../text.h"
     16 #include "../util.h"
     17 
     18 #include <errno.h>
     19 #include <fcntl.h>
     20 #include <pwd.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include <sys/resource.h>
     25 #include <sys/stat.h>
     26 #include <sys/wait.h>
     27 #include <unistd.h>
     28 
     29 #include "../config.h"
     30 
     31 int
     32 pw_init(void)
     33 {
     34   struct rlimit rlim;
     35 
     36   rlim.rlim_cur = 0;
     37   rlim.rlim_max = 0;
     38   if (setrlimit(RLIMIT_CORE, &rlim) < 0)
     39     eprintf("setrlimit:");
     40   return 0;
     41 }
     42 
     43 int
     44 pw_check(const struct passwd *pw, const char *pass)
     45 {
     46   char       *cryptpass, *p;
     47   const char *stored;
     48 
     49   stored = pw->pw_passwd;
     50   if (stored[0] == '!' || stored[0] == '*') {
     51     weprintf("denied\n");
     52     return -1;
     53   }
     54 
     55   if (stored[0] == '\0') {
     56     if (pass[0] == '\0')
     57       return 1;
     58     weprintf("incorrect password\n");
     59     return 0;
     60   }
     61 
     62   if (stored[0] == 'x' && stored[1] == '\0') {
     63     errno  = 0;
     64     stored = pw_shadow_hash(pw->pw_name);
     65     if (!stored) {
     66       if (errno)
     67         weprintf("getspnam: %s:", pw->pw_name);
     68       else
     69         weprintf("who are you?\n");
     70       return -1;
     71     }
     72     if (stored[0] == '!' || stored[0] == '*') {
     73       weprintf("denied\n");
     74       return -1;
     75     }
     76     /* same empty-hash path as above, redone because stored was just
     77      * reassigned from the shadow entry. an empty shadow hash means a
     78      * passwordless account, while crypt() with an empty salt would match
     79      * nothing */
     80     if (stored[0] == '\0') {
     81       if (pass[0] == '\0')
     82         return 1;
     83       weprintf("incorrect password\n");
     84       return 0;
     85     }
     86   }
     87 
     88   cryptpass = crypt(pass, stored);
     89   if (!cryptpass) {
     90     weprintf("crypt:");
     91     return -1;
     92   }
     93   p = estrdup(cryptpass);
     94   if (strcmp(p, stored) != 0) {
     95     free(p);
     96     weprintf("incorrect password\n");
     97     return 0;
     98   }
     99   free(p);
    100   return 1;
    101 }
    102 
    103 int
    104 pwdb_lookup(struct pwdb_entry *ent, const char *name)
    105 {
    106   struct passwd *pw;
    107   const char    *hash;
    108 
    109   errno = 0;
    110   pw    = getpwnam(name);
    111   if (!pw) {
    112     if (errno)
    113       weprintf("getpwnam: %s:", name);
    114     else
    115       weprintf("who are you?\n");
    116     return -1;
    117   }
    118 
    119   ent->name = estrdup(pw->pw_name);
    120   ent->uid  = pw->pw_uid;
    121   ent->gid  = pw->pw_gid;
    122   hash      = pw->pw_passwd;
    123 
    124   if (hash[0] == 'x' && hash[1] == '\0') {
    125     const char *shadow = pw_shadow_hash(name);
    126     if (shadow)
    127       hash = shadow;
    128   }
    129 
    130   ent->hash = estrdup(hash);
    131   return 0;
    132 }
    133 
    134 static int
    135 fill_random(void *buf, size_t n)
    136 {
    137   if (getentropy(buf, n) == 0)
    138     return 0;
    139   {
    140     int    fd;
    141     size_t off;
    142     fd = open(ARUU_PATH_DEVURANDOM, O_RDONLY);
    143     if (fd < 0) {
    144       weprintf("open /dev/urandom:");
    145       return -1;
    146     }
    147     off = 0;
    148     while (off < n) {
    149       ssize_t r = read(fd, (char *)buf + off, n - off);
    150       if (r <= 0) {
    151         close(fd);
    152         weprintf("read /dev/urandom:");
    153         return -1;
    154       }
    155       off += (size_t)r;
    156     }
    157     close(fd);
    158   }
    159   return 0;
    160 }
    161 
    162 int
    163 pw_gensalt_cipher(char *salt, size_t salt_sz, const char *prefix, size_t rand_len)
    164 {
    165   static const char b64[] = "./"
    166                             "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"
    167                             "ijklmnopqrstuvwxyz";
    168   unsigned char     raw[32];
    169   char              body[48];
    170   size_t            i, n, body_len;
    171   unsigned          v;
    172   int               prefix_len;
    173 
    174   if (rand_len > sizeof(raw))
    175     rand_len = sizeof(raw);
    176   if (rand_len == 0) {
    177     salt[0] = '\0';
    178     return 0;
    179   }
    180 
    181   prefix_len = 0;
    182   if (prefix) {
    183     prefix_len = snprintf(salt, salt_sz, "%s", prefix);
    184     if (prefix_len < 0 || (size_t)prefix_len >= salt_sz) {
    185       weprintf("snprintf:");
    186       return -1;
    187     }
    188   }
    189 
    190   if (fill_random(raw, rand_len) < 0)
    191     return -1;
    192 
    193   body_len = (rand_len * 4 + 2) / 3;
    194   if (body_len >= sizeof(body))
    195     body_len = sizeof(body) - 1;
    196   n = 0;
    197   for (i = 0; i + 3 <= rand_len; i += 3) {
    198     v         = (raw[i] << 16) | (raw[i + 1] << 8) | raw[i + 2];
    199     body[n++] = b64[v & 0x3f];
    200     v >>= 6;
    201     body[n++] = b64[v & 0x3f];
    202     v >>= 6;
    203     body[n++] = b64[v & 0x3f];
    204     v >>= 6;
    205     body[n++] = b64[v & 0x3f];
    206   }
    207   if (i < rand_len) {
    208     v = raw[i] << 16;
    209     if (i + 1 < rand_len)
    210       v |= raw[i + 1] << 8;
    211     body[n++] = b64[v & 0x3f];
    212     v >>= 6;
    213     body[n++] = b64[v & 0x3f];
    214     v >>= 6;
    215     if (i + 1 < rand_len)
    216       body[n++] = b64[v & 0x3f];
    217   }
    218   body[n] = '\0';
    219 
    220   if ((size_t)prefix_len + n + 1 >= salt_sz) {
    221     weprintf("salt buffer too small\n");
    222     return -1;
    223   }
    224   memcpy(salt + prefix_len, body, n + 1);
    225   return 0;
    226 }
    227 
    228 int
    229 pw_gensalt(char *salt, size_t salt_sz)
    230 {
    231   return pw_gensalt_cipher(salt, salt_sz, PW_CIPHER, 16);
    232 }
    233 
    234 int
    235 pw_update_file(const char *path, const char *name, const char *newhash)
    236 {
    237   struct passwd *pw, cur;
    238   FILE          *fp, *tfp;
    239   int            wrote = 0;
    240 
    241   fp = fopen(path, "r+");
    242   if (!fp) {
    243     weprintf("fopen %s:", path);
    244     return -1;
    245   }
    246   tfp = tmpfile();
    247   if (!tfp) {
    248     weprintf("tmpfile:");
    249     fclose(fp);
    250     return -1;
    251   }
    252   while ((pw = fgetpwent(fp))) {
    253     cur = *pw;
    254     if (strcmp(cur.pw_name, name) == 0) {
    255       cur.pw_passwd = (char *)newhash;
    256       wrote         = 1;
    257     }
    258     errno = 0;
    259     if (putpwent(&cur, tfp) == -1) {
    260       weprintf("putpwent:");
    261       fclose(tfp);
    262       fclose(fp);
    263       return -1;
    264     }
    265   }
    266   if (!wrote) {
    267     weprintf("passwd: no matching entry\n");
    268     fclose(tfp);
    269     fclose(fp);
    270     return -1;
    271   }
    272   fflush(tfp);
    273   rewind(tfp);
    274   rewind(fp);
    275   fconcat(tfp, "tmpfile", fp, path);
    276   ftruncate(fileno(fp), ftell(tfp));
    277   {
    278     int fd = fileno(fp);
    279     if (fd >= 0)
    280       fsync(fd);
    281   }
    282   fclose(tfp);
    283   fclose(fp);
    284   return 0;
    285 }
    286 
    287 int
    288 pw_update_master(const char *name, const char *newhash)
    289 {
    290   int   r;
    291   pid_t pid, wr;
    292   int   st;
    293 
    294   r = pw_update_file(ARUU_BSD_PATH_MASTER_PASSWD, name, newhash);
    295   if (r != 0)
    296     return r;
    297   pid = fork();
    298   if (pid < 0) {
    299     weprintf("fork:");
    300     return -1;
    301   }
    302   if (pid == 0) {
    303     execl(ARUU_BSD_PATH_PWD_MKDB, "pwd_mkdb", "-p", NULL);
    304     weprintf("exec %s:", ARUU_BSD_PATH_PWD_MKDB);
    305     _exit(127);
    306   }
    307   do {
    308     wr = waitpid(pid, &st, 0);
    309   } while (wr < 0 && errno == EINTR);
    310   if (wr < 0) {
    311     weprintf("waitpid:");
    312     return -1;
    313   }
    314   if (!WIFEXITED(st)) {
    315     weprintf("pwd_mkdb did not exit normally\n");
    316     return -1;
    317   }
    318   if (WEXITSTATUS(st) != 0) {
    319     weprintf("pwd_mkdb exited with status %d\n", WEXITSTATUS(st));
    320     return -1;
    321   }
    322   return 0;
    323 }
    324 
    325 int
    326 pwdb_update(const char *name, const char *newhash)
    327 {
    328   int r;
    329 
    330   if ((r = pw_shadow_update(name, newhash)) != 1)
    331     return r;
    332   return pw_update_file(ARUU_PATH_PASSWD, name, newhash);
    333 }