File: addr.c

package info (click to toggle)
deliver 2.1.14-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 488 kB
  • ctags: 914
  • sloc: ansic: 6,050; yacc: 405; makefile: 131; sh: 34
file content (237 lines) | stat: -rw-r--r-- 4,656 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* $Id: addr.c,v 1.4 1999/12/02 01:06:06 chip Exp $
 *
 * Operations on addresses, (ASCII representations of destinations).
 *
 * $Log: addr.c,v $
 * Revision 1.4  1999/12/02 01:06:06  chip
 * Make SANITIZE contain only graphical danger characters.
 *
 * Revision 1.3  1993/10/28 16:49:51  chip
 * Declare function return types, including void.
 *
 * Revision 1.2  1991/05/23  17:23:19  chip
 * Follow RFC822 definition of header syntax.
 * Guard isxxx() macros against negative values.
 *
 * Revision 1.1  1991/05/13  18:36:55  chip
 * Initial revision
 *
 */

#include "deliver.h"

/*----------------------------------------------------------------------
 * Check an address for cleanliness.
 * That is, make sure that it is devoid of shell metacharacters.
 * And make sure it's got at least one character, too.
 */

int
addr_clean(addr)
char *addr;
{
    char *p;
    static char sanitize[] = SANITIZE;

    if (*addr == 0)
	return FALSE;

    for (p = addr; *p; ++p)
    {
	int c = *p & 0xFF;
	if (
#ifdef HH_STRING
	    !isgraph(c)
#else
	    (c < 0x20 || c > 0x7F)
#endif
	    || strchr(sanitize, c))
	{
	    return FALSE;
	}
    }

    return TRUE;
}

/*----------------------------------------------------------------------
 * Report the class of a string representation of a destination.
 */

DCLASS
addr_class(s)
char *s;
{
    char *p;

    for (p = s; *p; ++p)
    {
	switch (*p)
	{
	case '!':
	    return CL_UUCP;
	case ':':
	    return CL_MBOX;
	case '|':
	    return CL_PROG;
	case '?':
	    return CL_USER;
	}
    }

    return CL_USER;
}

/*----------------------------------------------------------------------
 * Parse a string representation of a destination.
 * If dfile_ct is non-NULL, then a missing user name refers to that user;
 *	otherwise, a missing user name refers to the effective user id.
 * If dfile_ct is NULL, then we only permit user names and UUCP addresses;
 *      otherwise, we permit all kinds of addresses.
 */

DEST *
addr_dest(s, dfile_ct)
char *s;
CONTEXT *dfile_ct;
{
    DEST *d;
    CONTEXT *ct;
    char *buf, *p, *q, *user, *param;
    char sep;
    int i, legal;

    /*
     * If this is NOT a delivery file, restrict address class
     * to user and UUCP.
     */

    if (!dfile_ct)
    {
	DCLASS class;

	class = addr_class(s);
	if ((class != CL_USER && class != CL_UUCP)
	    || strcmp(s, DFILE_DROP) == 0)
	{
	    d = dest(s, CL_USER, (char *) NULL);
	    dest_err(d, E_DFONLY);
	    return d;
	}
    }

    /*
     * Find the additional information after the user name.
     * Bang means UUCP, colon means mailbox, pipe means pipe,
     * question means error.
     */

    for (i = 0; (sep = s[i]) != 0; ++i)
    {
	if (sep == '!' || sep == ':' || sep == '|' || sep == '?')
	    break;
    }

    /*
     * Deal with UUCP paths before the user-name parsing begins.
     */

    if (sep == '!')
	return dest(s, CL_UUCP, (char *) NULL);

    /*
     * Copy string to local buffer so we can mangle it.
     * Chop it up into user name and parameter.
     */

    buf = copystr(s);
    if (buf[i])
	buf[i++] = 0;
    user = buf;
    param = buf + i;

    /*
     * If user name is blank, provide default.
     */

    if (*user == 0)
	user = (dfile_ct ? dfile_ct : eff_ct)->ct_name;

    /*
     * Determine if we can get there from here.
     */

    if ((ct = name_context(user)) == NULL)
	legal = TRUE;		/* we can report "no such user" later */
    else if (dfile_ct)
	legal = ok_context(dfile_ct->ct_uid, dfile_ct->ct_uid,
			   dfile_ct->ct_gid, ct);
    else
	legal = ok_context(eff_uid, real_uid, real_gid, ct);

    /*
     * Now take action based on address class.
     */

    /* Could be "user|program" ... */

    if (sep == '|')
	d = legal ? dest(user, CL_PROG, param) : NULL;

    /* Could be "user?error" ... */

    else if (sep == '?')
    {
	if (legal)
	{
	    d = dest(user, CL_USER, (char *) NULL);
	    dest_err(d, E_ERRMSG);
	    d->d_errmsg = copystr(param);
	}
	else
	    d = NULL;
    }

    /* Must be "user" or "user:mailbox" */

    else
    {
	/*
	 * Strip whitespace, eliminate duplicate slashes from
	 * mailbox filename.
	 */

	p = q = param;
	while (*p)
	{
	    if (isspace(*p & 0xFF))
		++p;
	    else if ((*q++ = *p++) == '/')
	    {
		while (*p == '/')
		    ++p;
	    }
	}
	*q = 0;

	/*
	 * Now that blanks are gone, we decide mailbox or not.
	 * Delaying this test means that "user" equals "user: ".
	 */

	if (*param)
	    d = legal ? dest(user, CL_MBOX, param) : NULL;
	else
	    d = dest(user, CL_USER, (char *) NULL);
    }

    /*
     * We allocated the buffer; now we free it.
     */

    free(buf);

    /* Return parsed destination. */

    return d;
}