File: usrgrp.c

package info (click to toggle)
xchpst 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 304 kB
  • sloc: ansic: 2,792; sh: 75; makefile: 47
file content (354 lines) | stat: -rw-r--r-- 8,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* SPDX-License-Identifier: MIT */
/* SPDX-FileCopyrightText: (c) Copyright 2024 Andrew Bower <andrew@bower.uk> */

#include <assert.h>
#include <ctype.h>
#include <getopt.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#include "usrgrp.h"

int usrgrp_parse(struct users_groups *ug, const char *arg) {
  enum tok_type t = TOK_NAME;
  enum {
    STATE_USER,
    STATE_GROUP,
    STATE_SUPPLEMENTAL,
  } state = STATE_USER;
  char *copy;
  char *scan;
  ssize_t n_toks;
  int i;

  copy = strdup(arg);
  if (copy == NULL) return -1;

  ug->buf_tok = copy;
  if (*copy == ':') {
    t = TOK_ID;
    copy++;
  }

  for (n_toks = 0, scan = copy; strsep(&scan, ":"); n_toks++);
  if (n_toks > 2) {
    ug->num_supplemental = n_toks - 2;
    ug->supplemental = malloc(ug->num_supplemental * sizeof(struct sys_entry));
    if (ug->supplemental == NULL) {
      free(ug->buf_tok);
      return -1;
    }
  }

  scan = copy;
  for(i = 0; i < n_toks; i++) {
    size_t len = strlen(scan);
    struct sys_entry entry = (struct sys_entry) {
      .tok = scan,
      .tok_type = len ? t : TOK_NONE,
    };
    switch (state) {
    case STATE_USER:
      ug->user = entry;
      state = STATE_GROUP;
      break;
    case STATE_GROUP:
      ug->group = entry;
      state = STATE_SUPPLEMENTAL;
      break;
    case STATE_SUPPLEMENTAL:
      ug->supplemental[i - 2] = entry;
    }
    scan += len + 1;
  }

  return 0;
}

void usrgrp_resolve_uid(struct users_groups *ug, uid_t nid, bool lookup) {
  struct sys_entry *entry = &ug->user;
  struct passwd *password;

  entry->tok_type = TOK_ID;
  entry->uid = nid;
  entry->resolved = true;
  entry->user_gid = (gid_t) -1;
  ug->username = ug->home = ug->shell = NULL;
  if (!lookup)
    return;

  /* Look up numeric ID - but chpst does not do this. Could
   * be useful in future - let's catch build regressions. */
  password = getpwuid(entry->uid);
  if (password) {
    entry->user_gid = password->pw_gid;
    ug->username = strdup(password->pw_name);
    ug->home = strdup(password->pw_dir);
    ug->shell = strdup(password->pw_shell);
  }
}

static int resolve_user(struct users_groups *ug) {
  struct sys_entry *entry = &ug->user;
  struct passwd *password;
  int toks = 0;
  int rc = 0;
  long nid;

  switch (entry->tok_type) {
  case TOK_NONE:
    entry->uid = (uid_t) -1;
    entry->user_gid = (gid_t) -1;
    entry->resolved = false;
    break;
  case TOK_NAME:
    errno = 0;
    password = getpwnam(entry->tok);
    if (password) {
      entry->uid = password->pw_uid;
      entry->user_gid = password->pw_gid;
      entry->resolved = true;
      ug->username = strdup(password->pw_name);
      ug->home = strdup(password->pw_dir);
      ug->shell = strdup(password->pw_shell);
    } else {
      if (errno != 0) {
        rc = errno;
        fprintf(stderr, "getpwnam(\"%s\"): %s\n", entry->tok, strerror(rc));
      } else {
        rc = ENOENT;
        fprintf(stderr, "no such user: %s\n", entry->tok);
      }
      entry->uid = (uid_t) -1;
      entry->user_gid = (gid_t) -1;
      entry->resolved = false;
      ug->username = ug->home = ug->shell = NULL;
    }
    break;
  case TOK_ID:
    toks = sscanf(entry->tok, "%ld", &nid);
    if (toks == 1) {
      usrgrp_resolve_uid(ug, nid, false);
    } else {
      entry->resolved = false;
    }
  }

  return rc;
}

static int resolve_group(struct sys_entry *entry) {
  struct group *group;
  size_t toks;
  int rc = 0;
  long nid;

  switch (entry->tok_type) {
  case TOK_NONE:
    entry->gid = (gid_t) -1;
    entry->resolved = false;
    break;
  case TOK_NAME:
    errno = 0;
    group = getgrnam(entry->tok);
    if (group) {
      entry->gid = group->gr_gid;
      entry->resolved = true;
    } else {
      if (errno != 0) {
        rc = errno;
        fprintf(stderr, "getgrnam(\"%s\": %s\n", entry->tok, strerror(rc));
      } else {
        rc = ENOENT;
        fprintf(stderr, "no such group: %s\n", entry->tok);
      }
      entry->gid = (gid_t) -1;
      entry->resolved = false;
    }
    break;
  case TOK_ID:
    toks = sscanf(entry->tok, "%ld", &nid);
    if (toks == 1) {
      entry->gid = nid;
      entry->resolved = true;
    } else {
      entry->resolved = false;
    }
  }

  return rc;
}

static const char *tok_type_name(enum tok_type tok_type) {
  switch (tok_type) {
  case TOK_NONE:
    return "NONE";
  case TOK_NAME:
    return "NAME";
  case TOK_ID:
    return "ID";
  default:
    return "?";
  }
}

static void print_user(FILE *out, struct sys_entry *entry) {
  fprintf(out, "%s:%d:%d:%s:%s\n",
          entry->tok ? entry->tok : "",
          entry->uid, entry->user_gid,
          tok_type_name(entry->tok_type),
          entry->resolved ? "RESOLVED" : "");
}

static void print_group(FILE *out, struct sys_entry *entry) {
  fprintf(out, "%s:%d:%s:%s\n",
          entry->tok ? entry->tok : "",
          entry->gid,
          tok_type_name(entry->tok_type),
          entry->resolved ? "RESOLVED" : "");
}

void usrgrp_print(FILE *out, const char *what, struct users_groups *ug) {
  int i;

  fprintf(out, "%s:\n  user: ", what);
  print_user(out, &ug->user);
  fprintf(out, "  group: ");
  print_group(out, &ug->group);
  for (i = 0; i < ug->num_supplemental; i++) {
    fprintf(out, "  supplemental: ");
    print_group(out, ug->supplemental + i);
  }
}

int usrgrp_resolve(struct users_groups *ug) {
  int errors = 0;
  int i;

  if (resolve_user(ug))
    errors++;
  if (resolve_group(&ug->group))
    errors++;
  for (i = 0; i < ug->num_supplemental; i++) {
    if (resolve_group(&ug->supplemental[i]))
      errors++;
  }

  /* Use user's group if another one wasn't requested. */
  if (ug->group.tok_type == TOK_NONE &&
      ug->user.resolved == true &&
      ug->user.user_gid != (gid_t) -1) {
    ug->group.tok_type = TOK_ID;
    ug->group.gid = ug->user.user_gid;
    ug->group.resolved = true;

    /* And add its supplemental groups */
    if (ug->username && ug->num_supplemental == 0) {
      int n_groups = 0, n;
      gid_t *groups = NULL;

      n = getgrouplist(ug->username, ug->group.gid, NULL, &n_groups);
      groups = malloc(n_groups * sizeof *groups);
      if (groups == NULL)
        return errors + 1;
      if ((n = getgrouplist(ug->username, ug->group.gid, groups, &n_groups)) == -1) {
        free(groups);
        return errors + 1;
      }
      if ((ug->supplemental = malloc(n * sizeof ug->supplemental[0])) == NULL) {
        free(groups);
        return errors + 1;
      }

      for (i = 0; i < n; i++) {
        if (groups[i] == ug->group.gid)
          continue;
        ug->supplemental[ug->num_supplemental] = (struct sys_entry) {
          .tok = NULL,
          .gid = groups[i],
          .tok_type = TOK_ID,
          .resolved = true,
        };
        ug->num_supplemental++;
      }
      free(groups);
    }
  }

  return errors;
}

void usrgrp_free(struct users_groups *ug) {
  free(ug->home);
  free(ug->shell);
  free(ug->username);
  if (ug->buf_tok)
    free(ug->buf_tok);
  if (ug->supplemental)
    free(ug->supplemental);
}

int usrgrp_from_env(struct users_groups *ug,
                    const char *uid, const char *gid, const char *gidlist) {
  char *scan, *copy;
  ssize_t n_toks, toks;
  int i;
  long nid;
  struct sys_entry *entry;

  if (uid && *uid) {
    entry = &ug->user;
    toks = sscanf(uid, "%ld", &nid);
    if (toks != 1)
      goto fail;
    entry->tok_type = TOK_ID;
    entry->uid = nid;
    entry->resolved = true;
    entry->user_gid = (gid_t) -1;
    ug->username = ug->home = ug->shell = NULL;
  }
  if (gid && *gid) {
    entry = &ug->group;
    toks = sscanf(gid, "%ld", &nid);
    if (toks != 1)
      goto fail;
    entry->tok_type = TOK_ID;
    entry->gid = nid;
    entry->resolved = true;
  }
  if (gidlist && *gidlist) {
    if ((copy = strdup(gidlist)) == NULL)
      goto fail;
    for (n_toks = 0, scan = copy; strsep(&scan, ","); n_toks++);
    ug->num_supplemental = n_toks;
    ug->supplemental = malloc(ug->num_supplemental * sizeof(struct sys_entry));
    if (ug->supplemental == NULL) {
      goto fails;
    }
    scan = copy;
    for(i = 0; i < n_toks; i++) {
      entry = ug->supplemental + i;
      toks = sscanf(scan, "%ld", &nid);
      if (toks != 1)
        goto fails;
      entry->tok_type = TOK_ID;
      entry->gid = nid;
      entry->resolved = true;
      scan += strlen(scan) + 1;
    }
  fails:
    free(copy);
  }
  return 0;

fail:
  return -1;
}