1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* Copyright (c) The Exim Maintainers 2020 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-only */
12 #include "lf_functions.h"
16 /*************************************************
17 * Check a file's credentials *
18 *************************************************/
20 /* fstat can normally be expected to work on an open file, but there are some
21 NFS states where it may not.
24 fd an open file descriptor or -1
25 filename a file name if fd is -1
26 s_type type of file (S_IFREG or S_IFDIR)
27 modemask a mask specifying mode bits that must *not* be set
28 owners NULL or a list of of allowable uids, count in the first item
29 owngroups NULL or a list of allowable gids, count in the first item
30 type name of lookup type for putting in error message
31 errmsg where to put an error message
33 Returns: -1 stat() or fstat() failed
35 +1 something didn't match
37 Side effect: sets errno to ERRNO_BADUGID, ERRNO_NOTREGULAR or ERRNO_BADMODE for
38 bad uid/gid, not a regular file, or bad mode; otherwise leaves it
39 to what fstat set it to.
43 lf_check_file(int fd, const uschar * filename, int s_type, int modemask,
44 uid_t * owners, gid_t * owngroups, const char * type, uschar ** errmsg)
48 if ((fd >= 0 && fstat(fd, &statbuf) != 0) ||
49 (fd < 0 && Ustat(filename, &statbuf) != 0))
51 int save_errno = errno;
52 *errmsg = string_sprintf("%s: stat failed", filename);
57 if ((statbuf.st_mode & S_IFMT) != s_type)
59 if (s_type == S_IFREG)
61 *errmsg = string_sprintf("%s is not a regular file (%s lookup)",
63 errno = ERRNO_NOTREGULAR;
67 *errmsg = string_sprintf("%s is not a directory (%s lookup)",
69 errno = ERRNO_NOTDIRECTORY;
74 if ((statbuf.st_mode & modemask) != 0)
76 *errmsg = string_sprintf("%s (%s lookup): file mode %.4o should not contain "
77 "%.4o", filename, type, statbuf.st_mode & 07777,
78 statbuf.st_mode & modemask);
79 errno = ERRNO_BADMODE;
86 for (int i = 1; i <= (int)owners[0]; i++)
87 if (owners[i] == statbuf.st_uid) { uid_ok = TRUE; break; }
90 *errmsg = string_sprintf("%s (%s lookup): file has wrong owner", filename,
92 errno = ERRNO_BADUGID;
97 if (owngroups != NULL)
100 for (int i = 1; i <= (int)owngroups[0]; i++)
101 if (owngroups[i] == statbuf.st_gid) { gid_ok = TRUE; break; }
104 *errmsg = string_sprintf("%s (%s lookup): file has wrong group", filename,
106 errno = ERRNO_BADUGID;
114 /* End of lf_check_file.c */