diff options
author | Sunil Nimmagadda <sunil@sunilnimmagadda.com> | 2013-10-25 15:56:05 +0530 |
---|---|---|
committer | Sunil Nimmagadda <sunil@sunilnimmagadda.com> | 2013-10-25 15:56:05 +0530 |
commit | cdc881ea6d680f04a4301911b0dd3e9772820091 (patch) | |
tree | 5a5db803035896f19558c54d9dd7be5059e4a892 /contrib | |
parent | Use chacha_random instead of libc's random. (diff) | |
download | OpenSMTPD-cdc881ea6d680f04a4301911b0dd3e9772820091.tar.xz OpenSMTPD-cdc881ea6d680f04a4301911b0dd3e9772820091.zip |
Accept string from stdin.
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/libexec/encrypt/Makefile.am | 2 | ||||
-rw-r--r-- | contrib/libexec/encrypt/encrypt.c | 31 |
2 files changed, 25 insertions, 8 deletions
diff --git a/contrib/libexec/encrypt/Makefile.am b/contrib/libexec/encrypt/Makefile.am index e331fd53..9b09d910 100644 --- a/contrib/libexec/encrypt/Makefile.am +++ b/contrib/libexec/encrypt/Makefile.am @@ -1,6 +1,6 @@ pkglibexec_PROGRAMS = encrypt -encrypt_SOURCES = encrypt.c +encrypt_SOURCES = encrypt.c $(top_srcdir)/smtpd/log.c INCLUDES = -I$(top_srcdir)/openbsd-compat diff --git a/contrib/libexec/encrypt/encrypt.c b/contrib/libexec/encrypt/encrypt.c index 74da6fde..95d1baf3 100644 --- a/contrib/libexec/encrypt/encrypt.c +++ b/contrib/libexec/encrypt/encrypt.c @@ -1,4 +1,3 @@ - /* * Copyright (c) 2013 Sunil Nimmagadda <sunil@sunilnimmagadda.com> * @@ -16,6 +15,7 @@ */ #include <stdio.h> +#include <stdlib.h> #include <unistd.h> #include "openbsd-compat.h" @@ -26,26 +26,43 @@ static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; static void to64(char *, long int, int); +static void print_passwd(const char *); int main(int argc, char *argv[]) { - char *c, salt[PASSWORD_LEN]; + char *s; + size_t len; - if (argc != 2) { - fprintf(stderr, "usage: encrypt string"); + if (argc == 2) { + print_passwd(argv[1]); + return (0); + } else if (argc > 2) { + fprintf(stderr, "usage: encrypt string\n"); return (1); } + while ((s = fgetln(stdin, &len)) != NULL) { + s[len - 1] = '\0'; + print_passwd(s); + } + + return (0); +} + +void +print_passwd(const char *string) +{ + char *c, salt[PASSWORD_LEN]; + to64(&salt[0], chacha_random(), 2); salt[2] = '\0'; - if ((c = crypt(argv[1], salt)) == NULL) { + if ((c = crypt(string, salt)) == NULL) { fprintf(stderr, "crypt failed"); - return (1); + exit(1); } printf("%s\n", c); - return (0); } void |