aruu

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

accounts_linux.c (6572B)


      1 /* see LICENSE file for copyright and license details */
      2 #ifndef _DEFAULT_SOURCE
      3 #define _DEFAULT_SOURCE
      4 #endif
      5 #include "../accounts.h"
      6 #include "../paths.h"
      7 
      8 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
      9 #include <errno.h>
     10 #include <fcntl.h>
     11 #include <limits.h>
     12 #include <stdio.h>
     13 #include <stdlib.h>
     14 #include <string.h>
     15 #include <sys/stat.h>
     16 #include <time.h>
     17 #include <unistd.h>
     18 
     19 #define LINE_MAX_ACCT 4096
     20 /* the ids people are given; below are the system's */
     21 #define UID_FIRST 1000
     22 #define UID_LAST  60000
     23 
     24 /* what a line becomes: NULL drops it, the line itself keeps it, else the
     25  * new text, written into buf */
     26 typedef const char *edit_fn(const char *line, char *buf, size_t n, void *arg);
     27 
     28 /* path written again through a file beside it, renamed over it: each line
     29  * put through fn (kept when fn is NULL), add appended when not NULL. the
     30  * file keeps its mode and owner */
     31 static int
     32 rewrite(const char *path, edit_fn *fn, void *arg, const char *add)
     33 {
     34   char        tmp[PATH_MAX], line[LINE_MAX_ACCT], buf[LINE_MAX_ACCT];
     35   const char *r;
     36   struct stat st;
     37   FILE       *in, *out;
     38   int         fd, ok = 1;
     39 
     40   if (!(in = fopen(path, "r")))
     41     return -1;
     42   if (fstat(fileno(in), &st) < 0
     43       || snprintf(tmp, sizeof(tmp), "%s.acct", path) >= (int)sizeof(tmp)) {
     44     fclose(in);
     45     return -1;
     46   }
     47   unlink(tmp);
     48   if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, st.st_mode & 07777)) < 0) {
     49     fclose(in);
     50     return -1;
     51   }
     52   /* the umask took bits off; root keeps the owner, anyone else cannot */
     53   fchmod(fd, st.st_mode & 07777);
     54   if (fchown(fd, st.st_uid, st.st_gid) < 0 && geteuid() == 0)
     55     ok = 0;
     56   if (!(out = fdopen(fd, "w"))) {
     57     close(fd);
     58     unlink(tmp);
     59     fclose(in);
     60     return -1;
     61   }
     62   while (ok && fgets(line, sizeof(line), in)) {
     63     line[strcspn(line, "\n")] = '\0';
     64     r                         = fn ? fn(line, buf, sizeof(buf), arg) : line;
     65     if (r && fprintf(out, "%s\n", r) < 0)
     66       ok = 0;
     67   }
     68   if (ok && add && fprintf(out, "%s\n", add) < 0)
     69     ok = 0;
     70   ok = ok && !ferror(in);
     71   fclose(in);
     72   ok = fflush(out) == 0 && fsync(fd) == 0 && ok;
     73   ok = fclose(out) == 0 && ok;
     74   if (!ok || rename(tmp, path) < 0) {
     75     unlink(tmp);
     76     return -1;
     77   }
     78   return 0;
     79 }
     80 
     81 /* whether line is the entry of name */
     82 static int
     83 named(const char *line, const char *name)
     84 {
     85   size_t n = strlen(name);
     86 
     87   return !strncmp(line, name, n) && line[n] == ':';
     88 }
     89 
     90 static int
     91 has(const char *path, const char *name)
     92 {
     93   char  line[LINE_MAX_ACCT];
     94   FILE *f     = fopen(path, "r");
     95   int   found = 0;
     96 
     97   if (!f)
     98     return 0;
     99   while (!found && fgets(line, sizeof(line), f))
    100     found = named(line, name);
    101   fclose(f);
    102   return found;
    103 }
    104 
    105 /* the first id from UID_FIRST that neither passwd nor group uses */
    106 static long
    107 free_id(void)
    108 {
    109   const char *paths[] = {ARUU_PATH_PASSWD, ARUU_PATH_GROUP};
    110   char        line[LINE_MAX_ACCT], *f;
    111   long        top = UID_FIRST - 1, id;
    112   FILE       *fp;
    113 
    114   for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
    115     if (!(fp = fopen(paths[i], "r")))
    116       continue;
    117     while (fgets(line, sizeof(line), fp)) {
    118       /* the third field: name:x:id */
    119       if (!(f = strchr(line, ':')) || !(f = strchr(f + 1, ':')))
    120         continue;
    121       id = strtol(f + 1, NULL, 10);
    122       if (id > top && id < UID_LAST)
    123         top = id;
    124     }
    125     fclose(fp);
    126   }
    127   return top + 1 < UID_LAST ? top + 1 : -1;
    128 }
    129 
    130 static const char *
    131 drop(const char *line, char *buf, size_t n, void *arg)
    132 {
    133   (void)buf;
    134   (void)n;
    135   return named(line, arg) ? NULL : line;
    136 }
    137 
    138 struct membership {
    139   const char *group, *name;
    140   int         in;
    141 };
    142 
    143 /* a group line with name taken out of its members, and put in when it is
    144  * the group asked for and in says so */
    145 static const char *
    146 members(const char *line, char *buf, size_t n, void *arg)
    147 {
    148   const struct membership *m = arg;
    149   char                     list[LINE_MAX_ACCT], *tok, *save;
    150   const char              *at  = line;
    151   size_t                   len = 0;
    152   int                      put = 0;
    153 
    154   /* name:x:gid: then the members */
    155   for (int colons = 0; colons < 3; colons++)
    156     if (!(at = strchr(at, ':')))
    157       return line;
    158     else
    159       at++;
    160   if (m->group && !named(line, m->group))
    161     return line;
    162   len = (size_t)(at - line);
    163   if (len >= n)
    164     return line;
    165   memcpy(buf, line, len);
    166   buf[len] = '\0';
    167   snprintf(list, sizeof(list), "%s", at);
    168   for (tok = strtok_r(list, ",", &save); tok; tok = strtok_r(NULL, ",", &save)) {
    169     if (!*tok || !strcmp(tok, m->name))
    170       continue;
    171     len += (size_t)snprintf(buf + len, len < n ? n - len : 0, "%s%s", put++ ? "," : "", tok);
    172   }
    173   if (m->group && m->in)
    174     len += (size_t)snprintf(buf + len, len < n ? n - len : 0, "%s%s", put ? "," : "", m->name);
    175   return len < n ? buf : line;
    176 }
    177 
    178 int
    179 acct_add(const char *name, const char *home, const char *shell, const char *hash, uid_t *uid)
    180 {
    181   char line[LINE_MAX_ACCT];
    182   long id;
    183 
    184   if (!acct_valid_name(name) || strchr(home, ':') || strchr(shell, ':') || strchr(hash, ':')) {
    185     errno = EINVAL;
    186     return -1;
    187   }
    188   if (has(ARUU_PATH_PASSWD, name) || has(ARUU_PATH_GROUP, name)) {
    189     errno = EEXIST;
    190     return -1;
    191   }
    192   if ((id = free_id()) < 0) {
    193     errno = ENOSPC;
    194     return -1;
    195   }
    196   /* the group and the password first: a person is there once passwd says so */
    197   snprintf(line, sizeof(line), "%s:x:%ld:", name, id);
    198   if (rewrite(ARUU_PATH_GROUP, NULL, NULL, line) < 0)
    199     return -1;
    200   snprintf(line, sizeof(line), "%s:%s:%ld:0:99999:7:::", name, hash, (long)(time(NULL) / 86400));
    201   if (rewrite(ARUU_PATH_SHADOW, NULL, NULL, line) < 0)
    202     return -1;
    203   snprintf(line, sizeof(line), "%s:x:%ld:%ld::%s:%s", name, id, id, home, shell);
    204   if (rewrite(ARUU_PATH_PASSWD, NULL, NULL, line) < 0)
    205     return -1;
    206   if (uid)
    207     *uid = (uid_t)id;
    208   return 0;
    209 }
    210 
    211 int
    212 acct_remove(const char *name)
    213 {
    214   struct membership m = {NULL, name, 0};
    215 
    216   if (!acct_valid_name(name) || !has(ARUU_PATH_PASSWD, name)) {
    217     errno = ENOENT;
    218     return -1;
    219   }
    220   /* passwd first: a person is gone once passwd says so */
    221   if (rewrite(ARUU_PATH_PASSWD, drop, (void *)name, NULL) < 0
    222       || rewrite(ARUU_PATH_SHADOW, drop, (void *)name, NULL) < 0
    223       || rewrite(ARUU_PATH_GROUP, drop, (void *)name, NULL) < 0)
    224     return -1;
    225   return rewrite(ARUU_PATH_GROUP, members, &m, NULL);
    226 }
    227 
    228 int
    229 acct_member(const char *group, const char *name, int in)
    230 {
    231   struct membership m = {group, name, in};
    232 
    233   if (!has(ARUU_PATH_GROUP, group) || !has(ARUU_PATH_PASSWD, name)) {
    234     errno = ENOENT;
    235     return -1;
    236   }
    237   return rewrite(ARUU_PATH_GROUP, members, &m, NULL);
    238 }
    239 #endif