示例#1
0
文件: pktool.c 项目: alhazred/onarm
/*
 * MAIN() -- where all the action is
 */
int
main(int argc, char *argv[], char *envp[])
/* ARGSUSED2 */
{
	int	i, found = -1;
	int	rv;
	int	pk_argc = 0;
	char	**pk_argv = NULL;
	int	save_errno = 0;

	/* Set up for i18n/l10n. */
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
#define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
#endif
	(void) textdomain(TEXT_DOMAIN);

	init_command_list();

	/* Get program base name and move pointer over 0th arg. */
	prog = basename(argv[0]);
	argv++, argc--;

	/* Set up for debug and error output. */
	if (argc == 0) {
		usage(-1);
		return (1);
	}

	/* Check for help options.  For CLIP-compliance. */
	if (strcmp(argv[0], "-?") == 0) {
		return (pk_help(argc, argv));
	} else if (strcmp(argv[0], "-f") == 0 && argc == 2) {
		rv = process_arg_file(argv[1], &pk_argv, &pk_argc);
		if (rv)
			return (rv);
	} else if (argc >= 1 && argv[0][0] == '-') {
		usage(-1);
		return (1);
	}

	/* Always turns off Metaslot so that we can see softtoken. */
	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
		save_errno = errno;
		cryptoerror(LOG_STDERR,
		    gettext("Disabling Metaslot failed (%s)."),
		    strerror(save_errno));
		return (1);
	}

	/* Begin parsing command line. */
	if (pk_argc == 0 && pk_argv == NULL) {
		pk_argc = argc;
		pk_argv = argv;
	}

	/* Check for valid verb (or an abbreviation of it). */
	found = -1;
	for (i = 0; i < num_cmds; i++) {
		if (strcmp(cmds[i].verb, pk_argv[0]) == 0) {
			if (found < 0) {
				found = i;
				break;
			}
		}
	}
	/* Stop here if no valid verb found. */
	if (found < 0) {
		cryptoerror(LOG_STDERR, gettext("Invalid verb: %s"),
		    pk_argv[0]);
		return (1);
	}

	/* Get to work! */
	rv = (*cmds[found].action)(pk_argc, pk_argv);
	switch (rv) {
	case PK_ERR_NONE:
		break;		/* Command succeeded, do nothing. */
	case PK_ERR_USAGE:
		usage(found);
		break;
	case PK_ERR_QUIT:
		exit(0);
		/* NOTREACHED */
	case PK_ERR_PK11:
	case PK_ERR_SYSTEM:
	case PK_ERR_OPENSSL:
	case PK_ERR_NSS:
	default:
		break;
	}
	return (rv);
}
示例#2
0
文件: pktool.c 项目: andreiw/polaris
/*
 * MAIN() -- where all the action is
 */
int
main(int argc, char *argv[], char *envp[])
/* ARGSUSED2 */
{
	int	i, found = -1;
	int	rv;
	int	pk_argc = 0;
	char	**pk_argv = NULL;
	int	save_errno = 0;

	/* Set up for i18n/l10n. */
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D. */
#define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it isn't. */
#endif
	(void) textdomain(TEXT_DOMAIN);

	/* Get program base name and move pointer over 0th arg. */
	prog = basename(argv[0]);
	argv++, argc--;

	/* Set up for debug and error output. */
	cryptodebug_init(prog);

	if (argc == 0) {
		usage();
		return (1);
	}

	/* Check for help options.  For CLIP-compliance. */
	if (argc == 1 && argv[0][0] == '-') {
		switch (argv[0][1]) {
		case '?':
			return (pk_help(argc, argv));
		default:
			usage();
			return (1);
		}
	}

	/* Always turns off Metaslot so that we can see softtoken. */
	cryptodebug("disabling Metaslot");
	if (setenv("METASLOT_ENABLED", "false", 1) < 0) {
		save_errno = errno;
		cryptoerror(LOG_STDERR,
		    gettext("Disabling Metaslot failed (%s)."),
		    strerror(save_errno));
		return (1);
	}

	/* Begin parsing command line. */
	cryptodebug("begin parsing command line");
	pk_argc = argc;
	pk_argv = argv;

	/* Check for valid verb (or an abbreviation of it). */
	found = -1;
	for (i = 0; i < num_cmds; i++) {
		if (strcmp(cmds[i].verb, pk_argv[0]) == 0) {
			if (found < 0) {
				cryptodebug("found cmd %s", cmds[i].verb);
				found = i;
				break;
			} else {
				cryptodebug("also found cmd %s, skipping",
				    cmds[i].verb);
			}
		}
	}
	/* Stop here if no valid verb found. */
	if (found < 0) {
		cryptoerror(LOG_STDERR, gettext("Invalid verb: %s"),
		    pk_argv[0]);
		return (1);
	}

	/* Get to work! */
	cryptodebug("begin executing cmd action");
	rv = (*cmds[found].action)(pk_argc, pk_argv);
	cryptodebug("end executing cmd action");
	switch (rv) {
	case PK_ERR_NONE:
		cryptodebug("subcommand succeeded");
		break;		/* Command succeeded, do nothing. */
	case PK_ERR_USAGE:
		cryptodebug("usage error detected");
		usage();
		break;
	case PK_ERR_QUIT:
		cryptodebug("quit command received");
		exit(0);
		/* NOTREACHED */
	case PK_ERR_PK11:
		cryptoerror(LOG_STDERR, "%s",
		    gettext("Command failed due to PKCS#11 error."));
		break;
	case PK_ERR_SYSTEM:
		cryptoerror(LOG_STDERR, "%s",
		    gettext("Command failed due to system error."));
		break;
	case PK_ERR_OPENSSL:
		cryptoerror(LOG_STDERR, "%s",
		    gettext("Command failed due to OpenSSL error."));
		break;
	default:
		cryptoerror(LOG_STDERR, "%s (%d).",
		    gettext("Unknown error value"), rv);
		break;
	}
	return (rv);
}