use getarg

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@5817 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
1999-04-01 14:53:10 +00:00
parent 4f3d9ec5ab
commit 5f6d5c8c09
2 changed files with 284 additions and 302 deletions

View File

@@ -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).
* All rights reserved.
*
@@ -37,39 +37,39 @@
*/
#include "otp_locl.h"
#include <getarg.h>
RCSID("$Id$");
#define USAGE_STRING \
"Usage: %s [-r] [-f alg] [-u user] num seed\n" \
" or -[d|l|o] [-u user]\n" \
" or -h\n"
static int listp;
static int deletep;
static int openp;
static int renewp;
static char* alg_string;
static char *user;
static int version_flag;
static int help_flag;
#define HELP_STRING \
"This program sets, renews, deletes or lists one-time passwords (OTP)\n" \
"\tdefault: set directly OTP\n" \
"\t-r: renew securely OTP\n" \
"\t-d: delete OTP\n" \
"\t-l: list OTP status\n" \
"\t-h: help!\n" \
"\t-o: open up the locked OTP\n" \
"\t-u user: specify a user, default is the current user.\n" \
"\t only root can use this option.\n" \
"\t-f alg: encryption algorithm (md4|md5|sha), default is md5.\n" \
"\tnum seed: number of iterations and seed for OTP\n"
struct getargs args[] = {
{ "list", 'l', arg_flag, &listp, "list OTP status" },
{ "delete", 'd', arg_flag, &deletep, "delete OTP" },
{ "open", 'o', arg_flag, &openp, "open a locked OTP" },
{ "renew", 'r', arg_flag, &renewp, "securely renew OTP" },
{ "hash", 'f', arg_string, &alg_string,
"hash algorithm (md4, md5, or sha)", "algorithm"},
{ "user", 'u', arg_string, &user,
"user other than current user (root only)", "user" },
{ "version", 0, arg_flag, &version_flag },
{ "help", 'h', arg_flag, &help_flag }
};
int num_args = sizeof(args) / sizeof(args[0]);
static void
help (void)
usage(int code)
{
fprintf(stderr, USAGE_STRING HELP_STRING, __progname);
exit (0);
}
static void
usage (void)
{
fprintf(stderr, USAGE_STRING, __progname);
exit (1);
arg_printusage(args, num_args, NULL, "[num seed]");
exit(code);
}
/*
@@ -86,9 +86,6 @@ renew (int argc, char **argv, OtpAlgorithm *alg, char *user)
void *dbm;
int ret;
if (argc != 2)
usage();
newctx.alg = alg;
newctx.user = user;
newctx.n = atoi (argv[0]);
@@ -151,9 +148,6 @@ set (int argc, char **argv, OtpAlgorithm *alg, char *user)
int ret;
int i;
if (argc != 2)
usage();
ctx.alg = alg;
ctx.user = strdup (user);
if (ctx.user == NULL)
@@ -194,9 +188,6 @@ delete_otp (int argc, char **argv, char *user)
OtpContext ctx;
int ret;
if (argc != 0)
usage();
db = otp_db_open ();
if(db == NULL)
errx (1, "otp_db_open failed");
@@ -261,9 +252,6 @@ open_otp (int argc, char **argv, char *user)
OtpContext ctx;
int ret;
if (argc != 0)
usage ();
db = otp_db_open ();
if (db == NULL)
errx (1, "otp_db_open failed");
@@ -286,9 +274,6 @@ list_otps (int argc, char **argv, char *user)
void *db;
struct passwd *pw;
if (argc != 0)
usage();
db = otp_db_open ();
if(db == NULL)
errx (1, "otp_db_open failed");
@@ -307,47 +292,30 @@ list_otps (int argc, char **argv, char *user)
int
main (int argc, char **argv)
{
int c;
int renewp = 0, listp = 0, deletep = 0, defaultp = 0, openp = 0;
int defaultp = 0;
int uid = getuid();
OtpAlgorithm *alg = otp_find_alg (OTP_ALG_DEFAULT);
char *user = NULL;
int optind = 0;
set_progname (argv[0]);
while ((c = getopt (argc, argv, "hrf:u:ldo")) != EOF)
switch (c) {
case 'h' :
help();
break;
case 'l' :
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(getarg(args, num_args, argc, argv, &optind))
usage(1);
if(help_flag)
usage(0);
if(version_flag) {
print_version(NULL);
exit(0);
}
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;
argv += optind;
@@ -355,8 +323,15 @@ main (int argc, char **argv)
defaultp = 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)
return list_otps (argc, argv, user);

View File

@@ -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).
* All rights reserved.
*
@@ -37,16 +37,34 @@
*/
#include "otp_locl.h"
#include <getarg.h>
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
usage (void)
usage(int code)
{
fprintf(stderr,
"Usage: %s [-e] [-h] [-n count] [-f alg] num seed\n",
__progname);
exit (1);
arg_printusage(args, num_args, NULL, "num seed");
exit(code);
}
static int
@@ -63,7 +81,7 @@ print (int argc,
char *seed;
if (argc != 2)
usage ();
usage (1);
n = atoi(argv[0]);
seed = argv[1];
if (des_read_pw_string (pw, sizeof(pw), "Pass-phrase: ", 0))
@@ -84,36 +102,25 @@ print (int argc,
int
main (int argc, char **argv)
{
int c;
int count = 10;
int hexp = 0;
int extendedp = 0;
int optind = 0;
void (*fn)(OtpKey, char *, size_t);
OtpAlgorithm *alg = otp_find_alg (OTP_ALG_DEFAULT);
set_progname (argv[0]);
while ((c = getopt (argc, argv, "ehn:f:")) != EOF)
switch (c) {
case 'e' :
extendedp = 1;
break;
case 'n' :
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(getarg(args, num_args, argc, argv, &optind))
usage(1);
if(help_flag)
usage(0);
if(version_flag) {
print_version(NULL);
exit(0);
}
if(alg_string) {
alg = otp_find_alg (alg_string);
if (alg == NULL)
errx(1, "Unknown algorithm: %s", alg_string);
}
argc -= optind;
argv += optind;