Debian Codegraph @

hfaxd/FileSystem.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1995-1996 Sam Leffler
4 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * File commands other than transfer.
29 */
30 #include "HylaFAXServer.h"
31 #include "Sys.h"
32 #include "tiffio.h"
33
34 #include <limits.h>
35 #include <ctype.h>
36
37 /*
38 * Delete a file (submitted document or received facsimile).
39 */
40 void
41 HylaFAXServer::deleCmd(const char* pathname)
42 {
43 struct stat sb;
44 SpoolDir* dir = fileAccess(pathname, W_OK, sb);
45 if (dir) {
46 if (!IS(PRIVILEGED)) {
47 if (!S_ISREG(sb.st_mode)) {
48 reply(550, "%s: not a plain file.", pathname);
49 return;
50 }
51 if (!dir->deleAble) {
52 perror_reply(550, pathname, EPERM);
53 return;
54 }
55 }
56 /*
57 * XXX must decide what to do with jobs/documents/etc...
58 * XXX e.g. should job be treated like jdele; should we
59 * XXX cross-check document use for consistency....
60 */
61 if (Sys::unlink(pathname) < 0)
62 perror_reply(550, pathname, errno);
63 else {
64 const char* cp = strrchr(pathname, '/');
65 logNotice("%s of %s [%s] deleted %s%s"
66 , (const char*) the_user
67 , (const char*) remotehost
68 , (const char*) remoteaddr
69 , dir->pathname, cp ? cp+1 : pathname
70 );
71 ack(250, cmdToken(T_DELE));
72 FileCache::flush(pathname);
73 }
74 }
75 }
76
77 /*
78 * Set ownership of a file.
79 */
80 void
81 HylaFAXServer::chownCmd(const char* pathname, const char* user)
82 {
83 struct stat sb;
84 SpoolDir* dir = fileAccess(pathname, W_OK, sb);
85 if (dir) {
86 u_int id;
87 if (isdigit(user[0]))
88 id = (u_int) atoi(user);
89 else if (!userID(user, id))
90 return;
91 if (!FileCache::chown(pathname, sb.st_uid, (gid_t) id))
92 perror_reply(550, pathname, errno);
93 else
94 ack(250, cmdToken(T_CHOWN));
95 }
96 }
97
98 /*
99 * Set protection of a file.
100 */
101 void
102 HylaFAXServer::chmodCmd(const char* pathname, u_int mode)
103 {
104 struct stat sb;
105 SpoolDir* dir = fileAccess(pathname, W_OK, sb);
106 if (dir) {
107 mode = 0600 | (mode&066);
108 if (!FileCache::chmod(pathname, mode))
109 perror_reply(550, pathname, errno);
110 else
111 ack(250, cmdToken(T_CHMOD));
112 }
113 }
114
115 /*
116 * Return the last modification time for a file.
117 */
118 void
119 HylaFAXServer::mdtmCmd(const char* pathname)
120 {
121 struct stat sb;
122 SpoolDir* dir = fileAccess(pathname, X_OK, sb);
123 if (dir) {
124 struct tm* t = cvtTime(sb.st_mtime);
125 reply(213, "%d%02d%02d%02d%02d%02d"
126 , t->tm_year+1900
127 , t->tm_mon+1
128 , t->tm_mday
129 , t->tm_hour
130 , t->tm_min
131 , t->tm_sec
132 );
133 }
134 }
135
136 /*
137 * NB: this array is ordered by expected frequency of access.
138 */
139 SpoolDir HylaFAXServer::dirs[] = {
140 { "/status/", false, false, false, 0,
141 &HylaFAXServer::isVisibletrue,
142 &HylaFAXServer::listStatus, &HylaFAXServer::listStatusFile,
143 &HylaFAXServer::nlstStatus, &HylaFAXServer::nlstUnixFile, },
144 { "/sendq/", false, false, false, 0,
145 &HylaFAXServer::isVisibleSendQFile,
146 &HylaFAXServer::listSendQ, &HylaFAXServer::listSendQFile,
147 &HylaFAXServer::nlstSendQ, &HylaFAXServer::nlstSendQFile, },
148 { "/doneq/", false, false, false, 0,
149 &HylaFAXServer::isVisibleSendQFile,
150 &HylaFAXServer::listSendQ, &HylaFAXServer::listSendQFile,
151 &HylaFAXServer::nlstSendQ, &HylaFAXServer::nlstSendQFile, },
152 { "/docq/", false, true, true, 0,
153 &HylaFAXServer::isVisibleDocQFile,
154 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
155 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
156 { "/tmp/", false, true, true, 0,
157 &HylaFAXServer::isVisibletrue,
158 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
159 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
160 { "/log/", false, false, false, 0,
161 &HylaFAXServer::isVisibletrue,
162 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
163 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
164 { "/recvq/", false, false, true, 0,
165 &HylaFAXServer::isVisibleRecvQFile,
166 &HylaFAXServer::listRecvQ, &HylaFAXServer::listRecvQFile,
167 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
168 { "/archive/", false, false, false, 0,
169 &HylaFAXServer::isVisibletrue,
170 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
171 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
172 { "/pollq/", false, true, true, 0,
173 &HylaFAXServer::isVisibleRecvQFile,
174 &HylaFAXServer::listRecvQ, &HylaFAXServer::listRecvQFile,
175 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
176 { "/", false, false, false, 0,
177 &HylaFAXServer::isVisibleRootFile,
178 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
179 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
180 { "/etc/", true, false, false, 0,
181 &HylaFAXServer::isVisibletrue,
182 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
183 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
184 { "/info/", false, false, false, 0,
185 &HylaFAXServer::isVisibletrue,
186 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
187 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
188 { "/bin/", true, false, false, 0,
189 &HylaFAXServer::isVisibletrue,
190 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
191 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
192 { "/config/", false, false, false, 0,
193 &HylaFAXServer::isVisibletrue,
194 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
195 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
196 { "/client/", true, false, false, 0,
197 &HylaFAXServer::isVisibletrue,
198 &HylaFAXServer::listDirectory, &HylaFAXServer::listUnixFile,
199 &HylaFAXServer::nlstDirectory, &HylaFAXServer::nlstUnixFile, },
200 };
201 #define N(a) (sizeof (a) / sizeof (a[0]))
202
203 /*
204 * Initialize the directory handling.
205 */
206 void
207 HylaFAXServer::dirSetup(void)
208 {
209 for (u_int i = 0, n = N(dirs); i < n; i++) {
210 struct stat sb;
211 if (!FileCache::lookup(dirs[i].pathname, sb) || !S_ISDIR(sb.st_mode)) {
212 logError("%s: Not a directory.", dirs[i].pathname);
213 continue;
214 }
215 dirs[i].ino = sb.st_ino;
216 if (streq(dirs[i].pathname, "/"))
217 cwd = &dirs[i];
218 }
219 }
220
221 /*
222 * Return a directory handle given a pathname.
223 */
224 SpoolDir*
225 HylaFAXServer::dirLookup(const char* path)
226 {
227 struct stat sb;
228 return (!FileCache::lookup(path, sb) || !S_ISDIR(sb.st_mode) ?
229 (SpoolDir*) NULL : dirLookup(sb.st_ino));
230 }
231
232 /*
233 * Return a directory handle given an inode number.
234 */
235 SpoolDir*
236 HylaFAXServer::dirLookup(ino_t ino)
237 {
238 for (u_int i = 0, n = N(dirs); i < n; i++)
239 if (dirs[i].ino == ino)
240 return (&dirs[i]);
241 return (NULL);
242 }
243
244 /*
245 * Check file protection mode; we use the
246 * normal ``other'' bits for public access
247 * and the group bits for the ``fax uid''.
248 */
249 bool
250 HylaFAXServer::checkFileRights(int op, const struct stat& sb)
251 {
252 if (IS(PRIVILEGED))
253 return (true);
254
255 if (sb.st_mode & op) // public access
256 return (true);
257
258 if ((sb.st_gid==uid) && ((sb.st_mode>>3)&op)) // owner access
259 return (true);
260 return false;
261
262 }
263
264 /*
265 * Check if the client is permitted to do the
266 * request operation on the specified file.
267 * Operations are: X_OK (list/stat), R_OK (look
268 * at the contents), W_OK (write/delete contents).
269 *
270 * If the operation is permitted the stat result
271 * for the target file is returned to the caller
272 * for use in futher checks (e.g. type of file).
273 */
274 SpoolDir*
275 HylaFAXServer::fileAccess(const char* path, int op, struct stat& sb)
276 {
277 if (!FileCache::lookup(path, sb)) { // file not found
278 if (op != W_OK || errno != ENOENT) {
279 perror_reply(550, path, errno);
280 return (NULL);
281 }
282 /*
283 * The file does not exist, fake up the
284 * information that would result from a
285 * successful file creation--for use below.
286 */
287 sb.st_mode = S_IFREG|S_IRGRP|S_IWGRP;
288 sb.st_gid = (gid_t) uid;
289 sb.st_ino = 0; // NB: to be created
290 }
291 /*
292 * Validate containing directory and target file.
293 * The directory must exist and the client must
294 * have permission to access it. The target file
295 * must not be a directory if the client is about
296 * to do a read/write-style operation or the target
297 * file must be owned by the client (other checks
298 * are left to the caller).
299 */
300 struct stat db;
301 const char* cp = strrchr(path, '/');
302 if (cp) {
303 cp++; // include "/"
304 if (!FileCache::lookup(fxStr(path, cp-path), db)) {
305 perror_reply(550, path, ENOENT);
306 return (NULL);
307 }
308 } else {
309 if (!FileCache::lookup(cwd->pathname, db)) { // implicit ref to "."
310 perror_reply(550, path, ENOENT);
311 return (NULL);
312 }
313 cp = path;
314 }
315 SpoolDir* dir = dirLookup(db.st_ino);
316 if (!dir) { // no containing dir
317 perror_reply(550, path, ENOENT);
318 return (NULL);
319 } else if (!IS(PRIVILEGED)) { // unprivileged client
320 if (dir->adminOnly) { // requires admin priv's
321 perror_reply(550, path, EPERM);
322 return (NULL);
323 } else if (!fileVisible(*dir, cp, sb)) { // not visible w/o admin
324 perror_reply(550, path, EPERM);
325 return (NULL);
326 } else if (op != X_OK) { // client wants to r/w
327 if (S_ISDIR(sb.st_mode)) { // cannot r/w directory
328 reply(550, "%s: not a plain file.", path);
329 return (NULL);
330 } else if (sb.st_ino == 0 && !dir->storAble) {
331 perror_reply(550, path, EPERM); // cannot stor in dir.
332 return (NULL);
333 }
334 if (!checkFileRights(op, sb)) {
335 perror_reply(550, path, EPERM);
336 return (NULL);
337 }
338 }
339 } else { // privileged client
340 if (op != X_OK && S_ISDIR(sb.st_mode)) { // cannot r/w directory
341 reply(550, "%s: not a plain file.", path);
342 return (NULL);
343 }
344 }
345 return (dir);
346 }
347
348 /*
349 * Like fileAccess, but when the target is a directory.
350 * The implicit operation is X_OK and the directory handle
351 * returned is for the target, not the containing directory.
352 */
353 SpoolDir*
354 HylaFAXServer::dirAccess(const char* path)
355 {
356 struct stat sb;
357 if (!FileCache::lookup(path, sb)) { // file not found
358 perror_reply(550, path, errno);
359 return (NULL);
360 }
361 if (!S_ISDIR(sb.st_mode)) {
362 perror_reply(550, path, ENOTDIR);
363 return (NULL);
364 }
365 // NB: this assumes all directories are top-level
366 SpoolDir* dir = dirLookup(sb.st_ino);
367 if (!dir) { // XXX should not happen
368 perror_reply(550, path, ENOENT);
369 return (NULL);
370 } else if (dir->adminOnly && !IS(PRIVILEGED)) { // requires admin priv's
371 perror_reply(550, path, EPERM);
372 return (NULL);
373 }
374 return (dir);
375 }
376
377 /*
378 * Force the specified file to have the GID setup as
379 * the effective GID of the process. On System-V-style
380 * filesystems this happens automatically and there is
381 * nothing to do. On BSD-style filesystems however the
382 * file's GID is set from the GID of the containing
383 * directory so we must explicitly set the value. Note
384 * that we must auto-detect the appropriate semantics
385 * because some systems support both styles and the
386 * specific scheme is selectable on a per-filesystem
387 * basis (e.g. IRIX).
388 */
389 void
390 HylaFAXServer::setFileOwner(const char* file)
391 {
392 if (IS(CHECKGID)) { // auto-detect how GID handled
393 struct stat sb;
394 if (!FileCache::lookup(file, sb)) {
395 fatal("setFileOwner called for non-existent file (check)");
396 }
397 if (sb.st_gid != (gid_t) uid) {
398 state |= S_SETGID; // not set, must force it
399 } else {
400 state &= ~S_SETGID; // set by OS, no work to do
401 }
402 if (TRACE(SERVER)) {
403 logDebug("Filesystem has %s-style file creation semantics.",
404 IS(SETGID) ? "BSD" : "SysV");
405 }
406 state &= ~S_CHECKGID;
407 }
408 if (IS(SETGID)) {
409 struct stat sb;
410 if (!FileCache::lookup(file, sb)) {
411 fatal("setFileOwner called for non-existent file (set)");
412 }
413 if (!FileCache::chown(file, sb.st_uid, (gid_t) uid)) {
414 logError("%s: chown: %s", file, strerror(errno));
415 }
416 }
417 }
418
419 /*
420 * Is the specified file visible to the client.
421 */
422 bool
423 HylaFAXServer::fileVisible(const SpoolDir& dir, const char* filename, const struct stat& sb)
424 {
425 return (IS(PRIVILEGED) || (this->*dir.isVisibleFile)(filename, sb));
426 }
427 bool
428 HylaFAXServer::isVisibletrue(const char*, const struct stat&)
429 { return (true); }
430 bool
431 HylaFAXServer::isVisibleDocQFile(const char* filename, const struct stat&)
432 { return (strncmp(filename, "doc", 3) == 0 || strncmp(filename, "cover", 5) == 0); }
433 bool
434 HylaFAXServer::isVisibleRootFile(const char*, const struct stat& sb)
435 { return (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode)); }
436
437 /*
438 * Change the working (pseudo) directory.
439 */
440 void
441 HylaFAXServer::cwdCmd(const char* path)
442 {
443 SpoolDir* dir = dirAccess(path);
444 if (dir) {
445 if (Sys::chdir(path) >= 0) {
446 ack(250, cmdToken(T_CWD));
447 cwd = dir;
448 } else
449 perror_reply(550, path, errno);
450 }
451 }
452
453 /*
454 * Return the path of the current working (pseudo) directory.
455 */
456 void
457 HylaFAXServer::pwdCmd(void)
458 {
459 u_int len = strlen(cwd->pathname)-1; // strip trailing "/"
460 reply(257, "\"%.*s\" is the current directory.",
461 len ? len : len+1, cwd->pathname);
462 }
463
464 /*
465 * LIST a directory.
466 */
467 void
468 HylaFAXServer::listCmd(const char* pathname)
469 {
470 SpoolDir* sd = dirAccess(pathname);
471 if (sd) {
472 DIR* dir = opendir(pathname);
473 if (dir != NULL) {
474 int code;
475 FILE* dout = openDataConn("w", code);
476 if (dout != NULL) {
477 reply(code, "%s for \"%s\".", dataConnMsg(code), pathname);
478 if (setjmp(urgcatch) == 0) {
479 state |= S_TRANSFER;
480 (this->*sd->listDirectory)(dout, *sd, dir);
481 fflush(dout);
482 reply(226, "Transfer complete.");
483 }
484 state &= ~S_TRANSFER;
485 closeDataConn(dout);
486 }
487 closedir(dir);
488 } else if (errno != 0)
489 perror_reply(550, pathname, errno);
490 else
491 reply(550, "%s: Cannot open directory.", pathname);
492 }
493 }
494
495 void
496 HylaFAXServer::listDirectory(FILE* fd, const SpoolDir& sd, DIR* dir)
497 {
498 KeyStringArray listing;
499 /*
500 * Use an absolute pathname when doing file
501 * lookups to improve cache locality.
502 */
503 fxStr path(sd.pathname);
504 struct dirent* dp;
505 while ((dp = readdir(dir))) {
506 if (dp->d_name[0] == '.' &&
507 (dp->d_name[1] == '\0' || strcmp(dp->d_name, "..") == 0))
508 continue;
509 struct stat sb;
510 if (!FileCache::update(path | dp->d_name, sb))
511 continue;
512 if ((this->*sd.isVisibleFile)(dp->d_name, sb)) {
513 if (fileSortFormat.length() == 0) {
514 Fprintf(fd, fileFormat, dp->d_name, sb);
515 fputs("\r\n", fd);
516 } else
517 {
518 fxStackBuffer buf;
519 Fprintf(buf, fileFormat, dp->d_name, sb);
520 fxStr content(buf, buf.getLength());
521 buf.reset();
522 Fprintf(buf, fileSortFormat, dp->d_name, sb);
523 fxStr key(buf, buf.getLength());
524 listing.append(KeyString(key, content));
525 }
526 }
527 }
528
529 if (listing.length() > 1)
530 listing.qsort();
531
532 for (u_int i = 0; i < listing.length(); i++)
533 {
534 fwrite(listing[i], listing[i].length(), 1, fd);
535 fputs("\r\n", fd);
536 }
537
538
539 }
540
541 void
542 HylaFAXServer::listUnixFile(FILE* fd, const SpoolDir&,
543 const char* filename, const struct stat& sb)
544 {
545 Fprintf(fd, fileFormat, filename, sb);
546 }
547
548 static const char fformat[] = {
549 's', // a (last access time)
550 'b', // b
551 's', // c (create time)
552 'o', // d (device)
553 'e', // e
554 's', // f (filename)
555 'u', // g (GID of file)
556 'h', // h
557 'u', // i (inode number)
558 'j', // j
559 'k', // k
560 'u', // l (link count)
561 's', // m (last modification time)
562 'n', // n
563 's', // o (owner based on file GID)
564 's', // p (fax-style protection flags, no group bits)
565 's', // q (UNIX-style protection flags)
566 'o', // r (root device)
567 'u', // s (file size in bytes)
568 't', // t
569 'u', // u (UID of file)
570 'v', // v
571 'w', // w
572 'x', // x
573 'y', // y
574 'z' // z
575 };
576
577 /*
578 * Print a formatted string with fields filled in from
579 * a file's stat buffer. This functionality is
580 * used to permit clients to get modem status listings
581 * in preferred formats.
582 */
583 void
584 HylaFAXServer::Fprintf(fxStackBuffer& buf, const char* fmt,
585 const char* filename, const struct stat& sb)
586 {
587 for (const char* cp = fmt; *cp; cp++) {
588 if (*cp == '%') {
589 #define MAXSPEC 20
590 char fspec[MAXSPEC];
591 char* fp = fspec;
592 *fp++ = '%';
593 char c = *++cp;
594 if (c == '-')
595 *fp++ = c, c = *++cp;
596 if (isdigit(c)) {
597 do {
598 *fp++ = c;
599 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-3]);
600 }
601 if (c == '.') {
602 do {
603 *fp++ = c;
604 } while (isdigit(c = *++cp) && fp < &fspec[MAXSPEC-2]);
605 }
606 if (!islower(c)) {
607 if (c == '%') // %% -> %
608 buf.put(c);
609 else
610 buf.fput("%.*s%c", fp-fspec, fspec, c);
611 continue;
612 }
613 fp[0] = fformat[c-'a']; // printf format string
614 fp[1] = '\0';
615 switch (c) {
616 case 'a':
617 buf.fput(fspec, asctime(cvtTime(sb.st_atime))+4);
618 break;
619 case 'c':
620 buf.fput(fspec, asctime(cvtTime(sb.st_ctime))+4);
621 break;
622 case 'd':
623 buf.fput(fspec, (u_int) sb.st_dev);
624 break;
625 case 'f':
626 buf.fput(fspec, filename);
627 break;
628 case 'g':
629 buf.fput(fspec, (u_int) sb.st_gid);
630 break;
631 case 'i':
632 buf.fput(fspec, (u_int) sb.st_ino); // XXX
633 break;
634 case 'l':
635 buf.fput(fspec, (u_int) sb.st_nlink);
636 break;
637 case 'm':
638 buf.fput(fspec, asctime(cvtTime(sb.st_mtime))+4);
639 break;
640 case 'o':
641 buf.fput(fspec, userName((u_int) sb.st_gid));
642 break;
643 case 'p':
644 case 'q':
645 { char prot[10]; // XXX HP C++
646 makeProt(sb, c == 'q', prot);
647 buf.fput(fspec, prot);
648 }
649 break;
650 case 'r':
651 buf.fput(fspec, (u_int) sb.st_rdev);
652 break;
653 case 's':
654 buf.fput(fspec, (u_int) sb.st_size); // XXX
655 break;
656 case 'u':
657 buf.fput(fspec, (u_int) sb.st_uid);
658 break;
659 }
660 } else
661 buf.put(*cp);
662 }
663 }
664 void
665 HylaFAXServer::Fprintf(FILE* fd, const char* fmt,
666 const char* filename, const struct stat& sb)
667 {
668 fxStackBuffer buf;
669 Fprintf(buf, fmt, filename, sb);
670 fwrite((const char*)buf, buf.getLength(), 1, fd);
671 }
672
673 void
674 HylaFAXServer::makeProt(const struct stat& sb, bool withGrp, char prot[10])
675 {
676 char* pp = prot;
677 *pp++ = S_ISREG(sb.st_mode) ? '-' :
678 S_ISDIR(sb.st_mode) ? 'd' :
679 S_ISFIFO(sb.st_mode) ? 'p' :
680 #ifdef S_ISSOCK
681 S_ISSOCK(sb.st_mode) ? 's' :
682 #endif
683 '?' ;
684 *pp++ = (sb.st_mode&S_IRUSR) ? 'r' : '-';
685 *pp++ = (sb.st_mode&S_IWUSR) ? 'w' : '-';
686 *pp++ = (sb.st_mode&S_IXUSR) ? 'x' : '-';
687 if (withGrp) {
688 *pp++ = (sb.st_mode&S_IRGRP) ? 'r' : '-';
689 *pp++ = (sb.st_mode&S_IWGRP) ? 'w' : '-';
690 *pp++ = (sb.st_mode&S_IXGRP) ? 'x' : '-';
691 }
692 *pp++ = (sb.st_mode&S_IROTH) ? 'r' : '-';
693 *pp++ = (sb.st_mode&S_IWOTH) ? 'w' : '-';
694 *pp++ = (sb.st_mode&S_IXOTH) ? 'x' : '-';
695 *pp++ = '\0';
696 }
697
698 void
699 HylaFAXServer::statFileCmd(const char* pathname)
700 {
701 struct stat sb;
702 SpoolDir* dir = fileAccess(pathname, X_OK, sb);
703 if (dir) {
704 (void) FileCache::update(pathname, sb); // insure up to date info
705 lreply(211, "Status of %s:", pathname);
706 const char* cp = strrchr(pathname, '/');
707 (this->*dir->listFile)(stdout, *dir, cp ? cp+1 : pathname, sb);
708 fputs("\r\n", stdout);
709 reply(211, "End of Status");
710 }
711 }
712
713 /*
714 * NLST a directory.
715 */
716 void
717 HylaFAXServer::nlstCmd(const char* pathname)
718 {
719 SpoolDir* sd = dirAccess(pathname);
720 if (sd) {
721 DIR* dir = opendir(pathname);
722 if (dir != NULL) {
723 int code;
724 FILE* dout = openDataConn("w", code);
725 if (dout != NULL) {
726 reply(code, "%s for \"%s\".", dataConnMsg(code), pathname);
727 if (setjmp(urgcatch) == 0) {
728 state |= S_TRANSFER;
729 (this->*sd->nlstDirectory)(dout, *sd, dir);
730 fflush(dout);
731 reply(226, "Transfer complete.");
732 }
733 state &= ~S_TRANSFER;
734 closeDataConn(dout);
735 }
736 closedir(dir);
737 } else if (errno != 0)
738 perror_reply(550, pathname, errno);
739 else
740 reply(550, "%s: Cannot open directory.", pathname);
741 }
742 }
743
744 void
745 HylaFAXServer::nlstDirectory(FILE* fd, const SpoolDir& sd, DIR* dir)
746 {
747 /*
748 * Use an absolute pathname when doing file
749 * lookups to improve cache locality.
750 */
751 fxStr path(sd.pathname);
752 struct dirent* dp;
753 while ((dp = readdir(dir))) {
754 if (dp->d_name[0] == '.' &&
755 (dp->d_name[1] == '\0' || strcmp(dp->d_name, "..") == 0))
756 continue;
757 struct stat sb;
758 if (!FileCache::update(path | dp->d_name, sb))
759 continue;
760 if ((this->*sd.isVisibleFile)(dp->d_name, sb)) {
761 (this->*sd.nlstFile)(fd, sd, dp->d_name, sb);
762 fputs("\r\n", fd);
763 }
764 }
765 }
766
767 void
768 HylaFAXServer::nlstUnixFile(FILE* fd, const SpoolDir&,
769 const char* filename, const struct stat&)
770 {
771 fprintf(fd, "%s", filename);
772 }