1 /* $Id: chngproc.c,v 1.13 2019/04/01 04:18:54 naddy Exp $ */ 2 /* 3 * Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <assert.h> 19 #include <err.h> 20 #include <errno.h> 21 #include <fcntl.h> 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <unistd.h> 26 27 #include "extern.h" 28 29 int 30 chngproc(int netsock, const char *root) 31 { 32 char *tok = NULL, *th = NULL, *fmt = NULL, **fs = NULL; 33 size_t i, fsz = 0; 34 int rc = 0, fd = -1, cc; 35 long lval; 36 enum chngop op; 37 void *pp; 38 39 if (chroot(root) == -1) { 40 warn("chroot"); 41 goto out; 42 } 43 if (chdir("/") == -1) { 44 warn("chdir"); 45 goto out; 46 } 47 if (pledge("stdio cpath wpath", NULL) == -1) { 48 warn("pledge"); 49 goto out; 50 } 51 52 /* 53 * Loop while we wait to get a thumbprint and token. 54 * We'll get this for each SAN request. 55 */ 56 57 for (;;) { 58 op = CHNG__MAX; 59 if ((lval = readop(netsock, COMM_CHNG_OP)) == 0) 60 op = CHNG_STOP; 61 else if (lval == CHNG_SYN) 62 op = lval; 63 64 if (op == CHNG__MAX) { 65 warnx("unknown operation from netproc"); 66 goto out; 67 } else if (op == CHNG_STOP) 68 break; 69 70 assert(op == CHNG_SYN); 71 72 /* 73 * Read the thumbprint and token. 74 * The token is the filename, so store that in a vector 75 * of tokens that we'll later clean up. 76 */ 77 78 if ((th = readstr(netsock, COMM_THUMB)) == NULL) 79 goto out; 80 else if ((tok = readstr(netsock, COMM_TOK)) == NULL) 81 goto out; 82 83 /* Vector appending... */ 84 85 pp = reallocarray(fs, (fsz + 1), sizeof(char *)); 86 if (pp == NULL) { 87 warn("realloc"); 88 goto out; 89 } 90 fs = pp; 91 fs[fsz] = tok; 92 tok = NULL; 93 fsz++; 94 95 if (asprintf(&fmt, "%s.%s", fs[fsz - 1], th) == -1) { 96 warn("asprintf"); 97 goto out; 98 } 99 100 /* 101 * Create and write to our challenge file. 102 * Note: we use file descriptors instead of FILE 103 * because we want to minimise our pledges. 104 */ 105 fd = open(fs[fsz - 1], O_WRONLY|O_EXCL|O_CREAT, 0444); 106 if (fd == -1) { 107 warn("%s", fs[fsz - 1]); 108 goto out; 109 } 110 if (write(fd, fmt, strlen(fmt)) == -1) { 111 warn("%s", fs[fsz - 1]); 112 goto out; 113 } 114 if (close(fd) == -1) { 115 warn("%s", fs[fsz - 1]); 116 goto out; 117 } 118 fd = -1; 119 120 free(th); 121 free(fmt); 122 th = fmt = NULL; 123 124 dodbg("%s/%s: created", root, fs[fsz - 1]); 125 126 /* 127 * Write our acknowledgement. 128 * Ignore reader failure. 129 */ 130 131 cc = writeop(netsock, COMM_CHNG_ACK, CHNG_ACK); 132 if (cc == 0) 133 break; 134 if (cc < 0) 135 goto out; 136 } 137 138 rc = 1; 139 out: 140 close(netsock); 141 if (fd != -1) 142 close(fd); 143 for (i = 0; i < fsz; i++) { 144 if (unlink(fs[i]) == -1 && errno != ENOENT) 145 warn("%s", fs[i]); 146 free(fs[i]); 147 } 148 free(fs); 149 free(fmt); 150 free(th); 151 free(tok); 152 return rc; 153 } 154