3 * Fake rmt(8) server for hashing and storing files
5 * (c) 2010 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 /*----- Header files ------------------------------------------------------*/
27 #define _FILE_OFFSET_BITS 64
37 #include <sys/types.h>
45 #include <mLib/dstr.h>
46 #include <mLib/quis.h>
47 #include <mLib/report.h>
49 #include <nettle/sha.h>
51 /*----- Configuration -----------------------------------------------------*/
54 # define BKP "/mnt/bkp"
57 /*----- Main code ---------------------------------------------------------*/
60 #define LSEEK_GET_TAPEPOS 10
61 #define LSEEK_GO2_TAPEPOS 11
68 static const struct flag openflag[] = {
69 /* ;;; Emacs Lisp to generate the table below. Place your cursor just
70 ;;; after the closing `)' and press C-x C-e.
72 (let ((flags '(rdonly wronly rdwr creat excl trunc nonblock ndelay
73 noctty append dsync rsync sync cloexec async
74 direct noatime nofollow shlock exlock defer)))
76 (goto-char (point-min))
77 (search-forward (concat "***" "BEGIN openflag" "***"))
79 (delete-region (point)
81 (search-forward "***END***")
84 (dolist (f (sort (copy-list flags) #'string<))
85 (let ((up (upcase (symbol-name f))))
86 (insert (format "#ifdef O_%s\n" up))
87 (insert (format " { \"%s\", O_%s },\n" up up))
88 (insert "#endif\n")))))
90 /***BEGIN openflag***/
92 { "APPEND", O_APPEND },
98 { "CLOEXEC", O_CLOEXEC },
101 { "CREAT", O_CREAT },
104 { "DEFER", O_DEFER },
107 { "DIRECT", O_DIRECT },
110 { "DSYNC", O_DSYNC },
116 { "EXLOCK", O_EXLOCK },
119 { "NDELAY", O_NDELAY },
122 { "NOATIME", O_NOATIME },
125 { "NOCTTY", O_NOCTTY },
128 { "NOFOLLOW", O_NOFOLLOW },
131 { "NONBLOCK", O_NONBLOCK },
134 { "RDONLY", O_RDONLY },
140 { "RSYNC", O_RSYNC },
143 { "SHLOCK", O_SHLOCK },
149 { "TRUNC", O_TRUNC },
152 { "WRONLY", O_WRONLY },
158 int main(int argc, char *argv[])
161 dstr d = DSTR_INIT, dd = DSTR_INIT;
162 unsigned char buf[BUFSZ];
163 int fd = -1, hfd = -1;
171 const char *bkp = 0, *host = 0;
172 struct sha256_ctx hc;
175 setvbuf(stdin, 0, _IONBF, 0);
176 signal(SIGPIPE, SIG_IGN);
179 int o = getopt(argc, argv, "H:r:");
182 case 'H': host = optarg; break;
183 case 'r': bkp = optarg; break;
184 default: f |= f_bogus; break;
187 argc -= optind; argv += optind;
188 if ((f & f_bogus) || argc) {
189 pquis(stderr, "usage: $ [-r ROOT] [-H HOST]\n");
193 if (!bkp) bkp = getenv("BKP");
196 if (!host) host = getenv("BKP_HOST");
200 if (!p) p = getenv("LOGNAME");
203 if ((pw = getpwuid(u)) == 0)
204 die(1, "no passwd entry (you don't exist?)");
207 if (strncmp(p, "bkp-", 4) != 0) {
208 die(1, "can't deduce host name: "
209 "login name `%s' doesn't begin with `bkp-'",
216 if (fflush(stdout)) goto fail;
218 if (ch == EOF) break;
219 DRESET(&d); DRESET(&dd); rc = 0;
221 #define CHECKFD do { if (fd < 0) goto badf; } while (0)
222 #define ERROR(what) do moan(what ": %s", strerror(errno)); while (0)
223 #define ERROR1(what, arg) \
224 do moan(what ": %s", arg, strerror(errno)); while (0)
225 #define ARG(d) do { \
226 if (dstr_putline(&d, stdin) == EOF || ferror(stdin) || feof(stdin)) \
227 { moan("read (stdin)", strerror(errno)); goto fail; } \
230 #define SKIPWS do { while (isspace((unsigned char)*p)) p++; } while (0)
235 /* Ofile\nmode\n -- open file */
237 const struct flag *ff;
243 if (fd >= 0 && close(fd)) ERROR("close (fd)");
244 if (hfd >= 0 && close(hfd)) ERROR("close (hash)");
247 if (chdir(bkp) || chdir(host)) ERROR1("chdir (%s)", host);
249 if ((q = strchr(p, '/')) == 0)
250 { moan("bad path: missing `/')"); goto inval; }
252 if (chdir(p) || chdir("prepare/incoming")) ERROR1("chdir (%s)", p);
254 memmove(d.buf, q, d.len - (q - d.buf) + 1);
258 mode = strtol(dd.buf, &p, 0);
259 if (errno) ERROR("bad mode");
260 else if (mode < 0 || mode > INT_MAX)
261 { moan("bad mode: range"); goto range; }
264 switch (mode & O_ACCMODE) {
265 case O_RDONLY: mode = O_RDONLY; break;
266 case O_WRONLY: mode = O_WRONLY | O_TRUNC | O_CREAT; break;
267 case O_RDWR: mode = O_RDWR;
268 default: moan("bad mode: unknown access type"); goto inval;
273 if (p[0] == 'O' && p[1] == '_') {
275 n = strcspn(p, " \t|");
276 for (ff = openflag; ff->name; ff++) {
277 if (strncmp(p, ff->name, n) == 0 && !ff->name[n])
280 moan("bad mode: unknown flag O_%.*s", (int)n, p);
285 } else if (isdigit((unsigned long)*p)) {
287 f = strtol(p, &p, 0);
288 if (errno) ERROR("bad mode");
289 else if (f < 0 || f > INT_MAX)
290 { moan("bad mode: range"); goto range; }
293 moan("bad mode: unexpected token");
299 else if (*p != '|') {
300 moan("bad mode: expected `|'");
308 if ((fd = open(d.buf, mode, 0666)) < 0) ERROR1("open (%s)", d.buf);
309 if ((mode & O_ACCMODE) == O_WRONLY) {
311 if ((hfd = open(d.buf,
312 mode & (O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC),
315 ERROR1("open (%s)", d.buf);
323 /* Chunoz\n -- close file */
325 uint8_t h[SHA256_DIGEST_SIZE], *p;
326 char hex[SHA256_DIGEST_SIZE * 2 + 1], *q;
330 if (close(fd)) ERROR("close (fd)");
333 sha256_digest(&hc, sizeof(h), h);
334 for (p = h, q = hex; p < h + sizeof(h); p++, q += 2)
335 sprintf(q, "%02x", *p);
338 if (write(hfd, hex, sizeof(hex)) < sizeof(hex) || close(hfd))
339 ERROR("close (hash)");
345 /* Loffset\nwhence\n -- seek
346 * (warning: the manual page gets these the wrong way round)
352 ARG(d); ARG(dd); CHECKFD;
353 offset = atoi(d.buf); whence = strtoull(dd.buf, 0, 0);
355 case LSEEK_GET_TAPEPOS: whence = SEEK_CUR; offset = 0; break;
356 case LSEEK_GO2_TAPEPOS: whence = SEEK_SET; break;
360 if (!offset) { rc = off; break; }
362 rc = lseek(fd, offset, whence);
363 if (rc == (off_t)-1) ERROR("seek");
370 /* Wlen\ndata... -- write */
378 rc = sz = strtoul(d.buf, 0, 0);
380 nn = sz > BUFSZ ? BUFSZ : sz;
381 n = fread(buf, 1, nn, stdin);
383 if (feof(stdin)) { moan("eof on stdin"); goto fail;}
384 else ERROR("read (stdin)");
386 if (hfd >= 0) sha256_update(&hc, n, buf);
388 while (!botch && n) {
389 if ((ssz = write(fd, p, n)) > 0) {
390 p += ssz; off += ssz; n -= ssz;
391 } else if (!ssz) { moan("zero-length write"); goto fail; }
392 else if (errno != EINTR) { botch = errno; }
396 if (botch) { errno = botch; ERROR("write"); }
406 nn = strtoul(d.buf, 0, 0); if (nn > BUFSZ) nn = BUFSZ;
407 if ((ssz = read(fd, buf, nn)) < 0) ERROR("read");
409 printf("A%ld\n", (long)ssz);
410 if (fwrite(buf, 1, ssz, stdout) < ssz)
411 { moan("write (stdout): %s", strerror(errno)); goto fail; }
416 /* Iop\ncount\n -- ioctl */
417 ARG(d); ARG(dd); CHECKFD; goto notty;
425 if ((ch = getchar()) == EOF) goto fail;
432 printf("A%llu\n", (unsigned long long)rc);
435 badf: errno = EBADF; goto error;
436 range: errno = ERANGE; goto error;
437 inval: errno = EINVAL; goto error;
438 notty: errno = ENOTTY; goto error;
440 printf("E%d\n%s\n", errno, strerror(errno));
443 if (fflush(stdout) || ferror(stdout) || ferror(stdin)) goto fail;
449 /*----- That's all, folks -------------------------------------------------*/