1 /* $OpenBSD: table.c,v 1.16 2015/01/16 06:39:51 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 /* 33 * Routines to handle insertion, deletion, etc on the table 34 * of requests kept by the daemon. Nothing fancy here, linear 35 * search on a double-linked list. A time is kept with each 36 * entry so that overly old invitations can be eliminated. 37 * 38 * Consider this a mis-guided attempt at modularity 39 */ 40 #include <sys/time.h> 41 #include <sys/socket.h> 42 #include <sys/queue.h> 43 #include <protocols/talkd.h> 44 #include <syslog.h> 45 #include <unistd.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include "talkd.h" 50 51 #define MAX_ID 16000 /* << 2^15 so I don't have sign troubles */ 52 53 struct timeval tp; 54 struct timezone txp; 55 56 typedef struct table_entry TABLE_ENTRY; 57 58 struct table_entry { 59 CTL_MSG request; 60 time_t time; 61 TAILQ_ENTRY(table_entry) list; 62 }; 63 TAILQ_HEAD(, table_entry) table; 64 65 static void delete(TABLE_ENTRY *); 66 67 /* 68 * Init the table 69 */ 70 void 71 init_table(void) 72 { 73 TAILQ_INIT(&table); 74 } 75 76 /* 77 * Look in the table for an invitation that matches the current 78 * request looking for an invitation 79 */ 80 CTL_MSG * 81 find_match(CTL_MSG *request) 82 { 83 TABLE_ENTRY *ptr, *next; 84 time_t current_time; 85 86 gettimeofday(&tp, &txp); 87 current_time = tp.tv_sec; 88 if (debug) 89 print_request("find_match", request); 90 for (ptr = TAILQ_FIRST(&table); ptr != NULL; ptr = next) { 91 next = TAILQ_NEXT(ptr, list); 92 if ((current_time - ptr->time) > MAX_LIFE) { 93 /* the entry is too old */ 94 if (debug) 95 print_request("deleting expired entry", 96 &ptr->request); 97 delete(ptr); 98 continue; 99 } 100 if (debug) 101 print_request("", &ptr->request); 102 if (ptr->request.type == LEAVE_INVITE && 103 strcmp(request->l_name, ptr->request.r_name) == 0 && 104 strcmp(request->r_name, ptr->request.l_name) == 0) 105 return (&ptr->request); 106 } 107 if (debug) 108 syslog(LOG_DEBUG, "find_match: not found"); 109 110 return ((CTL_MSG *)0); 111 } 112 113 /* 114 * Look for an identical request, as opposed to a complimentary 115 * one as find_match does 116 */ 117 CTL_MSG * 118 find_request(CTL_MSG *request) 119 { 120 TABLE_ENTRY *ptr, *next; 121 time_t current_time; 122 123 gettimeofday(&tp, &txp); 124 current_time = tp.tv_sec; 125 /* 126 * See if this is a repeated message, and check for 127 * out of date entries in the table while we are it. 128 */ 129 if (debug) 130 print_request("find_request", request); 131 for (ptr = TAILQ_FIRST(&table); ptr != NULL; ptr = next) { 132 next = TAILQ_NEXT(ptr, list); 133 if ((current_time - ptr->time) > MAX_LIFE) { 134 /* the entry is too old */ 135 if (debug) 136 print_request("deleting expired entry", 137 &ptr->request); 138 delete(ptr); 139 continue; 140 } 141 if (debug) 142 print_request("", &ptr->request); 143 if (request->pid == ptr->request.pid && 144 request->type == ptr->request.type && 145 strcmp(request->r_name, ptr->request.r_name) == 0 && 146 strcmp(request->l_name, ptr->request.l_name) == 0) { 147 /* update the time if we 'touch' it */ 148 ptr->time = current_time; 149 return (&ptr->request); 150 } 151 } 152 return ((CTL_MSG *)0); 153 } 154 155 void 156 insert_table(CTL_MSG *request, CTL_RESPONSE *response) 157 { 158 TABLE_ENTRY *ptr; 159 time_t current_time; 160 161 if (debug) 162 print_request( "insert_table", request ); 163 gettimeofday(&tp, &txp); 164 current_time = tp.tv_sec; 165 request->id_num = new_id(); 166 response->id_num = htonl(request->id_num); 167 /* insert a new entry into the top of the list */ 168 ptr = malloc(sizeof(TABLE_ENTRY)); 169 if (ptr == NULL) { 170 syslog(LOG_ERR, "insert_table: Out of memory"); 171 _exit(1); 172 } 173 ptr->time = current_time; 174 ptr->request = *request; 175 TAILQ_INSERT_HEAD(&table, ptr, list); 176 } 177 178 /* 179 * Generate a unique non-zero sequence number 180 */ 181 int 182 new_id(void) 183 { 184 static int current_id = 0; 185 186 current_id = (current_id + 1) % MAX_ID; 187 /* 0 is reserved, helps to pick up bugs */ 188 if (current_id == 0) 189 current_id = 1; 190 return (current_id); 191 } 192 193 /* 194 * Delete the invitation with id 'id_num' 195 */ 196 int 197 delete_invite(int id_num) 198 { 199 TABLE_ENTRY *ptr; 200 201 if (debug) 202 syslog(LOG_DEBUG, "delete_invite(%d)", id_num); 203 TAILQ_FOREACH(ptr, &table, list) { 204 if (ptr->request.id_num == id_num) 205 break; 206 if (debug) 207 print_request("", &ptr->request); 208 } 209 if (ptr != NULL) { 210 delete(ptr); 211 return (SUCCESS); 212 } 213 return (NOT_HERE); 214 } 215 216 /* 217 * Classic delete from a double-linked list 218 */ 219 static void 220 delete(TABLE_ENTRY *ptr) 221 { 222 223 if (debug) 224 print_request("delete", &ptr->request); 225 TAILQ_REMOVE(&table, ptr, list); 226 free((char *)ptr); 227 } 228