Skip to content

Commit 97c5f07

Browse files
committed
Merge pull request #27 from mp39590/sysexits
use proper sysexits(3) codes
2 parents 3f774a0 + de196e3 commit 97c5f07

File tree

8 files changed

+59
-60
lines changed

8 files changed

+59
-60
lines changed

TODO

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
- unquote/handle quoted local recipients
2-
- use proper sysexit codes
32
- handle/use ESMTP extensions
43
- .forward support
54
- suggest way to run a queue flush on boot

conf.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ trim_line(char *line)
6464
if (line[0] == '.') {
6565
if ((linelen + 2) > 1000) {
6666
syslog(LOG_CRIT, "Cannot escape leading dot. Buffer overflow");
67-
exit(1);
67+
exit(EX_DATAERR);
6868
}
6969
memmove((line + 1), line, (linelen + 1));
7070
line[0] = '.';
@@ -101,7 +101,7 @@ parse_authfile(const char *path)
101101

102102
a = fopen(path, "r");
103103
if (a == NULL) {
104-
errlog(1, "can not open auth file `%s'", path);
104+
errlog(EX_NOINPUT, "can not open auth file `%s'", path);
105105
/* NOTREACHED */
106106
}
107107

@@ -121,7 +121,7 @@ parse_authfile(const char *path)
121121

122122
au = calloc(1, sizeof(*au));
123123
if (au == NULL)
124-
errlog(1, NULL);
124+
errlog(EX_OSERR, NULL);
125125

126126
data = strdup(line);
127127
au->login = strsep(&data, "|");
@@ -131,8 +131,7 @@ parse_authfile(const char *path)
131131
if (au->login == NULL ||
132132
au->host == NULL ||
133133
au->password == NULL) {
134-
errlogx(1, "syntax error in authfile %s:%d",
135-
path, lineno);
134+
errlogx(EX_CONFIG, "syntax error in authfile %s:%d", path, lineno);
136135
/* NOTREACHED */
137136
}
138137

@@ -160,7 +159,7 @@ parse_conf(const char *config_path)
160159
/* Don't treat a non-existing config file as error */
161160
if (errno == ENOENT)
162161
return;
163-
errlog(1, "can not open config `%s'", config_path);
162+
errlog(EX_NOINPUT, "can not open config `%s'", config_path);
164163
/* NOTREACHED */
165164
}
166165

@@ -211,7 +210,7 @@ parse_conf(const char *config_path)
211210
} else {
212211
host = data;
213212
}
214-
if (host && *host == 0)
213+
if (host && *host == 0)
215214
host = NULL;
216215
if (user && *user == 0)
217216
user = NULL;
@@ -232,13 +231,13 @@ parse_conf(const char *config_path)
232231
else if (strcmp(word, "NULLCLIENT") == 0 && data == NULL)
233232
config.features |= NULLCLIENT;
234233
else {
235-
errlogx(1, "syntax error in %s:%d", config_path, lineno);
234+
errlogx(EX_CONFIG, "syntax error in %s:%d", config_path, lineno);
236235
/* NOTREACHED */
237236
}
238237
}
239238

240239
if ((config.features & NULLCLIENT) && config.smarthost == NULL) {
241-
errlogx(1, "%s: NULLCLIENT requires SMARTHOST", config_path);
240+
errlogx(EX_CONFIG, "%s: NULLCLIENT requires SMARTHOST", config_path);
242241
/* NOTREACHED */
243242
}
244243

dma-mbox-create.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555

5656
static void
57-
logfail(const char *fmt, ...)
57+
logfail(int exitcode, const char *fmt, ...)
5858
{
5959
int oerrno = errno;
6060
va_list ap;
@@ -73,7 +73,7 @@ logfail(const char *fmt, ...)
7373
else
7474
syslog(LOG_ERR, errno ? "%m" : "unknown error");
7575

76-
exit(1);
76+
exit(exitcode);
7777
}
7878

7979
/*
@@ -98,21 +98,21 @@ main(int argc, char **argv)
9898
errno = 0;
9999
gr = getgrnam(DMA_GROUP);
100100
if (!gr)
101-
logfail("cannot find dma group `%s'", DMA_GROUP);
101+
logfail(EX_CONFIG, "cannot find dma group `%s'", DMA_GROUP);
102102

103103
mail_gid = gr->gr_gid;
104104

105105
if (setgid(mail_gid) != 0)
106-
logfail("cannot set gid to %d (%s)", mail_gid, DMA_GROUP);
106+
logfail(EX_NOPERM, "cannot set gid to %d (%s)", mail_gid, DMA_GROUP);
107107
if (getegid() != mail_gid)
108-
logfail("cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid());
108+
logfail(EX_NOPERM, "cannot set gid to %d (%s), still at %d", mail_gid, DMA_GROUP, getegid());
109109

110110
/*
111111
* We take exactly one argument: the username.
112112
*/
113113
if (argc != 2) {
114114
errno = 0;
115-
logfail("no arguments");
115+
logfail(EX_USAGE, "no arguments");
116116
}
117117
user = argv[1];
118118

@@ -121,36 +121,36 @@ main(int argc, char **argv)
121121
/* the username may not contain a pathname separator */
122122
if (strchr(user, '/')) {
123123
errno = 0;
124-
logfail("path separator in username `%s'", user);
124+
logfail(EX_DATAERR, "path separator in username `%s'", user);
125125
exit(1);
126126
}
127127

128128
/* verify the user exists */
129129
errno = 0;
130130
pw = getpwnam(user);
131131
if (!pw)
132-
logfail("cannot find user `%s'", user);
132+
logfail(EX_NOUSER, "cannot find user `%s'", user);
133133

134134
user_uid = pw->pw_uid;
135135

136136
error = snprintf(fn, sizeof(fn), "%s/%s", _PATH_MAILDIR, user);
137137
if (error < 0 || (size_t)error >= sizeof(fn)) {
138138
if (error >= 0) {
139139
errno = 0;
140-
logfail("mbox path too long");
140+
logfail(EX_USAGE, "mbox path too long");
141141
}
142-
logfail("cannot build mbox path for `%s/%s'", _PATH_MAILDIR, user);
142+
logfail(EX_CANTCREAT, "cannot build mbox path for `%s/%s'", _PATH_MAILDIR, user);
143143
}
144144

145145
f = open(fn, O_RDONLY|O_CREAT, 0600);
146146
if (f < 0)
147-
logfail("cannot open mbox `%s'", fn);
147+
logfail(EX_NOINPUT, "cannt open mbox `%s'", fn);
148148

149149
if (fchown(f, user_uid, mail_gid))
150-
logfail("cannot change owner of mbox `%s'", fn);
150+
logfail(EX_OSERR, "cannot change owner of mbox `%s'", fn);
151151

152152
if (fchmod(f, 0620))
153-
logfail("cannot change permissions of mbox `%s'", fn);
153+
logfail(EX_OSERR, "cannot change permissions of mbox `%s'", fn);
154154

155155
/* file should be present with the right owner and permissions */
156156

dma.c

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ go_background(struct queue *queue)
248248

249249
if (daemonize && daemon(0, 0) != 0) {
250250
syslog(LOG_ERR, "can not daemonize: %m");
251-
exit(1);
251+
exit(EX_OSERR);
252252
}
253253
daemonize = 0;
254254

@@ -265,7 +265,7 @@ go_background(struct queue *queue)
265265
switch (pid) {
266266
case -1:
267267
syslog(LOG_ERR, "can not fork: %m");
268-
exit(1);
268+
exit(EX_OSERR);
269269
break;
270270

271271
case 0:
@@ -287,11 +287,11 @@ go_background(struct queue *queue)
287287
break;
288288
case 1:
289289
if (doqueue)
290-
exit(0);
290+
exit(EX_OK);
291291
syslog(LOG_WARNING, "could not lock queue file");
292-
exit(1);
292+
exit(EX_SOFTWARE);
293293
default:
294-
exit(1);
294+
exit(EX_SOFTWARE);
295295
}
296296
dropspool(queue, it);
297297
return (it);
@@ -307,7 +307,7 @@ go_background(struct queue *queue)
307307
}
308308

309309
syslog(LOG_CRIT, "reached dead code");
310-
exit(1);
310+
exit(EX_SOFTWARE);
311311
}
312312

313313
static void
@@ -332,12 +332,12 @@ deliver(struct qitem *it)
332332
case 0:
333333
delqueue(it);
334334
syslog(LOG_INFO, "delivery successful");
335-
exit(0);
335+
exit(EX_OK);
336336

337337
case 1:
338338
if (stat(it->queuefn, &st) != 0) {
339339
syslog(LOG_ERR, "lost queue file `%s'", it->queuefn);
340-
exit(1);
340+
exit(EX_SOFTWARE);
341341
}
342342
if (gettimeofday(&now, NULL) == 0 &&
343343
(now.tv_sec - st.st_mtim.tv_sec > MAX_TIMEOUT)) {
@@ -439,16 +439,16 @@ main(int argc, char **argv)
439439
pw = getpwnam(DMA_ROOT_USER);
440440
if (pw == NULL) {
441441
if (errno == 0)
442-
errx(1, "user '%s' not found", DMA_ROOT_USER);
442+
errx(EX_CONFIG, "user '%s' not found", DMA_ROOT_USER);
443443
else
444-
err(1, "cannot drop root privileges");
444+
err(EX_OSERR, "cannot drop root privileges");
445445
}
446446

447447
if (setuid(pw->pw_uid) != 0)
448-
err(1, "cannot drop root privileges");
448+
err(EX_OSERR, "cannot drop root privileges");
449449

450450
if (geteuid() == 0 || getuid() == 0)
451-
errx(1, "cannot drop root privileges");
451+
errx(EX_OSERR, "cannot drop root privileges");
452452
}
453453

454454
atexit(deltmp);
@@ -461,15 +461,15 @@ main(int argc, char **argv)
461461
argv++; argc--;
462462
showq = 1;
463463
if (argc != 0)
464-
errx(1, "invalid arguments");
464+
errx(EX_USAGE, "invalid arguments");
465465
goto skipopts;
466466
} else if (strcmp(argv[0], "newaliases") == 0) {
467467
logident_base = "dma";
468468
setlogident(NULL);
469469

470470
if (read_aliases() != 0)
471-
errx(1, "could not parse aliases file `%s'", config.aliases);
472-
exit(0);
471+
errx(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
472+
exit(EX_OK);
473473
}
474474

475475
opterr = 0;
@@ -548,18 +548,18 @@ main(int argc, char **argv)
548548

549549
default:
550550
fprintf(stderr, "invalid argument: `-%c'\n", optopt);
551-
exit(1);
551+
exit(EX_USAGE);
552552
}
553553
}
554554
argc -= optind;
555555
argv += optind;
556556
opterr = 1;
557557

558558
if (argc != 0 && (showq || doqueue))
559-
errx(1, "sending mail and queue operations are mutually exclusive");
559+
errx(EX_USAGE, "sending mail and queue operations are mutually exclusive");
560560

561561
if (showq + doqueue > 1)
562-
errx(1, "conflicting queue operations");
562+
errx(EX_USAGE, "conflicting queue operations");
563563

564564
skipopts:
565565
if (logident_base == NULL)
@@ -579,46 +579,46 @@ main(int argc, char **argv)
579579

580580
if (showq) {
581581
if (load_queue(&queue) < 0)
582-
errlog(1, "can not load queue");
582+
errlog(EX_NOINPUT, "can not load queue");
583583
show_queue(&queue);
584584
return (0);
585585
}
586586

587587
if (doqueue) {
588588
flushqueue_signal();
589589
if (load_queue(&queue) < 0)
590-
errlog(1, "can not load queue");
590+
errlog(EX_NOINPUT, "can not load queue");
591591
run_queue(&queue);
592592
return (0);
593593
}
594594

595595
if (read_aliases() != 0)
596-
errlog(1, "could not parse aliases file `%s'", config.aliases);
596+
errlog(EX_SOFTWARE, "could not parse aliases file `%s'", config.aliases);
597597

598598
if ((sender = set_from(&queue, sender)) == NULL)
599-
errlog(1, NULL);
599+
errlog(EX_SOFTWARE, NULL);
600600

601601
if (newspoolf(&queue) != 0)
602-
errlog(1, "can not create temp file in `%s'", config.spooldir);
602+
errlog(EX_CANTCREAT, "can not create temp file in `%s'", config.spooldir);
603603

604604
setlogident("%s", queue.id);
605605

606606
for (i = 0; i < argc; i++) {
607607
if (add_recp(&queue, argv[i], EXPAND_WILDCARD) != 0)
608-
errlogx(1, "invalid recipient `%s'", argv[i]);
608+
errlogx(EX_DATAERR, "invalid recipient `%s'", argv[i]);
609609
}
610610

611611
if (LIST_EMPTY(&queue.queue) && !recp_from_header)
612-
errlogx(1, "no recipients");
612+
errlogx(EX_NOINPUT, "no recipients");
613613

614614
if (readmail(&queue, nodot, recp_from_header) != 0)
615-
errlog(1, "can not read mail");
615+
errlog(EX_NOINPUT, "can not read mail");
616616

617617
if (LIST_EMPTY(&queue.queue))
618-
errlogx(1, "no recipients");
618+
errlogx(EX_NOINPUT, "no recipients");
619619

620620
if (linkspool(&queue) != 0)
621-
errlog(1, "can not create spools");
621+
errlog(EX_CANTCREAT, "can not create spools");
622622

623623
/* From here on the mail is safe. */
624624

dma.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <arpa/inet.h>
4545
#include <openssl/ssl.h>
4646
#include <netdb.h>
47+
#include <sysexits.h>
4748

4849
#define VERSION "DragonFly Mail Agent " DMA_VERSION
4950

local.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ create_mbox(const char *name)
8282

8383
execl(LIBEXEC_PATH "/dma-mbox-create", "dma-mbox-create", name, NULL);
8484
syslog(LOG_ERR, "cannot execute "LIBEXEC_PATH"/dma-mbox-create: %m");
85-
exit(1);
85+
exit(EX_SOFTWARE);
8686

8787
default:
8888
/* parent */

0 commit comments

Comments
 (0)