1 /*:ts=8*/
2 /*****************************************************************************
3  * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
4  *
5  * $Id: aliases.c,v 4.15 2004/08/22 20:19:11 n0ll Exp $
6  *
7  * Read user name aliases from file. The alias.users format is as follows:
8  *	username    Z:N/F.P    Full Name
9  *
10  *****************************************************************************
11  * Copyright (C) 1990-2004
12  *  _____ _____
13  * |	 |___  |   Martin Junius
14  * | | | |   | |   Radiumstr. 18  	     Internet:	mj.at.n0ll.dot.net
15  * |_|_|_|@home|   D-51069 Koeln, Germany
16  *
17  * This file is part of FIDOGATE.
18  *
19  * FIDOGATE is free software; you can redistribute it and/or modify it
20  * under the terms of the GNU General Public License as published by the
21  * Free Software Foundation; either version 2, or (at your option) any
22  * later version.
23  *
24  * FIDOGATE is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
27  * General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with FIDOGATE; see the file COPYING.  If not, write to the Free
31  * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32  *****************************************************************************/
33 
34 #include "fidogate.h"
35 
36 
37 
38 /*
39  * Local prototypes
40  */
41 static int    anodeeq		(Node *, Node *);
42 static Alias *alias_parse_line	(char *);
43 static int    alias_do_file	(char *);
44 
45 
46 /*
47  * Alias list
48  */
49 static Alias *alias_list = NULL;
50 static Alias *alias_last = NULL;
51 
52 
53 
54 /*
55  * Read list of aliases from LIBDIR/ALIASES file.
56  *
57  * Format:
58  *     ALIAS	NODE	"FULL NAME"
59  */
alias_parse_line(char * buf)60 static Alias *alias_parse_line(char *buf)
61 {
62     Alias *p;
63     char *u, *n, *f;
64     Node node;
65     char *un, *ud;
66 
67     u = xstrtok(buf,  " \t");	/* User name */
68     n = xstrtok(NULL, " \t");	/* FTN node */
69     f = xstrtok(NULL, " \t");	/* Full name */
70     if(u==NULL || n==NULL)
71 	return NULL;
72     if(strieq(u, "include"))
73     {
74 	alias_do_file(n);
75 	return NULL;
76     }
77     if(f==NULL)
78 	return NULL;
79 
80     if( asc_to_node(n, &node, FALSE) == ERROR )
81     {
82 	logit("hosts: illegal FTN address %s", n);
83 	return NULL;
84     }
85 
86     p = (Alias *)xmalloc(sizeof(Alias));
87     p->next     = NULL;
88     p->node     = node;
89     un = xstrtok(u,  "@");	/* User name */
90     ud = xstrtok(NULL, " \t");	/* User domain */
91     p->username = strsave(un);
92     p->userdom  = ud ? strsave(ud) : NULL;
93     p->fullname = strsave(f);
94 
95     if(p->userdom)
96 	debug(15, "aliases: %s@%s %s %s", p->username, p->userdom,
97 	      znfp1(&p->node), p->fullname);
98     else
99 	debug(15, "aliases: %s %s %s", p->username,
100 	      znfp1(&p->node), p->fullname);
101 
102     return p;
103 }
104 
105 
alias_do_file(char * name)106 static int alias_do_file(char *name)
107 {
108     FILE *fp;
109     Alias *p;
110 
111     debug(14, "Reading aliases file %s", name);
112 
113     fp = fopen_expand_name(name, R_MODE_T, FALSE);
114     if(!fp)
115 	return ERROR;
116 
117     while(cf_getline(buffer, BUFFERSIZE, fp))
118     {
119 	p = alias_parse_line(buffer);
120 	if(!p)
121 	    continue;
122 
123 	/*
124 	 * Put into linked list
125 	 */
126 	if(alias_list)
127 	    alias_last->next = p;
128 	else
129 	    alias_list       = p;
130 	alias_last       = p;
131     }
132 
133     fclose(fp);
134 
135     return OK;
136 }
137 
138 
alias_init(void)139 void alias_init(void)
140 {
141     alias_do_file( cf_p_aliases() );
142 }
143 
144 
145 
146 /*
147  * Lookup alias in alias_list
148  *
149  * Parameters:
150  *     node, username, NULL     --- lookup by FTN node and username
151  *     node, NULL    , fullname --- lookup by FTN node and fullname
152  *
153  * The lookup for node is handled somewhat special: if node->point !=0,
154  * then the alias matching the complete address including point will
155  * be found. If not, then the alias comparison ignores the point address.
156  * e.g.:
157  *     mj    2:2452/110.1    "Martin Junius"
158  * An alias_lookup(2:2452/110.1, "mj", NULL) as well as
159  * alias_lookkup(2:2452/110, "mj", NULL) will return this alias entry.
160  */
alias_lookup(Node * node,char * username,char * fullname)161 Alias *alias_lookup(Node *node, char *username, char *fullname)
162 {
163     Alias *a;
164 
165     for(a=alias_list; a; a=a->next)
166     {
167 	if(username)
168 	    if(!stricmp(a->username, username) && anodeeq(node, &a->node))
169 		return a;
170 	if(fullname)
171 	    if(!stricmp(a->fullname, fullname) && anodeeq(node, &a->node))
172 		return a;
173     }
174 
175     return NULL;
176 }
177 
178 
179 
alias_lookup_strict(Node * node,char * username,char * fullname)180 Alias *alias_lookup_strict(Node *node, char *username, char *fullname)
181 {
182     Alias *a;
183 
184     for(a=alias_list; a; a=a->next)
185     {
186 	if(username)
187 	    if(!stricmp(a->username, username) && node_eq(node, &a->node))
188 		return a;
189 	if(fullname)
190 	    if(!stricmp(a->fullname, fullname) && node_eq(node, &a->node))
191 		return a;
192     }
193     for(a=alias_list; a; a=a->next)
194     {
195 	if(username)
196 	    if(!stricmp(a->username, username) && node_np_eq(node, &a->node))
197 		return a;
198 	if(fullname)
199 	    if(!stricmp(a->fullname, fullname) && node_np_eq(node, &a->node))
200 		return a;
201     }
202 
203     return NULL;
204 }
205 
alias_lookup_userdom(Node * node,RFCAddr * rfc,char * fullname)206 Alias *alias_lookup_userdom(Node *node, RFCAddr *rfc, char *fullname)
207 {
208     Alias *a;
209 
210     if(!rfc)
211 	return NULL;
212 
213     for(a=alias_list; a; a=a->next)
214     {
215 	if( a->userdom && (!stricmp(a->username, rfc->user) && !stricmp(a->userdom, rfc->addr)) )
216 	    return a;
217     }
218 
219     return NULL;
220 }
221 
222 
223 
224 /*
225  * anodeeq() --- compare node adresses
226  *
227  * Special point treatment: if a->point != 0 and b->point !=0, compare the
228  * complete FTN address including point. If not, compare ignoring the point
229  * address.
230  */
anodeeq(Node * a,Node * b)231 static int anodeeq(Node *a, Node *b)
232             			/* Sender/receiver address */
233             			/* ALIASES address */
234 {
235     return a->point && b->point
236 	   ?
237 	   a->zone==b->zone && a->net==b->net && a->node==b->node &&
238 	   a->point==b->point
239 	   :
240 	   a->zone==b->zone && a->net==b->net && a->node==b->node
241 	   ;
242 }
243