Skip to content

Commit 38e83d2

Browse files
committed
Remove unused S_IFMT_* funcs; flip nodots to allowdots; deprecate "filter=dir implies allowdots"
1 parent bea4bbb commit 38e83d2

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
@@ -129,8 +129,8 @@ dsearch_find(
129129
{
130130
struct stat statbuf;
131131
int save_errno;
132-
unsigned filter_by_type = 0;
133-
int exclude_dotdotdot = 0;
132+
ifmt_set_t filter_by_type = 0;
133+
int allowdots = 0;
134134
enum {
135135
RET_KEY, /* return the key */
136136
RET_DIR, /* return the dir without the key */
@@ -166,7 +166,7 @@ if (opts)
166166
else if (Ustrcmp(ele, "dir") == 0)
167167
ret_mode = RET_DIR;
168168
#if 0
169-
/* XXX ret=key is excluded from opts by special-case code in by search_find() */
169+
/* NOTE ret=key is excluded from opts by special-case code in by search_find() */
170170
else if (Ustrcmp(ele, "key") == 0)
171171
ret_mode = RET_KEY;
172172
#endif
@@ -179,11 +179,19 @@ if (opts)
179179
else if (Ustrncmp(ele, "filter=", 7) == 0)
180180
{
181181
ele += 7;
182-
int i = S_IFMTix_from_name(ele);
183-
if (i>=0)
184-
filter_by_type |= BIT(i);
185-
else if (Ustrcmp(ele, "subdir") == 0) filter_by_type |= BIT(S_IFMT_to_index(S_IFDIR)), exclude_dotdotdot = 1; /* dir but not "." or ".." */
186-
else if (Ustrcmp(ele, "nodots") == 0) exclude_dotdotdot = 1; /* any but "." or ".." */
182+
ifmt_set_t m = S_IFMTset_from_name(ele);
183+
if (m)
184+
{
185+
filter_by_type |= m;
186+
/* XXX issue immediate deprecation warning */
187+
#ifndef NO_DIR_IMPLIES_ALLOWDOTS
188+
/* allow "." or ".." when "dir" rather than "subdir" */
189+
if (m == S_IFMT_to_set(S_IFDIR) && ele[0] == 'd')
190+
allowdots = 1;
191+
#endif
192+
}
193+
else if (Ustrcmp(ele, "allowdots") == 0)
194+
allowdots = 1; /* allow "." or ".." */
187195
else
188196
{
189197
*errmsg = string_sprintf("unknown parameter for dsearch lookup: %s", ele-=7);
@@ -206,15 +214,15 @@ if (ignore_key)
206214
else if (keystring == NULL || keystring[0] == 0) /* in case lstat treats "/dir/" the same as "/dir/." */
207215
return FAIL;
208216

209-
DEBUG(D_lookup) debug_printf_indent(" dsearch_find: %s%sfilterbits=%#x ret=%s key=%s\n",
217+
DEBUG(D_lookup) debug_printf_indent(" dsearch_find: %s%sfilter_set=%04jx ret=%s key=%s\n",
210218
follow_symlink ? "follow, " : "",
211-
exclude_dotdotdot ? "filter=nodots, " : "",
212-
filter_by_type,
219+
allowdots ? "filter=allowdots, " : "",
220+
(uintmax_t) filter_by_type,
213221
ret_mode == RET_FULL ? "full" : ret_mode == RET_DIR ? "dir" : "key",
214222
keystring);
215223

216224
/* exclude "." and ".." when {filter=subdir} included */
217-
if (exclude_dotdotdot
225+
if (! allowdots
218226
&& keystring[0] == '.'
219227
&& (keystring[1] == 0
220228
|| keystring[1] == '.' && keystring[2] == 0))
@@ -235,7 +243,7 @@ else
235243
if (stat_result >= 0)
236244
{
237245
if (!filter_by_type
238-
|| filter_by_type & BIT(S_IFMT_to_index(statbuf.st_mode)))
246+
|| filter_by_type & S_IFMT_to_set(statbuf.st_mode))
239247
{
240248
switch (ret_mode)
241249
{

src/src/lookups/lf_check_file.c

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

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

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

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

src/src/lookups/lf_functions.h

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

61+
typedef unsigned long ifmt_set_t;
6162

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

67-
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 */
68-
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 */
69-
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 */
70-
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 */
68+
static inline const uschar *
69+
S_IFMT_to_long_name(int ifmt) { /* NULL on error */
70+
int i = S_IFMT_to_index(ifmt);
71+
return i<0 ? NULL : S_IFMTix_to_long_name(i);
72+
}
7173

7274
/* End of lf_functions.h */

0 commit comments

Comments
 (0)