-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkcaldav.passwd.c
569 lines (470 loc) · 11.8 KB
/
kcaldav.passwd.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/param.h>
#include <assert.h>
#include <ctype.h>
#if HAVE_ERR
# include <err.h>
#endif
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#if HAVE_MD5
# include <md5.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_READPASSPHRASE
# include <readpassphrase.h>
#endif
#if !HAVE_ARC4RANDOM
# include <time.h> /* time(3) */
#endif
#include <unistd.h>
#include "libkcaldav.h"
#include "db.h"
static int verbose;
/*
* See http_safe_string() in util.c.
* This isn't included directly because it's in the kcaldav front-end
* library.
* It verifies non-empty strings against RFC 3986, section 3.3.
*/
static int
check_safe_string(const char *cp)
{
if (*cp == '\0')
return 0;
if (strcmp(cp, ".") == 0 || strcmp(cp, "..") == 0)
return 0;
for (; *cp != '\0'; cp++)
switch (*cp) {
case '.':
case '-':
case '_':
case '~':
case '!':
case '$':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
case ':':
case '@':
break;
default:
if (isalnum((unsigned char)*cp))
break;
return 0;
}
return 1;
}
/*
* Read the file "name" into the returned buffer.
* The file is guaranteed to be a proper iCalendar file.
*/
static char *
read_whole_file(const char *name)
{
int fd;
char *p = NULL;
char buf[BUFSIZ];
ssize_t ssz;
size_t sz = 0;
struct ical *ical = NULL;
/*
* Open the file readonly and read it in BUFSIZ at a time til
* we've read the whole file.
* NUL-terminate always, although we might read in NUL bytes
* (these are filtered in ical_parse()).
*/
if (-1 == (fd = open(name, O_RDONLY, 0)))
err(1, "%s", name);
while ((ssz = read(fd, buf, sizeof(buf))) > 0) {
p = realloc(p, sz + ssz + 1);
if (NULL == p)
err(1, NULL);
memcpy(&p[sz], buf, ssz);
p[sz + ssz] = '\0';
sz += ssz;
}
if (ssz < 0)
err(1, "%s", name);
close(fd);
fd = -1;
/* Try to parse as an iCalendar. */
if ((ical = ical_parse(name, p, sz, NULL, NULL)) == NULL)
errx(1, "%s: not an iCalendar file", name);
ical_free(ical);
return p;
}
/*
* Get a new password from the operator.
* Store this in "digest", which must be MD5_DIGEST_LENGTH*2+1 in length
* (as usual).
* Both the user and realm strings must be non-empty.
*/
static void
gethash(int new, char *digest,
const char *user, const char *realm)
{
char pbuf[BUFSIZ];
char *pp;
const char *phrase;
MD5_CTX ctx;
size_t i;
unsigned char ha[MD5_DIGEST_LENGTH];
switch (new) {
case 0:
phrase = "Old password: ";
break;
case 1:
phrase = "New password: ";
break;
case 2:
phrase = "Repeat new password: ";
break;
default:
abort();
}
/* Read in password. */
pp = readpassphrase(phrase, pbuf,
sizeof(pbuf), RPP_REQUIRE_TTY);
if (pp == NULL) {
explicit_bzero(pbuf, sizeof(pbuf));
errx(1, "unable to read passphrase");
} else if (pbuf[0] == '\0') {
explicit_bzero(pbuf, sizeof(pbuf));
exit(1);
}
if (strlen(pbuf) < 6) {
explicit_bzero(pbuf, sizeof(pbuf));
errx(1, "come on: more than five letters");
}
/* Hash according to RFC 2069's HA1. */
MD5Init(&ctx);
MD5Update(&ctx, (const uint8_t *)user, strlen(user));
MD5Update(&ctx, (const uint8_t *)":", 1);
MD5Update(&ctx, (const uint8_t *)realm, strlen(realm));
MD5Update(&ctx, (const uint8_t *)":", 1);
MD5Update(&ctx, (const uint8_t *)pbuf, strlen(pbuf));
MD5Final(ha, &ctx);
explicit_bzero(pbuf, sizeof(pbuf));
/* Convert to hex format. */
for (i = 0; i < MD5_DIGEST_LENGTH; i++)
snprintf(&digest[i * 2], 3, "%02x", ha[i]);
}
static void
db_msg_info(void *arg, const char *id, const char *fmt, va_list ap)
{
if (verbose >= 1) {
vprintf(fmt, ap);
putchar('\n');
}
}
static void
db_msg_err(void *arg, const char *id, const char *fmt, va_list ap)
{
if (verbose >= 1)
vwarn(fmt, ap);
}
static void
db_msg_errx(void *arg, const char *id, const char *fmt, va_list ap)
{
if (verbose >= 1)
vwarnx(fmt, ap);
}
int
main(int argc, char *argv[])
{
int c, adduser = 0, rc = 0, passwd = 1;
char dnew[MD5_DIGEST_LENGTH * 2 + 1],
drep[MD5_DIGEST_LENGTH * 2 + 1],
dold[MD5_DIGEST_LENGTH * 2 + 1];
const char *realm = KREALM, *altuser = NULL,
*dir = CALPREFIX, *email = NULL,
*coln = NULL, *uid, *cp;
size_t i, sz;
uid_t euid = geteuid();
gid_t egid = getegid();
struct prncpl *p = NULL;
struct coln *col;
char *user = NULL, *emailp = NULL, *res;
#if HAVE_PLEDGE
if (pledge("stdio rpath cpath wpath flock fattr tty id", NULL) == -1)
err(1, "pledge");
#endif
#if !HAVE_ARC4RANDOM
srandom(time(NULL));
#endif
/*
* Temporarily drop privileges.
* We don't want to have them until we're opening the password
* file itself.
*/
if (setegid(getgid()) == -1)
err(1, "setegid");
else if (seteuid(getuid()) == -1)
err(1, "seteuid");
while ((c = getopt(argc, argv, "Cd:e:f:nu:v")) != -1)
switch (c) {
case 'C':
adduser = 1;
break;
case 'd':
coln = optarg;
break;
case 'e':
email = optarg;
break;
case 'f':
dir = optarg;
break;
case 'n':
passwd = 0;
break;
case 'u':
altuser = optarg;
break;
case 'v':
verbose++;
break;
default:
goto usage;
}
argv += optind;
argc -= optind;
/* Override -n used with with -C. */
if (adduser)
passwd = 1;
/* Safety: check collection name. */
if (coln != NULL && !check_safe_string(coln))
errx(1, "%s: unsafe collection name", coln);
/* Safety: check resource names. */
for (i = 0; i < (size_t)argc; i++) {
if ((uid = strrchr(argv[i], '/')) == NULL)
uid = argv[i];
else
uid++;
if (!check_safe_string(uid))
errx(1, "%s: unsafe resource name", argv[i]);
}
/*
* Assign our principal name.
* This is either going to be given with -u (which is a
* privileged operation) or inherited from our login creds.
*/
if (altuser == NULL) {
if ((cp = getlogin()) == NULL)
err(1, "getlogin");
user = strdup(cp);
} else
user = strdup(altuser);
if (user == NULL)
err(1, NULL);
/* Safety: check user name. */
if (!check_safe_string(user))
errx(1, "%s: unsafe principal name", user);
/*
* If we're not creating a new entry and we're setting our own
* password, get the existing password.
*/
if (!adduser && altuser == NULL)
gethash(0, dold, user, realm);
/* If we're going to set our password, hash it now. */
if (passwd) {
gethash(1, dnew, user, realm);
gethash(2, drep, user, realm);
if (memcmp(dnew, drep, sizeof(dnew)))
errx(1, "passwords do not match");
}
/* Drop TTY pledge. */
#if HAVE_PLEDGE
if (pledge("stdio rpath cpath wpath flock fattr id", NULL) == -1)
err(1, "pledge");
#endif
/* Regain privileges. */
if (setgid(egid) == -1)
err(1, "setgid");
if (setuid(euid) == -1)
err(1, "setuid");
/*
* Open the database file itself.
* This is a privileged operation, hence occuring with our
* regained effective credentials.
* We only try to create the database if "adduser" is set.
*/
db_set_msg_dbg(db_msg_info);
db_set_msg_info(db_msg_info);
db_set_msg_err(db_msg_err);
db_set_msg_errx(db_msg_errx);
if (!db_init(dir, adduser))
errx(1, "failed to open database");
/* Now drop privileges entirely! */
if (setgid(getgid()) == -1)
err(1, "setgid");
if (setuid(getuid()) == -1)
err(1, "setuid");
/*
* Drop id-setting pledge.
* The rest of this is for database management and errors.
*/
#if HAVE_PLEDGE
if (pledge("stdio rpath cpath wpath flock fattr", NULL) == -1)
err(1, "pledge");
#endif
/*
* Check security of privileged operations.
* This will also create the database if the database has been
* created with "adduser" but doesn't exist yet.
*/
if (adduser || altuser != NULL) {
if ((c = db_owner_check_or_set(getuid())) == 0)
errx(1, "db owner does not match real user");
else if (c < 0)
errx(1, "failed check or set db owner");
}
/* Now either create or update the principal. */
if (adduser) {
/*
* Create e-mail by having our user followed by @
* followed by the system hostname.
* XXX: check if 256 for hostname is insufficient.
*/
if (email == NULL) {
sz = strlen(user) + 256 /* max hostname */ + 2;
if ((emailp = malloc(sz)) == NULL)
err(1, NULL);
strlcpy(emailp, user, sz);
strlcat(emailp, "@", sz);
gethostname
(emailp + strlen(user) + 1,
sz - strlen(user) - 1);
email = emailp;
}
c = db_prncpl_new(user, dnew, email,
coln == NULL ? "calendar" : coln);
if (c == 0)
errx(1, "%s: principal already exists", user);
else if (c < 0)
errx(1, "failed to create principal");
printf("principal created: %s\n", user);
} else {
if ((c = db_prncpl_load(&p, user)) == 0)
errx(1, "%s: principal does not exist", user);
else if (c < 0)
errx(1, "failed to load principal");
/*
* If we have a new collection, create it.
* Don't report if it already exists.
*/
if (coln != NULL) {
if ((c = db_collection_new(coln, p)) < 0)
errx(1, "failed to create collection");
else if (c > 0)
printf("collection added: %s\n", coln);
}
/* Does the hash match what we gave? */
if (altuser == NULL)
if (memcmp(p->hash, dold, sizeof(dold)))
errx(1, "password mismatch");
/* Set new e-mail, if applicable. */
if (email != NULL) {
free(p->email);
if ((p->email = strdup(email)) == NULL)
err(1, NULL);
}
/* Set new password (hash), if applicable. */
if (passwd) {
free(p->hash);
if ((p->hash = strdup(dnew)) == NULL)
err(1, NULL);
}
if ((c = db_prncpl_update(p)) == 0)
errx(1, "%s: e-mail already exists", p->email);
else if (c < 0)
errx(1, "failed to update principal");
printf("principal updated: %s\n", user);
}
/* If we have no resources on the command line, exit. */
if (argc == 0)
goto out;
/*
* We need to load some resources.
* First re-load our principal, which will have its new
* directories contained therein.
*/
db_prncpl_free(p);
p = NULL;
if ((c = db_prncpl_load(&p, user)) == 0)
errx(1, "%s: principal disappeared!?", user);
else if (c < 0)
errx(1, "failed to re-load principal");
/*
* Search for the new collection, defaulting to the standard
* calendar name if we didn't provide on.
*/
if (coln == NULL)
coln = "calendar";
for (col = NULL, i = 0; i < p->colsz; i++)
if (strcmp(p->cols[i].url, coln) == 0) {
col = &p->cols[i];
break;
}
if (col == NULL)
errx(1, "%s: collection disappeared!?", coln);
/*
* Now go through each file on the command line, load it, then
* push it into the collection.
*/
for (i = 0; i < (size_t)argc; i++) {
res = read_whole_file(argv[i]);
assert(res != NULL);
/* Get file-name component. */
if ((uid = strrchr(argv[i], '/')) == NULL)
uid = argv[i];
else
uid++;
rc = db_resource_new(res, uid, col->id);
free(res);
if (rc == 0)
errx(1, "%s: resource exists", argv[i]);
else if (rc < 0)
errx(1, "failed to create resource");
printf("resource added: %s\n", argv[i]);
}
out:
db_prncpl_free(p);
free(user);
free(emailp);
return 0;
usage:
fprintf(stderr, "usage: %s "
"[-Cnv] "
"[-d collection] "
"[-e email] "
"[-f caldir] "
"[-u principal] [resource...]\n",
getprogname());
return 1;
}