summaryrefslogtreecommitdiffstats
path: root/ktalkd/ktalkd/repairs.c
blob: 9473a473ace1af486a5a5a5328d8e41fc7fb6d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
 * Copyright 1998 David A. Holland.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder(s) nor the names of their
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

char repairs_rcsid[] = 
  "$Id$";

/*
 * Most, but not quite all, of the voodoo for detecting and handling
 * malformed packets goes here.
 *
 * Basically anything which we can detect by examining the packet we
 * try to do in rationalize_packet(). We return a quirk code for what
 * we did so we can send back replies that will make sense to the other
 * end. That's irrationalize_reply().
 */

#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <syslog.h>
#include "prot_talkd.h"
#include "proto.h"

uint32_t
byte_swap32(uint32_t k)
{
	return (k<<24) | ((k&0xff00) << 8) | ((k>>8) & 0xff00)  | (k>>24);
}

static uint16_t
byte_swap16(uint16_t k)
{
	return (k<<8) | (k>>8);
}

/***************************************************/

/* 
 * probe for strings that are meaningful in talkd packets.
 * rejects all control characters and delete. newlines and tabs have
 * no business in tty names or usernames.
 */
static int probe_string(const char *buf, size_t len) {
    size_t i;
    int ch;
    for (i=0; i<len; i++) {
	if (buf[i]==0) return 0;  /* success */
	ch = (unsigned char) buf[i];
	if ((ch&127) < 32 || (ch&127)==127) return -1;
    }
    return -1; /* no null-terminator, assume it's not a string */
}

/*
 * Check if an address from a talk packet matches the actual sender
 * address. If it doesn't, it's a good bet it's not the right packet format.
 * Allow assorted endianness though.
 * In an ideal world we'd save the endianness info for use elsewhere instead
 * of reprobing it, but oh well.
 */
static int probe_addr(struct talk_addr *ta, struct sockaddr_in *sn) {
    uint16_t family = sn->sin_family;
    uint16_t xfamily = byte_swap16(family);
    uint16_t port = sn->sin_port;
    uint16_t xport = byte_swap16(port);
    uint32_t addr = sn->sin_addr.s_addr;
    uint32_t xaddr = byte_swap32(addr);

    if (ta->ta_family != family && ta->ta_family != xfamily) return -1;
    if (ta->ta_port != port && ta->ta_port != xport) return -1;
    if (ta->ta_addr != addr && ta->ta_addr != xaddr) return -1;
    return 0;
}

/***************************************************/

/*
 * warning warning: in some cases this packet may need compiler
 * pragmas to force the compiler to not pad it. shouldn't with
 * gcc though.
 */

#define OTALK_PACKET_SIZE 76

#define OLD_NAME_SIZE   9
struct otalk_packet {
	char type;
	char l_name[OLD_NAME_SIZE];
	char r_name[OLD_NAME_SIZE];
	char filler;
	uint32_t id_num;
	uint32_t pid;
	char r_tty[TTY_SIZE];
	struct talk_addr addr;
	struct talk_addr ctl_addr;
};

struct otalk_reply {
	char type;
	char answer;
	uint16_t filler;
	uint32_t id_num;
	struct talk_addr addr;
};

/* additional types */
#define OLD_DELETE_INVITE  4
#define OLD_AUTO_LOOK_UP   5
#define OLD_AUTO_DELETE    6

static int probe_otalk_packet(char *buf, size_t len, size_t maxlen, 
			      struct sockaddr_in *sn)
{
	struct otalk_packet otp;
	CTL_MSG m;

	ktalk_debug("Probing for QUIRK_OTALK\n");

	if (sizeof(otp)!=OTALK_PACKET_SIZE) {
		syslog(LOG_ERR, "QUIRK_OTALK: struct otalk_packet padding "
		       "is wrong\n");
		return -1;
	}

	if (len!=sizeof(otp)) {
		ktalk_debug("QUIRK_OTALK: wrong size\n");
		return -1;
	}
	
	memcpy(&otp, buf, len);
	if (probe_string(otp.l_name, sizeof(otp.l_name))) {
		ktalk_debug("QUIRK_OTALK: l_name not a string\n");
		return -1;
	}
	if (probe_string(otp.r_name, sizeof(otp.r_name))) {
		ktalk_debug("QUIRK_OTALK: r_name not a string\n");
		return -1;
	}
	if (probe_string(otp.r_tty, sizeof(otp.r_tty))) {
		ktalk_debug("QUIRK_OTALK: r_tty not a string\n");
		return -1;
	}
	if (probe_addr(&otp.ctl_addr, sn)) {
		ktalk_debug("QUIRK_OTALK: addresses do not match\n");
		return -1;
	}

	switch (otp.type) {
	    case LEAVE_INVITE:
	    case LOOK_UP:
	    case DELETE:
	    case ANNOUNCE:
		break;
	    /* I'm not sure these will work. */
            case OLD_DELETE_INVITE: otp.type = DELETE; break;
	    case OLD_AUTO_LOOK_UP: otp.type = LOOK_UP; break;
	    case OLD_AUTO_DELETE: otp.type = DELETE; break;
	    default: 
		ktalk_debug("QUIRK_OTALK: invalid type field\n");
		return -1;
	}

	if (OLD_NAME_SIZE >= NAME_SIZE) {
		syslog(LOG_ERR, "QUIRK_OTALK: OLD_NAME_SIZE >= NAME_SIZE\n");
		syslog(LOG_ERR, "QUIRK_OTALK: fix repairs.c and recompile\n");
		return -1;
	}
	if (maxlen < sizeof(CTL_MSG)) {
		syslog(LOG_ERR, "QUIRK_OTALK: maxlen too small; enlarge "
		       "inbuf[] in talkd.c and recompile\n");
		return -1;
	}

	m.vers = TALK_VERSION;
	m.type = otp.type;
	m.answer = 0;
	m.pad = 0;
	m.id_num = otp.id_num;
	m.addr = otp.addr;
	m.ctl_addr = otp.ctl_addr;
	m.pid = otp.pid;
	memcpy(m.l_name, otp.l_name, OLD_NAME_SIZE);
	m.l_name[OLD_NAME_SIZE] = 0;
	memcpy(m.r_name, otp.r_name, OLD_NAME_SIZE);
	m.r_name[OLD_NAME_SIZE] = 0;
	memcpy(m.r_tty, otp.r_tty, TTY_SIZE);
	m.r_tty[TTY_SIZE-1] = 0;
	memcpy(buf, &m, sizeof(m));
	return 0;
}

static size_t do_otalk_reply(char *buf, size_t maxlen) {
	struct otalk_reply or;
	CTL_RESPONSE *r = (CTL_RESPONSE *)buf;
	if (sizeof(or) > maxlen) {
		syslog(LOG_ERR, "QUIRK_OTALK: reply: maxlen too small; "
		       "enlarge buf[] in send_packet and recompile\n");
		return sizeof(CTL_RESPONSE);
	}
	
	/* 
	 * If we changed the type above, this might break. Should we encode
	 * it in the quirk code?
	 */
	or.type = r->type;
	or.answer = r->answer;
	or.filler = 0;
	or.id_num = r->id_num;
	or.addr = r->addr;
	memcpy(buf, &or, sizeof(or));
	return sizeof(or);
}

/***************************************************/


/*
 * Return 0 if the packet's normal, -1 if we can't figure it out,
 * otherwise a quirk code from the quirk list.
 *
 * For now, we don't support any quirks. Need more data.
 */
int
rationalize_packet(char *buf, size_t len, size_t mlen, struct sockaddr_in *sn)
{
	if (len == sizeof(CTL_MSG)) {
		return 0;
	}

	ktalk_debug("Malformed packet (length %u)\n", len);

	if (probe_otalk_packet(buf, len, mlen, sn)==0) {
		ktalk_debug("Using QUIRK_OTALK\n");
		return QUIRK_OTALK;
	}
	return -1;
}

size_t
irrationalize_reply(char *buf, size_t maxlen, int quirk)
{
        switch (quirk) {
	    case QUIRK_NONE: return sizeof(CTL_RESPONSE);
	    case QUIRK_OTALK: return do_otalk_reply(buf, maxlen);
        }
	/* ? */
	return 0;
}