This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchngproc.c
212 lines (189 loc) · 4.87 KB
/
chngproc.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
/* $Id$ */
/*
* Copyright (c) 2016 Kristaps Dzonsons <[email protected]>
*
* 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 AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/queue.h>
#include <sys/stat.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
int
chngproc(int netsock, const char *root, const struct config *cfg)
{
char *alt = NULL, *tok = NULL, *th = NULL, *fmt = NULL,
*fmtbuf = NULL;
char **fs = NULL;
size_t i, fsz = 0, sz;
ssize_t ssz;
int rc = 0, fd = -1, cc;
long lval;
enum chngop op;
void *pp;
/* File-system and sandbox jailing. */
if ( ! sandbox_before())
goto out;
else if ( ! dropfs(NULL != cfg->challenge ? PATH_VAR_EMPTY : root))
goto out;
else if ( ! sandbox_after(NULL != cfg->challenge))
goto out;
/*
* Make sure that the created file can be read by all parties,
* but not written to.
* (Our default umask for root is sometimes to disallow others
* from reading the file.)
*/
(void)umask(S_IWGRP | S_IWOTH);
/*
* Loop while we wait to get a thumbprint and token.
* We'll get this for each SAN request.
*/
for (;;) {
op = CHNG__MAX;
if (0 == (lval = readop(netsock, COMM_CHNG_OP)))
op = CHNG_STOP;
else if (CHNG_SYN == lval)
op = lval;
if (CHNG__MAX == op) {
warnx("unknown operation from netproc");
goto out;
} else if (CHNG_STOP == op)
break;
assert(CHNG_SYN == op);
/*
* Read the alt, thumbprint, and token.
* The token is the filename, so store that in a vector
* of tokens that we'll later clean up.
*/
if (NULL == (alt = readstr(netsock, COMM_DNSA)))
goto out;
else if (NULL == (th = readstr(netsock, COMM_THUMB)))
goto out;
else if (NULL == (tok = readstr(netsock, COMM_TOK)))
goto out;
/* Vector appending... */
pp = realloc(fs, (fsz + 1) * sizeof(char *));
if (NULL == pp) {
warn("realloc");
goto out;
}
fs = pp;
fs[fsz] = tok;
tok = NULL;
fsz++;
if (NULL != cfg->challenge) {
/*
* If we have a specific challenge request, then
* we write the request and thumbprint to stdout
* and wait for a reply (which must be an echo
* of the output) to indicate that all's well.
*/
fmt = doasprintf("%s %s %s.%s\n",
cfg->challenge, alt, fs[fsz - 1], th);
if (NULL == fmt) {
warn("asprintf");
goto out;
} else if (NULL == (fmtbuf = strdup(fmt))) {
warn("strdup");
goto out;
}
sz = strlen(fmt);
ssz = write(STDOUT_FILENO, fmt, sz);
if (-1 == ssz) {
warn("<stdout>");
goto out;
} else if ((size_t)ssz != sz) {
warnx("<stdout>: short write");
goto out;
}
doddbg("%s: challenge written", fs[fsz - 1]);
ssz = read(STDIN_FILENO, fmt, sz);
if (-1 == ssz) {
warn("<stdin>");
goto out;
} else if ((size_t)ssz != sz) {
warnx("<stdin>: short read");
goto out;
} else if (strcmp(fmt, fmtbuf)) {
warnx("<stdin>: token mismatch");
goto out;
}
doddbg("%s: challenge read", fs[fsz - 1]);
} else {
/*
* Create and write to our challenge file.
* Note: we use file descriptors instead of FILE
* because we want to minimise our pledges.
*/
fmt = doasprintf("%s.%s", fs[fsz - 1], th);
if (NULL == fmt) {
warn("asprintf");
goto out;
}
fd = open(fs[fsz - 1],
O_WRONLY|O_EXCL|O_CREAT, 0444);
if (-1 == fd) {
warn("%s", fs[fsz - 1]);
goto out;
} if (-1 == write(fd, fmt, strlen(fmt))) {
warn("%s", fs[fsz - 1]);
goto out;
} else if (-1 == close(fd)) {
warn("%s", fs[fsz - 1]);
goto out;
}
fd = -1;
dodbg("%s/%s: created", root, fs[fsz - 1]);
}
free(th);
free(fmt);
free(fmtbuf);
th = fmt = fmtbuf = NULL;
/*
* Write our acknowledgement.
* Ignore reader failure.
*/
cc = writeop(netsock, COMM_CHNG_ACK, CHNG_ACK);
if (0 == cc)
break;
if (cc < 0)
goto out;
}
rc = 1;
out:
close(netsock);
if (-1 != fd)
close(fd);
if (NULL == cfg->challenge)
for (i = 0; i < fsz; i++) {
if (-1 == unlink(fs[i]) && ENOENT != errno)
warn("%s", fs[i]);
free(fs[i]);
}
free(fs);
free(fmt);
free(fmtbuf);
free(th);
free(tok);
return(rc);
}