-
Notifications
You must be signed in to change notification settings - Fork 4
/
files.c
433 lines (399 loc) · 12.7 KB
/
files.c
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* System-dependent definitions of various files, spool directories, etc */
#include "top.h"
#include "global.h"
#include "lib/std/stdio.h"
#include <ctype.h>
#include "files.h"
#include "lib/util/md5.h"
#include "lib/inet/netuser.h"
#ifdef MSDOS
char System[] = "MSDOS";
char *Startup = "/autoexec.net"; /* Initialization file */
char *Userfile = "/ftpusers"; /* Authorized FTP users and passwords */
char *Maillog = "/spool/mail.log"; /* mail log */
char *Mailspool = "/spool/mail"; /* Incoming mail */
char *Mailqdir = "/spool/mqueue"; /* Outgoing mail spool */
char *Mailqueue = "/spool/mqueue/*.wrk"; /* Outgoing mail work files */
char *Routeqdir = "/spool/rqueue"; /* queue for router */
char *Alias = "/alias"; /* the alias file */
char *Dfile = "/domain.txt"; /* Domain cache */
char *Fdir = "/finger"; /* Finger info directory */
char *Arealist = "/spool/areas";/* List of message areas */
char *Helpdir = "/spool/help"; /* Mailbox help file directory */
char *Rewritefile = "/spool/rewrite"; /* Address rewrite file */
char *Newsdir = "/spool/news"; /* News messages and NNTP data */
char *Popusers = "/popusers"; /* POP user and passwd file */
char *Signature = "/spool/signatur"; /* Mail signature file directory */
char *Forwardfile = "/spool/forward.bbs"; /* Mail forwarding file */
char *Historyfile = "/spool/history"; /* Message ID history file */
char *Tmpdir = "/tmp";
char Eol[] = "\r\n";
#define SEPARATOR "/"
#endif
#ifdef UNIX
char System[] = "UNIX";
char *Startup = "./startup.net"; /* Initialization file */
char *Config = "./config.net"; /* Device configuration list */
char *Userfile = "./ftpusers";
char *Mailspool = "./mail";
char *Maillog = "./mail.log"; /* mail log */
char *Mailqdir = "./mqueue";
char *Mailqueue = "./mqueue/*.wrk";
char *Routeqdir = "./rqueue"; /* queue for router */
char *Alias = "./alias"; /* the alias file */
char *Dfile = "./domain.txt"; /* Domain cache */
char *Fdir = "./finger"; /* Finger info directory */
char *Arealist = "./areas"; /* List of message areas */
char *Helpdir = "./help"; /* Mailbox help file directory */
char *Rewritefile = "./rewrite"; /* Address rewrite file */
char *Newsdir = "./news"; /* News messages and NNTP data */
char *Popusers = "./popusers"; /* POP user and passwd file */
char *Signature = "./signatur"; /* Mail signature file directory */
char *Forwardfile = "./forward.bbs"; /* Mail forwarding file */
char *Historyfile = "./history"; /* Message ID history file */
char *Tmpdir = "/tmp";
#define SEPARATOR "/"
char Eol[] = "\n";
#endif
#ifdef AMIGA
char System[] = "AMIGA";
char *Startup = "TCPIP:net-startup";
char *Config = "TCPIP:config.net"; /* Device configuration list */
char *Userfile = "TCPIP:ftpusers";
char *Mailspool = "TCPIP:spool/mail";
char *Maillog = "TCPIP:spool/mail.log";
char *Mailqdir = "TCPIP:spool/mqueue";
char *Mailqueue = "TCPIP:spool/mqueue/#?.wrk";
char *Routeqdir = "TCPIP:spool/rqueue"; /* queue for router */
char *Alias = "TCPIP:alias"; /* the alias file */
char *Dfile = "TCPIP:domain.txt"; /* Domain cache */
char *Fdir = "TCPIP:finger"; /* Finger info directory */
char *Arealist = "TCPIP:spool/areas"; /* List of message areas */
char *Helpdir = "TCPIP:spool/help"; /* Mailbox help file directory */
char *Rewritefile = "TCPIP:spool/rewrite"; /* Address rewrite file */
char *Newsdir = "TCPIP:spool/news"; /* News messages and NNTP data */
char *Popusers = "TCPIP:/popusers"; /* POP user and passwd file */
char *Signature = "TCPIP:spool/signatur"; /* Mail signature file directory */
char *Forwardfile = "TCPIP:spool/forward.bbs"; /* Mail forwarding file */
char *Historyfile = "TCPIP:spool/history"; /* Message ID history file */
char *Tmpdir = "TCPIP:tmp";
#define SEPARATOR "/"
char Eol[] = "\r\n";
#endif
#ifdef MAC
char System[] = "MACOS";
char *Startup ="Mikes Hard Disk:net.start";
char *Config = "Mikes Hard Disk:config.net"; /* Device configuration list */
char *Userfile = "Mikes Hard Disk:ftpusers";
char *Mailspool = "Mikes Hard Disk:spool:mail:";
char *Maillog = "Mikes Hard Disk:spool:mail.log:";
char *Mailqdir = "Mikes Hard Disk:spool:mqueue:";
char *Mailqueue = "Mikes Hard Disk:spool:mqueue:*.wrk";
char *Routeqdir = "Mikes Hard Disk:spool/rqueue:"; /* queue for router */
char *Alias = "Mikes Hard Disk:alias"; /* the alias file */
char *Dfile = "Mikes Hard Disk:domain:txt"; /* Domain cache */
char *Fdir = "Mikes Hard Disk:finger"; /* Finger info directory */
char *Arealist = "Mikes Hard Disk:spool/areas"; /* List of message areas */
char *Helpdir = "Mikes Hard Disk:spool/help"; /* Mailbox help file directory */
char *Rewritefile = "Mikes Hard Disk:spool/rewrite"; /* Address rewrite file */
char *Newsdir = "Mikes Hard Disk:spool/news"; /* News messages and NNTP data */
char *Popusers = "Mikes Hard Disk:/popusers"; /* POP user and passwd file */
char *Signature = "Mikes Hard Disk:spool/signatur"; /* Mail signature file directory */
char *Forwardfile = "Mikes Hard Disk:spool/forward.bbs"; /* Mail forwarding file */
char *Historyfile = "Mikes Hard Disk:spool/history"; /* Message ID history file */
char *Tmpdir = "Mikes Hard Disk:tmp";
#define SEPARATOR "/"
char Eol[] = "\r";
#endif
static char *rootdir = "";
/* Establish a root directory other than the default. Can only be called
* once, at startup time
*/
void
initroot(root)
char *root;
{
rootdir = strdup( root );
Startup = rootdircat(Startup);
Userfile = rootdircat(Userfile);
Maillog = rootdircat(Maillog);
Mailspool = rootdircat(Mailspool);
Mailqdir = rootdircat(Mailqdir);
Mailqueue = rootdircat(Mailqueue);
Routeqdir = rootdircat(Routeqdir);
Alias = rootdircat(Alias);
Dfile = rootdircat(Dfile);
Fdir = rootdircat(Fdir);
Arealist = rootdircat(Arealist);
Helpdir = rootdircat(Helpdir);
Rewritefile = rootdircat(Rewritefile);
Newsdir = rootdircat(Newsdir);
Signature = rootdircat(Signature);
Forwardfile = rootdircat(Forwardfile);
Historyfile = rootdircat(Historyfile);
}
/* Concatenate root, separator and arg strings into a malloc'ed output
* buffer, then remove repeated occurrences of the separator char
*/
char *
rootdircat(filename)
char *filename;
{
char *out = filename;
if ( strlen(rootdir) > 0 ) {
char *separator = SEPARATOR;
out = mallocw( strlen(rootdir)
+ strlen(separator)
+ strlen(filename) + 1);
strcpy(out,rootdir);
strcat(out,separator);
strcat(out,filename);
if(*separator != '\0'){
char *p1, *p2;
/* Remove any repeated occurrences */
p1 = p2 = out;
while(*p2 != '\0'){
*p1++ = *p2++;
while(p2[0] == p2[-1] && p2[0] == *separator)
p2++;
}
*p1 = '\0';
}
}
return out;
}
/* Read through FTPUSERS looking for user record
* Returns line which matches username, or NULL when no match.
* Each of the other variables must be copied before freeing the line.
*/
char *
userlookup(username,password,directory,permission,ip_address)
char *username;
char **password;
char **directory;
int *permission;
int32 *ip_address;
{
kFILE *fp;
char *buf;
char *cp;
if((fp = kfopen(Userfile,READ_TEXT)) == NULL)
/* Userfile doesn't exist */
return NULL;
buf = mallocw(kBUFSIZ);
while ( kfgets(buf,kBUFSIZ,fp) != NULL ){
if(*buf == '#')
continue; /* Comment */
if((cp = strchr(buf,' ')) == NULL)
/* Bogus entry */
continue;
*cp++ = '\0'; /* Now points to password */
if( STRICMP(username,buf) == 0 )
break; /* Found user */
}
if(kfeof(fp)){
/* username not found in file */
kfclose(fp);
free(buf);
return NULL;
}
kfclose(fp);
if ( password != NULL )
*password = cp;
/* Look for space after password field in file */
if((cp = strchr(cp,' ')) == NULL) {
/* Invalid file entry */
free(buf);
return NULL;
}
*cp++ = '\0'; /* Now points to directory field */
if ( directory != NULL )
*directory = cp;
if((cp = strchr(cp,' ')) == NULL) {
/* Permission field missing */
free(buf);
return NULL;
}
*cp++ = '\0'; /* now points to permission field */
if ( permission != NULL )
*permission = (int)strtol( cp, NULL, 0 );
if((cp = strchr(cp,' ')) == NULL) {
/* IP address missing */
if ( ip_address != NULL )
*ip_address = 0L;
} else {
*cp++ = '\0'; /* now points at IP address field */
if ( ip_address != NULL )
*ip_address = resolve( cp );
}
return buf;
}
/* Subroutine for logging in the user whose name is name and password is pass.
* The buffer path should be long enough to keep a line from the userfile.
* If pwdignore is true, the password check will be overridden.
* The return value is the permissions field or -1 if the login failed.
* Path is set to point at the path field, and pwdignore will be true if no
* particular password was needed for this user.
*/
int
userlogin(name,pass,path,len,pwdignore)
char *name;
char *pass;
char **path; /* Can be NULL if not interested */
int len; /* Length of buffer pointed at by *path */
int *pwdignore;
{
char *buf;
char *password;
char *directory;
int permission;
int anonymous;
char *cp;
uint8 hashpass[16],digest[16];
MD5_CTX md;
if ( (buf = userlookup( name, &password, &directory,
&permission, NULL )) == NULL ) {
return -1;
}
anonymous = *pwdignore;
if(strcmp(password,"*") == 0){
anonymous = TRUE; /* User ID is password-free */
} else {
if(readhex(hashpass,password,sizeof(hashpass)) != sizeof(hashpass)){
/* Invalid hashed password in file */
free(buf);
return -1;
}
MD5Init(&md);
MD5Update(&md,(unsigned char *)name,strlen(name));
MD5Update(&md,(unsigned char *)pass,strlen(pass));
MD5Final(digest,&md);
if(memcmp(digest,hashpass,sizeof(hashpass)) != 0){
/* Incorrect password given */
free(buf);
return -1;
}
}
if ( path != NULL ) {
if (strlen( directory ) + 1 > len ) {
/* not enough room for path */
free(buf);
return -1;
}
#if defined(AMIGA)
/*
* Well, on the Amiga, a file can be referenced by many names:
* device names (DF0:) or volume names (My_Disk:). This hunk of code
* passed the pathname specified in the ftpusers file, and gets the
* absolute path copied into the user's buffer. We really should just
* allocate the buffer and return a pointer to it, since the caller
* really doesn't have a good idea how long the path string is..
*/
if ( (directory = pathname("", directory)) != NULL ) {
strcpy(*path, directory);
free(directory);
} else {
**path = '\0';
}
#else
strcpy(*path,directory);
/* Convert any backslashes to forward slashes, for backward
* compatibility with the old NET
*/
while((cp = strchr(*path,'\\')) != NULL)
*cp = '/';
#endif
}
free(buf);
*pwdignore = anonymous;
/* Finally return the permission bits */
return permission;
}
/* MD5 hash plaintext passwords in user file */
void
usercvt()
{
kFILE *fp,*fptmp;
char *buf;
uint8 hexbuf[16],digest[16];
int needsit = 0;
int len,nlen,plen,i;
char *pass;
MD5_CTX md;
if((fp = kfopen(Userfile,READ_TEXT)) == NULL)
return; /* Userfile doesn't exist */
buf = mallocw(kBUFSIZ);
while(kfgets(buf,kBUFSIZ,fp) != NULL){
rip(buf);
len = strlen(buf);
if(len == 0 || *buf == '#')
continue; /* Blank or comment line */
if((nlen = strcspn(buf,Whitespace)) == len)
continue; /* No end to the name! */
/* Skip whitespace between name and pass */
for(pass=&buf[nlen];isspace(*pass);pass++)
;
if(*pass != '\0' && *pass != '*'
&& readhex(hexbuf,pass,sizeof(hexbuf)) != 16){
needsit = 1;
break;
}
}
if(!needsit){
/* Everything is in order */
kfclose(fp);
free(buf);
return;
}
/* At least one entry needs its password hashed */
krewind(fp);
fptmp = ktmpfile();
while(kfgets(buf,kBUFSIZ,fp) != NULL){
rip(buf);
if((len = strlen(buf)) == 0 || *buf == '#'
|| (nlen = strcspn(buf,Whitespace)) == len){
/* Line is blank, a comment or unparseable;
* copy unchanged
*/
kfputs(buf,fptmp);
kfputc('\n',fptmp);
continue;
}
/* Skip whitespace between name and pass */
for(pass=&buf[nlen];isspace(*pass);pass++)
;
if(*pass == '\0' || *pass == '*'
|| (plen = strcspn(pass,Whitespace)) == strlen(pass)
|| readhex(hexbuf,pass,sizeof(hexbuf)) == sizeof(hexbuf)){
/* Other fields are missing, no password is required,
* or password is already hashed; copy unchanged
*/
kfputs(buf,fptmp);
kfputc('\n',fptmp);
continue;
}
MD5Init(&md);
MD5Update(&md,(unsigned char *)buf,nlen); /* Hash name */
MD5Update(&md,(unsigned char *)pass,plen); /* Hash password */
MD5Final(digest,&md);
kfwrite(buf,1,nlen,fptmp); /* Write name */
kfputc(' ',fptmp); /* and space */
for(i=0;i<16;i++) /* Write hashed password */
kfprintf(fptmp,"%02x",digest[i]);
kfputs(&pass[plen],fptmp); /* Write remainder of line */
kfputc('\n',fptmp);
}
/* Now copy the temp file back into the userfile */
kfclose(fp);
krewind(fptmp);
if((fp = kfopen(Userfile,WRITE_TEXT)) == NULL){
kprintf("Can't rewrite %s\n",Userfile);
free(buf);
return;
}
while(kfgets(buf,kBUFSIZ,fptmp) != NULL)
kfputs(buf,fp);
kfclose(fp);
kfclose(fptmp);
free(buf);
}