#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: pwhash.c,v 1.16 2019/10/21 02:36:48 jhigh Exp $");
#endif
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <login_cap.h>
#include <util.h>
#define DO_MAKEKEY 0
#define DO_DES 1
#define DO_MD5 2
#define DO_BLF 3
#define DO_SHA1 4
#ifdef HAVE_ARGON2
#define DO_ARGON2 5
#define ARGON2_DEFAULT_VARIANT_STR "argon2id"
#endif
__dead static void
usage(void)
{
(void)fprintf(stderr,
#ifdef HAVE_ARGON2
"Usage: %s [-km] [-A variant[,params]] [-b rounds] [-S rounds] [-s salt] [-p | string]\n",
#else
"Usage: %s [-km] [-b rounds] [-S rounds] [-s salt] [-p | string]\n",
#endif
getprogname());
exit(1);
}
static char *
trim(char *line)
{
char *ptr;
for (ptr = &line[strlen(line) - 1]; ptr > line; ptr--) {
if (!isspace((unsigned char)*ptr))
break;
}
ptr[1] = '\0';
for (ptr = line; *ptr && isspace((unsigned char)*ptr); ptr++)
continue;
return ptr;
}
static void
print_passwd(char *string, int operation, const char *extra)
{
char buf[_PASSWORD_LEN];
char option[LINE_MAX], *key, *opt;
int error = 0;
const char *salt = buf;
switch(operation) {
case DO_MAKEKEY:
if (strlen(string) != 10) {
error = EFTYPE;
break;
}
salt = &string[8];
break;
case DO_MD5:
error = pw_gensalt(buf, _PASSWORD_LEN, "md5", extra);
break;
case DO_SHA1:
error = pw_gensalt(buf, _PASSWORD_LEN, "sha1", extra);
break;
case DO_BLF:
error = pw_gensalt(buf, _PASSWORD_LEN, "blowfish", extra);
break;
case DO_DES:
salt = extra;
break;
#ifdef HAVE_ARGON2
case DO_ARGON2:
snprintf(option, sizeof(option), "%s", extra);
opt = option;
key = strsep(&opt, ",");
error = pw_gensalt(buf, _PASSWORD_LEN, key, opt);
break;
#endif
default:
pw_getconf(option, sizeof(option), "default", "localcipher");
opt = option;
key = strsep(&opt, ",");
error = pw_gensalt(buf, _PASSWORD_LEN, key, opt);
break;
}
if (error)
err(1, "Cannot generate salt");
(void)fputs(crypt(string, salt), stdout);
}
int
main(int argc, char **argv)
{
int opt;
int operation = -1;
int prompt = 0;
const char *extra = NULL;
setprogname(argv[0]);
if (strcmp(getprogname(), "makekey") == 0)
operation = DO_MAKEKEY;
#ifdef HAVE_ARGON2
while ((opt = getopt(argc, argv, "kmpS:s:b:A:")) != -1) {
#else
while ((opt = getopt(argc, argv, "kmpS:s:b:")) != -1) {
#endif
switch (opt) {
case 'k':
if (operation != -1 || prompt)
usage();
operation = DO_MAKEKEY;
break;
case 'm':
if (operation != -1)
usage();
operation = DO_MD5;
extra = NULL;
break;
case 'p':
if (operation == DO_MAKEKEY)
usage();
prompt = 1;
break;
case 'S':
if (operation != -1)
usage();
operation = DO_SHA1;
extra = optarg;
break;
case 's':
if (operation != -1 || optarg[0] == '$')
usage();
operation = DO_DES;
extra = optarg;
break;
case 'b':
if (operation != -1)
usage();
operation = DO_BLF;
extra = optarg;
break;
#ifdef HAVE_ARGON2
case 'A':
if (operation != -1)
usage();
operation = DO_ARGON2;
extra = optarg;
break;
#endif
default:
usage();
}
}
if (((argc - optind) < 1) || operation == DO_MAKEKEY) {
char line[LINE_MAX], *string;
if (prompt) {
string = getpass("Enter string: ");
print_passwd(string, operation, extra);
(void)fputc('\n', stdout);
} else {
while (!feof(stdin) &&
(fgets(line, sizeof(line), stdin) != NULL)) {
string = trim(line);
if (*string == '\0')
continue;
print_passwd(string, operation, extra);
if (operation == DO_MAKEKEY) {
(void)fflush(stdout);
break;
}
(void)fputc('\n', stdout);
}
}
} else {
char *string;
if (prompt)
usage();
if ((string = strdup(argv[optind])) == NULL)
err(1, NULL);
(void)memset(argv[optind], 0, strlen(argv[optind]));
print_passwd(string, operation, extra);
(void)fputc('\n', stdout);
(void)memset(string, 0, strlen(string));
free(string);
}
return 0;
}