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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
/* gpg -- a translator for encrypting, decrypting, and verifying files
Copyright (C) 2016 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <argz.h>
#include <argp.h>
#include <dirent.h>
#include <error.h>
#include <hurd/netfs.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <pthread.h>
#include <hurd/ihash.h>
#include <hurd/paths.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <version.h>
#include "chroot.h"
#include "libnetfs/fs_S.h"
#include "libnetfs/io_S.h"
#include "libnetfs/fsys_S.h"
#include "libports/notify_S.h"
#include "libports/interrupt_S.h"
const char *argp_program_version = STANDARD_HURD_VERSION (gpg);
char *netfs_server_name = "gpg";
char *netfs_server_version = HURD_VERSION;
int netfs_maxsymlinks = 16; /* arbitrary */
struct chroot_node
{
unsigned int encrypt:1;
file_t shadow_file;
};
#define GPG_DEFAULT_FILENAME "/usr/bin/gpg2"
static char* gpg_filename = GPG_DEFAULT_FILENAME;
static file_t gpg_file;
int symmetric;
char *recipients;
size_t recipients_len;
static error_t
execute_gpg (mach_port_t in, mach_port_t out, mach_port_t extra,
char *argz, size_t argz_len, pid_t *pid)
{
error_t err;
task_t task;
process_t proc;
mach_port_t ports[INIT_PORT_MAX];
mach_port_t fds[STDERR_FILENO + 2];
int ints[INIT_INT_MAX];
int i;
for (i = 0; i < INIT_PORT_MAX; i++)
ports[i] = MACH_PORT_NULL;
for (i = 0; i < STDERR_FILENO + 2; i++)
fds[i] = MACH_PORT_NULL;
memset (ints, 0, INIT_INT_MAX * sizeof(int));
ports[INIT_PORT_CWDIR] = getcwdir ();
ports[INIT_PORT_CRDIR] = getcrdir ();
ports[INIT_PORT_AUTH] = getauth ();
fds[STDIN_FILENO] = in;
fds[STDOUT_FILENO] = out;
fds[STDERR_FILENO] = getdport (STDERR_FILENO);
fds[STDERR_FILENO+1] = extra;
err = task_create (mach_task_self (), 0, &task);
if (err)
goto lose;
proc = getproc ();
proc_child (proc, task);
/* Try and exec GnuPG in TASK... */
err = file_exec (gpg_file, task, EXEC_DEFAULTS,
argz, argz_len, 0, 0,
fds, MACH_MSG_TYPE_COPY_SEND, STDERR_FILENO + 2,
ports, MACH_MSG_TYPE_COPY_SEND, INIT_PORT_MAX,
ints, INIT_INT_MAX, 0, 0, 0, 0);
if (err)
goto lose;
err = proc_task2pid (proc, task, pid);
lose:
for (i = 0; i < INIT_PORT_MAX; i++)
mach_port_deallocate (mach_task_self (), ports[i]);
mach_port_deallocate (mach_task_self (), fds[STDERR_FILENO]);
return err;
}
error_t
clone_file (file_t file, file_t *new_file)
{
error_t err;
off_t newp;
retry_type do_retry;
string_t retry_name;
err = dir_lookup (file, "", O_RDONLY, 0, &do_retry, retry_name, new_file);
if (err)
return err;
return io_seek (*new_file, 0, SEEK_SET, &newp);
}
static int
gpg_verify (file_t sigfile, file_t orig_file)
{
error_t err;
file_t file;
pid_t pid;
char *argz = NULL;
size_t argz_len = 0;
err = clone_file (orig_file, &file);
if (err)
return 0;
argz_add (&argz, &argz_len, "gpg2");
argz_add (&argz, &argz_len, "--verify");
argz_add (&argz, &argz_len, "/dev/fd/3");
argz_add (&argz, &argz_len, "-");
err = execute_gpg (file, getdport (STDERR_FILENO), sigfile, argz, argz_len, &pid);
mach_port_deallocate (mach_task_self (), file);
if (err)
{
error (0, err, "execute_gpg");
return 0;
}
int status;
while (waitpid (pid, &status, 0) == (pid_t)-1 && errno == EINTR)
{ }
return WIFEXITED (status) && WEXITSTATUS (status) == 0;
}
static int
gpg_decrypt (file_t cipherfile, file_t plainfile)
{
error_t err;
pid_t pid;
char *argz = NULL;
size_t argz_len = 0;
argz_add (&argz, &argz_len, "gpg2");
argz_add (&argz, &argz_len, "--output");
argz_add (&argz, &argz_len, "-");
err = execute_gpg (cipherfile, plainfile, MACH_PORT_NULL,
argz, argz_len, &pid);
if (err)
{
error (0, err, "execute_gpg");
return 0;
}
int status;
while (waitpid (pid, &status, 0) == (pid_t)-1 && errno == EINTR)
{ }
return WIFEXITED (status) && WEXITSTATUS (status) == 0;
}
static int
gpg_encrypt (file_t plainfile, file_t cipherfile)
{
error_t err;
pid_t pid;
char *argz = NULL;
size_t argz_len = 0;
argz_add (&argz, &argz_len, "gpg2");
argz_add (&argz, &argz_len, "--output");
argz_add (&argz, &argz_len, "-");
if (recipients)
{
char *recipient;
argz_add (&argz, &argz_len, "--encrypt");
for (recipient = recipients;
recipient;
recipient = argz_next (recipients, recipients_len, recipient))
{
argz_add (&argz, &argz_len, "--recipient");
argz_add (&argz, &argz_len, recipient);
}
}
if (symmetric)
argz_add (&argz, &argz_len, "--symmetric");
err = execute_gpg (plainfile, cipherfile, MACH_PORT_NULL,
argz, argz_len, &pid);
if (err)
{
error (0, err, "execute_gpg");
return 0;
}
int status;
while (waitpid (pid, &status, 0) == (pid_t)-1 && errno == EINTR)
{ }
return WIFEXITED (status) && WEXITSTATUS (status) == 0;
}
static error_t
make_tmp_file (file_t *file)
{
char name[] = "/tmp/gpgtmp-XXXXXX";
int fd;
fd = mkstemp (name);
if (fd == -1)
return errno;
if (unlink (name) == -1)
return errno;
*file = getdport (fd);
close (fd);
return 0;
}
error_t
netfs_S_dir_lookup (struct protid *diruser,
char *filename,
int flags,
mode_t mode,
retry_type *do_retry,
char *retry_name,
mach_port_t *retry_port,
mach_msg_type_name_t *retry_port_type)
{
struct node *dnp, *np;
error_t err;
struct protid *newpi;
struct iouser *user;
file_t file;
file_t dir;
if (! diruser)
return EOPNOTSUPP;
dnp = diruser->po->np;
dir = netfs_node_netnode (dnp)->file;
err = dir_lookup (dir, filename,
flags & (O_NOLINK|O_RDWR|O_EXEC|O_CREAT|O_EXCL|O_NONBLOCK),
mode, do_retry, retry_name, &file);
if (err == ENOENT)
{
file_t gpgfile;
char *gpgname;
asprintf (&gpgname, "%s.gpg", filename);
gpgfile = file_name_lookup_under (dir, gpgname, O_RDONLY, 0);
if (MACH_PORT_VALID (gpgfile))
{
file_t tmpfile;
int ok;
off_t newp;
err = make_tmp_file (&tmpfile);
if (err)
return err;
ok = gpg_decrypt (gpgfile, tmpfile);
mach_port_deallocate (mach_task_self (), gpgfile);
if (! ok)
{
mach_port_deallocate (mach_task_self (), tmpfile);
return EIO;
}
err = io_seek (tmpfile, 0, SEEK_SET, &newp);
if (err)
{
mach_port_deallocate (mach_task_self (), tmpfile);
return err;
}
*do_retry = FS_RETRY_NORMAL;
retry_name[0] = 0;
*retry_port = tmpfile;
*retry_port_type = MACH_MSG_TYPE_MOVE_SEND;
return 0;
}
free (gpgname);
}
if (err)
return err;
switch (*do_retry)
{
case FS_RETRY_NORMAL:
break;
case FS_RETRY_REAUTH:
error (0, 0, "FS_RETRY_REAUTH");
/* Fallthrough. */
case FS_RETRY_MAGICAL:
*retry_port = file;
*retry_port_type = MACH_MSG_TYPE_MOVE_SEND;
return 0;
default:
/* Invalid response to our dir_lookup request. */
if (MACH_PORT_VALID (file))
mach_port_deallocate (mach_task_self (), file);
*retry_port = MACH_PORT_NULL;
*retry_port_type = MACH_MSG_TYPE_COPY_SEND;
return EOPNOTSUPP;
}
/* We have a new port to an underlying node.
Find or make our corresponding virtual node. */
np = 0;
pthread_mutex_lock (&dnp->lock);
err = chroot_new_node (file, sizeof (struct chroot_node), &np);
pthread_mutex_unlock (&dnp->lock);
if (!err)
{
chroot_node (np)->encrypt = chroot_node (dnp)->encrypt;
chroot_node (np)->shadow_file = MACH_PORT_NULL;
err = netfs_validate_stat (np, diruser->user);
}
if (err)
goto lose;
if (chroot_node (np)->encrypt && flags & O_WRITE
&& strncmp (filename, "dev/", 4) != 0)
{
char *ciphname;
asprintf (&ciphname, "%s.gpg", filename);
if (ciphname == NULL)
{
err = errno;
goto lose;
}
/* Fixup name. */
err = dir_link (dir, file, ciphname, 0);
free (ciphname);
if (err)
return err;
err = dir_unlink (dir, filename);
if (err)
return err;
err = make_tmp_file (&chroot_node (np)->shadow_file);
if (err)
return err;
}
if (retry_name[0] == 0)
{
file_t sig;
char *signame;
asprintf (&signame, "%s.sig", filename);
if (signame == NULL)
{
err = errno;
goto lose;
}
sig = file_name_lookup_under (dir, signame, O_RDONLY, 0);
if (MACH_PORT_VALID (sig))
{
if (! gpg_verify (sig, file))
err = EIO;
free (signame);
signame = NULL;
mach_port_deallocate (mach_task_self (), sig);
if (err)
goto lose;
}
free (signame);
}
assert (*do_retry == FS_RETRY_NORMAL);
flags &= ~(O_CREAT|O_EXCL|O_NOLINK|O_NOTRANS|O_NONBLOCK);
err = iohelp_dup_iouser (&user, diruser->user);
if (!err)
{
newpi = netfs_make_protid (netfs_make_peropen (np, flags, diruser->po),
user);
if (! newpi)
{
iohelp_free_iouser (user);
err = errno;
}
else
{
*retry_port = ports_get_right (newpi);
*retry_port_type = MACH_MSG_TYPE_MAKE_SEND;
ports_port_deref (newpi);
}
}
lose:
if (dir != netfs_node_netnode (dnp)->file)
mach_port_deallocate (mach_task_self (), dir);
if (np != NULL)
netfs_nput (np);
return err;
}
error_t
netfs_attempt_mkfile (struct iouser *user, struct node *dir,
mode_t mode, struct node **np)
{
file_t newfile;
error_t err = dir_mkfile (netfs_node_netnode (dir)->file, O_RDWR|O_EXEC,
mode, &newfile);
pthread_mutex_unlock (&dir->lock);
if (err)
return err;
err = chroot_new_node (newfile, sizeof (struct chroot_node), np);
if (err)
return err;
if (chroot_node (dir)->encrypt)
{
err = make_tmp_file (&chroot_node (*np)->shadow_file);
if (err)
return err;
}
else
chroot_node (*np)->shadow_file = MACH_PORT_NULL;
pthread_mutex_unlock (&(*np)->lock);
return err;
}
error_t
netfs_attempt_read (struct iouser *cred, struct node *np,
off_t offset, size_t *len, void *data)
{
struct chroot_node *n = chroot_node (np);
char *buf = data;
error_t err = io_read (n->shadow_file ?: netfs_node_netnode (np)->file,
&buf, len, offset, *len);
if (err == 0 && buf != data)
{
memcpy (data, buf, *len);
munmap (buf, *len);
}
return err;
}
error_t
netfs_attempt_write (struct iouser *cred, struct node *np,
off_t offset, size_t *len, void *data)
{
struct chroot_node *n = chroot_node (np);
return io_write (n->shadow_file ?: netfs_node_netnode (np)->file,
data, *len, offset, len);
}
error_t
netfs_attempt_sync (struct iouser *cred, struct node *np, int wait)
{
error_t err;
file_t gpg_file = netfs_node_netnode (np)->file;
struct chroot_node *n = chroot_node (np);
err = file_sync (n->shadow_file ?: gpg_file, wait, 0);
if (err)
return err;
if (wait && MACH_PORT_VALID (n->shadow_file))
{
file_t shadow_file;
off_t newp;
err = clone_file (n->shadow_file, &shadow_file);
if (err)
return err;
err = io_seek (gpg_file, 0, SEEK_SET, &newp);
if (err)
return err;
err = gpg_encrypt (shadow_file, gpg_file);
if (err)
return err;
mach_port_deallocate (mach_task_self (), shadow_file);
err = file_sync (gpg_file, wait, 0);
}
return err;
}
error_t
netfs_attempt_link (struct iouser *user, struct node *dir,
struct node *file, char *name, int excl)
{
/* XXX translate name if file is encrypted. */
return dir_link (netfs_node_netnode (dir)->file,
netfs_node_netnode (file)->file, name, excl);
}
void
chroot_node_norefs (struct node *np)
{
error_t err;
file_t gpg_file = netfs_node_netnode (np)->file;
struct chroot_node *n = chroot_node (np);
if (n->encrypt)
{
off_t newp;
err = io_seek (n->shadow_file, 0, SEEK_SET, &newp);
if (err)
return;
err = io_seek (gpg_file, 0, SEEK_SET, &newp);
if (err)
return;
err = gpg_encrypt (n->shadow_file, gpg_file);
if (err)
return;
mach_port_deallocate (mach_task_self (), n->shadow_file);
}
}
error_t
netfs_get_dirents (struct iouser *cred, struct node *dir,
int entry, int nentries, char **data,
mach_msg_type_number_t *datacnt,
vm_size_t bufsize, int *amt)
{
error_t err;
char *ptr;
struct dirent *dent;
err = dir_readdir (netfs_node_netnode (dir)->file, data, datacnt,
entry, nentries, bufsize, amt);
if (err)
return err;
for (ptr = *data, dent = (struct dirent *) ptr;
ptr - *data < *datacnt;
ptr += dent->d_reclen, dent = (struct dirent *) ptr)
{
size_t len = strlen (dent->d_name);
if (len > 4 && strcmp (&dent->d_name[len - 4], ".gpg") == 0)
memset (&dent->d_name[len - 4], 0, 4);
}
return 0;
}
#define OPT_GPG_PROGRAM -1
static struct argp_option options[] =
{
{NULL, 0, NULL, 0, "How to encrypt:", 1},
{"recipient", 'r', "NAME", 0, "Encrypt for user NAME. "
"Can be given multiple times.", 1},
{"symmetric", 'c', NULL, 0, "Encrypt with a password.", 1},
{NULL, 0, NULL, 0, "Advanced options:", 2},
{"gpg-program", OPT_GPG_PROGRAM, "FILENAME", 0,
"Path to the GNU Privacy Guard executable ["GPG_DEFAULT_FILENAME"].", 2},
{0}
};
static char *args_doc = "";
static char *doc = "Transparently encrypt, decrypt, and verify signatures"
" using GnuPG."
"\vFor everyday use, the commands 'encrypt', 'decrypt', and 'verify'"
" offer a simple and convenient frontend for this translator.";
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
error_t err;
switch (key)
{
case 'r':
err = argz_add (&recipients, &recipients_len, arg);
if (err)
error (1, err, "argz_add");
break;
case 'c':
symmetric = 1;
break;
case OPT_GPG_PROGRAM:
gpg_filename = arg;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
int
main (int argc, char **argv)
{
error_t err;
mach_port_t bootstrap;
struct argp argp =
{
.options = options,
.parser = parse_opt,
.args_doc = args_doc,
.doc = doc,
};
/* Parse our command line arguments (all none of them). */
argp_parse (&argp, argc, argv, 0, 0, 0);
task_get_bootstrap_port (mach_task_self (), &bootstrap);
if (! MACH_PORT_VALID (bootstrap))
error (1, 0, "Must be started as a translator");
gpg_file = file_name_lookup (gpg_filename, O_EXEC, 0);
if (! MACH_PORT_VALID (gpg_file))
error (1, errno, "%s", gpg_filename);
netfs_init ();
chroot_init ();
/* Get our underlying node (we presume it's a directory) and use
that to make the root node of the filesystem. */
err = chroot_new_node (netfs_startup (bootstrap, O_READ),
sizeof (struct chroot_node), &netfs_root_node);
if (err)
error (5, err, "Cannot create root node");
err = netfs_validate_stat (netfs_root_node, 0);
if (err)
error (6, err, "Cannot stat underlying node");
netfs_root_node->nn_stat.st_mode &= ~(S_IPTRANS | S_IATRANS);
netfs_root_node->nn_stat.st_mode |= S_IROOT;
pthread_mutex_unlock (&netfs_root_node->lock);
chroot_node (netfs_root_node)->encrypt = 1;
chroot_node (netfs_root_node)->shadow_file = MACH_PORT_NULL;
netfs_server_loop (); /* Never returns. */
/*NOTREACHED*/
return 0;
}
|