Skip to content

Commit c9498ca

Browse files
committed
Remove unused S_IFMT_* funcs; flip nodots to allowdots; deprecate "filter=dir implies allowdots"
1 parent 76e2013 commit c9498ca

File tree

3 files changed

+36
-41
lines changed

3 files changed

+36
-41
lines changed

src/src/lookups/dsearch.c

+21-13
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ dsearch_find(
128128
{
129129
struct stat statbuf;
130130
int save_errno;
131-
unsigned filter_by_type = 0;
132-
int exclude_dotdotdot = 0;
131+
ifmt_set_t filter_by_type = 0;
132+
int allowdots = 0;
133133
enum {
134134
RET_KEY, /* return the key */
135135
RET_DIR, /* return the dir without the key */
@@ -165,7 +165,7 @@ if (opts)
165165
else if (Ustrcmp(ele, "dir") == 0)
166166
ret_mode = RET_DIR;
167167
#if 0
168-
/* XXX ret=key is excluded from opts by special-case code in by search_find() */
168+
/* NOTE ret=key is excluded from opts by special-case code in by search_find() */
169169
else if (Ustrcmp(ele, "key") == 0)
170170
ret_mode = RET_KEY;
171171
#endif
@@ -178,11 +178,19 @@ if (opts)
178178
else if (Ustrncmp(ele, "filter=", 7) == 0)
179179
{
180180
ele += 7;
181-
int i = S_IFMTix_from_name(ele);
182-
if (i>=0)
183-
filter_by_type |= BIT(i);
184-
else if (Ustrcmp(ele, "subdir") == 0) filter_by_type |= BIT(S_IFMT_to_index(S_IFDIR)), exclude_dotdotdot = 1; /* dir but not "." or ".." */
185-
else if (Ustrcmp(ele, "nodots") == 0) exclude_dotdotdot = 1; /* any but "." or ".." */
181+
ifmt_set_t m = S_IFMTset_from_name(ele);
182+
if (m)
183+
{
184+
filter_by_type |= m;
185+
/* XXX issue immediate deprecation warning */
186+
#ifndef NO_DIR_IMPLIES_ALLOWDOTS
187+
/* allow "." or ".." when "dir" rather than "subdir" */
188+
if (m == S_IFMT_to_set(S_IFDIR) && ele[0] == 'd')
189+
allowdots = 1;
190+
#endif
191+
}
192+
else if (Ustrcmp(ele, "allowdots") == 0)
193+
allowdots = 1; /* allow "." or ".." */
186194
else
187195
{
188196
*errmsg = string_sprintf("unknown parameter for dsearch lookup: %s", ele-=7);
@@ -205,15 +213,15 @@ if (ignore_key)
205213
else if (keystring == NULL || keystring[0] == 0) /* in case lstat treats "/dir/" the same as "/dir/." */
206214
return FAIL;
207215

208-
DEBUG(D_lookup) debug_printf_indent(" dsearch_find: %s%sfilterbits=%#x ret=%s key=%s\n",
216+
DEBUG(D_lookup) debug_printf_indent(" dsearch_find: %s%sfilter_set=%04jx ret=%s key=%s\n",
209217
follow_symlink ? "follow, " : "",
210-
exclude_dotdotdot ? "filter=nodots, " : "",
211-
filter_by_type,
218+
allowdots ? "filter=allowdots, " : "",
219+
(uintmax_t) filter_by_type,
212220
ret_mode == RET_FULL ? "full" : ret_mode == RET_DIR ? "dir" : "key",
213221
keystring);
214222

215223
/* exclude "." and ".." when {filter=subdir} included */
216-
if (exclude_dotdotdot
224+
if (! allowdots
217225
&& keystring[0] == '.'
218226
&& (keystring[1] == 0
219227
|| keystring[1] == '.' && keystring[2] == 0))
@@ -234,7 +242,7 @@ else
234242
if (stat_result >= 0)
235243
{
236244
if (!filter_by_type
237-
|| filter_by_type & BIT(S_IFMT_to_index(statbuf.st_mode)))
245+
|| filter_by_type & S_IFMT_to_set(statbuf.st_mode))
238246
{
239247
switch (ret_mode)
240248
{

src/src/lookups/lf_check_file.c

+5-20
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ static const struct {
7676
/* sorted in descending order of likelihood */
7777
{ CUS "file", S_IFMT_to_index(S_IFREG) },
7878
{ CUS "dir", S_IFMT_to_index(S_IFDIR) },
79+
{ CUS "subdir", S_IFMT_to_index(S_IFDIR) },
7980
#ifdef S_IFLNK
8081
{ CUS "symlink", S_IFMT_to_index(S_IFLNK) },
8182
{ CUS "link", S_IFMT_to_index(S_IFLNK) },
@@ -99,13 +100,13 @@ static const struct {
99100
};
100101
static const size_t num_ni_map = nelem(ni_map);
101102

102-
int
103-
S_IFMTix_from_name(const uschar *name)
103+
ifmt_set_t
104+
S_IFMTset_from_name(const uschar *name)
104105
{
105106
for (int i=0 ; i < num_ni_map ; ++i)
106107
if (Ustrcmp(ni_map[i].name, name) == 0)
107-
return ni_map[i].index;
108-
return -1;
108+
return 1UL << ni_map[i].index;
109+
return 0;
109110
}
110111

111112
const uschar *
@@ -116,22 +117,6 @@ if (index < 0 || index >= num_S_IF_names)
116117
return S_IF_longnames[index];
117118
}
118119

119-
const uschar *
120-
S_IFMTix_to_name(int index)
121-
{
122-
if (index < 0 || index >= num_S_IF_names)
123-
return NULL; /* invalid file type */
124-
return S_IF_names[index];
125-
}
126-
127-
const uschar *
128-
S_IFMTix_to_ucname(int index)
129-
{
130-
if (index < 0 || index >= num_S_IF_names)
131-
return NULL; /* invalid file type */
132-
return S_IF_ucnames[index];
133-
}
134-
135120
/*************************************************
136121
* Check a file's credentials *
137122
*************************************************/

src/src/lookups/lf_functions.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern int lf_sqlperform(const uschar *, const uschar *, const uschar *,
2929
printf("%s is a %s\n", filename, t);
3030
else
3131
printf("%s is unknown\n", filename);
32-
if ( allowed_filetype_mask & BIT(S_IFMT_to_index(s.st_mode)) )
32+
if ( allowed_filetype_mask & S_IFMT_to_set(s.st_mode) )
3333
printf("\tALLOWED\n");
3434
else
3535
printf("\tFORBIDDEN\n");
@@ -57,15 +57,17 @@ extern int lf_sqlperform(const uschar *, const uschar *, const uschar *,
5757
#define S_IFMT_to_index(S) ( (S_IFMT & (S)) / S_IFMT_scale )
5858
#define S_IFMT_from_index(I) ( S_IFMT & (I) * S_IFMT_scale )
5959

60+
typedef unsigned long ifmt_set_t;
6061

61-
extern const uschar *S_IFMTix_to_name(int index); /* NULL on error */
62+
#define S_IFMT_to_set(S) (1UL << S_IFMT_to_index(S))
63+
64+
extern ifmt_set_t S_IFMTset_from_name(const uschar *name); /* zero on error */
6265
extern const uschar *S_IFMTix_to_long_name(int index); /* NULL on error */
63-
extern const uschar *S_IFMTix_to_ucname(int index); /* NULL on error */
64-
extern int S_IFMTix_from_name(const uschar *name); /* negative on error */
6566

66-
static inline const uschar *S_IFMT_to_name(int index) { int i = S_IFMT_to_index(index); return i<0 ? NULL : S_IFMTix_to_name(i); } /* NULL on error */
67-
static inline const uschar *S_IFMT_to_long_name(int index) { int i = S_IFMT_to_index(index); return i<0 ? NULL : S_IFMTix_to_long_name(i); } /* NULL on error */
68-
static inline const uschar *S_IFMT_to_ucname(int index) { int i = S_IFMT_to_index(index); return i<0 ? NULL : S_IFMTix_to_ucname(i); } /* NULL on error */
69-
static inline int S_IFMT_from_name(const uschar *name) { int i = S_IFMTix_from_name(name); return i<0 ? -1 : S_IFMT_from_index(i); } /* negative on error */
67+
static inline const uschar *
68+
S_IFMT_to_long_name(int ifmt) { /* NULL on error */
69+
int i = S_IFMT_to_index(ifmt);
70+
return i<0 ? NULL : S_IFMTix_to_long_name(i);
71+
}
7072

7173
/* End of lf_functions.h */

0 commit comments

Comments
 (0)