-
Notifications
You must be signed in to change notification settings - Fork 0
/
setalias.c
706 lines (648 loc) · 14 KB
/
setalias.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
* This file is part of setalias
* Copyright © M. Kristall
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of version 2 of the GNU General Public License as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of version 2 of the GNU General Public
* License with this program; if not, write to the Free Software Foundation, Inc
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <limits.h>
#include <stdarg.h>
#include <arpa/inet.h>
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <pwd.h>
#ifndef ALIASFILE
#define ALIASFILE "/etc/aliases"
#endif
#ifndef NEWALIASES
#define NEWALIASES "/usr/bin/newaliases"
#endif
#define NEWSUFFIX ".new"
#define OLDSUFFIX ".bak"
#define SUPERUID 0
const char *progname;
char *skipspaces (char *s) {
while (*s && isspace (*s))
s++;
return s;
}
char *strnchr (char *s, int c, size_t l) {
for (; *s && l > 0; l--, s++) {
if (*s == c)
return (char *)s;
}
return NULL;
}
char *copyquoted (char *in, char *out, size_t m) {
char *p;
size_t l;
if (*in != '"')
return NULL;
in++;
// the quote must terminate
if (!(p = strnchr (in, '"', m)))
return NULL;
// length of output string
l = p - in;
// advance past "
p++;
// m must be bigger than l so there is space for the \0
if (m <= l)
return NULL;
memcpy (out, in, l);
out[l] = '\0';
return p;
}
char *gettoken (char *in, char *out, size_t len) {
in = skipspaces (in);
if (!*in)
return NULL;
if (*in == '"')
return copyquoted (in, out, len);
while (*in) {
if (isspace (*in))
break;
switch (*in) {
case '\n':
case '#':
case ':':
goto done;
case '"':
return NULL;
default:
// need space for this character and \0
if (len < 2)
return NULL;
*out++ = *in++;
len--;
}
}
done:
*out = '\0';
return in;
}
int puttoken (char *out, const char *in, size_t m) {
size_t chrs;
int quote = 0;
if (strpbrk (in, "\t\n :#"))
quote = 1;
// \0
chrs = strlen (in) + 1;
if (m < chrs + quote * 2)
return 0;
if (quote)
*out++ = '"';
memcpy (out, in, chrs);
out += strlen (out);
if (quote)
*out++ = '"';
*out = '\0';
return 1;
}
// name: alias
int getalias (char *in, char *user, char *alias, size_t m) {
*user = *alias = '\0';
in = skipspaces (in);
// comment or end of line
if (!*in || *in == '\n' || *in == '#')
return 0;
// name:
if (!(in = gettoken (in, user, m)))
return 0;
in = skipspaces (in);
if (*in != ':')
return 0;
in++;
// alias
if (!(in = gettoken (in, alias, m)))
return 0;
in = skipspaces (in);
// comment or end of line
if (*in && *in != '#' && *in != '\n')
return 0;
return 1;
}
int setalias (char *line, const char *alias, size_t n) {
char *p = strchr (line, ':');
if (!p)
return 0;
p++;
if (!(p = skipspaces (p)))
return 0;
// space remaining
n -= p - line;
if (!puttoken (p, alias, n))
return 0;
p += strlen (p) + 1;
if (n < p - line + 2)
return 0;
*p++ = '\n';
*p = '\0';
return 1;
}
int makealias (char *line, const char *user, const char *alias, size_t n) {
size_t len;
if (!puttoken (line, user, n))
return 0;
len = strlen (line);
if (n < len + 3)
return 0;
line[len] = ':';
line[len + 1] = '\t';
if (!puttoken (line + len + 2, alias, n - len - 2))
return 0;
return 1;
}
struct optss {
int delete, get, verbose;
char *user, *file, *suffix;
};
void vprint (const struct optss *o, int l, const char *fmt, ...) {
va_list ap;
if (l > o->verbose)
return;
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
}
enum {
TYPEFREE,
TYPEBOOL,
TYPEINC,
TYPEINT,
TYPESTR
};
struct optt {
int type;
size_t ofs;
int root;
};
int aliases (const struct optss *o, const char *newalias) {
FILE *in, *out = NULL;
int ifd, ofd = -1;
int lockret;
struct stat st;
size_t n = 0, l = strlen (o->file);
ssize_t r;
char new[strlen (NEWSUFFIX) + l + 1], old[strlen (OLDSUFFIX) + l + 1];
char *line = NULL;
int ok = 0;
if (!(in = fopen (o->file, "r"))) {
fprintf (stderr, "%s: could not aliases file: %s\n",
progname, strerror (errno));
return 0;
}
ifd = fileno (in);
#define RETRY(r, e) do { r = (e); } while (r == -1 && errno == EINTR)
RETRY (lockret, flock (ifd, LOCK_SH));
if (lockret == -1) {
fprintf (stderr, "%s: could not lock %s: %s\n",
progname, o->file, strerror (errno));
goto fail;
}
if (!o->get) {
RETRY (lockret, flock (ifd, LOCK_EX));
if (lockret == -1) {
fprintf (stderr, "%s: could not lock %s: %s\n",
progname, o->file, strerror (errno));
goto fail;
}
if (fstat (ifd, &st) != 0) {
fprintf (stderr, "%s: could not stat %s: %s\n",
progname, o->file, strerror (errno));
goto fail;
}
memcpy (new, o->file, l);
strcpy (new + l, NEWSUFFIX);
if (!(out = fopen (new, "w"))) {
fprintf (stderr, "%s: could not open %s: %s\n",
progname, new, strerror (errno));
goto fail;
}
ofd = fileno (out);
RETRY (lockret, flock (ofd, LOCK_EX));
if (lockret == -1) {
fprintf (stderr, "%s: could not lock temp file: %s\n",
progname, strerror (errno));
goto fail;
}
}
while ((r = getline (&line, &n, in)) != -1) {
char user[1000], alias[1000];
if (r == 0)
continue;
if (!getalias (line, user, alias, 1000))
;
else if (strcmp (user, o->user) == 0) {
ok = 1;
if (o->get) {
printf ("%s's alias is %s\n",
o->user, alias);
break;
}
if (o->delete) {
printf ("removed %s's alias (%s)\n",
o->user, alias);
continue;
}
if (strcmp (alias, newalias) == 0) {
printf ("no change\n");
goto fail;
}
if (!setalias (line, newalias, n)) {
fprintf (stderr, "%s: could not set alias\n",
progname);
goto fail;
}
printf ("set %s's alias to %s (from %s)\n",
o->user, newalias, alias);
r = strlen (line);
}
if (out && !fwrite (line, r, 1, out)) {
fprintf (stderr, "%s: updating failed: %s\n",
progname, strerror (errno));
goto fail;
}
/* no trailing \n */
if (line[r - 1] != '\n')
fwrite ("\n", 1, 1, out);
}
if (!ok) {
if (o->get)
printf ("%s has no alias\n", o->user);
else if (o->delete) {
fprintf (stderr, "%s: no change\n", progname);
goto fail;
} else if (!makealias (line, o->user, newalias, 1000)) {
fprintf (stderr, "%s: updating failed\n",
progname);
goto fail;
} else {
fprintf (out, "%s\n", line);
printf ("set %s's alias to %s\n", o->user, newalias);
}
}
free (line);
n = 0;
flock (ifd, LOCK_UN);
fclose (in);
in = NULL;
if (out) {
if (fchmod (ofd, st.st_mode) != 0) {
fprintf (stderr, "%s: could not chmod: %s\n",
progname, strerror (errno));
goto fail;
}
flock (ofd, LOCK_UN);
fclose (out);
out = NULL;
memcpy (old, o->file, l);
strcpy (old + l, OLDSUFFIX);
unlink (old);
if (rename (o->file, old) != 0) {
fprintf (stderr, "%s: could not move aliases file: %s\n",
progname, strerror (errno));
goto fail;
}
if (rename (new, o->file) != 0) {
rename (old, o->file);
fprintf (stderr, "%s: could not move new aliases file %s: %s\n",
progname, new, strerror (errno));
}
}
return 1;
fail:
if (n)
free (line);
if (in) {
flock (ifd, LOCK_UN);
fclose (in);
}
if (out) {
flock (ofd, LOCK_UN);
fclose (out);
unlink (new);
}
return 0;
}
static struct optt args[CHAR_MAX];
void _setupargs (void) {
/* -f path to alias file */
args['f'].type = TYPESTR,
args['f'].ofs = offsetof (struct optss, file),
args['f'].root = 1;
/* -S suffix */
args['S'].type = TYPESTR,
args['S'].ofs = offsetof (struct optss, suffix),
args['S'].root = 1;
/* -u user to set */
args['u'].type = TYPESTR,
args['u'].ofs = offsetof (struct optss, user),
args['u'].root = 1;
/* -d delete/unset alias */
args['d'].type = TYPEBOOL,
args['d'].ofs = offsetof (struct optss, delete);
/* -v verbosity */
args['v'].type = TYPEINC,
args['v'].ofs = offsetof (struct optss, verbose);
}
/* handle args, returns number of arguments consumed */
int parg (struct optss *o, int argc, char **argv, int n, int root) {
char *c;
for (c = argv[n]; *c == '-'; n++) {
void *p;
int a;
for (p = NULL, c++; (a = *c); p = NULL, c++) {
if (a < 0 || !args[a].type) {
fprintf (stderr,
"%s: invalid argument '-%c'\n",
progname, a);
return -1;
}
if (args[a].root && !root) {
fprintf (stderr,
"%s: permission denied setting '-%c'\n",
progname, a);
return -1;
}
p = (char *)o + args[a].ofs;
if (args[a].type != TYPEINC && *(char *)p) {
fprintf (stderr,
"%s: duplicate '-%c'\n",
progname, a);
return -1;
}
/* arguments expecting no value here */
if (args[a].type == TYPEBOOL)
*(int *)p = 1;
else if (args[a].type == TYPEINC)
(*(int *)p)++;
else
/* other arg types eat arguments */
break;
}
if (!p)
continue;
/* we are expecting a value */
/* we've reached the end of this arg so check the next */
c++;
if (!*c) {
if (++n >= argc) {
fprintf (stderr,
"%s: argument expected for '-%c'\n",
progname, a);
return -1;
}
c = argv[n];
}
if (args[a].type == TYPEINT) {
char *b;
long v = strtol (c, &b, 10);
if (*b) {
fprintf (stderr,
"%s: expected integer for '-%c', got '%s'\n",
progname, a, c);
return -1;
}
if (v < INT_MIN || v > INT_MAX) {
fprintf (stderr,
"%s: value for '-%c' out of range\n",
progname, a);
return -1;
}
*(int *)p = (int)v;
} else if (args[a].type == TYPESTR)
*(char **)p = c;
else {
fprintf (stderr,
"%s: '-%c' is not fully implemented\n",
progname, a);
return -2;
}
}
return n;
}
int validip (char *t) {
struct in6_addr a;
char *e;
int af = AF_INET, r;
if (*t++ != '[')
return 0;
if (!(e = strchr (t, ']')) || e[1])
return 0;
*e = '\0';
if (strncmp (t, "IPv6:", 5) == 0) {
t += 5;
af = AF_INET6;
}
r = inet_pton (af, t, &a);
*e = ']';
return r;
}
#define UTFCONT if ((*(++(*t)) & 0xc0) != 0x80) return 0
int validutf8 (char **t) {
switch (**t & 0xf0) {
default: return 0;
case 0xf0: UTFCONT;
case 0xe0: UTFCONT;
case 0xc0: UTFCONT;
case 0x80: UTFCONT;
}
return 1;
}
int validalias (char *t) {
char *s = t;
int at = 0;
/* this is a local user account */
if (getpwnam (t))
return 1;
/* otherwise, allow an email address */
for (; *t; t++) {
while (validutf8 (&t))
;
if (!*t)
break;
if (isalnum (*t))
;
else if (*t == '.') {
/* cannot be first or last */
if (
t == s ||
t[-1] == '@' ||
t[1] == '@' ||
t[1] == '\0'
)
return 0;
} else if (at) {
/* - cannot be first or last in domain */
if (*t == '-') {
if (t[-1] == '@' || t[1] == '\0')
return 0;
} else
return 0;
} else if (*t == '@') {
/* cannot be first or last */
if (t == s || !t[1])
return 0;
at = 1;
if (t[1] == '[')
return validip (t + 1);
} else {
/* in username */
switch (*t) {
/* these are not all contiguous, so… */
case '!': case '#': case '$': case '%':
case '&': case '\'': case '*': case '+':
case '/': case '=': case '?': case '^':
case '_': case '`': case '{': case '|':
case '}': case '~':
break;
default:
return 0;
}
}
}
return at;
}
int main (int argc, char **argv) {
struct optss opts = {0};
uid_t uid =
#ifdef NDEBUG
getuid ();
#else
SUPERUID;
#endif
char *alias = NULL;
int i;
progname = argv[0];
_setupargs ();
for (i = 1; i < argc; i++) {
if (*argv[i] == '-') {
i = parg (&opts, argc, argv, i, uid == SUPERUID);
if (i < 0)
return 1;
--i;
} else {
if (alias) {
fprintf (stderr,
"%s: you can only specify one alias\n",
progname);
return 1;
}
alias = argv[i];
}
}
/* now figure out how we are operating */
if (!opts.file)
opts.file = ALIASFILE;
if (!opts.suffix)
opts.suffix = OLDSUFFIX;
/* try to figure out what user means */
if (opts.user) {
struct passwd *pw;
/* is this a uid? */
if (isdigit (*opts.user)) {
uid = atoi (opts.user);
if (!(pw = getpwuid (uid))) {
endpwent ();
fprintf (stderr,
"%s: invalid uid '%d'\n",
progname, uid);
return 1;
}
opts.user = pw->pw_name;
} else if (!(pw = getpwnam (opts.user))) {
endpwent ();
fprintf (stderr,
"%s: invalid user '%s'\n",
progname, opts.user);
return 1;
}
} else {
struct passwd *pw = getpwuid (uid);
if (!pw) {
endpwent ();
fprintf (stderr,
"%s: you have no passwd entry\n",
progname);
return 0;
}
opts.user = pw->pw_name;
}
if (alias) {
if (strcmp (opts.user, alias) == 0)
opts.delete = 1;
else if (!validalias (alias)) {
fprintf (stderr,
"%s: '%s' does not look like a valid alias\n",
progname, alias);
return 1;
}
}
opts.get = !alias && !opts.delete;
vprint (&opts, 1,
"%s configuration:\n"
" verbosity = %d\n"
" user = %s\n"
" file = %s\n"
" suffix = %s\n"
" delete = %s\n"
" get = %s\n"
" alias = %s\n",
progname,
opts.verbose, opts.user, opts.file, opts.suffix,
opts.delete ? "true" : "false",
opts.get ? "true" : "false",
alias);
if (NEWALIASES) {
struct stat st;
if (stat (NEWALIASES, &st) != 0) {
fprintf (stderr,
"%s: cannot stat %s\n",
progname, NEWALIASES);
return 2;
}
if (st.st_uid != SUPERUID) {
fprintf (stderr,
"%s: %s is not owned by root\n",
progname, NEWALIASES);
return 2;
}
if (st.st_mode & (S_IWGRP | S_IWOTH)) {
fprintf (stderr,
"%s: %s does not seem secure\n",
progname, NEWALIASES);
return 2;
}
}
if (!aliases (&opts, alias)) {
endpwent ();
return 1;
}
endpwent ();
if (NEWALIASES) {
setuid (SUPERUID);
execl (NEWALIASES, NEWALIASES, (char *)NULL);
}
return 0;
}