aruu

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

passwd_linux.c (2200B)


      1 /* see LICENSE file for copyright and license details */
      2 #if FEATURE_PASSWD_EXT
      3 #ifndef _GNU_SOURCE
      4 #define _GNU_SOURCE
      5 #endif
      6 #ifndef _DEFAULT_SOURCE
      7 #define _DEFAULT_SOURCE
      8 #endif
      9 #endif
     10 #include "../passwd.h"
     11 #include "../paths.h"
     12 #include "../text.h"
     13 #include "../util.h"
     14 
     15 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
     16 #include <errno.h>
     17 #include <shadow.h>
     18 #include <stdio.h>
     19 #include <string.h>
     20 #include <unistd.h>
     21 
     22 char *
     23 pw_shadow_hash(const char *name)
     24 {
     25   struct spwd *spw;
     26 
     27   errno = 0;
     28   spw   = getspnam(name);
     29   return spw ? spw->sp_pwdp : NULL;
     30 }
     31 
     32 static int
     33 update_shadow(const char *name, const char *newhash)
     34 {
     35   struct spwd *spw, cur;
     36   FILE        *fp, *tfp;
     37   int          wrote = 0;
     38   char         path[256];
     39 
     40   snprintf(path, sizeof(path), ARUU_PATH_ETC "/tcb/%s/shadow", name);
     41   fp = fopen(path, "r+");
     42   if (!fp) {
     43     memcpy(path, ARUU_PATH_SHADOW, sizeof(ARUU_PATH_SHADOW));
     44     fp = fopen(path, "r+");
     45   }
     46   if (!fp) {
     47     weprintf("fopen %s:", path);
     48     return -1;
     49   }
     50 
     51   tfp = tmpfile();
     52   if (!tfp) {
     53     weprintf("tmpfile:");
     54     fclose(fp);
     55     return -1;
     56   }
     57 
     58   /* fgetspent(fp) reads from the stream we already opened above;
     59    * getspent() reads an implicit global shadow stream that musl
     60    * never actually backs with this (or any) file and always
     61    * returns null for, silently turning every update into a
     62    * "no matching entry" failure */
     63   while ((spw = fgetspent(fp))) {
     64     cur = *spw;
     65     if (strcmp(cur.sp_namp, name) == 0) {
     66       cur.sp_pwdp = (char *)newhash;
     67       wrote       = 1;
     68     }
     69     errno = 0;
     70     if (putspent(&cur, tfp) == -1) {
     71       weprintf("putspent:");
     72       fclose(tfp);
     73       fclose(fp);
     74       return -1;
     75     }
     76   }
     77   if (!wrote) {
     78     weprintf("shadow: no matching entry\n");
     79     fclose(tfp);
     80     fclose(fp);
     81     return -1;
     82   }
     83   fflush(tfp);
     84   rewind(tfp);
     85   rewind(fp);
     86   fconcat(tfp, "tmpfile", fp, "shadow");
     87   ftruncate(fileno(fp), ftell(tfp));
     88   fclose(tfp);
     89   {
     90     int fd = fileno(fp);
     91     if (fd >= 0)
     92       fsync(fd);
     93   }
     94   fclose(fp);
     95   return 0;
     96 }
     97 
     98 int
     99 pw_shadow_update(const char *name, const char *newhash)
    100 {
    101   errno = 0;
    102   if (!getspnam(name))
    103     return 1;
    104   return update_shadow(name, newhash);
    105 }
    106 #endif