|
|
@ -29,16 +29,30 @@
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
#include <wordexp.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "kasmpasswd.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void usage(const char *prog)
|
|
|
|
static void usage(const char *prog)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [file]\n", prog);
|
|
|
|
fprintf(stderr, "Usage: %s -u username [-wnod] [file]\n"
|
|
|
|
fprintf(stderr, " %s -f\n", prog);
|
|
|
|
"-w Write permission\n"
|
|
|
|
|
|
|
|
"-o Owner\n"
|
|
|
|
|
|
|
|
"-n Don't change password, change permissions only\n"
|
|
|
|
|
|
|
|
"-d Delete this user\n"
|
|
|
|
|
|
|
|
"\n"
|
|
|
|
|
|
|
|
"The file is updated atomically.\n\n"
|
|
|
|
|
|
|
|
"To pass the password via a pipe, use\n"
|
|
|
|
|
|
|
|
"echo -e \"password\\npassword\\n\" | %s [-args]\n",
|
|
|
|
|
|
|
|
prog, prog);
|
|
|
|
exit(1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void enableEcho(unsigned char enable) {
|
|
|
|
static void enableEcho(unsigned char enable) {
|
|
|
|
struct termios attrs;
|
|
|
|
struct termios attrs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!isatty(fileno(stdin)))
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
tcgetattr(fileno(stdin), &attrs);
|
|
|
|
tcgetattr(fileno(stdin), &attrs);
|
|
|
|
if (enable)
|
|
|
|
if (enable)
|
|
|
|
attrs.c_lflag |= ECHO;
|
|
|
|
attrs.c_lflag |= ECHO;
|
|
|
@ -53,7 +67,7 @@ static const char *encryptpw(const char *in) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char* getpassword(const char* prompt, char *buf) {
|
|
|
|
static char* getpassword(const char* prompt, char *buf) {
|
|
|
|
if (prompt) fputs(prompt, stdout);
|
|
|
|
if (prompt && isatty(fileno(stdin))) fputs(prompt, stdout);
|
|
|
|
enableEcho(0);
|
|
|
|
enableEcho(0);
|
|
|
|
char* result = fgets(buf, 4096, stdin);
|
|
|
|
char* result = fgets(buf, 4096, stdin);
|
|
|
|
enableEcho(1);
|
|
|
|
enableEcho(1);
|
|
|
@ -68,18 +82,6 @@ static char* getpassword(const char* prompt, char *buf) {
|
|
|
|
static char pw1[4096];
|
|
|
|
static char pw1[4096];
|
|
|
|
static char pw2[4096];
|
|
|
|
static char pw2[4096];
|
|
|
|
|
|
|
|
|
|
|
|
// Reads passwords from stdin and prints encrypted passwords to stdout.
|
|
|
|
|
|
|
|
static int encrypt_pipe() {
|
|
|
|
|
|
|
|
char *result = getpassword(NULL, pw1);
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("%s", encryptpw(result));
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char *readpassword() {
|
|
|
|
static const char *readpassword() {
|
|
|
|
while (1) {
|
|
|
|
while (1) {
|
|
|
|
if (!getpassword("Password:", pw1)) {
|
|
|
|
if (!getpassword("Password:", pw1)) {
|
|
|
@ -100,6 +102,10 @@ static const char *readpassword() {
|
|
|
|
exit(1);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcmp(pw1, pw2) != 0) {
|
|
|
|
if (strcmp(pw1, pw2) != 0) {
|
|
|
|
|
|
|
|
if (!isatty(fileno(stdin))) {
|
|
|
|
|
|
|
|
fprintf(stderr,"Passwords don't match\n");
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
fprintf(stderr,"Passwords don't match - try again\n");
|
|
|
|
fprintf(stderr,"Passwords don't match - try again\n");
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -110,20 +116,49 @@ static const char *readpassword() {
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
char* fname = 0;
|
|
|
|
const char *fname = NULL;
|
|
|
|
|
|
|
|
const char *user = NULL;
|
|
|
|
for (int i = 1; i < argc; i++) {
|
|
|
|
const char args[] = "u:wnod";
|
|
|
|
if (strncmp(argv[i], "-f", 2) == 0) {
|
|
|
|
int opt;
|
|
|
|
return encrypt_pipe();
|
|
|
|
|
|
|
|
} else if (argv[i][0] == '-') {
|
|
|
|
unsigned char nopass = 0, writer = 0, owner = 0, deleting = 0;
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
|
|
} else if (!fname) {
|
|
|
|
while ((opt = getopt(argc, argv, args)) != -1) {
|
|
|
|
fname = argv[i];
|
|
|
|
switch (opt) {
|
|
|
|
} else {
|
|
|
|
case 'u':
|
|
|
|
|
|
|
|
user = optarg;
|
|
|
|
|
|
|
|
if (strlen(user) + 1 > sizeof(((struct kasmpasswd_entry_t *)0)->user)) {
|
|
|
|
|
|
|
|
fprintf(stderr, "Username %s too long\n", user);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
|
|
|
|
nopass = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
|
|
|
|
writer = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
|
|
|
|
owner = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
|
|
|
|
deleting = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (deleting && (nopass || writer || owner))
|
|
|
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
|
|
|
|
fname = argv[optind];
|
|
|
|
|
|
|
|
|
|
|
|
if (!fname) {
|
|
|
|
if (!fname) {
|
|
|
|
wordexp_t wexp;
|
|
|
|
wordexp_t wexp;
|
|
|
|
if (!wordexp("~/.kasmpasswd", &wexp, WRDE_NOCMD) && wexp.we_wordv[0])
|
|
|
|
if (!wordexp("~/.kasmpasswd", &wexp, WRDE_NOCMD) && wexp.we_wordv[0])
|
|
|
@ -133,23 +168,54 @@ int main(int argc, char** argv)
|
|
|
|
if (!fname)
|
|
|
|
if (!fname)
|
|
|
|
usage(argv[0]);
|
|
|
|
usage(argv[0]);
|
|
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Action
|
|
|
|
const char *encrypted = readpassword();
|
|
|
|
struct kasmpasswd_t *set = readkasmpasswd(fname);
|
|
|
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
|
|
FILE* fp = fopen(fname, "w");
|
|
|
|
if (nopass) {
|
|
|
|
if (!fp) {
|
|
|
|
for (i = 0; i < set->num; i++) {
|
|
|
|
fprintf(stderr, "Couldn't open %s for writing\n", fname);
|
|
|
|
if (!strcmp(set->entries[i].user, user)) {
|
|
|
|
exit(1);
|
|
|
|
set->entries[i].write = writer;
|
|
|
|
}
|
|
|
|
set->entries[i].owner = owner;
|
|
|
|
chmod(fname, S_IRUSR|S_IWUSR);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fwrite(encrypted, strlen(encrypted), 1, fp) != 1) {
|
|
|
|
writekasmpasswd(fname, set);
|
|
|
|
fprintf(stderr,"Writing to %s failed\n",fname);
|
|
|
|
return 0;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "No user named %s found\n", user);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
} else if (deleting) {
|
|
|
|
|
|
|
|
for (i = 0; i < set->num; i++) {
|
|
|
|
|
|
|
|
if (!strcmp(set->entries[i].user, user)) {
|
|
|
|
|
|
|
|
set->entries[i].user[0] = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writekasmpasswd(fname, set);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "No user named %s found\n", user);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const char *encrypted = readpassword();
|
|
|
|
|
|
|
|
for (i = 0; i < set->num; i++) {
|
|
|
|
|
|
|
|
if (!strcmp(set->entries[i].user, user))
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// No existing user by that name?
|
|
|
|
|
|
|
|
if (i >= set->num) {
|
|
|
|
|
|
|
|
i = set->num++;
|
|
|
|
|
|
|
|
set->entries = realloc(set->entries, set->num * sizeof(struct kasmpasswd_entry_t));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
strcpy(set->entries[i].user, user);
|
|
|
|
|
|
|
|
strcpy(set->entries[i].password, encrypted);
|
|
|
|
|
|
|
|
set->entries[i].write = writer;
|
|
|
|
|
|
|
|
set->entries[i].owner = owner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
writekasmpasswd(fname, set);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|