use getarg
git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@5817 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
133
appl/otp/otp.c
133
appl/otp/otp.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1995, 1996, 1997 Kungliga Tekniska H<>gskolan
|
* Copyright (c) 1995-1997, 1999 Kungliga Tekniska H<>gskolan
|
||||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
@@ -37,39 +37,39 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "otp_locl.h"
|
#include "otp_locl.h"
|
||||||
|
#include <getarg.h>
|
||||||
|
|
||||||
RCSID("$Id$");
|
RCSID("$Id$");
|
||||||
|
|
||||||
#define USAGE_STRING \
|
static int listp;
|
||||||
"Usage: %s [-r] [-f alg] [-u user] num seed\n" \
|
static int deletep;
|
||||||
" or -[d|l|o] [-u user]\n" \
|
static int openp;
|
||||||
" or -h\n"
|
static int renewp;
|
||||||
|
static char* alg_string;
|
||||||
|
static char *user;
|
||||||
|
static int version_flag;
|
||||||
|
static int help_flag;
|
||||||
|
|
||||||
#define HELP_STRING \
|
struct getargs args[] = {
|
||||||
"This program sets, renews, deletes or lists one-time passwords (OTP)\n" \
|
{ "list", 'l', arg_flag, &listp, "list OTP status" },
|
||||||
"\tdefault: set directly OTP\n" \
|
{ "delete", 'd', arg_flag, &deletep, "delete OTP" },
|
||||||
"\t-r: renew securely OTP\n" \
|
{ "open", 'o', arg_flag, &openp, "open a locked OTP" },
|
||||||
"\t-d: delete OTP\n" \
|
{ "renew", 'r', arg_flag, &renewp, "securely renew OTP" },
|
||||||
"\t-l: list OTP status\n" \
|
{ "hash", 'f', arg_string, &alg_string,
|
||||||
"\t-h: help!\n" \
|
"hash algorithm (md4, md5, or sha)", "algorithm"},
|
||||||
"\t-o: open up the locked OTP\n" \
|
{ "user", 'u', arg_string, &user,
|
||||||
"\t-u user: specify a user, default is the current user.\n" \
|
"user other than current user (root only)", "user" },
|
||||||
"\t only root can use this option.\n" \
|
{ "version", 0, arg_flag, &version_flag },
|
||||||
"\t-f alg: encryption algorithm (md4|md5|sha), default is md5.\n" \
|
{ "help", 'h', arg_flag, &help_flag }
|
||||||
"\tnum seed: number of iterations and seed for OTP\n"
|
};
|
||||||
|
|
||||||
|
int num_args = sizeof(args) / sizeof(args[0]);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
help (void)
|
usage(int code)
|
||||||
{
|
{
|
||||||
fprintf(stderr, USAGE_STRING HELP_STRING, __progname);
|
arg_printusage(args, num_args, NULL, "[num seed]");
|
||||||
exit (0);
|
exit(code);
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
usage (void)
|
|
||||||
{
|
|
||||||
fprintf(stderr, USAGE_STRING, __progname);
|
|
||||||
exit (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -86,9 +86,6 @@ renew (int argc, char **argv, OtpAlgorithm *alg, char *user)
|
|||||||
void *dbm;
|
void *dbm;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
usage();
|
|
||||||
|
|
||||||
newctx.alg = alg;
|
newctx.alg = alg;
|
||||||
newctx.user = user;
|
newctx.user = user;
|
||||||
newctx.n = atoi (argv[0]);
|
newctx.n = atoi (argv[0]);
|
||||||
@@ -151,9 +148,6 @@ set (int argc, char **argv, OtpAlgorithm *alg, char *user)
|
|||||||
int ret;
|
int ret;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (argc != 2)
|
|
||||||
usage();
|
|
||||||
|
|
||||||
ctx.alg = alg;
|
ctx.alg = alg;
|
||||||
ctx.user = strdup (user);
|
ctx.user = strdup (user);
|
||||||
if (ctx.user == NULL)
|
if (ctx.user == NULL)
|
||||||
@@ -194,9 +188,6 @@ delete_otp (int argc, char **argv, char *user)
|
|||||||
OtpContext ctx;
|
OtpContext ctx;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (argc != 0)
|
|
||||||
usage();
|
|
||||||
|
|
||||||
db = otp_db_open ();
|
db = otp_db_open ();
|
||||||
if(db == NULL)
|
if(db == NULL)
|
||||||
errx (1, "otp_db_open failed");
|
errx (1, "otp_db_open failed");
|
||||||
@@ -261,9 +252,6 @@ open_otp (int argc, char **argv, char *user)
|
|||||||
OtpContext ctx;
|
OtpContext ctx;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (argc != 0)
|
|
||||||
usage ();
|
|
||||||
|
|
||||||
db = otp_db_open ();
|
db = otp_db_open ();
|
||||||
if (db == NULL)
|
if (db == NULL)
|
||||||
errx (1, "otp_db_open failed");
|
errx (1, "otp_db_open failed");
|
||||||
@@ -286,9 +274,6 @@ list_otps (int argc, char **argv, char *user)
|
|||||||
void *db;
|
void *db;
|
||||||
struct passwd *pw;
|
struct passwd *pw;
|
||||||
|
|
||||||
if (argc != 0)
|
|
||||||
usage();
|
|
||||||
|
|
||||||
db = otp_db_open ();
|
db = otp_db_open ();
|
||||||
if(db == NULL)
|
if(db == NULL)
|
||||||
errx (1, "otp_db_open failed");
|
errx (1, "otp_db_open failed");
|
||||||
@@ -307,47 +292,30 @@ list_otps (int argc, char **argv, char *user)
|
|||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c;
|
int defaultp = 0;
|
||||||
int renewp = 0, listp = 0, deletep = 0, defaultp = 0, openp = 0;
|
|
||||||
int uid = getuid();
|
int uid = getuid();
|
||||||
OtpAlgorithm *alg = otp_find_alg (OTP_ALG_DEFAULT);
|
OtpAlgorithm *alg = otp_find_alg (OTP_ALG_DEFAULT);
|
||||||
char *user = NULL;
|
int optind = 0;
|
||||||
|
|
||||||
set_progname (argv[0]);
|
set_progname (argv[0]);
|
||||||
|
if(getarg(args, num_args, argc, argv, &optind))
|
||||||
while ((c = getopt (argc, argv, "hrf:u:ldo")) != EOF)
|
usage(1);
|
||||||
switch (c) {
|
if(help_flag)
|
||||||
case 'h' :
|
usage(0);
|
||||||
help();
|
if(version_flag) {
|
||||||
break;
|
print_version(NULL);
|
||||||
case 'l' :
|
exit(0);
|
||||||
listp = 1;
|
|
||||||
break;
|
|
||||||
case 'd' :
|
|
||||||
if (uid != 0)
|
|
||||||
errx (1, "Only root can delete OTPs");
|
|
||||||
deletep = 1;
|
|
||||||
break;
|
|
||||||
case 'o':
|
|
||||||
openp = 1;
|
|
||||||
break;
|
|
||||||
case 'r' :
|
|
||||||
renewp = 1;
|
|
||||||
break;
|
|
||||||
case 'f' :
|
|
||||||
alg = otp_find_alg (optarg);
|
|
||||||
if (alg == NULL)
|
|
||||||
errx (1, "Unknown algorithm: %s", optarg);
|
|
||||||
break;
|
|
||||||
case 'u' :
|
|
||||||
if (uid != 0)
|
|
||||||
errx (1, "Only root can use `-u'");
|
|
||||||
user = optarg;
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
usage ();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(deletep && uid != 0)
|
||||||
|
errx (1, "Only root can delete OTPs");
|
||||||
|
if(alg_string) {
|
||||||
|
alg = otp_find_alg (alg_string);
|
||||||
|
if (alg == NULL)
|
||||||
|
errx (1, "Unknown algorithm: %s", alg_string);
|
||||||
|
}
|
||||||
|
if (user && uid != 0)
|
||||||
|
errx (1, "Only root can use `-u'");
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
@@ -355,8 +323,15 @@ main (int argc, char **argv)
|
|||||||
defaultp = 1;
|
defaultp = 1;
|
||||||
|
|
||||||
if ( listp + deletep + renewp + defaultp + openp != 1)
|
if ( listp + deletep + renewp + defaultp + openp != 1)
|
||||||
usage(); /* one of -d or -l or -r or none */
|
usage(1); /* one of -d or -l or -r or none */
|
||||||
|
|
||||||
|
if(deletep || openp || listp) {
|
||||||
|
if(argc != 0)
|
||||||
|
errx(1, "delete, open, and list requires no arguments\n");
|
||||||
|
} else {
|
||||||
|
if(argc != 2)
|
||||||
|
errx(1, "setup, and renew requires `num', and `seed'");
|
||||||
|
}
|
||||||
if (listp)
|
if (listp)
|
||||||
return list_otps (argc, argv, user);
|
return list_otps (argc, argv, user);
|
||||||
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 1995, 1996, 1997, 1998 Kungliga Tekniska H<>gskolan
|
* Copyright (c) 1995-1999 Kungliga Tekniska H<>gskolan
|
||||||
* (Royal Institute of Technology, Stockholm, Sweden).
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
@@ -37,16 +37,34 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "otp_locl.h"
|
#include "otp_locl.h"
|
||||||
|
#include <getarg.h>
|
||||||
|
|
||||||
RCSID("$Id$");
|
RCSID("$Id$");
|
||||||
|
|
||||||
|
static int extendedp;
|
||||||
|
static int count = 10;
|
||||||
|
static int hexp;
|
||||||
|
static char* alg_string;
|
||||||
|
static int version_flag;
|
||||||
|
static int help_flag;
|
||||||
|
|
||||||
|
struct getargs args[] = {
|
||||||
|
{ "extended", 'e', arg_flag, &extendedp, "print keys in extended format" },
|
||||||
|
{ "count", 'n', arg_integer, &count, "number of keys to print" },
|
||||||
|
{ "hexadecimal", 'h', arg_flag, &hexp, "output in hexadecimal" },
|
||||||
|
{ "hash", 'f', arg_string, &alg_string,
|
||||||
|
"hash algorithm (md4, md5, or sha)", "algorithm"},
|
||||||
|
{ "version", 0, arg_flag, &version_flag },
|
||||||
|
{ "help", 0, arg_flag, &help_flag }
|
||||||
|
};
|
||||||
|
|
||||||
|
int num_args = sizeof(args) / sizeof(args[0]);
|
||||||
|
|
||||||
static void
|
static void
|
||||||
usage (void)
|
usage(int code)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
arg_printusage(args, num_args, NULL, "num seed");
|
||||||
"Usage: %s [-e] [-h] [-n count] [-f alg] num seed\n",
|
exit(code);
|
||||||
__progname);
|
|
||||||
exit (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -63,7 +81,7 @@ print (int argc,
|
|||||||
char *seed;
|
char *seed;
|
||||||
|
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
usage ();
|
usage (1);
|
||||||
n = atoi(argv[0]);
|
n = atoi(argv[0]);
|
||||||
seed = argv[1];
|
seed = argv[1];
|
||||||
if (des_read_pw_string (pw, sizeof(pw), "Pass-phrase: ", 0))
|
if (des_read_pw_string (pw, sizeof(pw), "Pass-phrase: ", 0))
|
||||||
@@ -84,36 +102,25 @@ print (int argc,
|
|||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c;
|
int optind = 0;
|
||||||
int count = 10;
|
|
||||||
int hexp = 0;
|
|
||||||
int extendedp = 0;
|
|
||||||
void (*fn)(OtpKey, char *, size_t);
|
void (*fn)(OtpKey, char *, size_t);
|
||||||
OtpAlgorithm *alg = otp_find_alg (OTP_ALG_DEFAULT);
|
OtpAlgorithm *alg = otp_find_alg (OTP_ALG_DEFAULT);
|
||||||
|
|
||||||
set_progname (argv[0]);
|
set_progname (argv[0]);
|
||||||
|
if(getarg(args, num_args, argc, argv, &optind))
|
||||||
while ((c = getopt (argc, argv, "ehn:f:")) != EOF)
|
usage(1);
|
||||||
switch (c) {
|
if(help_flag)
|
||||||
case 'e' :
|
usage(0);
|
||||||
extendedp = 1;
|
if(version_flag) {
|
||||||
break;
|
print_version(NULL);
|
||||||
case 'n' :
|
exit(0);
|
||||||
count = atoi (optarg);
|
|
||||||
break;
|
|
||||||
case 'h' :
|
|
||||||
hexp = 1;
|
|
||||||
break;
|
|
||||||
case 'f' :
|
|
||||||
alg = otp_find_alg (optarg);
|
|
||||||
if (alg == NULL)
|
|
||||||
errx(1, "Unknown algorithm: %s", optarg);
|
|
||||||
break;
|
|
||||||
default :
|
|
||||||
usage ();
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(alg_string) {
|
||||||
|
alg = otp_find_alg (alg_string);
|
||||||
|
if (alg == NULL)
|
||||||
|
errx(1, "Unknown algorithm: %s", alg_string);
|
||||||
|
}
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
argv += optind;
|
argv += optind;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user