This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlpcli.c
342 lines (303 loc) · 6.99 KB
/
lpcli.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define LP_STATIC
#define LP_IMPLEMENTATION
#include "lp.h"
#include "lpcli.h"
#define ERRORS_X \
X(NONE, "Not an error") \
X(OPTION, "Unrecognized or incorrect options specified") \
X(VALUE, "Cannot set %s value to %i") \
X(PASSWORD, "Failed to read the password") \
X(GENERATE, "LP_generate returned error code %i") \
X(CLIPBOARD, "Cannot copy to clipboard") \
// ERR_XXX
#define X(A, B) ERR_##A,
enum {ERRORS_X};
#undef X
// "XXX\n"
#define X(A, B) B "\n",
static const char *errstr[] = {ERRORS_X};
#undef X
#undef ERRORS_X
int print_usage(void)
{
fprintf(stderr,
"Usage: lpcli <site> [login] [options]" "\n"
"Options:" "\n"
" --lowercase, -l include lowercase characters" "\n"
" --uppercase, -u include uppercase characters" "\n"
" --digits, -d include digits" "\n"
" --symbols, -s include symbols" "\n"
"\n"
" --length, -n number of characters (default 16)" "\n"
" --counter, -c number to add to salt (default 1)" "\n"
"\n"
" --print, -p print instead of copying to clipboard." "\n"
#ifdef USE_XCLIP
" xclip is required to copy to clipboard." "\n"
#endif
);
fflush(stderr);
return LPCLI_FAIL;
}
int print_error(const char *format, ...)
{
fputs("Error: ", stderr);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
fflush(stderr);
return LPCLI_FAIL;
}
//#define PRINT_ERROR(X,...) print_error(errstr[ERR_##A],__VA_ARGS__)
typedef struct parsed_args
{
const char *site;
const char *login;
const char *password;
unsigned charsets;
unsigned length;
unsigned counter;
unsigned flags;
} LPCLI_OPTS;
//option flags
enum
{
OPTS_CHARSETS = 0x01,
OPTS_LENGTH = 0x02,
OPTS_COUNTER = 0x04,
OPTS_PRINT = 0x08,
};
static bool is_option_set(LPCLI_OPTS *opts, unsigned flag)
{
return (opts->flags & flag) ? true : false;
}
static void set_opt_length(LPCLI_OPTS *opts, unsigned value)
{
opts->flags |= OPTS_LENGTH;
opts->length = value;
}
static void set_opt_counter(LPCLI_OPTS *opts, unsigned value)
{
opts->flags |= OPTS_COUNTER;
opts->counter = value;
}
static void set_opt_charsets(LPCLI_OPTS *opts, unsigned flag)
{
opts->flags |= OPTS_CHARSETS;
opts->charsets |= flag;
}
static int read_args(int argc, char **argv, LPCLI_OPTS *opts)
{
if (argc < 2)
return LPCLI_FAIL;
int i = 0;
char *ptr;
char *ptrEnd;
ptr = argv[++i];
opts->site = ptr;
ptr = argv[++i]; // next
opts->login = "";
if (ptr && *ptr != '-')
{
opts->login = ptr;
ptr = argv[++i]; // next
}
//if (ptr && *ptr != '-')
//{
// opts->password = ptr;
// set_option(opts, OPTS_PASSWORD);
// ptr = argv[++i]; // next
//}
bool post_shopt = false; // is it just after a short option?
while (argv[i])
{
if (!post_shopt)
{
if (*ptr != '-')
return LPCLI_FAIL;
ptr++;
}
switch (*ptr)
{
case '\0': // -
if (!post_shopt)
return LPCLI_FAIL;
post_shopt = false; //stop short option reading
ptr = argv[++i];
break;
case '-': // --
if (post_shopt)
return LPCLI_FAIL;
ptr++;
if (strcmp(ptr, "lowercase") == 0)
{
set_opt_charsets(opts, LP_CSF_LOWERCASE);
}
else if (strcmp(ptr, "uppercase") == 0)
{
set_opt_charsets(opts, LP_CSF_UPPERCASE);
}
else if (strcmp(ptr, "digits") == 0)
{
set_opt_charsets(opts, LP_CSF_DIGITS);
}
else if (strcmp(ptr, "symbols") == 0)
{
set_opt_charsets(opts, LP_CSF_SYMBOLS);
}
else if (strcmp(ptr, "print") == 0)
{
opts->flags |= OPTS_PRINT;
}
else if (strcmp(ptr, "length") == 0)
{
if (!(ptr = argv[++i]))
return LPCLI_FAIL; //next!=null
set_opt_length(opts, strtol(ptr, &ptrEnd, 10)); //FIXME: handle negative?
if (*ptrEnd != '\0')
return LPCLI_FAIL;
}
else if (strcmp(ptr, "counter") == 0)
{
if (!(ptr = argv[++i]))
return LPCLI_FAIL; //next!=null
set_opt_counter(opts, strtol(ptr, &ptrEnd, 10));
if (*ptrEnd != '\0')
return LPCLI_FAIL;
}
else
{
return LPCLI_FAIL;
}
ptr = argv[++i]; //next
break;
case 'n':
ptr++;
if (*ptr == '\0' && !(ptr = argv[++i]))
return LPCLI_FAIL;
set_opt_length(opts, strtol(ptr, &ptrEnd, 10));
ptr = ptrEnd;
post_shopt = true;
break;
case 'c':
ptr++;
if (*ptr == '\0' && !(ptr = argv[++i]))
return LPCLI_FAIL;
set_opt_counter(opts, strtol(ptr, &ptrEnd, 10));
ptr = ptrEnd;
post_shopt = true;
break;
case 'l':
set_opt_charsets(opts, LP_CSF_LOWERCASE);
ptr++;
post_shopt = true;
break;
case 'u':
set_opt_charsets(opts, LP_CSF_UPPERCASE);
ptr++;
post_shopt = true;
break;
case 'd':
set_opt_charsets(opts, LP_CSF_DIGITS);
ptr++;
post_shopt = true;
break;
case 's':
set_opt_charsets(opts, LP_CSF_SYMBOLS);
ptr++;
post_shopt = true;
break;
case 'p':
opts->flags |= OPTS_PRINT;
ptr++;
post_shopt = true;
break;
default:
return LPCLI_FAIL;
}
}
return LPCLI_OK;
}
void print_options(LP_CTX *t)
{
printf("Options: -");
if (t->charsets & LP_CSF_LOWERCASE)
{ printf("l"); }
if (t->charsets & LP_CSF_UPPERCASE)
{ printf("u"); }
if (t->charsets & LP_CSF_DIGITS)
{ printf("d"); }
if (t->charsets & LP_CSF_SYMBOLS)
{ printf("s"); }
printf("c%u", t->counter);
printf("n%u", t->length);
printf("\n");
}
#ifndef zeromem
#define zeromem(dst,len) lpcli_zeromemory(dst,len)
#endif
int lpcli_main(int argc, char **argv)
{
if (argc == 1 || (argc == 2 && (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0)))
{
return print_usage();
}
LPCLI_OPTS options = {0};
if (read_args(argc, argv, &options) != LPCLI_OK)
{
return print_error(errstr[ERR_OPTION]);
}
LP_CTX ctx;
LP_CTX_init(&ctx);
if (is_option_set(&options, OPTS_CHARSETS))
{
ctx.charsets = options.charsets;
}
if (is_option_set(&options, OPTS_LENGTH))
{
if (!LP_check_length(options.length))
{
return print_error(errstr[ERR_VALUE], "length", options.length);
}
ctx.length = options.length;
}
if (is_option_set(&options, OPTS_COUNTER))
{
if (!LP_check_counter(options.counter))
{
return print_error(errstr[ERR_VALUE], "counter", options.counter);
}
ctx.counter = options.counter;
}
print_options(&ctx);
char passwd_in[LPMAXSTRLEN];
if (lpcli_readpassword("Enter Password: ", passwd_in, sizeof passwd_in) != LPCLI_OK)
{
return print_error(errstr[ERR_PASSWORD]);
}
int ret = LP_generate(&ctx, options.site, options.login, (const char *) passwd_in);
zeromem(passwd_in, sizeof passwd_in); // clean password read
if (ret < 1)
{
// zeromem(&ctx, sizeof ctx); //no need
return print_error(errstr[ERR_GENERATE], ret);
}
bool clipboardcopy = is_option_set(&options, OPTS_PRINT) ? false : true;
if (clipboardcopy)
{
if (lpcli_clipboardcopy(ctx.buffer) != LPCLI_OK)
{ return print_error(errstr[ERR_CLIPBOARD]); }
}
else
{
printf("%s\n", ctx.buffer);
}
zeromem(&ctx, sizeof ctx); // clean generated password
return LPCLI_OK;
}