-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyd.c
282 lines (234 loc) · 6.66 KB
/
copyd.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
/*
* Copyright 2006-2017 Niklas Edmundsson <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Emulate RCS $Id$, simply because it's handy to be able to run ident
on an executable/library/etc and see the version.
*/
static const char rcsid[] = "$Id: httpcachecopyd " GIT_SOURCE_DESC " $";
#define _GNU_SOURCE 1
#define _XOPEN_SOURCE 600
#define _ALL_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#define _LARGE_FILE_API 1
/* For directio() */
#ifdef __sun
#define __EXTENSIONS__
#endif /* __sun */
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <pwd.h>
#include "config.h"
#define IS_COPYD
#ifdef USE_COPYD
#error Source setup error - can not use and be copyd at the same time
#endif
#include "cleanpath.c"
#include "cacheopen.c"
static int debug=1;
void *handle_conn(void * arg) {
/* To avoid the bogus gcc cast to/from pointer of different size warning */
size_t argtmp = (size_t) arg;
int fd = (int) argtmp;
char buf[PATH_MAX], cachepath[PATH_MAX];
ssize_t amt;
int realfd = -1, cachefd = -1, oflag;
struct stat64 realst, cachest;
if(debug) {
fprintf(stderr, "copyd: handle_conn: fd=%d\n", fd);
}
amt = read(fd, buf, sizeof(buf)-1);
if(amt == -1) {
perror("handle_conn: read");
goto err;
}
else if(amt == 0) {
if(debug) {
fprintf(stderr, "copyd: handle_conn: read: no data\n");
}
goto err;
}
/* We assume a null terminated value as input, make sure we don't travel
outside our buffer */
buf[sizeof(buf)-1] = '\0';
/* Clean it from . .. // */
cleanpath(buf);
if(debug) {
fprintf(stderr, "copyd: handle_conn: buf=%s\n", buf);
}
if(cacheopen_check(buf) == -1) {
if(debug) {
fprintf(stderr, "copyd: cacheopen_check fail\n");
}
goto err;
}
oflag = O_RDONLY
#ifdef USE_O_DIRECT
| O_DIRECT
#endif
/* FIXME: Use directio() on solaris */
;
realfd = open(buf, oflag);
if(realfd == -1) {
if(debug) {
perror("open realfd");
}
goto err;
}
if(fstat64(realfd, &realst) == -1) {
if(debug) {
perror("fstat64 realfd");
}
goto err;
}
cacheopen_prepare(&realst, cachepath);
cachefd = cacheopen(&cachest, &realst, oflag, cachepath, open, fstat64,
close);
if(cachefd == CACHEOPEN_DECLINED) {
if(debug) {
fprintf(stderr, "cacheopen DECLINED\n");
}
goto err;
}
/* Write reply when we're pretty sure this will work in order not to pause
requesting process until we're finished */
if(write(fd, "OK", 3) < 0) {
perror("write reply OK");
}
close(fd);
fd = -1;
if(cachefd == CACHEOPEN_FAIL || cachefd == CACHEOPEN_STALE) {
/* Either no cached file or stale cached file */
copy_file(realfd, oflag, realst.st_size, realst.st_mtime, cachepath,
open, stat64, fstat64, read, close);
}
goto ok;
err:
if(write(fd, "FAIL", 5) < 0) {
/* Keep quiet even if we fail */
}
ok:
if(fd >= 0) {
close(fd);
}
if(realfd >= 0) {
close(realfd);
}
if(cachefd >= 0) {
close(cachefd);
}
fprintf(stderr, "copyd: handle_conn: done\n");
return NULL;
}
int main(void) {
struct sockaddr_un sa;
int sock, rc;
socklen_t salen;
pthread_attr_t attr;
struct passwd *pw;
if(debug) {
fprintf(stderr, "copyd: %s starting\n", rcsid);
}
signal(SIGPIPE, SIG_IGN);
signal(SIGCLD, SIG_IGN);
if(pthread_attr_init(&attr) != 0) {
perror("copyd: pthread_attr_init");
exit(1);
}
if(pthread_attr_setstacksize(&attr, 131072) != 0) {
perror("copyd: pthread_attr_setstacksize");
exit(1);
}
if(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
perror("copyd: pthread_attr_setdetachstate");
exit(1);
}
/* Clean up old debris */
unlink(SOCKPATH);
/* Create the socket and stuff */
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if(sock == -1) {
perror("copyd: socket");
exit(1);
}
sa.sun_family = AF_UNIX;
strcpy(sa.sun_path, SOCKPATH);
if(bind(sock, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
perror("copyd: bind");
exit(2);
}
if(listen(sock, 1024) != 0) {
perror("copyd: listen");
exit(3);
}
/* Get UID/GID of user to run as */
pw = getpwnam(COPYD_USER);
if(pw == NULL) {
fprintf(stderr, "copyd: No such COPYD_USER %s\n", COPYD_USER);
exit(4);
}
/* Change owner of the socket to our running user */
if(chown(SOCKPATH, pw->pw_uid, pw->pw_gid) != 0) {
perror("chown " SOCKPATH);
exit(4);
}
if(chmod(SOCKPATH, 0600) != 0) {
perror("chmod " SOCKPATH);
exit(4);
}
/* Change to the user we'll run as */
if(setregid(pw->pw_gid, pw->pw_gid) < 0) {
perror("setregid");
exit(5);
}
if(setreuid(pw->pw_uid, pw->pw_uid) < 0) {
perror("setreuid");
exit(6);
}
if(debug) {
fprintf(stderr, "copyd: Init done\n");
}
do {
salen = sizeof(sa);
rc = accept(sock, (struct sockaddr *)&sa, &salen);
if(debug) {
fprintf(stderr, "copyd: accept rc=%d errno=%d\n", rc, errno);
}
if(rc >= 0) {
pthread_t thr;
/* To avoid bogus gcc cast to/from pointer of diff. size warning */
size_t arg = rc; /* Pass fd as argument to thread */
if(pthread_create(&thr, &attr, handle_conn, (void *) arg) != 0) {
perror("pthread_create");
exit(25);
}
}
else if (rc == -1 && (errno == EINTR || errno == ECONNABORTED || errno == ENOTCONN)) {
continue;
}
} while(rc != -1);
perror("copyd: accept");
exit(99);
}