-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm.c
138 lines (117 loc) · 2.74 KB
/
pwm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "def.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
static const char *usage_str =
"pwm [<opts>] <cmd> [<args>]\n\n"
"options:\n"
" -d <path> database directory\n"
" -h show this help\n"
" -k <key> gpg key id\n"
" -v show version";
typedef struct {
char *cmd;
char *bin;
char *help;
} cmd_t;
static const cmd_t cmds[] = {
{"del", PACKAGE_EXEC_DIR"/pwm-del", "delete a password"},
{"gen", PACKAGE_EXEC_DIR"/pwm-gen", "generate a password"},
{"get", PACKAGE_EXEC_DIR"/pwm-get", "retrieve a password"},
{"list", PACKAGE_EXEC_DIR"/pwm-list", "list all passwords"},
{"log", PACKAGE_EXEC_DIR"/pwm-log", "print the log"},
{"note", PACKAGE_EXEC_DIR"/pwm-note", "get, set or del a password note"},
{"set", PACKAGE_EXEC_DIR"/pwm-set", "set a password"}
};
typedef struct {
char *home;
char *key_id;
} opts_t;
#define OPTS_DEFAULT {NULL,NULL}
static void
usage() {
size_t i;
fprintf(stderr, "usage: %s\n\ncommands:\n", usage_str);
for (i = 0; i < (sizeof(cmds)/sizeof(cmd_t)); i++) {
fprintf(stderr, " %-14s %s\n", cmds[i].cmd, cmds[i].help);
}
exit(EXIT_FAILURE);
}
static void
version() {
fprintf(stderr, "pwm %s\n", PACKAGE_VERSION);
exit(EXIT_SUCCESS);
}
static const cmd_t *
cmd_find(const char *cmd) {
size_t i;
for (i = 0; i < (sizeof(cmds)/sizeof(cmd_t)); i++) {
if (strcmp(cmd, cmds[i].cmd) == 0) {
return &cmds[i];
}
}
return NULL;
}
static int
cmd_exec(const cmd_t *cmd, char **argv) {
pid_t pid;
int rc;
if ((pid = fork()) < 0) {
perror("fork");
return 1;
}
if (pid == 0) {
argv[0] = basename(cmd->bin);
execv(cmd->bin, argv);
perror("execv");
exit(EXIT_FAILURE);
}
if (waitpid(pid, &rc, 0) < 0) {
perror("waitpid");
return 1;
}
return WIFEXITED(rc) ? WEXITSTATUS(rc) : 1;
}
int
main(int argc, char **argv) {
opts_t opts = OPTS_DEFAULT;
const cmd_t *cmd;
char c;
while ((c = getopt(argc, argv, "+:d:hk:v")) > -1) {
switch (c) {
case 'd':
opts.home = optarg;
break;
case 'k':
opts.key_id = optarg;
break;
case 'v':
version();
break;
default:
usage();
}
}
if (optind >= argc) {
usage();
}
if (opts.home != NULL && setenv(PWM_HOME_ENV_VAR, opts.home, 1) < 0) {
perror("setenv");
exit(EXIT_FAILURE);
}
if (opts.key_id != NULL && setenv(PWM_KEY_ID_ENV_VAR, opts.key_id, 1) < 0) {
perror("setenv");
exit(EXIT_FAILURE);
}
if ((cmd = cmd_find(argv[optind])) == NULL) {
usage();
}
exit(cmd_exec(cmd, &argv[optind]));
}