When not reading from a tty use getline().

This commit is contained in:
Nikos Mavrogiannopoulos
2014-01-30 19:22:02 +01:00
parent aeb0b40221
commit 313f1b67fd
+27 -15
View File
@@ -36,7 +36,7 @@
#define DEFAULT_OCPASSWD "/etc/ocserv/ocpasswd" #define DEFAULT_OCPASSWD "/etc/ocserv/ocpasswd"
static const char alphabet[] = static const char alphabet[] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
#define SALT_SIZE 16 #define SALT_SIZE 16
@@ -128,7 +128,7 @@ crypt_int(const char *fpasswd, const char *username, const char *groupname,
} }
free(line); free(line);
fclose(fd); fclose(fd);
if (found == 0) if (found == 0)
fprintf(fd2, "%s:%s:%s\n", username, groupname, cr_passwd); fprintf(fd2, "%s:%s:%s\n", username, groupname, cr_passwd);
} }
@@ -189,7 +189,7 @@ lock_user(const char *fpasswd, const char *username)
if (p == NULL) if (p == NULL)
continue; continue;
p++; p++;
l = p-line; l = p-line;
fwrite(line, 1, l, fd2); fwrite(line, 1, l, fd2);
fputc('!', fd2); fputc('!', fd2);
@@ -253,7 +253,7 @@ unlock_user(const char *fpasswd, const char *username)
if (p == NULL) if (p == NULL)
continue; continue;
p++; p++;
l = p-line; l = p-line;
fwrite(line, 1, l, fd2); fwrite(line, 1, l, fd2);
@@ -277,7 +277,8 @@ int main(int argc, char **argv)
{ {
int ret, optct; int ret, optct;
const char *username, *groupname, *fpasswd; const char *username, *groupname, *fpasswd;
const char* passwd; char* passwd;
size_t l, i;
if ((ret = gnutls_global_init()) < 0) { if ((ret = gnutls_global_init()) < 0) {
fprintf(stderr, "global_init: %s\n", gnutls_strerror(ret)); fprintf(stderr, "global_init: %s\n", gnutls_strerror(ret));
@@ -289,12 +290,12 @@ int main(int argc, char **argv)
optct = optionProcess(&ocpasswdOptions, argc, argv); optct = optionProcess(&ocpasswdOptions, argc, argv);
argc -= optct; argc -= optct;
argv += optct; argv += optct;
if (argc > 0) if (argc > 0)
username = argv[0]; username = argv[0];
else { else {
fprintf(stderr, "Please specify a user\n"); fprintf(stderr, "Please specify a user\n");
exit(1); exit(1);
} }
if (HAVE_OPT(PASSWD)) { if (HAVE_OPT(PASSWD)) {
@@ -308,22 +309,23 @@ int main(int argc, char **argv)
else { else {
groupname = "*"; groupname = "*";
} }
if (HAVE_OPT(LOCK)) if (HAVE_OPT(LOCK))
lock_user(fpasswd, username); lock_user(fpasswd, username);
else if (HAVE_OPT(UNLOCK)) else if (HAVE_OPT(UNLOCK))
unlock_user(fpasswd, username); unlock_user(fpasswd, username);
else { /* set password */ else { /* set password */
passwd = getpass("Enter password: ");
if (passwd == NULL) {
fprintf(stderr, "Please specify a password\n");
exit(1);
}
if (isatty(STDIN_FILENO)) { if (isatty(STDIN_FILENO)) {
char* p2; char* p2;
passwd = getpass("Enter password: ");
if (passwd == NULL) {
fprintf(stderr, "Please specify a password\n");
exit(1);
}
p2 = strdup(passwd); p2 = strdup(passwd);
passwd = getpass("Re-enter password: "); passwd = getpass("Re-enter password: ");
if (passwd == NULL) { if (passwd == NULL) {
@@ -336,11 +338,21 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
free(p2); free(p2);
} else {
passwd = NULL;
l = getline(&passwd, &i, stdin);
if (l <= 1) {
fprintf(stderr, "Please specify a password\n");
exit(1);
}
if (passwd[l-1] == '\n')
passwd[l-1] = 0;
} }
crypt_int(fpasswd, username, groupname, passwd); crypt_int(fpasswd, username, groupname, passwd);
} }
gnutls_global_deinit(); gnutls_global_deinit();
return 0; return 0;
} }