/* System-dependent definitions of various files, spool directories, etc */
/* Mods by PA0GRI */
#include <stdio.h>
#include "global.h"
#include "netuser.h"
#include "files.h"

#ifdef	MSDOS
char *Startup	= "/autoexec.nos";		/* Initialization file */
char *Userfile	= "/ftpusers";			/* Authorized FTP users and passwords */
char *Hostfile	= "/net.rc";			/* hosts 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 *Signature	= "/spool/signatur";		/* Mail signature file directory */
char *Popusers	= "/popusers";			/* POP user and password file */
char *Newsdir	= "/spool/news";		/* News messages and NNTP data */
char *Forwardfile = "/spool/forward.bbs";	/* Mail forwarding file */
char *Historyfile = "/spool/history";		/* Message ID history file */
char *Netromfile = "/netrom.sav";		/* Netrom node save file */
char Eol[]	= "\r\n";
#define	SEPARATOR	"/"
#endif

#ifdef	UNIX
char *Startup	= "./startup.nos";		/* Initialization file */
char *Config	= "./config.nos";		/* Device configuration list */
char *Hostfile	= "./net.rc";			/* hosts and passwords */
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 *Signature	= "./signatur";			/* Mail signature file directory */
char *Popusers	= "./popusers";			/* POP user and password file */
char *Newsdir	= "./news";			/* News messages and NNTP data */
char *Forwardfile = "./forward.bbs";		/* Mail forwarding file */
char *Historyfile = "./history";		/* Message ID history file */
char *Netromfile = "./netrom.sav";		/* Netrom node save file */
char Eol[]	= "\n";
#define	SEPARATOR	"/"
#endif

#ifdef	AMIGA
char *Startup	= "TCPIP:nos-startup";
char *Config	= "TCPIP:config.nos";		/* Device configuration list */
char *Hostfile	= "TCPIP:net.rc";		/* hosts and passwords */
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 *Signature	= "TCPIP:spool/signatur";	/* Mail signature file directory */
char *Popusers	= "TCPIP:/popusers";		/* POP user and password file */
char *Newsdir	= "TCPIP:/spool/news";		/* News messages and NNTP data */
char *Forwardfile = "TCPIP:spool/forward.bbs";	/* Mail forwarding file */
char *Historyfile = "TCPIP:spool/history";	/* Message ID history file */
char *Netromfile = "TCPIP:/netrom.sav";		/* Netrom node save file */
char Eol[]	= "\r\n";
#define	SEPARATOR	"/"
#endif

#ifdef	MAC
char *Startup	="Mikes Hard Disk:nos.start";
char *Config	= "Mikes Hard Disk:config.nos";	/* Device configuration list */
char *Hostfile	= "Mikes Hard Disk:net.rc";	/* hosts and passwords */
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 *Signature	= "Mikes Hard Disk:spool/signatur";	/* Mail signature file directory */
char *Popusers	= "Mikes Hard Disk:/popusers";		/* POP user and password file */
char *Newsdir	= "Mikes Hard Disk:/spool/news";	/* News messages and NNTP data */
char *Forwardfile = "Mikes Hard Disk:spool/forward.bbs"; /* Mail forwarding file */
char *Historyfile = "Mikes Hard Disk:spool/history";	/* Message ID history file */
char *Netromfile = "/Mikes Hard Disk:netrom.sav";	/* Netrom node save file */
char Eol[]	= "\r";
#define	SEPARATOR	":"
#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);
	Hostfile = rootdircat(Hostfile);
	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);
	Signature = rootdircat(Signature);
	Popusers = rootdircat(Popusers);
	Newsdir = rootdircat(Newsdir);
	Forwardfile = rootdircat(Forwardfile);
	Historyfile = rootdircat(Historyfile);
	Netromfile = rootdircat(Netromfile);
}

/* 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;
}


/* 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;
int len;			/* Length of buffer pointed at by *path */
int *pwdignore;
{
	char *cp,*cp1;
	FILE *fp;
	char *anonymous = NULLCHAR;
	int anony,perm;

	if((fp = fopen(Userfile,READ_TEXT)) == NULLFILE)
		/* Userfile doesn't exist */
		return -1;
	while(fgets(*path,len,fp),!feof(fp)){
		if(*path[0] == '#')
			continue;	/* Comment */
		if((cp = strchr(*path,' ')) == NULLCHAR)
			/* Bogus entry */
			continue;
		*cp++ = '\0';		/* Now points to password */
		if(stricmp(name,*path) == 0)
			break;		/* Found user name */
		if(stricmp("anonymous",*path) == 0)
			anonymous = strdup(cp);	/* remember their anon entry */
	}
	if((anonymous == NULLCHAR) && (feof(fp))){
		/* User name not found in file, nor was anonymous */
		fclose(fp);
		return -1;
	}
	if(feof(fp)){
		/* restore anonymous to the buffer */
		strcpy(cp = *path, anonymous);
	}
	fclose(fp);
	/* Look for space after password field in file */
	if((cp1 = strchr(cp,' ')) == NULLCHAR)
		/* Invalid file entry */
		return -1;
	*cp1++ = '\0';	/* Now points to path field */
	anony = *pwdignore;
	if(strcmp(cp,"*") == 0)
		anony = 1;	/* User ID is password-free */
	if(!anony && strcmp(cp,pass) != 0)
		/* Password required, but wrong one given */
		return -1;
	if((cp = strchr(cp1,' ')) == NULLCHAR)
		/* Permission field missing */
		return -1;
	*cp++ = '\0';	/* now points to permission field */
	perm = atoi(cp);
#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..
	 */
	cp1 = pathname("", cp1);
	if (cp1)
		strcpy(*path, cp1);
	else
		**path = '\0';
	free(cp1);
#else
	strcpy(*path,cp1);
	/* Convert any backslashes to forward slashes, for backward
	 * compatibility with the old NET
	 */
	while((cp = strchr(*path,'\\')) != NULLCHAR)
		*cp = '/';
#endif
	*pwdignore = anony;
	/* Finally return the permission bits */
	return perm;
}
	
