BSD 3 development
[unix-history] / usr / src / cmd / uucp / chkpth.c
CommitLineData
8291abf9
BJ
1 /* chkpth 2.2 5/21/79 13:20:43 */
2#include "uucp.h"
3#include <sys/types.h>
4#include <sys/stat.h>
5
6static char SiD[] = "@(#)chkpth 2.2";
7
8
9#define DFLTNAME "default"
10#define MAXUSERS 20
11struct userpath {
12 char *us_lname;
13 char *us_mname;
14 char us_callback;
15 char **us_path;
16};
17struct userpath Upt[15];
18struct userpath *Mchdef = NULL, *Logdef = NULL;
19int Nbrusers = 0;
20int Uptfirst = 1;
21
22/*******
23 * chkpth(logname, mchname, path)
24 * char *path, *logname, *mchname;
25 *
26 * chkpth - this routine will check the path table for the
27 * machine or log name (non-null parameter) to see if the
28 * input path (path)
29 * starts with an acceptable prefix.
30 *
31 * return codes: 0 | FAIL
32 */
33
34chkpth(logname, mchname, path)
35char *path, *logname, *mchname;
36{
37 struct userpath *u;
38 extern char *lastpart();
39 char **p, *s;
40 char c;
41 int ret, i;
42
43 if (Uptfirst) {
44 ret = rdpth(Upt);
45 ASSERT(ret == 0, "INIT USERFILE %d", Nbrusers);
46 Uptfirst = 0;
47 }
48 for (u = Upt, i = 0; i < Nbrusers; i++, u++) {
49 if (*logname != '\0' && strcmp(logname, u->us_lname) == SAME)
50 break;
51 if (*mchname != '\0' && strcmp(mchname, u->us_mname) == SAME)
52 break;
53
54 }
55 if (i >= Nbrusers) {
56 if (*logname == '\0')
57 u = Mchdef;
58 else
59 u = Logdef;
60 if (u == NULL)
61 return(FAIL);
62 }
63 /* found user name */
64 p = u->us_path;
65 /* check for /../ in path name */
66 for (s = path; *s != '\0'; s++) {
67 if (*s == '/' && prefix("../", (++s)))
68 return(FAIL);
69 }
70
71 for (p = u->us_path; *p != NULL; p++)
72 if (prefix(*p, path))
73 return(0);
74
75 if (prefix(Spool, path)) {
76 if ((c = *lastpart(path)) == DATAPRE
77 || c == XQTPRE)
78 return(0);
79 }
80 /* path name not valid */
81 return(FAIL);
82}
83
84
85/***
86 * rdpth(u)
87 * struct userpath *u;
88 *
89 * rdpth - this routine will read the USFILE and
90 * construct the userpath structure pointed to by (u);
91 *
92 * return codes: 0 | FAIL
93 */
94
95rdpth(u)
96struct userpath *u;
97{
98 char buf[BUFSIZ + 1], *pbuf[BUFSIZ + 1], *pc, **cp;
99 extern char *calloc(), *index();
100 FILE *uf;
101
102 if ((uf = fopen(USERFILE, "r")) == NULL) {
103 /* can not open file */
104 return(FAIL);
105 }
106
107 while (fgets(buf, BUFSIZ, uf) != NULL) {
108 int nargs, i;
109 if (++Nbrusers > MAXUSERS) {
110 fclose(uf);
111 return(FAIL);
112 }
113 if ((pc = calloc(strlen(buf) + 1, sizeof (char)))
114 == NULL) {
115 /* can not allocate space */
116 fclose(uf);
117 return(FAIL);
118 }
119
120 strcpy(pc, buf);
121 nargs = getargs(pc, pbuf);
122 u->us_lname = pbuf[0];
123 pc = index(u->us_lname, ',');
124 if (pc != NULL)
125 *pc++ = '\0';
126 else
127 pc = u + strlen(u->us_lname);
128 u->us_mname = pc;
129 if (*u->us_lname == '\0' && Logdef == NULL)
130 Logdef = u;
131 else if (*u->us_mname == '\0' && Mchdef == NULL)
132 Mchdef = u;
133 i = 1;
134 if (strcmp(pbuf[1], "c") == SAME) {
135 u->us_callback = 1;
136 i++;
137 }
138 else
139 u->us_callback = 0;
140 if ((cp = u->us_path =
141 calloc(nargs - i + 1, sizeof (char *))) == NULL) {
142 /* can not allocate space */
143 fclose(uf);
144 return(FAIL);
145 }
146
147 while (i < nargs)
148 *cp++ = pbuf[i++];
149 *cp = NULL;
150 u++;
151 }
152
153 fclose(uf);
154 return(0);
155}
156
157
158/***
159 * callback(name) check for callback
160 * char *name;
161 *
162 * return codes:
163 * 0 - no call back
164 * 1 - call back
165 */
166
167callback(name)
168char *name;
169{
170 struct userpath *u;
171 int ret, i;
172
173 if (Uptfirst) {
174 ret = rdpth(Upt);
175 ASSERT(ret == 0, "INIT USERFILE %d", Nbrusers);
176 Uptfirst = 0;
177 }
178
179 for (u = Upt, i = 0; i < Nbrusers; u++, i++) {
180 if (strcmp(u->us_lname, name) != SAME)
181 continue;
182
183 /* found user name */
184 return(u->us_callback);
185 }
186
187 /* userid not found */
188 return(0);
189}
190
191
192/***
193 * chkperm(file, user, mopt) check write permission of file
194 * char *file, *user;
195 * char *mopt; none NULL - create directories
196 *
197 * if mopt != NULL and permissions are ok,
198 * a side effect of this routine is to make
199 * directories up to the last part of the
200 * filename (if they do not exist).
201 *
202 * return 0 | FAIL
203 */
204
205chkperm(file, user, mopt)
206char *file, *user, *mopt;
207{
208 struct stat s;
209 int ret;
210 char dir[MAXFULLNAME];
211 extern char *lastpart();
212
213 if (stat(file, &s) != -1)
214 return(0);
215
216 strcpy(dir, file);
217 *lastpart(dir) = '\0';
218 if ((ret = stat(dir, &s)) == -1
219 && mopt == NULL)
220 return(FAIL);
221
222 if (ret != -1) {
223 if ((s.st_mode & ANYWRITE) == 0)
224 return(FAIL);
225 else
226 return(0);
227 }
228
229 /* make directories */
230 return(mkdirs(file));
231}