server.c revision 1.25 1 1.25 simonb /* $NetBSD: server.c,v 1.25 2003/01/20 05:30:12 simonb Exp $ */
2 1.11 thorpej
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1983, 1993
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.13 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.11 thorpej #if 0
39 1.11 thorpej static char sccsid[] = "@(#)server.c 8.1 (Berkeley) 6/9/93";
40 1.11 thorpej #else
41 1.25 simonb __RCSID("$NetBSD: server.c,v 1.25 2003/01/20 05:30:12 simonb Exp $");
42 1.11 thorpej #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.12 mrg #include <sys/types.h>
46 1.5 cgd #include <sys/wait.h>
47 1.12 mrg
48 1.12 mrg #include <errno.h>
49 1.23 wiz #include <fcntl.h>
50 1.23 wiz #include <grp.h>
51 1.12 mrg #include <pwd.h>
52 1.23 wiz #include <stdarg.h>
53 1.12 mrg
54 1.1 cgd #include "defs.h"
55 1.1 cgd
56 1.18 mrg #define ack() do { if (write(rem, "\0\n", 2) < 0) error("ack failed: %s\n", strerror(errno)); } while (0)
57 1.18 mrg #define err() do { if (write(rem, "\1\n", 2) < 0) error("err failed: %s\n", strerror(errno)); } while (0)
58 1.1 cgd
59 1.1 cgd struct linkbuf *ihead; /* list of files with more than one link */
60 1.1 cgd char buf[BUFSIZ]; /* general purpose buffer */
61 1.1 cgd char target[BUFSIZ]; /* target/source directory name */
62 1.1 cgd char *tp; /* pointer to end of target name */
63 1.1 cgd char *Tdest; /* pointer to last T dest*/
64 1.1 cgd int catname; /* cat name to target name */
65 1.1 cgd char *stp[32]; /* stack of saved tp's for directories */
66 1.1 cgd int oumask; /* old umask for creating files */
67 1.1 cgd
68 1.1 cgd extern FILE *lfp; /* log file for mailing changes */
69 1.1 cgd
70 1.23 wiz static int chkparent(char *);
71 1.23 wiz static void clean(char *);
72 1.23 wiz static void comment(char *);
73 1.23 wiz static void dospecial(char *);
74 1.23 wiz static int fchtogm(int, char *, time_t, char *, char *, mode_t);
75 1.23 wiz static void hardlink(char *);
76 1.23 wiz static void note(const char *, ...)
77 1.19 is __attribute__((__format__(__printf__, 1, 2)));
78 1.23 wiz static void query(char *);
79 1.23 wiz static void recvf(char *, int);
80 1.23 wiz static void removeit(struct stat *);
81 1.23 wiz static int response(void);
82 1.23 wiz static void rmchk(int);
83 1.5 cgd static struct linkbuf *
84 1.23 wiz savelink(struct stat *);
85 1.23 wiz static void sendf(char *, int);
86 1.23 wiz static int update(char *, int, struct stat *);
87 1.1 cgd
88 1.1 cgd /*
89 1.1 cgd * Server routine to read requests and process them.
90 1.1 cgd * Commands are:
91 1.1 cgd * Tname - Transmit file if out of date
92 1.1 cgd * Vname - Verify if file out of date or not
93 1.1 cgd * Qname - Query if file exists. Return mtime & size if it does.
94 1.1 cgd */
95 1.5 cgd void
96 1.23 wiz server(void)
97 1.1 cgd {
98 1.1 cgd char cmdbuf[BUFSIZ];
99 1.13 lukem char *cp;
100 1.1 cgd
101 1.1 cgd signal(SIGHUP, cleanup);
102 1.1 cgd signal(SIGINT, cleanup);
103 1.1 cgd signal(SIGQUIT, cleanup);
104 1.1 cgd signal(SIGTERM, cleanup);
105 1.1 cgd signal(SIGPIPE, cleanup);
106 1.1 cgd
107 1.1 cgd rem = 0;
108 1.1 cgd oumask = umask(0);
109 1.9 thorpej (void) snprintf(buf, sizeof(buf), "V%d\n", VERSION);
110 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
111 1.18 mrg error("server: could not write remote end: %s\n",
112 1.18 mrg strerror(errno));
113 1.1 cgd
114 1.1 cgd for (;;) {
115 1.1 cgd cp = cmdbuf;
116 1.1 cgd if (read(rem, cp, 1) <= 0)
117 1.1 cgd return;
118 1.1 cgd if (*cp++ == '\n') {
119 1.1 cgd error("server: expected control record\n");
120 1.1 cgd continue;
121 1.1 cgd }
122 1.1 cgd do {
123 1.1 cgd if (read(rem, cp, 1) != 1)
124 1.5 cgd cleanup(0);
125 1.1 cgd } while (*cp++ != '\n' && cp < &cmdbuf[BUFSIZ]);
126 1.1 cgd *--cp = '\0';
127 1.1 cgd cp = cmdbuf;
128 1.1 cgd switch (*cp++) {
129 1.1 cgd case 'T': /* init target file/directory name */
130 1.1 cgd catname = 1; /* target should be directory */
131 1.1 cgd goto dotarget;
132 1.1 cgd
133 1.1 cgd case 't': /* init target file/directory name */
134 1.1 cgd catname = 0;
135 1.1 cgd dotarget:
136 1.1 cgd if (exptilde(target, cp) == NULL)
137 1.1 cgd continue;
138 1.1 cgd tp = target;
139 1.1 cgd while (*tp)
140 1.1 cgd tp++;
141 1.1 cgd ack();
142 1.1 cgd continue;
143 1.1 cgd
144 1.1 cgd case 'R': /* Transfer a regular file. */
145 1.1 cgd recvf(cp, S_IFREG);
146 1.1 cgd continue;
147 1.1 cgd
148 1.1 cgd case 'D': /* Transfer a directory. */
149 1.1 cgd recvf(cp, S_IFDIR);
150 1.1 cgd continue;
151 1.1 cgd
152 1.1 cgd case 'K': /* Transfer symbolic link. */
153 1.1 cgd recvf(cp, S_IFLNK);
154 1.1 cgd continue;
155 1.1 cgd
156 1.1 cgd case 'k': /* Transfer hard link. */
157 1.1 cgd hardlink(cp);
158 1.1 cgd continue;
159 1.1 cgd
160 1.1 cgd case 'E': /* End. (of directory) */
161 1.1 cgd *tp = '\0';
162 1.1 cgd if (catname <= 0) {
163 1.1 cgd error("server: too many 'E's\n");
164 1.1 cgd continue;
165 1.1 cgd }
166 1.1 cgd tp = stp[--catname];
167 1.1 cgd *tp = '\0';
168 1.1 cgd ack();
169 1.1 cgd continue;
170 1.1 cgd
171 1.1 cgd case 'C': /* Clean. Cleanup a directory */
172 1.1 cgd clean(cp);
173 1.1 cgd continue;
174 1.1 cgd
175 1.1 cgd case 'Q': /* Query. Does the file/directory exist? */
176 1.1 cgd query(cp);
177 1.1 cgd continue;
178 1.1 cgd
179 1.1 cgd case 'S': /* Special. Execute commands */
180 1.1 cgd dospecial(cp);
181 1.1 cgd continue;
182 1.1 cgd
183 1.1 cgd #ifdef notdef
184 1.1 cgd /*
185 1.1 cgd * These entries are reserved but not currently used.
186 1.1 cgd * The intent is to allow remote hosts to have master copies.
187 1.1 cgd * Currently, only the host rdist runs on can have masters.
188 1.1 cgd */
189 1.1 cgd case 'X': /* start a new list of files to exclude */
190 1.1 cgd except = bp = NULL;
191 1.1 cgd case 'x': /* add name to list of files to exclude */
192 1.1 cgd if (*cp == '\0') {
193 1.1 cgd ack();
194 1.1 cgd continue;
195 1.1 cgd }
196 1.1 cgd if (*cp == '~') {
197 1.1 cgd if (exptilde(buf, cp) == NULL)
198 1.1 cgd continue;
199 1.1 cgd cp = buf;
200 1.1 cgd }
201 1.1 cgd if (bp == NULL)
202 1.1 cgd except = bp = expand(makeblock(NAME, cp), E_VARS);
203 1.1 cgd else
204 1.1 cgd bp->b_next = expand(makeblock(NAME, cp), E_VARS);
205 1.1 cgd while (bp->b_next != NULL)
206 1.1 cgd bp = bp->b_next;
207 1.1 cgd ack();
208 1.1 cgd continue;
209 1.1 cgd
210 1.1 cgd case 'I': /* Install. Transfer file if out of date. */
211 1.1 cgd opts = 0;
212 1.1 cgd while (*cp >= '0' && *cp <= '7')
213 1.1 cgd opts = (opts << 3) | (*cp++ - '0');
214 1.1 cgd if (*cp++ != ' ') {
215 1.1 cgd error("server: options not delimited\n");
216 1.1 cgd return;
217 1.1 cgd }
218 1.1 cgd install(cp, opts);
219 1.1 cgd continue;
220 1.1 cgd
221 1.1 cgd case 'L': /* Log. save message in log file */
222 1.1 cgd log(lfp, cp);
223 1.1 cgd continue;
224 1.1 cgd #endif
225 1.1 cgd
226 1.1 cgd case '\1':
227 1.1 cgd nerrs++;
228 1.1 cgd continue;
229 1.1 cgd
230 1.1 cgd case '\2':
231 1.1 cgd return;
232 1.1 cgd
233 1.1 cgd default:
234 1.1 cgd error("server: unknown command '%s'\n", cp);
235 1.1 cgd case '\0':
236 1.1 cgd continue;
237 1.1 cgd }
238 1.1 cgd }
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd /*
242 1.1 cgd * Update the file(s) if they are different.
243 1.1 cgd * destdir = 1 if destination should be a directory
244 1.1 cgd * (i.e., more than one source is being copied to the same destination).
245 1.1 cgd */
246 1.5 cgd void
247 1.23 wiz install(char *src, char *dest, int destdir, int opts)
248 1.1 cgd {
249 1.1 cgd char *rname;
250 1.1 cgd char destcopy[BUFSIZ];
251 1.1 cgd
252 1.1 cgd if (dest == NULL) {
253 1.1 cgd opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
254 1.1 cgd dest = src;
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd if (nflag || debug) {
258 1.1 cgd printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
259 1.1 cgd opts & WHOLE ? " -w" : "",
260 1.1 cgd opts & YOUNGER ? " -y" : "",
261 1.1 cgd opts & COMPARE ? " -b" : "",
262 1.1 cgd opts & REMOVE ? " -R" : "", src, dest);
263 1.1 cgd if (nflag)
264 1.1 cgd return;
265 1.1 cgd }
266 1.1 cgd
267 1.1 cgd rname = exptilde(target, src);
268 1.1 cgd if (rname == NULL)
269 1.1 cgd return;
270 1.1 cgd tp = target;
271 1.1 cgd while (*tp)
272 1.1 cgd tp++;
273 1.1 cgd /*
274 1.1 cgd * If we are renaming a directory and we want to preserve
275 1.21 wiz * the directory hierarchy (-w), we must strip off the leading
276 1.1 cgd * directory name and preserve the rest.
277 1.1 cgd */
278 1.1 cgd if (opts & WHOLE) {
279 1.1 cgd while (*rname == '/')
280 1.1 cgd rname++;
281 1.1 cgd destdir = 1;
282 1.1 cgd } else {
283 1.13 lukem rname = strrchr(target, '/');
284 1.1 cgd if (rname == NULL)
285 1.1 cgd rname = target;
286 1.1 cgd else
287 1.1 cgd rname++;
288 1.1 cgd }
289 1.1 cgd if (debug)
290 1.1 cgd printf("target = %s, rname = %s\n", target, rname);
291 1.1 cgd /*
292 1.1 cgd * Pass the destination file/directory name to remote.
293 1.1 cgd */
294 1.9 thorpej (void) snprintf(buf, sizeof(buf), "%c%s\n", destdir ? 'T' : 't', dest);
295 1.1 cgd if (debug)
296 1.1 cgd printf("buf = %s", buf);
297 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
298 1.18 mrg error("could not pass filename to remote: %s\n",
299 1.18 mrg strerror(errno));
300 1.1 cgd if (response() < 0)
301 1.1 cgd return;
302 1.1 cgd
303 1.1 cgd if (destdir) {
304 1.1 cgd strcpy(destcopy, dest);
305 1.1 cgd Tdest = destcopy;
306 1.1 cgd }
307 1.1 cgd sendf(rname, opts);
308 1.1 cgd Tdest = 0;
309 1.1 cgd }
310 1.1 cgd
311 1.1 cgd #define protoname() (pw ? pw->pw_name : user)
312 1.1 cgd #define protogroup() (gr ? gr->gr_name : group)
313 1.1 cgd /*
314 1.1 cgd * Transfer the file or directory in target[].
315 1.1 cgd * rname is the name of the file on the remote host.
316 1.1 cgd */
317 1.5 cgd static void
318 1.23 wiz sendf(char *rname, int opts)
319 1.1 cgd {
320 1.13 lukem struct subcmd *sc;
321 1.1 cgd struct stat stb;
322 1.1 cgd int sizerr, f, u, len;
323 1.1 cgd off_t i;
324 1.1 cgd DIR *d;
325 1.16 christos struct dirent *dp;
326 1.1 cgd char *otp, *cp;
327 1.1 cgd extern struct subcmd *subcmds;
328 1.1 cgd static char user[15], group[15];
329 1.1 cgd
330 1.1 cgd if (debug)
331 1.1 cgd printf("sendf(%s, %x)\n", rname, opts);
332 1.1 cgd
333 1.1 cgd if (except(target))
334 1.1 cgd return;
335 1.1 cgd if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
336 1.1 cgd error("%s: %s\n", target, strerror(errno));
337 1.1 cgd return;
338 1.1 cgd }
339 1.1 cgd if ((u = update(rname, opts, &stb)) == 0) {
340 1.14 mycroft if (S_ISREG(stb.st_mode) && stb.st_nlink > 1)
341 1.1 cgd (void) savelink(&stb);
342 1.1 cgd return;
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd if (pw == NULL || pw->pw_uid != stb.st_uid)
346 1.1 cgd if ((pw = getpwuid(stb.st_uid)) == NULL) {
347 1.24 thorpej dolog(lfp, "%s: no password entry for uid %d \n",
348 1.1 cgd target, stb.st_uid);
349 1.1 cgd pw = NULL;
350 1.13 lukem (void)snprintf(user, sizeof(user), ":%lu",
351 1.13 lukem (u_long)stb.st_uid);
352 1.1 cgd }
353 1.1 cgd if (gr == NULL || gr->gr_gid != stb.st_gid)
354 1.1 cgd if ((gr = getgrgid(stb.st_gid)) == NULL) {
355 1.24 thorpej dolog(lfp, "%s: no name for group %d\n",
356 1.1 cgd target, stb.st_gid);
357 1.1 cgd gr = NULL;
358 1.9 thorpej (void)snprintf(group, sizeof(group), ":%lu",
359 1.13 lukem (u_long)stb.st_gid);
360 1.1 cgd }
361 1.1 cgd if (u == 1) {
362 1.1 cgd if (opts & VERIFY) {
363 1.24 thorpej dolog(lfp, "need to install: %s\n", target);
364 1.1 cgd goto dospecial;
365 1.1 cgd }
366 1.24 thorpej dolog(lfp, "installing: %s\n", target);
367 1.1 cgd opts &= ~(COMPARE|REMOVE);
368 1.1 cgd }
369 1.1 cgd
370 1.1 cgd switch (stb.st_mode & S_IFMT) {
371 1.1 cgd case S_IFDIR:
372 1.1 cgd if ((d = opendir(target)) == NULL) {
373 1.1 cgd error("%s: %s\n", target, strerror(errno));
374 1.1 cgd return;
375 1.1 cgd }
376 1.9 thorpej (void) snprintf(buf, sizeof(buf), "D%o %04o 0 0 %s %s %s\n",
377 1.9 thorpej opts, stb.st_mode & 07777, protoname(), protogroup(),
378 1.9 thorpej rname);
379 1.1 cgd if (debug)
380 1.1 cgd printf("buf = %s", buf);
381 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
382 1.18 mrg error("can not write dir spec to remote: %s\n",
383 1.18 mrg strerror(errno));
384 1.18 mrg
385 1.1 cgd if (response() < 0) {
386 1.1 cgd closedir(d);
387 1.1 cgd return;
388 1.1 cgd }
389 1.1 cgd
390 1.1 cgd if (opts & REMOVE)
391 1.1 cgd rmchk(opts);
392 1.1 cgd
393 1.1 cgd otp = tp;
394 1.1 cgd len = tp - target;
395 1.13 lukem while ((dp = readdir(d)) != NULL) {
396 1.1 cgd if (!strcmp(dp->d_name, ".") ||
397 1.1 cgd !strcmp(dp->d_name, ".."))
398 1.1 cgd continue;
399 1.1 cgd if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
400 1.1 cgd error("%s/%s: Name too long\n", target,
401 1.1 cgd dp->d_name);
402 1.1 cgd continue;
403 1.1 cgd }
404 1.1 cgd tp = otp;
405 1.1 cgd *tp++ = '/';
406 1.1 cgd cp = dp->d_name;
407 1.13 lukem while ((*tp++ = *cp++) != 0)
408 1.1 cgd ;
409 1.1 cgd tp--;
410 1.1 cgd sendf(dp->d_name, opts);
411 1.1 cgd }
412 1.1 cgd closedir(d);
413 1.18 mrg if (write(rem, "E\n", 2) < 0)
414 1.18 mrg error("can not write E to remote: %s\n",
415 1.18 mrg strerror(errno));
416 1.1 cgd (void) response();
417 1.1 cgd tp = otp;
418 1.1 cgd *tp = '\0';
419 1.1 cgd return;
420 1.1 cgd
421 1.1 cgd case S_IFLNK:
422 1.1 cgd if (u != 1)
423 1.1 cgd opts |= COMPARE;
424 1.1 cgd if (stb.st_nlink > 1) {
425 1.1 cgd struct linkbuf *lp;
426 1.1 cgd
427 1.1 cgd if ((lp = savelink(&stb)) != NULL) {
428 1.1 cgd /* install link */
429 1.1 cgd if (*lp->target == 0)
430 1.9 thorpej (void) snprintf(buf, sizeof(buf),
431 1.9 thorpej "k%o %s %s\n", opts, lp->pathname, rname);
432 1.1 cgd else
433 1.9 thorpej (void) snprintf(buf, sizeof(buf),
434 1.9 thorpej "k%o %s/%s %s\n", opts, lp->target,
435 1.9 thorpej lp->pathname, rname);
436 1.1 cgd if (debug)
437 1.1 cgd printf("buf = %s", buf);
438 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
439 1.18 mrg error("can not write link spec to remote: %s\n",
440 1.18 mrg strerror(errno));
441 1.1 cgd (void) response();
442 1.1 cgd return;
443 1.1 cgd }
444 1.1 cgd }
445 1.20 lukem (void) snprintf(buf, sizeof(buf), "K%o %o %lld %ld %s %s %s\n",
446 1.15 mrg opts, stb.st_mode & 07777, (unsigned long long)stb.st_size,
447 1.15 mrg (u_long)stb.st_mtime, protoname(), protogroup(), rname);
448 1.1 cgd if (debug)
449 1.1 cgd printf("buf = %s", buf);
450 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
451 1.18 mrg error("can not write link spec to remote: %s\n",
452 1.18 mrg strerror(errno));
453 1.1 cgd if (response() < 0)
454 1.1 cgd return;
455 1.1 cgd sizerr = (readlink(target, buf, BUFSIZ) != stb.st_size);
456 1.18 mrg if (write(rem, buf, stb.st_size) < 0)
457 1.18 mrg error("can not write link name to remote: %s\n",
458 1.18 mrg strerror(errno));
459 1.1 cgd if (debug)
460 1.1 cgd printf("readlink = %.*s\n", (int)stb.st_size, buf);
461 1.1 cgd goto done;
462 1.1 cgd
463 1.1 cgd case S_IFREG:
464 1.1 cgd break;
465 1.1 cgd
466 1.1 cgd default:
467 1.1 cgd error("%s: not a file or directory\n", target);
468 1.1 cgd return;
469 1.1 cgd }
470 1.1 cgd
471 1.1 cgd if (u == 2) {
472 1.1 cgd if (opts & VERIFY) {
473 1.24 thorpej dolog(lfp, "need to update: %s\n", target);
474 1.1 cgd goto dospecial;
475 1.1 cgd }
476 1.24 thorpej dolog(lfp, "updating: %s\n", target);
477 1.1 cgd }
478 1.1 cgd
479 1.1 cgd if (stb.st_nlink > 1) {
480 1.1 cgd struct linkbuf *lp;
481 1.1 cgd
482 1.1 cgd if ((lp = savelink(&stb)) != NULL) {
483 1.1 cgd /* install link */
484 1.1 cgd if (*lp->target == 0)
485 1.9 thorpej (void) snprintf(buf, sizeof(buf), "k%o %s %s\n", opts,
486 1.1 cgd lp->pathname, rname);
487 1.1 cgd else
488 1.9 thorpej (void) snprintf(buf, sizeof(buf), "k%o %s/%s %s\n",
489 1.9 thorpej opts, lp->target, lp->pathname, rname);
490 1.1 cgd if (debug)
491 1.1 cgd printf("buf = %s", buf);
492 1.18 mrg if (write(rem, buf, strlen(buf)) <0)
493 1.18 mrg error("write of file name failed: %s\n",
494 1.18 mrg strerror(errno));
495 1.1 cgd (void) response();
496 1.1 cgd return;
497 1.1 cgd }
498 1.1 cgd }
499 1.1 cgd
500 1.5 cgd if ((f = open(target, O_RDONLY, 0)) < 0) {
501 1.1 cgd error("%s: %s\n", target, strerror(errno));
502 1.1 cgd return;
503 1.1 cgd }
504 1.20 lukem (void)snprintf(buf, sizeof(buf), "R%o %o %lld %lu %s %s %s\n", opts,
505 1.15 mrg stb.st_mode & 07777, (unsigned long long)stb.st_size,
506 1.15 mrg (u_long)stb.st_mtime, protoname(), protogroup(), rname);
507 1.1 cgd if (debug)
508 1.1 cgd printf("buf = %s", buf);
509 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
510 1.18 mrg error("write of file name failed: %s\n", strerror(errno));
511 1.1 cgd if (response() < 0) {
512 1.1 cgd (void) close(f);
513 1.1 cgd return;
514 1.1 cgd }
515 1.1 cgd sizerr = 0;
516 1.1 cgd for (i = 0; i < stb.st_size; i += BUFSIZ) {
517 1.1 cgd int amt = BUFSIZ;
518 1.1 cgd if (i + amt > stb.st_size)
519 1.1 cgd amt = stb.st_size - i;
520 1.1 cgd if (sizerr == 0 && read(f, buf, amt) != amt)
521 1.1 cgd sizerr = 1;
522 1.18 mrg if (write(rem, buf, amt) < 0)
523 1.18 mrg error("write of file data failed: %s\n", strerror(errno));
524 1.1 cgd }
525 1.1 cgd (void) close(f);
526 1.1 cgd done:
527 1.1 cgd if (sizerr) {
528 1.1 cgd error("%s: file changed size\n", target);
529 1.1 cgd err();
530 1.1 cgd } else
531 1.1 cgd ack();
532 1.1 cgd f = response();
533 1.13 lukem if (f < 0 || (f == 0 && (opts & COMPARE)))
534 1.1 cgd return;
535 1.1 cgd dospecial:
536 1.1 cgd for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
537 1.1 cgd if (sc->sc_type != SPECIAL)
538 1.1 cgd continue;
539 1.1 cgd if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
540 1.1 cgd continue;
541 1.24 thorpej dolog(lfp, "special \"%s\"\n", sc->sc_name);
542 1.1 cgd if (opts & VERIFY)
543 1.1 cgd continue;
544 1.9 thorpej (void) snprintf(buf, sizeof(buf), "SFILE=%s;%s\n", target,
545 1.9 thorpej sc->sc_name);
546 1.1 cgd if (debug)
547 1.1 cgd printf("buf = %s", buf);
548 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
549 1.18 mrg error("write of special failed: %s\n", strerror(errno));
550 1.1 cgd while (response() > 0)
551 1.1 cgd ;
552 1.1 cgd }
553 1.1 cgd }
554 1.1 cgd
555 1.5 cgd static struct linkbuf *
556 1.23 wiz savelink(struct stat *stp)
557 1.1 cgd {
558 1.1 cgd struct linkbuf *lp;
559 1.1 cgd
560 1.1 cgd for (lp = ihead; lp != NULL; lp = lp->nextp)
561 1.1 cgd if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) {
562 1.1 cgd lp->count--;
563 1.1 cgd return(lp);
564 1.1 cgd }
565 1.1 cgd lp = (struct linkbuf *) malloc(sizeof(*lp));
566 1.1 cgd if (lp == NULL)
567 1.24 thorpej dolog(lfp, "out of memory, link information lost\n");
568 1.1 cgd else {
569 1.1 cgd lp->nextp = ihead;
570 1.1 cgd ihead = lp;
571 1.1 cgd lp->inum = stp->st_ino;
572 1.1 cgd lp->devnum = stp->st_dev;
573 1.1 cgd lp->count = stp->st_nlink - 1;
574 1.1 cgd strcpy(lp->pathname, target);
575 1.1 cgd if (Tdest)
576 1.1 cgd strcpy(lp->target, Tdest);
577 1.1 cgd else
578 1.1 cgd *lp->target = 0;
579 1.1 cgd }
580 1.1 cgd return(NULL);
581 1.1 cgd }
582 1.1 cgd
583 1.1 cgd /*
584 1.1 cgd * Check to see if file needs to be updated on the remote machine.
585 1.1 cgd * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
586 1.1 cgd * and 3 if comparing binaries to determine if out of date.
587 1.1 cgd */
588 1.5 cgd static int
589 1.23 wiz update(char *rname, int opts, struct stat *stp)
590 1.1 cgd {
591 1.13 lukem char *cp, *s;
592 1.13 lukem off_t size;
593 1.13 lukem time_t mtime;
594 1.1 cgd
595 1.1 cgd if (debug)
596 1.13 lukem printf("update(%s, %lx, %lx)\n", rname, (long)opts, (long)stp);
597 1.1 cgd
598 1.1 cgd /*
599 1.1 cgd * Check to see if the file exists on the remote machine.
600 1.1 cgd */
601 1.9 thorpej (void) snprintf(buf, sizeof(buf), "Q%s\n", rname);
602 1.1 cgd if (debug)
603 1.1 cgd printf("buf = %s", buf);
604 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
605 1.18 mrg error("write to remote failed: %s\n", strerror(errno));
606 1.1 cgd again:
607 1.1 cgd cp = s = buf;
608 1.1 cgd do {
609 1.1 cgd if (read(rem, cp, 1) != 1)
610 1.5 cgd lostconn(0);
611 1.1 cgd } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
612 1.1 cgd
613 1.1 cgd switch (*s++) {
614 1.1 cgd case 'Y':
615 1.1 cgd break;
616 1.1 cgd
617 1.1 cgd case 'N': /* file doesn't exist so install it */
618 1.1 cgd return(1);
619 1.1 cgd
620 1.1 cgd case '\1':
621 1.1 cgd nerrs++;
622 1.1 cgd if (*s != '\n') {
623 1.1 cgd if (!iamremote) {
624 1.1 cgd fflush(stdout);
625 1.1 cgd (void) write(2, s, cp - s);
626 1.1 cgd }
627 1.1 cgd if (lfp != NULL)
628 1.1 cgd (void) fwrite(s, 1, cp - s, lfp);
629 1.1 cgd }
630 1.1 cgd return(0);
631 1.1 cgd
632 1.1 cgd case '\3':
633 1.1 cgd *--cp = '\0';
634 1.1 cgd if (lfp != NULL)
635 1.24 thorpej dolog(lfp, "update: note: %s\n", s);
636 1.1 cgd goto again;
637 1.1 cgd
638 1.1 cgd default:
639 1.1 cgd *--cp = '\0';
640 1.1 cgd error("update: unexpected response '%s'\n", s);
641 1.1 cgd return(0);
642 1.1 cgd }
643 1.1 cgd
644 1.1 cgd if (*s == '\n')
645 1.1 cgd return(2);
646 1.1 cgd
647 1.1 cgd if (opts & COMPARE)
648 1.1 cgd return(3);
649 1.1 cgd
650 1.1 cgd size = 0;
651 1.17 christos while (isdigit((unsigned char)*s))
652 1.1 cgd size = size * 10 + (*s++ - '0');
653 1.1 cgd if (*s++ != ' ') {
654 1.1 cgd error("update: size not delimited\n");
655 1.1 cgd return(0);
656 1.1 cgd }
657 1.1 cgd mtime = 0;
658 1.17 christos while (isdigit((unsigned char)*s))
659 1.1 cgd mtime = mtime * 10 + (*s++ - '0');
660 1.1 cgd if (*s != '\n') {
661 1.1 cgd error("update: mtime not delimited\n");
662 1.1 cgd return(0);
663 1.1 cgd }
664 1.1 cgd /*
665 1.1 cgd * File needs to be updated?
666 1.1 cgd */
667 1.1 cgd if (opts & YOUNGER) {
668 1.1 cgd if (stp->st_mtime == mtime)
669 1.1 cgd return(0);
670 1.1 cgd if (stp->st_mtime < mtime) {
671 1.24 thorpej dolog(lfp, "Warning: %s: remote copy is newer\n",
672 1.24 thorpej target);
673 1.1 cgd return(0);
674 1.1 cgd }
675 1.1 cgd } else if (stp->st_mtime == mtime && stp->st_size == size)
676 1.1 cgd return(0);
677 1.1 cgd return(2);
678 1.1 cgd }
679 1.1 cgd
680 1.1 cgd /*
681 1.1 cgd * Query. Check to see if file exists. Return one of the following:
682 1.1 cgd * N\n - doesn't exist
683 1.1 cgd * Ysize mtime\n - exists and its a regular file (size & mtime of file)
684 1.1 cgd * Y\n - exists and its a directory or symbolic link
685 1.1 cgd * ^Aerror message\n
686 1.1 cgd */
687 1.5 cgd static void
688 1.23 wiz query(char *name)
689 1.1 cgd {
690 1.1 cgd struct stat stb;
691 1.1 cgd
692 1.1 cgd if (catname)
693 1.10 thorpej (void) snprintf(tp, sizeof(target) - (tp - target),
694 1.10 thorpej "/%s", name);
695 1.1 cgd
696 1.1 cgd if (lstat(target, &stb) < 0) {
697 1.18 mrg if (errno == ENOENT) {
698 1.18 mrg if (write(rem, "N\n", 2) < 0)
699 1.18 mrg error("write to remote failed: %s\n",
700 1.18 mrg strerror(errno));
701 1.18 mrg } else
702 1.1 cgd error("%s:%s: %s\n", host, target, strerror(errno));
703 1.1 cgd *tp = '\0';
704 1.1 cgd return;
705 1.1 cgd }
706 1.1 cgd
707 1.1 cgd switch (stb.st_mode & S_IFMT) {
708 1.1 cgd case S_IFREG:
709 1.20 lukem (void)snprintf(buf, sizeof(buf), "Y%lld %ld\n",
710 1.15 mrg (unsigned long long)stb.st_size, (u_long)stb.st_mtime);
711 1.18 mrg if (write(rem, buf, strlen(buf)) < 0)
712 1.18 mrg error("write to remote failed: %s\n", strerror(errno));
713 1.1 cgd break;
714 1.1 cgd
715 1.1 cgd case S_IFLNK:
716 1.1 cgd case S_IFDIR:
717 1.18 mrg if (write(rem, "Y\n", 2) < 0)
718 1.18 mrg error("write to remote failed: %s\n", strerror(errno));
719 1.1 cgd break;
720 1.1 cgd
721 1.1 cgd default:
722 1.1 cgd error("%s: not a file or directory\n", name);
723 1.1 cgd break;
724 1.1 cgd }
725 1.1 cgd *tp = '\0';
726 1.1 cgd }
727 1.1 cgd
728 1.5 cgd static void
729 1.23 wiz recvf(char *cmd, int type)
730 1.1 cgd {
731 1.13 lukem char *cp = cmd;
732 1.14 mycroft int f = -1, opts = 0, wrerr, olderrno;
733 1.14 mycroft mode_t mode;
734 1.1 cgd off_t i, size;
735 1.1 cgd time_t mtime;
736 1.1 cgd struct stat stb;
737 1.1 cgd char *owner, *group;
738 1.1 cgd char new[BUFSIZ];
739 1.1 cgd extern char *tempname;
740 1.1 cgd
741 1.1 cgd while (*cp >= '0' && *cp <= '7')
742 1.1 cgd opts = (opts << 3) | (*cp++ - '0');
743 1.1 cgd if (*cp++ != ' ') {
744 1.1 cgd error("recvf: options not delimited\n");
745 1.1 cgd return;
746 1.1 cgd }
747 1.1 cgd mode = 0;
748 1.1 cgd while (*cp >= '0' && *cp <= '7')
749 1.1 cgd mode = (mode << 3) | (*cp++ - '0');
750 1.1 cgd if (*cp++ != ' ') {
751 1.1 cgd error("recvf: mode not delimited\n");
752 1.1 cgd return;
753 1.1 cgd }
754 1.1 cgd size = 0;
755 1.17 christos while (isdigit((unsigned char)*cp))
756 1.1 cgd size = size * 10 + (*cp++ - '0');
757 1.1 cgd if (*cp++ != ' ') {
758 1.1 cgd error("recvf: size not delimited\n");
759 1.1 cgd return;
760 1.1 cgd }
761 1.1 cgd mtime = 0;
762 1.17 christos while (isdigit((unsigned char)*cp))
763 1.1 cgd mtime = mtime * 10 + (*cp++ - '0');
764 1.1 cgd if (*cp++ != ' ') {
765 1.1 cgd error("recvf: mtime not delimited\n");
766 1.1 cgd return;
767 1.1 cgd }
768 1.1 cgd owner = cp;
769 1.1 cgd while (*cp && *cp != ' ')
770 1.1 cgd cp++;
771 1.1 cgd if (*cp != ' ') {
772 1.1 cgd error("recvf: owner name not delimited\n");
773 1.1 cgd return;
774 1.1 cgd }
775 1.1 cgd *cp++ = '\0';
776 1.1 cgd group = cp;
777 1.1 cgd while (*cp && *cp != ' ')
778 1.1 cgd cp++;
779 1.1 cgd if (*cp != ' ') {
780 1.1 cgd error("recvf: group name not delimited\n");
781 1.1 cgd return;
782 1.1 cgd }
783 1.1 cgd *cp++ = '\0';
784 1.1 cgd
785 1.1 cgd if (type == S_IFDIR) {
786 1.1 cgd if (catname >= sizeof(stp)) {
787 1.1 cgd error("%s:%s: too many directory levels\n",
788 1.1 cgd host, target);
789 1.1 cgd return;
790 1.1 cgd }
791 1.1 cgd stp[catname] = tp;
792 1.1 cgd if (catname++) {
793 1.1 cgd *tp++ = '/';
794 1.13 lukem while ((*tp++ = *cp++) != 0)
795 1.1 cgd ;
796 1.1 cgd tp--;
797 1.1 cgd }
798 1.1 cgd if (opts & VERIFY) {
799 1.1 cgd ack();
800 1.1 cgd return;
801 1.1 cgd }
802 1.1 cgd if (lstat(target, &stb) == 0) {
803 1.14 mycroft if (S_ISDIR(stb.st_mode)) {
804 1.1 cgd if ((stb.st_mode & 07777) == mode) {
805 1.1 cgd ack();
806 1.1 cgd return;
807 1.1 cgd }
808 1.1 cgd buf[0] = '\0';
809 1.9 thorpej (void) snprintf(buf + 1, sizeof(buf) - 1,
810 1.18 mrg "%s: Warning: remote mode %o != local mode %o\n",
811 1.18 mrg target, stb.st_mode & 07777, mode);
812 1.18 mrg if (write(rem, buf, strlen(buf + 1) + 1) < 0)
813 1.18 mrg error("write to remote failed: %s\n",
814 1.18 mrg strerror(errno));
815 1.1 cgd return;
816 1.1 cgd }
817 1.1 cgd errno = ENOTDIR;
818 1.14 mycroft } else if ((errno == ENOENT && mkdir(target, mode) == 0) ||
819 1.13 lukem (chkparent(target) == 0 && mkdir(target, mode) == 0)) {
820 1.14 mycroft if (fchtogm(-1, target, mtime, owner, group, mode) == 0)
821 1.1 cgd ack();
822 1.1 cgd return;
823 1.1 cgd }
824 1.1 cgd error("%s:%s: %s\n", host, target, strerror(errno));
825 1.1 cgd tp = stp[--catname];
826 1.1 cgd *tp = '\0';
827 1.1 cgd return;
828 1.1 cgd }
829 1.1 cgd
830 1.1 cgd if (catname)
831 1.10 thorpej (void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
832 1.13 lukem cp = strrchr(target, '/');
833 1.1 cgd if (cp == NULL)
834 1.1 cgd strcpy(new, tempname);
835 1.1 cgd else if (cp == target)
836 1.9 thorpej (void) snprintf(new, sizeof(new), "/%s", tempname);
837 1.1 cgd else {
838 1.1 cgd *cp = '\0';
839 1.9 thorpej (void) snprintf(new, sizeof(new), "%s/%s", target, tempname);
840 1.1 cgd *cp = '/';
841 1.1 cgd }
842 1.1 cgd
843 1.1 cgd if (type == S_IFLNK) {
844 1.1 cgd int j;
845 1.1 cgd
846 1.1 cgd ack();
847 1.1 cgd cp = buf;
848 1.1 cgd for (i = 0; i < size; i += j) {
849 1.1 cgd if ((j = read(rem, cp, size - i)) <= 0)
850 1.5 cgd cleanup(0);
851 1.1 cgd cp += j;
852 1.1 cgd }
853 1.1 cgd *cp = '\0';
854 1.1 cgd if (response() < 0) {
855 1.1 cgd err();
856 1.1 cgd return;
857 1.1 cgd }
858 1.1 cgd if (symlink(buf, new) < 0) {
859 1.1 cgd if (errno != ENOENT || chkparent(new) < 0 ||
860 1.1 cgd symlink(buf, new) < 0)
861 1.5 cgd goto badnew1;
862 1.1 cgd }
863 1.1 cgd mode &= 0777;
864 1.1 cgd if (opts & COMPARE) {
865 1.1 cgd char tbuf[BUFSIZ];
866 1.1 cgd
867 1.1 cgd if ((i = readlink(target, tbuf, BUFSIZ)) >= 0 &&
868 1.1 cgd i == size && strncmp(buf, tbuf, size) == 0) {
869 1.1 cgd (void) unlink(new);
870 1.1 cgd ack();
871 1.1 cgd return;
872 1.1 cgd }
873 1.1 cgd if (opts & VERIFY)
874 1.1 cgd goto differ;
875 1.1 cgd }
876 1.1 cgd goto fixup;
877 1.1 cgd }
878 1.1 cgd
879 1.1 cgd if ((f = creat(new, mode)) < 0) {
880 1.1 cgd if (errno != ENOENT || chkparent(new) < 0 ||
881 1.1 cgd (f = creat(new, mode)) < 0)
882 1.5 cgd goto badnew1;
883 1.1 cgd }
884 1.1 cgd
885 1.1 cgd ack();
886 1.1 cgd wrerr = 0;
887 1.1 cgd for (i = 0; i < size; i += BUFSIZ) {
888 1.1 cgd int amt = BUFSIZ;
889 1.1 cgd
890 1.1 cgd cp = buf;
891 1.1 cgd if (i + amt > size)
892 1.1 cgd amt = size - i;
893 1.1 cgd do {
894 1.1 cgd int j = read(rem, cp, amt);
895 1.1 cgd
896 1.1 cgd if (j <= 0) {
897 1.1 cgd (void) close(f);
898 1.1 cgd (void) unlink(new);
899 1.5 cgd cleanup(0);
900 1.1 cgd }
901 1.1 cgd amt -= j;
902 1.1 cgd cp += j;
903 1.1 cgd } while (amt > 0);
904 1.1 cgd amt = BUFSIZ;
905 1.1 cgd if (i + amt > size)
906 1.1 cgd amt = size - i;
907 1.1 cgd if (wrerr == 0 && write(f, buf, amt) != amt) {
908 1.1 cgd olderrno = errno;
909 1.1 cgd wrerr++;
910 1.1 cgd }
911 1.1 cgd }
912 1.1 cgd if (response() < 0) {
913 1.1 cgd err();
914 1.5 cgd goto badnew2;
915 1.1 cgd }
916 1.5 cgd if (wrerr)
917 1.5 cgd goto badnew1;
918 1.1 cgd if (opts & COMPARE) {
919 1.1 cgd FILE *f1, *f2;
920 1.1 cgd int c;
921 1.1 cgd
922 1.1 cgd if ((f1 = fopen(target, "r")) == NULL)
923 1.5 cgd goto badtarget;
924 1.1 cgd if ((f2 = fopen(new, "r")) == NULL) {
925 1.5 cgd badnew1: error("%s:%s: %s\n", host, new, strerror(errno));
926 1.5 cgd goto badnew2;
927 1.1 cgd }
928 1.1 cgd while ((c = getc(f1)) == getc(f2))
929 1.1 cgd if (c == EOF) {
930 1.1 cgd (void) fclose(f1);
931 1.1 cgd (void) fclose(f2);
932 1.1 cgd ack();
933 1.5 cgd goto badnew2;
934 1.1 cgd }
935 1.1 cgd (void) fclose(f1);
936 1.1 cgd (void) fclose(f2);
937 1.1 cgd if (opts & VERIFY) {
938 1.5 cgd differ: buf[0] = '\0';
939 1.12 mrg (void)snprintf(buf + 1, sizeof(buf) - 1,
940 1.9 thorpej "need to update: %s\n",target);
941 1.1 cgd (void) write(rem, buf, strlen(buf + 1) + 1);
942 1.5 cgd goto badnew2;
943 1.1 cgd }
944 1.1 cgd }
945 1.1 cgd
946 1.14 mycroft if (fchtogm(f, new, mtime, owner, group, mode) < 0) {
947 1.12 mrg badnew2:
948 1.8 mrg if (f != -1)
949 1.8 mrg (void) close(f);
950 1.1 cgd (void) unlink(new);
951 1.1 cgd return;
952 1.1 cgd }
953 1.5 cgd (void) close(f);
954 1.5 cgd
955 1.5 cgd fixup: if (rename(new, target) < 0) {
956 1.5 cgd badtarget: error("%s:%s: %s\n", host, target, strerror(errno));
957 1.1 cgd (void) unlink(new);
958 1.1 cgd return;
959 1.1 cgd }
960 1.5 cgd
961 1.1 cgd if (opts & COMPARE) {
962 1.1 cgd buf[0] = '\0';
963 1.9 thorpej (void) snprintf(buf + 1, sizeof(buf) - 1,
964 1.9 thorpej "updated %s\n", target);
965 1.1 cgd (void) write(rem, buf, strlen(buf + 1) + 1);
966 1.1 cgd } else
967 1.1 cgd ack();
968 1.1 cgd }
969 1.1 cgd
970 1.1 cgd /*
971 1.1 cgd * Creat a hard link to existing file.
972 1.1 cgd */
973 1.5 cgd static void
974 1.23 wiz hardlink(char *cmd)
975 1.1 cgd {
976 1.13 lukem char *cp;
977 1.1 cgd struct stat stb;
978 1.1 cgd char *oldname;
979 1.1 cgd int opts, exists = 0;
980 1.1 cgd
981 1.1 cgd cp = cmd;
982 1.1 cgd opts = 0;
983 1.1 cgd while (*cp >= '0' && *cp <= '7')
984 1.1 cgd opts = (opts << 3) | (*cp++ - '0');
985 1.1 cgd if (*cp++ != ' ') {
986 1.1 cgd error("hardlink: options not delimited\n");
987 1.1 cgd return;
988 1.1 cgd }
989 1.1 cgd oldname = cp;
990 1.1 cgd while (*cp && *cp != ' ')
991 1.1 cgd cp++;
992 1.1 cgd if (*cp != ' ') {
993 1.1 cgd error("hardlink: oldname name not delimited\n");
994 1.1 cgd return;
995 1.1 cgd }
996 1.1 cgd *cp++ = '\0';
997 1.1 cgd
998 1.1 cgd if (catname) {
999 1.10 thorpej (void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
1000 1.1 cgd }
1001 1.1 cgd if (lstat(target, &stb) == 0) {
1002 1.14 mycroft if (!S_ISREG(stb.st_mode) && !S_ISLNK(stb.st_mode)) {
1003 1.14 mycroft error("%s: %s: not a regular file\n", host, target);
1004 1.1 cgd return;
1005 1.1 cgd }
1006 1.1 cgd exists = 1;
1007 1.1 cgd }
1008 1.1 cgd if (chkparent(target) < 0 ) {
1009 1.1 cgd error("%s:%s: %s (no parent)\n",
1010 1.1 cgd host, target, strerror(errno));
1011 1.1 cgd return;
1012 1.1 cgd }
1013 1.1 cgd if (exists && (unlink(target) < 0)) {
1014 1.1 cgd error("%s:%s: %s (unlink)\n",
1015 1.1 cgd host, target, strerror(errno));
1016 1.1 cgd return;
1017 1.1 cgd }
1018 1.1 cgd if (link(oldname, target) < 0) {
1019 1.1 cgd error("%s:can't link %s to %s\n",
1020 1.1 cgd host, target, oldname);
1021 1.1 cgd return;
1022 1.1 cgd }
1023 1.1 cgd ack();
1024 1.1 cgd }
1025 1.1 cgd
1026 1.1 cgd /*
1027 1.1 cgd * Check to see if parent directory exists and create one if not.
1028 1.1 cgd */
1029 1.5 cgd static int
1030 1.23 wiz chkparent(char *name)
1031 1.1 cgd {
1032 1.13 lukem char *cp;
1033 1.1 cgd struct stat stb;
1034 1.1 cgd
1035 1.13 lukem cp = strrchr(name, '/');
1036 1.1 cgd if (cp == NULL || cp == name)
1037 1.1 cgd return(0);
1038 1.1 cgd *cp = '\0';
1039 1.1 cgd if (lstat(name, &stb) < 0) {
1040 1.1 cgd if (errno == ENOENT && chkparent(name) >= 0 &&
1041 1.1 cgd mkdir(name, 0777 & ~oumask) >= 0) {
1042 1.1 cgd *cp = '/';
1043 1.1 cgd return(0);
1044 1.1 cgd }
1045 1.14 mycroft } else if (S_ISDIR(stb.st_mode)) {
1046 1.1 cgd *cp = '/';
1047 1.1 cgd return(0);
1048 1.1 cgd }
1049 1.1 cgd *cp = '/';
1050 1.1 cgd return(-1);
1051 1.1 cgd }
1052 1.1 cgd
1053 1.1 cgd /*
1054 1.1 cgd * Change owner, group and mode of file.
1055 1.1 cgd */
1056 1.5 cgd static int
1057 1.23 wiz fchtogm(int fd, char *file, time_t mtime, char *owner, char *group, __mode_t mode)
1058 1.1 cgd {
1059 1.13 lukem int i;
1060 1.14 mycroft struct timeval tv[2];
1061 1.14 mycroft uid_t uid;
1062 1.14 mycroft gid_t gid;
1063 1.1 cgd extern char user[];
1064 1.1 cgd
1065 1.1 cgd uid = userid;
1066 1.1 cgd if (userid == 0) {
1067 1.1 cgd if (*owner == ':') {
1068 1.1 cgd uid = atoi(owner + 1);
1069 1.1 cgd } else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
1070 1.1 cgd if ((pw = getpwnam(owner)) == NULL) {
1071 1.1 cgd if (mode & 04000) {
1072 1.1 cgd note("%s:%s: unknown login name, clearing setuid",
1073 1.1 cgd host, owner);
1074 1.1 cgd mode &= ~04000;
1075 1.1 cgd uid = 0;
1076 1.1 cgd }
1077 1.1 cgd } else
1078 1.1 cgd uid = pw->pw_uid;
1079 1.1 cgd } else
1080 1.1 cgd uid = pw->pw_uid;
1081 1.1 cgd if (*group == ':') {
1082 1.1 cgd gid = atoi(group + 1);
1083 1.1 cgd goto ok;
1084 1.1 cgd }
1085 1.1 cgd } else if ((mode & 04000) && strcmp(user, owner) != 0)
1086 1.1 cgd mode &= ~04000;
1087 1.1 cgd gid = -1;
1088 1.1 cgd if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
1089 1.1 cgd if ((*group == ':' && (getgrgid(gid = atoi(group + 1)) == NULL))
1090 1.1 cgd || ((gr = getgrnam(group)) == NULL)) {
1091 1.1 cgd if (mode & 02000) {
1092 1.1 cgd note("%s:%s: unknown group", host, group);
1093 1.1 cgd mode &= ~02000;
1094 1.1 cgd }
1095 1.1 cgd } else
1096 1.1 cgd gid = gr->gr_gid;
1097 1.1 cgd } else
1098 1.1 cgd gid = gr->gr_gid;
1099 1.1 cgd if (userid && gid >= 0) {
1100 1.1 cgd if (gr) for (i = 0; gr->gr_mem[i] != NULL; i++)
1101 1.1 cgd if (!(strcmp(user, gr->gr_mem[i])))
1102 1.1 cgd goto ok;
1103 1.1 cgd mode &= ~02000;
1104 1.1 cgd gid = -1;
1105 1.1 cgd }
1106 1.14 mycroft ok:
1107 1.14 mycroft (void) gettimeofday(&tv[0], (struct timezone *)0);
1108 1.14 mycroft tv[1].tv_sec = mtime;
1109 1.14 mycroft tv[1].tv_usec = 0;
1110 1.14 mycroft if (fd != -1 ? futimes(fd, tv) < 0 : utimes(file, tv) < 0)
1111 1.14 mycroft note("%s: %s utimes: %s", host, file, strerror(errno));
1112 1.14 mycroft if (fd != -1 ? fchown(fd, uid, gid) < 0 : chown(file, uid, gid) < 0)
1113 1.5 cgd note("%s: %s chown: %s", host, file, strerror(errno));
1114 1.5 cgd else if (mode & 07000 &&
1115 1.14 mycroft (fd != -1 ? fchmod(fd, mode) < 0 : chmod(file, mode) < 0))
1116 1.5 cgd note("%s: %s chmod: %s", host, file, strerror(errno));
1117 1.1 cgd return(0);
1118 1.1 cgd }
1119 1.1 cgd
1120 1.1 cgd /*
1121 1.1 cgd * Check for files on the machine being updated that are not on the master
1122 1.1 cgd * machine and remove them.
1123 1.1 cgd */
1124 1.5 cgd static void
1125 1.23 wiz rmchk(int opts)
1126 1.1 cgd {
1127 1.13 lukem char *cp, *s;
1128 1.1 cgd struct stat stb;
1129 1.1 cgd
1130 1.1 cgd if (debug)
1131 1.1 cgd printf("rmchk()\n");
1132 1.1 cgd
1133 1.1 cgd /*
1134 1.1 cgd * Tell the remote to clean the files from the last directory sent.
1135 1.1 cgd */
1136 1.9 thorpej (void) snprintf(buf, sizeof(buf), "C%o\n", opts & VERIFY);
1137 1.1 cgd if (debug)
1138 1.1 cgd printf("buf = %s", buf);
1139 1.1 cgd (void) write(rem, buf, strlen(buf));
1140 1.1 cgd if (response() < 0)
1141 1.1 cgd return;
1142 1.1 cgd for (;;) {
1143 1.1 cgd cp = s = buf;
1144 1.1 cgd do {
1145 1.1 cgd if (read(rem, cp, 1) != 1)
1146 1.5 cgd lostconn(0);
1147 1.1 cgd } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
1148 1.1 cgd
1149 1.1 cgd switch (*s++) {
1150 1.1 cgd case 'Q': /* Query if file should be removed */
1151 1.1 cgd /*
1152 1.1 cgd * Return the following codes to remove query.
1153 1.1 cgd * N\n -- file exists - DON'T remove.
1154 1.1 cgd * Y\n -- file doesn't exist - REMOVE.
1155 1.1 cgd */
1156 1.1 cgd *--cp = '\0';
1157 1.10 thorpej (void) snprintf(tp, sizeof(target) - (tp - target),
1158 1.10 thorpej "/%s", s);
1159 1.1 cgd if (debug)
1160 1.1 cgd printf("check %s\n", target);
1161 1.1 cgd if (except(target))
1162 1.1 cgd (void) write(rem, "N\n", 2);
1163 1.1 cgd else if (lstat(target, &stb) < 0)
1164 1.1 cgd (void) write(rem, "Y\n", 2);
1165 1.1 cgd else
1166 1.1 cgd (void) write(rem, "N\n", 2);
1167 1.1 cgd break;
1168 1.1 cgd
1169 1.1 cgd case '\0':
1170 1.1 cgd *--cp = '\0';
1171 1.1 cgd if (*s != '\0')
1172 1.24 thorpej dolog(lfp, "%s\n", s);
1173 1.1 cgd break;
1174 1.1 cgd
1175 1.1 cgd case 'E':
1176 1.1 cgd *tp = '\0';
1177 1.1 cgd ack();
1178 1.1 cgd return;
1179 1.1 cgd
1180 1.1 cgd case '\1':
1181 1.1 cgd case '\2':
1182 1.1 cgd nerrs++;
1183 1.1 cgd if (*s != '\n') {
1184 1.1 cgd if (!iamremote) {
1185 1.1 cgd fflush(stdout);
1186 1.1 cgd (void) write(2, s, cp - s);
1187 1.1 cgd }
1188 1.1 cgd if (lfp != NULL)
1189 1.1 cgd (void) fwrite(s, 1, cp - s, lfp);
1190 1.1 cgd }
1191 1.1 cgd if (buf[0] == '\2')
1192 1.5 cgd lostconn(0);
1193 1.1 cgd break;
1194 1.1 cgd
1195 1.1 cgd default:
1196 1.1 cgd error("rmchk: unexpected response '%s'\n", buf);
1197 1.1 cgd err();
1198 1.1 cgd }
1199 1.1 cgd }
1200 1.1 cgd }
1201 1.1 cgd
1202 1.1 cgd /*
1203 1.1 cgd * Check the current directory (initialized by the 'T' command to server())
1204 1.1 cgd * for extraneous files and remove them.
1205 1.1 cgd */
1206 1.5 cgd static void
1207 1.23 wiz clean(char *cp)
1208 1.1 cgd {
1209 1.1 cgd DIR *d;
1210 1.16 christos struct dirent *dp;
1211 1.1 cgd struct stat stb;
1212 1.1 cgd char *otp;
1213 1.1 cgd int len, opts;
1214 1.1 cgd
1215 1.1 cgd opts = 0;
1216 1.1 cgd while (*cp >= '0' && *cp <= '7')
1217 1.1 cgd opts = (opts << 3) | (*cp++ - '0');
1218 1.1 cgd if (*cp != '\0') {
1219 1.1 cgd error("clean: options not delimited\n");
1220 1.1 cgd return;
1221 1.1 cgd }
1222 1.1 cgd if ((d = opendir(target)) == NULL) {
1223 1.1 cgd error("%s:%s: %s\n", host, target, strerror(errno));
1224 1.1 cgd return;
1225 1.1 cgd }
1226 1.1 cgd ack();
1227 1.1 cgd
1228 1.1 cgd otp = tp;
1229 1.1 cgd len = tp - target;
1230 1.13 lukem while ((dp = readdir(d)) != NULL) {
1231 1.1 cgd if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1232 1.1 cgd continue;
1233 1.1 cgd if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
1234 1.1 cgd error("%s:%s/%s: Name too long\n",
1235 1.1 cgd host, target, dp->d_name);
1236 1.1 cgd continue;
1237 1.1 cgd }
1238 1.1 cgd tp = otp;
1239 1.1 cgd *tp++ = '/';
1240 1.25 simonb cp = dp->d_name;
1241 1.13 lukem while ((*tp++ = *cp++) != 0)
1242 1.1 cgd ;
1243 1.1 cgd tp--;
1244 1.1 cgd if (lstat(target, &stb) < 0) {
1245 1.1 cgd error("%s:%s: %s\n", host, target, strerror(errno));
1246 1.1 cgd continue;
1247 1.1 cgd }
1248 1.9 thorpej (void) snprintf(buf, sizeof(buf), "Q%s\n", dp->d_name);
1249 1.1 cgd (void) write(rem, buf, strlen(buf));
1250 1.1 cgd cp = buf;
1251 1.1 cgd do {
1252 1.1 cgd if (read(rem, cp, 1) != 1)
1253 1.5 cgd cleanup(0);
1254 1.1 cgd } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
1255 1.1 cgd *--cp = '\0';
1256 1.1 cgd cp = buf;
1257 1.1 cgd if (*cp != 'Y')
1258 1.1 cgd continue;
1259 1.1 cgd if (opts & VERIFY) {
1260 1.1 cgd cp = buf;
1261 1.1 cgd *cp++ = '\0';
1262 1.9 thorpej (void) snprintf(cp, sizeof(buf) - 1,
1263 1.9 thorpej "need to remove: %s\n", target);
1264 1.1 cgd (void) write(rem, buf, strlen(cp) + 1);
1265 1.1 cgd } else
1266 1.1 cgd removeit(&stb);
1267 1.1 cgd }
1268 1.1 cgd closedir(d);
1269 1.1 cgd (void) write(rem, "E\n", 2);
1270 1.1 cgd (void) response();
1271 1.1 cgd tp = otp;
1272 1.1 cgd *tp = '\0';
1273 1.1 cgd }
1274 1.1 cgd
1275 1.1 cgd /*
1276 1.1 cgd * Remove a file or directory (recursively) and send back an acknowledge
1277 1.1 cgd * or an error message.
1278 1.1 cgd */
1279 1.5 cgd static void
1280 1.23 wiz removeit(struct stat *stp)
1281 1.1 cgd {
1282 1.1 cgd DIR *d;
1283 1.16 christos struct dirent *dp;
1284 1.13 lukem char *cp;
1285 1.1 cgd struct stat stb;
1286 1.1 cgd char *otp;
1287 1.1 cgd int len;
1288 1.1 cgd
1289 1.1 cgd switch (stp->st_mode & S_IFMT) {
1290 1.1 cgd case S_IFREG:
1291 1.1 cgd case S_IFLNK:
1292 1.1 cgd if (unlink(target) < 0)
1293 1.1 cgd goto bad;
1294 1.1 cgd goto removed;
1295 1.1 cgd
1296 1.1 cgd case S_IFDIR:
1297 1.1 cgd break;
1298 1.1 cgd
1299 1.1 cgd default:
1300 1.1 cgd error("%s:%s: not a plain file\n", host, target);
1301 1.1 cgd return;
1302 1.1 cgd }
1303 1.1 cgd
1304 1.1 cgd if ((d = opendir(target)) == NULL)
1305 1.1 cgd goto bad;
1306 1.1 cgd
1307 1.1 cgd otp = tp;
1308 1.1 cgd len = tp - target;
1309 1.13 lukem while ((dp = readdir(d)) != NULL) {
1310 1.1 cgd if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1311 1.1 cgd continue;
1312 1.1 cgd if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
1313 1.1 cgd error("%s:%s/%s: Name too long\n",
1314 1.1 cgd host, target, dp->d_name);
1315 1.1 cgd continue;
1316 1.1 cgd }
1317 1.1 cgd tp = otp;
1318 1.1 cgd *tp++ = '/';
1319 1.25 simonb cp = dp->d_name;
1320 1.13 lukem while ((*tp++ = *cp++) != 0)
1321 1.1 cgd ;
1322 1.1 cgd tp--;
1323 1.1 cgd if (lstat(target, &stb) < 0) {
1324 1.1 cgd error("%s:%s: %s\n", host, target, strerror(errno));
1325 1.1 cgd continue;
1326 1.1 cgd }
1327 1.1 cgd removeit(&stb);
1328 1.1 cgd }
1329 1.1 cgd closedir(d);
1330 1.1 cgd tp = otp;
1331 1.1 cgd *tp = '\0';
1332 1.1 cgd if (rmdir(target) < 0) {
1333 1.1 cgd bad:
1334 1.1 cgd error("%s:%s: %s\n", host, target, strerror(errno));
1335 1.1 cgd return;
1336 1.1 cgd }
1337 1.1 cgd removed:
1338 1.1 cgd cp = buf;
1339 1.1 cgd *cp++ = '\0';
1340 1.9 thorpej (void) snprintf(cp, sizeof(buf) - 1, "removed %s\n", target);
1341 1.1 cgd (void) write(rem, buf, strlen(cp) + 1);
1342 1.1 cgd }
1343 1.1 cgd
1344 1.1 cgd /*
1345 1.1 cgd * Execute a shell command to handle special cases.
1346 1.1 cgd */
1347 1.5 cgd static void
1348 1.23 wiz dospecial(char *cmd)
1349 1.1 cgd {
1350 1.1 cgd int fd[2], status, pid, i;
1351 1.13 lukem char *cp, *s;
1352 1.1 cgd char sbuf[BUFSIZ];
1353 1.1 cgd
1354 1.1 cgd if (pipe(fd) < 0) {
1355 1.1 cgd error("%s\n", strerror(errno));
1356 1.1 cgd return;
1357 1.1 cgd }
1358 1.1 cgd if ((pid = fork()) == 0) {
1359 1.1 cgd /*
1360 1.1 cgd * Return everything the shell commands print.
1361 1.1 cgd */
1362 1.1 cgd (void) close(0);
1363 1.1 cgd (void) close(1);
1364 1.1 cgd (void) close(2);
1365 1.1 cgd (void) open(_PATH_DEVNULL, O_RDONLY);
1366 1.1 cgd (void) dup(fd[1]);
1367 1.1 cgd (void) dup(fd[1]);
1368 1.1 cgd (void) close(fd[0]);
1369 1.1 cgd (void) close(fd[1]);
1370 1.1 cgd setgid(groupid);
1371 1.1 cgd setuid(userid);
1372 1.1 cgd execl(_PATH_BSHELL, "sh", "-c", cmd, 0);
1373 1.1 cgd _exit(127);
1374 1.1 cgd }
1375 1.1 cgd (void) close(fd[1]);
1376 1.1 cgd s = sbuf;
1377 1.1 cgd *s++ = '\0';
1378 1.1 cgd while ((i = read(fd[0], buf, sizeof(buf))) > 0) {
1379 1.1 cgd cp = buf;
1380 1.1 cgd do {
1381 1.1 cgd *s++ = *cp++;
1382 1.1 cgd if (cp[-1] != '\n') {
1383 1.1 cgd if (s < &sbuf[sizeof(sbuf)-1])
1384 1.1 cgd continue;
1385 1.1 cgd *s++ = '\n';
1386 1.1 cgd }
1387 1.1 cgd /*
1388 1.1 cgd * Throw away blank lines.
1389 1.1 cgd */
1390 1.1 cgd if (s == &sbuf[2]) {
1391 1.1 cgd s--;
1392 1.1 cgd continue;
1393 1.1 cgd }
1394 1.1 cgd (void) write(rem, sbuf, s - sbuf);
1395 1.1 cgd s = &sbuf[1];
1396 1.1 cgd } while (--i);
1397 1.1 cgd }
1398 1.1 cgd if (s > &sbuf[1]) {
1399 1.1 cgd *s++ = '\n';
1400 1.1 cgd (void) write(rem, sbuf, s - sbuf);
1401 1.1 cgd }
1402 1.1 cgd while ((i = wait(&status)) != pid && i != -1)
1403 1.1 cgd ;
1404 1.1 cgd if (i == -1)
1405 1.1 cgd status = -1;
1406 1.1 cgd (void) close(fd[0]);
1407 1.1 cgd if (status)
1408 1.1 cgd error("shell returned %d\n", status);
1409 1.1 cgd else
1410 1.1 cgd ack();
1411 1.1 cgd }
1412 1.1 cgd
1413 1.5 cgd
1414 1.5 cgd void
1415 1.24 thorpej dolog(FILE *fp, const char *fmt, ...)
1416 1.1 cgd {
1417 1.5 cgd va_list ap;
1418 1.22 wiz
1419 1.22 wiz /* Print changes locally if not quiet mode */
1420 1.22 wiz if (!qflag) {
1421 1.22 wiz va_start(ap, fmt);
1422 1.5 cgd (void)vprintf(fmt, ap);
1423 1.22 wiz va_end(ap);
1424 1.22 wiz }
1425 1.1 cgd
1426 1.1 cgd /* Save changes (for mailing) if really updating files */
1427 1.22 wiz if (!(options & VERIFY) && fp != NULL) {
1428 1.22 wiz va_start(ap, fmt);
1429 1.5 cgd (void)vfprintf(fp, fmt, ap);
1430 1.22 wiz va_end(ap);
1431 1.22 wiz }
1432 1.1 cgd }
1433 1.1 cgd
1434 1.5 cgd void
1435 1.5 cgd error(const char *fmt, ...)
1436 1.1 cgd {
1437 1.1 cgd static FILE *fp;
1438 1.5 cgd va_list ap;
1439 1.22 wiz
1440 1.22 wiz ++nerrs;
1441 1.22 wiz if (!fp && !(fp = fdopen(rem, "w")))
1442 1.22 wiz return;
1443 1.5 cgd va_start(ap, fmt);
1444 1.1 cgd if (iamremote) {
1445 1.1 cgd (void)fprintf(fp, "%crdist: ", 0x01);
1446 1.5 cgd (void)vfprintf(fp, fmt, ap);
1447 1.1 cgd fflush(fp);
1448 1.1 cgd }
1449 1.1 cgd else {
1450 1.1 cgd fflush(stdout);
1451 1.1 cgd (void)fprintf(stderr, "rdist: ");
1452 1.5 cgd (void)vfprintf(stderr, fmt, ap);
1453 1.1 cgd fflush(stderr);
1454 1.1 cgd }
1455 1.22 wiz va_end(ap);
1456 1.1 cgd if (lfp != NULL) {
1457 1.1 cgd (void)fprintf(lfp, "rdist: ");
1458 1.22 wiz va_start(ap, fmt);
1459 1.5 cgd (void)vfprintf(lfp, fmt, ap);
1460 1.22 wiz va_end(ap);
1461 1.1 cgd fflush(lfp);
1462 1.1 cgd }
1463 1.1 cgd }
1464 1.1 cgd
1465 1.5 cgd void
1466 1.5 cgd fatal(const char *fmt, ...)
1467 1.1 cgd {
1468 1.1 cgd static FILE *fp;
1469 1.5 cgd va_list ap;
1470 1.22 wiz
1471 1.22 wiz ++nerrs;
1472 1.22 wiz if (!fp && !(fp = fdopen(rem, "w")))
1473 1.22 wiz return;
1474 1.5 cgd va_start(ap, fmt);
1475 1.1 cgd if (iamremote) {
1476 1.1 cgd (void)fprintf(fp, "%crdist: ", 0x02);
1477 1.5 cgd (void)vfprintf(fp, fmt, ap);
1478 1.1 cgd fflush(fp);
1479 1.1 cgd }
1480 1.1 cgd else {
1481 1.1 cgd fflush(stdout);
1482 1.1 cgd (void)fprintf(stderr, "rdist: ");
1483 1.5 cgd (void)vfprintf(stderr, fmt, ap);
1484 1.1 cgd fflush(stderr);
1485 1.1 cgd }
1486 1.22 wiz va_end(ap);
1487 1.1 cgd if (lfp != NULL) {
1488 1.1 cgd (void)fprintf(lfp, "rdist: ");
1489 1.22 wiz va_start(ap, fmt);
1490 1.5 cgd (void)vfprintf(lfp, fmt, ap);
1491 1.22 wiz va_end(ap);
1492 1.1 cgd fflush(lfp);
1493 1.1 cgd }
1494 1.5 cgd cleanup(0);
1495 1.1 cgd }
1496 1.1 cgd
1497 1.5 cgd static int
1498 1.23 wiz response(void)
1499 1.1 cgd {
1500 1.1 cgd char *cp, *s;
1501 1.1 cgd char resp[BUFSIZ];
1502 1.1 cgd
1503 1.1 cgd if (debug)
1504 1.1 cgd printf("response()\n");
1505 1.1 cgd
1506 1.1 cgd cp = s = resp;
1507 1.1 cgd do {
1508 1.1 cgd if (read(rem, cp, 1) != 1)
1509 1.5 cgd lostconn(0);
1510 1.1 cgd } while (*cp++ != '\n' && cp < &resp[BUFSIZ]);
1511 1.1 cgd
1512 1.1 cgd switch (*s++) {
1513 1.1 cgd case '\0':
1514 1.1 cgd *--cp = '\0';
1515 1.1 cgd if (*s != '\0') {
1516 1.24 thorpej dolog(lfp, "%s\n", s);
1517 1.1 cgd return(1);
1518 1.1 cgd }
1519 1.1 cgd return(0);
1520 1.1 cgd case '\3':
1521 1.1 cgd *--cp = '\0';
1522 1.24 thorpej dolog(lfp, "Note: %s\n",s);
1523 1.1 cgd return(response());
1524 1.1 cgd
1525 1.1 cgd default:
1526 1.1 cgd s--;
1527 1.1 cgd /* fall into... */
1528 1.1 cgd case '\1':
1529 1.1 cgd case '\2':
1530 1.1 cgd nerrs++;
1531 1.1 cgd if (*s != '\n') {
1532 1.1 cgd if (!iamremote) {
1533 1.1 cgd fflush(stdout);
1534 1.1 cgd (void) write(2, s, cp - s);
1535 1.1 cgd }
1536 1.1 cgd if (lfp != NULL)
1537 1.1 cgd (void) fwrite(s, 1, cp - s, lfp);
1538 1.1 cgd }
1539 1.1 cgd if (resp[0] == '\2')
1540 1.5 cgd lostconn(0);
1541 1.1 cgd return(-1);
1542 1.1 cgd }
1543 1.1 cgd }
1544 1.1 cgd
1545 1.1 cgd /*
1546 1.1 cgd * Remove temporary files and do any cleanup operations before exiting.
1547 1.1 cgd */
1548 1.1 cgd void
1549 1.23 wiz cleanup(int signo)
1550 1.1 cgd {
1551 1.1 cgd (void) unlink(tempfile);
1552 1.1 cgd exit(1);
1553 1.1 cgd }
1554 1.1 cgd
1555 1.5 cgd static void
1556 1.5 cgd note(const char *fmt, ...)
1557 1.1 cgd {
1558 1.1 cgd static char buf[BUFSIZ];
1559 1.5 cgd va_list ap;
1560 1.23 wiz
1561 1.5 cgd va_start(ap, fmt);
1562 1.5 cgd (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1563 1.5 cgd va_end(ap);
1564 1.1 cgd comment(buf);
1565 1.1 cgd }
1566 1.1 cgd
1567 1.5 cgd static void
1568 1.23 wiz comment(char *s)
1569 1.1 cgd {
1570 1.5 cgd char c;
1571 1.5 cgd
1572 1.5 cgd c = '\3';
1573 1.1 cgd write(rem, &c, 1);
1574 1.1 cgd write(rem, s, strlen(s));
1575 1.1 cgd c = '\n';
1576 1.1 cgd write(rem, &c, 1);
1577 1.1 cgd }
1578