xref: /openbsd-src/libexec/talkd/table.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: table.c,v 1.10 2003/06/11 14:24:46 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1983 Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 /*static char sccsid[] = "from: @(#)table.c	5.7 (Berkeley) 2/26/91";*/
34 static char rcsid[] = "$Id: table.c,v 1.10 2003/06/11 14:24:46 deraadt Exp $";
35 #endif /* not lint */
36 
37 /*
38  * Routines to handle insertion, deletion, etc on the table
39  * of requests kept by the daemon. Nothing fancy here, linear
40  * search on a double-linked list. A time is kept with each
41  * entry so that overly old invitations can be eliminated.
42  *
43  * Consider this a mis-guided attempt at modularity
44  */
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/socket.h>
48 #include <sys/queue.h>
49 #include <protocols/talkd.h>
50 #include <syslog.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "talkd.h"
56 
57 #define MAX_ID 16000	/* << 2^15 so I don't have sign troubles */
58 
59 struct	timeval tp;
60 struct	timezone txp;
61 
62 typedef struct table_entry TABLE_ENTRY;
63 
64 struct table_entry {
65 	CTL_MSG request;
66 	time_t	time;
67 	TAILQ_ENTRY(table_entry) list;
68 };
69 TAILQ_HEAD(, table_entry)	table;
70 
71 static void	delete(TABLE_ENTRY *);
72 
73 /*
74  * Init the table
75  */
76 void
77 init_table(void)
78 {
79 	TAILQ_INIT(&table);
80 }
81 
82 /*
83  * Look in the table for an invitation that matches the current
84  * request looking for an invitation
85  */
86 CTL_MSG *
87 find_match(CTL_MSG *request)
88 {
89 	TABLE_ENTRY *ptr;
90 	time_t current_time;
91 
92 	gettimeofday(&tp, &txp);
93 	current_time = tp.tv_sec;
94 	if (debug)
95 		print_request("find_match", request);
96 	for (ptr = table.tqh_first; ptr != NULL; ptr = ptr->list.tqe_next) {
97 		if ((current_time - ptr->time) > MAX_LIFE) {
98 			/* the entry is too old */
99 			if (debug)
100 				print_request("deleting expired entry",
101 				    &ptr->request);
102 			delete(ptr);
103 			continue;
104 		}
105 		if (debug)
106 			print_request("", &ptr->request);
107 		if (strcmp(request->l_name, ptr->request.r_name) == 0 &&
108 		    strcmp(request->r_name, ptr->request.l_name) == 0 &&
109 		     ptr->request.type == LEAVE_INVITE)
110 			return (&ptr->request);
111 	}
112 	if (debug)
113 		syslog(LOG_DEBUG, "find_match: not found");
114 
115 	return ((CTL_MSG *)0);
116 }
117 
118 /*
119  * Look for an identical request, as opposed to a complimentary
120  * one as find_match does
121  */
122 CTL_MSG *
123 find_request(CTL_MSG *request)
124 {
125 	TABLE_ENTRY *ptr;
126 	time_t current_time;
127 
128 	gettimeofday(&tp, &txp);
129 	current_time = tp.tv_sec;
130 	/*
131 	 * See if this is a repeated message, and check for
132 	 * out of date entries in the table while we are it.
133 	 */
134 	if (debug)
135 		print_request("find_request", request);
136 	for (ptr = table.tqh_first; ptr != NULL; ptr = ptr->list.tqe_next) {
137 		if ((current_time - ptr->time) > MAX_LIFE) {
138 			/* the entry is too old */
139 			if (debug)
140 				print_request("deleting expired entry",
141 				    &ptr->request);
142 			delete(ptr);
143 			continue;
144 		}
145 		if (debug)
146 			print_request("", &ptr->request);
147 		if (strcmp(request->r_name, ptr->request.r_name) == 0 &&
148 		    strcmp(request->l_name, ptr->request.l_name) == 0 &&
149 		    request->type == ptr->request.type &&
150 		    request->pid == ptr->request.pid) {
151 			/* update the time if we 'touch' it */
152 			ptr->time = current_time;
153 			return (&ptr->request);
154 		}
155 	}
156 	return ((CTL_MSG *)0);
157 }
158 
159 void
160 insert_table(CTL_MSG *request, CTL_RESPONSE *response)
161 {
162 	TABLE_ENTRY *ptr;
163 	time_t current_time;
164 
165 	if (debug)
166 		print_request( "insert_table", request );
167 	gettimeofday(&tp, &txp);
168 	current_time = tp.tv_sec;
169 	request->id_num = new_id();
170 	response->id_num = htonl(request->id_num);
171 	/* insert a new entry into the top of the list */
172 	ptr = (TABLE_ENTRY *)malloc(sizeof(TABLE_ENTRY));
173 	if (ptr == NULL) {
174 		syslog(LOG_ERR, "insert_table: Out of memory");
175 		_exit(1);
176 	}
177 	ptr->time = current_time;
178 	ptr->request = *request;
179 	TAILQ_INSERT_HEAD(&table, ptr, list);
180 }
181 
182 /*
183  * Generate a unique non-zero sequence number
184  */
185 int
186 new_id(void)
187 {
188 	static int current_id = 0;
189 
190 	current_id = (current_id + 1) % MAX_ID;
191 	/* 0 is reserved, helps to pick up bugs */
192 	if (current_id == 0)
193 		current_id = 1;
194 	return (current_id);
195 }
196 
197 /*
198  * Delete the invitation with id 'id_num'
199  */
200 int
201 delete_invite(int id_num)
202 {
203 	TABLE_ENTRY *ptr;
204 
205 	if (debug)
206 		syslog(LOG_DEBUG, "delete_invite(%d)", id_num);
207 	for (ptr = table.tqh_first; ptr != NULL; ptr = ptr->list.tqe_next) {
208 		if (ptr->request.id_num == id_num)
209 			break;
210 		if (debug)
211 			print_request("", &ptr->request);
212 	}
213 	if (ptr != NULL) {
214 		delete(ptr);
215 		return (SUCCESS);
216 	}
217 	return (NOT_HERE);
218 }
219 
220 /*
221  * Classic delete from a double-linked list
222  */
223 static void
224 delete(TABLE_ENTRY *ptr)
225 {
226 
227 	if (debug)
228 		print_request("delete", &ptr->request);
229 	TAILQ_REMOVE(&table, ptr, list);
230 	free((char *)ptr);
231 }
232