1 /*:ts=8*/
2 /*****************************************************************************
3 * FIDOGATE --- Gateway UNIX Mail/News <-> FIDO NetMail/EchoMail
4 *
5 * $Id: hosts.c,v 4.16 2004/08/22 20:19:11 n0ll Exp $
6 *
7 * Process hostname <-> node aliases from hosts file
8 *
9 *****************************************************************************
10 * Copyright (C) 1990-2004
11 * _____ _____
12 * | |___ | Martin Junius <mj.at.n0ll.dot.net>
13 * | | | | | | Radiumstr. 18
14 * |_|_|_|@home| D-51069 Koeln, Germany
15 *
16 * This file is part of FIDOGATE.
17 *
18 * FIDOGATE is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2, or (at your option) any
21 * later version.
22 *
23 * FIDOGATE is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with FIDOGATE; see the file COPYING. If not, write to the Free
30 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
31 *****************************************************************************/
32
33 #include "fidogate.h"
34
35
36
37 /*
38 * Local prototypes
39 */
40 static Host *hosts_parse_line (char *);
41 static int hosts_do_file (char *);
42
43
44 /*
45 * Hosts list
46 */
47 static Host *host_list = NULL;
48 static Host *host_last = NULL;
49
50
51
52 /*
53 * Read list of hosts from LIBDIR/HOSTS file.
54 *
55 * Format:
56 * NODE NAME [-options]
57 *
58 * Options:
59 * -p Addresses with pX point address
60 * y dito, old compatibility
61 * -d Host down
62 */
hosts_parse_line(char * buf)63 static Host *hosts_parse_line(char *buf)
64 {
65 Host *p;
66
67 char *f, *n, *o;
68 Node node;
69
70 f = strtok(buf, " \t"); /* FTN address */
71 n = strtok(NULL, " \t"); /* Internet address */
72 if(f==NULL || n==NULL)
73 return NULL;
74
75 if(strieq(f, "include"))
76 {
77 hosts_do_file(n);
78 return NULL;
79 }
80
81 if( asc_to_node(f, &node, FALSE) == ERROR )
82 {
83 logit("hosts: illegal FTN address %s", f);
84 return NULL;
85 }
86
87 p = (Host *)xmalloc(sizeof(Host));
88 p->next = NULL;
89 p->node = node;
90 p->flags = 0;
91 if(!strcmp(n, "-")) /* "-" == registered, but no name */
92 p->name = NULL;
93 else
94 {
95 if(n[strlen(n)-1] == '.') /* FQDN in HOSTS */
96 {
97 n[strlen(n)-1] = 0;
98 p->name = strsave(n);
99 }
100 else /* Add domain */
101 {
102 p->name = strsave2(n, cf_hostsdomain());
103 }
104 }
105
106 for(o=strtok(NULL, " \t"); /* Options */
107 o;
108 o=strtok(NULL, " \t") )
109 {
110 if(!strcmp(o, "y"))
111 {
112 /* y == -p */
113 p->flags |= HOST_POINT;
114 }
115 if(!strcmp(o, "-p"))
116 {
117 /* -p */
118 p->flags |= HOST_POINT;
119 }
120 if(!strcmp(o, "-d"))
121 {
122 /* -d */
123 p->flags |= HOST_DOWN;
124 }
125 #ifdef AI_1
126 if(!strcmp(o, "-a"))
127 {
128 /* -a */
129 p->flags |= HOST_ADDR;
130 }
131 #endif
132 }
133
134 debug(15, "hosts: %s %s %02x", znfp1(&p->node),
135 p->name ? p->name : "-", p->flags);
136
137 return p;
138 }
139
140
hosts_do_file(char * name)141 static int hosts_do_file(char *name)
142 {
143 FILE *fp;
144 Host *p;
145
146 debug(14, "Reading hosts file %s", name);
147
148 fp = fopen_expand_name(name, R_MODE_T, FALSE);
149 if(!fp)
150 return ERROR;
151
152 while(cf_getline(buffer, BUFFERSIZE, fp))
153 {
154 p = hosts_parse_line(buffer);
155 if(!p)
156 continue;
157
158 /* Put into linked list */
159 if(host_list)
160 host_last->next = p;
161 else
162 host_list = p;
163 host_last = p;
164 }
165
166 fclose(fp);
167
168 return OK;
169 }
170
171
hosts_init(void)172 void hosts_init(void)
173 {
174 hosts_do_file( cf_p_hosts() );
175 }
176
177
178
179 /*
180 * Lookup node/host in host_list
181 *
182 * Parameters:
183 * node, NULL --- lookup by FTN address
184 * NULL, name --- lookup by Internet address
185 */
hosts_lookup(Node * node,char * name)186 Host *hosts_lookup(Node *node, char *name)
187 {
188 Host *p;
189
190 /*
191 * FIXME: the search method should use hashing or similar
192 */
193
194 for(p=host_list; p; p=p->next)
195 {
196 if(node)
197 if(node->zone==p->node.zone &&
198 node->net ==p->node.net &&
199 node->node==p->node.node &&
200 (node->point==p->node.point || p->node.point==0) )
201 return p;
202 if(name && p->name && !stricmp(name, p->name))
203 return p;
204 }
205
206 return NULL;
207 }
208