I don't know how useful this will be, but for tools that script
signify, different exit codes may come in handy.
There's some overlap between codes, so they aren't perfect, but it's
probably more useful than all 1s. The scheme used is:
1- user error. mostly related to getopt style stuff
2- operational error. malloc failed.
3- file parsing error. key file not a key, unsupported algorithm.
4- signature verification failure.
Index: signify.c
===================================================================
RCS file: /cvs/src/usr.bin/signify/signify.c,v
retrieving revision 1.37
diff -u -p -r1.37 signify.c
--- signify.c 14 Jan 2014 21:34:30 -0000 1.37
+++ signify.c 14 Jan 2014 21:42:41 -0000
@@ -95,11 +95,11 @@ xopen(const char *fname, int flags, mode
else
fd = dup(STDIN_FILENO);
if (fd == -1)
- err(1, "dup failed");
+ err(2, "dup failed");
} else {
fd = open(fname, flags, mode);
if (fd == -1)
- err(1, "can't open %s for %s", fname,
+ err(2, "can't open %s for %s", fname,
(flags & O_WRONLY) ? "writing" : "reading");
}
return fd;
@@ -112,7 +112,7 @@ xmalloc(size_t len)
p = malloc(len);
if (!p)
- err(1, "malloc %zu", len);
+ err(2, "malloc %zu", len);
return p;
}
@@ -123,9 +123,9 @@ readall(int fd, void *buf, size_t len, c
x = read(fd, buf, len);
if (x == -1) {
- err(1, "read from %s", filename);
+ err(2, "read from %s", filename);
} else if (x != len) {
- errx(1, "short read from %s", filename);
+ errx(2, "short read from %s", filename);
}
}
@@ -139,20 +139,20 @@ parseb64file(const char *filename, char
commentend = strchr(b64, '\n');
if (!commentend || commentend - b64 <= COMMENTHDRLEN ||
memcmp(b64, COMMENTHDR, COMMENTHDRLEN))
- errx(1, "invalid comment in %s; must start with '%s'",
+ errx(3, "invalid comment in %s; must start with '%s'",
filename, COMMENTHDR);
*commentend = 0;
if (comment)
strlcpy(comment, b64 + COMMENTHDRLEN, COMMENTMAXLEN);
b64end = strchr(commentend + 1, '\n');
if (!b64end)
- errx(1, "missing new line after b64 in %s", filename);
+ errx(3, "missing new line after b64 in %s", filename);
*b64end = 0;
rv = b64_pton(commentend + 1, buf, len);
if (rv != len)
- errx(1, "invalid b64 encoding in %s", filename);
+ errx(3, "invalid b64 encoding in %s", filename);
if (memcmp(buf, PKALG, 2))
- errx(1, "unsupported file %s", filename);
+ errx(3, "unsupported file %s", filename);
return b64end - b64 + 1;
}
@@ -166,7 +166,7 @@ readb64file(const char *filename, void *
memset(b64, 0, sizeof(b64));
rv = read(fd, b64, sizeof(b64) - 1);
if (rv == -1)
- err(1, "read from %s", filename);
+ err(2, "read from %s", filename);
parseb64file(filename, b64, buf, len, comment);
memset(b64, 0, sizeof(b64));
close(fd);
@@ -182,12 +182,12 @@ readmsg(const char *filename, unsigned l
fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
if (fstat(fd, &sb) == -1)
- err(1, "fstat on %s", filename);
+ err(2, "fstat on %s", filename);
if (!S_ISREG(sb.st_mode))
- errx(1, "%s must be a regular file", filename);
+ errx(2, "%s must be a regular file", filename);
msglen = sb.st_size;
if (msglen > (1UL << 30))
- errx(1, "msg too large in %s", filename);
+ errx(3, "msg too large in %s", filename);
msg = xmalloc(msglen);
readall(fd, msg, msglen, filename);
close(fd);
@@ -203,9 +203,9 @@ writeall(int fd, const void *buf, size_t
x = write(fd, buf, len);
if (x == -1) {
- err(1, "write to %s", filename);
+ err(2, "write to %s", filename);
} else if (x != len) {
- errx(1, "short write to %s", filename);
+ errx(2, "short write to %s", filename);
}
}
@@ -232,7 +232,7 @@ writeb64file(const char *filename, const
snprintf(header, sizeof(header), "%s%s\n", COMMENTHDR, comment);
writeall(fd, header, strlen(header), filename);
if ((rv = b64_ntop(buf, len, b64, sizeof(b64)-1)) == -1)
- errx(1, "b64 encode failed");
+ errx(2, "b64 encode failed");
b64[rv++] = '\n';
writeall(fd, b64, rv, filename);
memset(b64, 0, sizeof(b64));
@@ -250,12 +250,12 @@ kdf(uint8_t *salt, size_t saltlen, int r
}
if (!readpassphrase("passphrase: ", pass, sizeof(pass), 0))
- errx(1, "readpassphrase");
+ errx(2, "readpassphrase");
if (strlen(pass) == 0)
errx(1, "please provide a password");
if (bcrypt_pbkdf(pass, strlen(pass), salt, saltlen, key,
keylen, rounds) == -1)
- errx(1, "bcrypt pbkdf");
+ errx(2, "bcrypt pbkdf");
memset(pass, 0, sizeof(pass));
}
@@ -333,7 +333,7 @@ sign(const char *seckeyfile, const char
readb64file(seckeyfile, &enckey, sizeof(enckey), comment);
if (memcmp(enckey.kdfalg, KDFALG, 2))
- errx(1, "unsupported KDF");
+ errx(3, "unsupported KDF");
rounds = ntohl(enckey.kdfrounds);
kdf(enckey.salt, sizeof(enckey.salt), rounds, xorkey, sizeof(xorkey));
for (i = 0; i < sizeof(enckey.seckey); i++)
@@ -401,7 +401,7 @@ verifymsg(uint8_t *pubkey, uint8_t *msg,
memcpy(sigbuf + SIGBYTES, msg, msglen);
if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
pubkey) == -1)
- errx(1, "signature verification failed");
+ errx(4, "signature verification failed");
free(sigbuf);
free(dummybuf);
}
@@ -432,7 +432,7 @@ verify(const char *pubkeyfile, const cha
#ifndef VERIFYONLY
inspect(NULL, pubkeyfile, sigfile);
#endif
- errx(1, "verification failed: checked against wrong key");
+ errx(4, "verification failed: checked against wrong key");
}
verifymsg(pubkey.pubkey, msg, msglen, sig.sig);