forked from virtadpt/eBBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtalk.c
397 lines (345 loc) · 9.44 KB
/
talk.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
Eagles Bulletin Board System
Copyright (C) 1995, Ray Rocker, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "server.h"
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.h>
#if USES_SYS_SELECT_H
# include <sys/select.h>
#endif
#ifndef INADDR_LOOPBACK
# define INADDR_LOOPBACK 0x7f000001
#endif
#define PAGE_INTERVAL 20 /* How often the page signal is sent */
int talkfd = -1;
int cloak_save;
USERDATA talk_theirdata, talk_mydata;
local_bbs_talk(pid, fd, sockp)
LONG pid;
LONG fd;
LONG *sockp;
{
int s, sinlen, nfds, rc;
struct sockaddr_in sin;
fd_set readfds;
struct timeval tv;
char cbuf[80];
SHORT mymode;
mymode = my_real_mode();
if (mymode == M_CHAT || mymode == M_TALK) {
return S_MODEVIOLATION;
}
if (mymode != M_PAGE) {
/* You can't page yourself! */
if (pid == getpid()) return S_TALKTOSELF;
if (utable_find_record(pid, &talk_theirdata) != S_OK) return S_NOSUCHUSER;
if (!has_page_permission(&talk_theirdata)) return S_PAGEROFF;
/* Don't interrupt an upload or download. */
if (talk_theirdata.u.mode & MODE_FLG_NOPAGE) return S_CANNOTPAGE;
/* Also if the client says it's not in a place where it can hear you. */
if (talk_theirdata.usermode & MODE_FLG_NOPAGE) return S_CANNOTPAGE;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
bbslog(0, "ERROR local_bbs_talk: socket failed\n");
return S_SYSERR;
}
memset(&sin, 0, sizeof sin);
sin.sin_family = PF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) {
close(s);
bbslog(0, "ERROR local_bbs_talk: bind failed\n");
return S_SYSERR;
}
sinlen = sizeof sin;
if (getsockname(s, (struct sockaddr *)&sin, &sinlen) == -1) {
close(s);
bbslog(0, "ERROR local_bbs_talk: getsockname failed\n");
return S_SYSERR;
}
if (listen(s, 1) == -1) {
close(s);
bbslog(0, "ERROR local_bbs_talk: listen failed\n");
return S_SYSERR;
}
set_real_mode(M_PAGE);
utable_get_record(my_utable_slot(), &talk_mydata);
talk_mydata.destpid = pid;
talk_mydata.port = (SHORT)htons(sin.sin_port);
utable_set_record(my_utable_slot(), &talk_mydata);
talkfd = s;
bbslog(4,"PAGE %s by %s\n",talk_theirdata.u.userid,talk_mydata.u.userid);
}
if (kill(pid, SIGUSR1) == -1) {
local_bbs_exit_talk();
return S_SYSERR;
}
nfds = (talkfd > fd ? talkfd+1 : fd+1);
while (1) {
tv.tv_sec = PAGE_INTERVAL;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(talkfd, &readfds);
FD_SET(fd, &readfds);
rc = select(nfds, &readfds, NULL, NULL, &tv);
switch (rc) {
case -1:
if (errno != EINTR) {
local_bbs_exit_talk();
bbslog(0, "ERROR local_bbs_talk: select failed\n");
return S_SYSERR;
}
break;
case 0:
return S_CMDTIMEOUT;
default:
if (FD_ISSET(talkfd, &readfds)) {
int talksock;
sinlen = sizeof sin;
talksock = accept(talkfd, (struct sockaddr *)&sin, &sinlen);
if (talksock == -1 || recv(talksock, cbuf, 1, 0) != 1) {
local_bbs_exit_talk();
bbslog(0, "ERROR local_bbs_talk: accept or recv failed\n");
return S_SYSERR;
}
if (cbuf[0] != 'y') {
local_bbs_exit_talk();
bbslog(4, "PAGE REFUSED from %s by %s\n",
talk_mydata.u.userid, talk_theirdata.u.userid);
return S_PAGEREFUSED;
}
set_real_mode(M_TALK);
utable_get_record(my_utable_slot(), &talk_mydata);
talk_mydata.port = 0;
/* Force pager's cloak setting to match pagee's cloak setting */
if ((talk_mydata.u.flags & FLG_CLOAK) !=
(talk_theirdata.u.flags & FLG_CLOAK)) {
cloak_save = (talk_mydata.u.flags & FLG_CLOAK) ? 1 : -1;
talk_mydata.u.flags ^= FLG_CLOAK;
}
utable_set_record(my_utable_slot(), &talk_mydata);
bbslog(4, "PAGE ACCEPTED from %s by %s\n",
talk_mydata.u.userid, talk_theirdata.u.userid);
*sockp = (LONG)talksock;
return S_OK;
}
else if (FD_ISSET(fd, &readfds)) {
int i, cc = read(fd, cbuf, sizeof cbuf);
if (cc <= 0) {
local_bbs_exit_talk();
return S_SYSERR;
}
for (i=0; i<cc; i++)
if (cbuf[i] == 3 || cbuf[i] == 4) {
local_bbs_exit_talk();
return S_INTERRUPT;
}
}
}
}
/*NOTREACHED*/
return 0;
}
local_bbs_exit_talk()
{
USERDATA udata;
if (talkfd == -1) return S_OK;
close(talkfd);
talkfd = -1;
utable_get_record(my_utable_slot(), &udata);
udata.destpid = 0;
udata.port = 0;
if (cloak_save) {
if (cloak_save > 0) udata.u.flags |= FLG_CLOAK;
else udata.u.flags &= ~FLG_CLOAK;
cloak_save = 0;
}
utable_set_record(my_utable_slot(), &udata);
set_real_mode(M_UNDEFINED);
return S_OK;
}
/*ARGSUSED*/
_find_whos_paging(indx, udata, urec)
int indx;
USERDATA *udata;
USEREC *urec;
{
if (udata->destpid == urec->pid && udata->port != 0) {
memcpy(urec, &udata->u, sizeof(*urec));
/* Kludge: pass port back in flags field. */
urec->flags = udata->port;
return ENUM_QUIT;
}
return S_OK;
}
local_bbs_get_talk_request(urec, paddr, pport)
USEREC *urec;
LONG *paddr; /* may be NULL */
SHORT *pport; /* may be NULL */
{
memset(urec, 0, sizeof(*urec));
urec->pid = getpid();
utable_enumerate(0, NULL, _find_whos_paging, urec);
if (urec->userid[0] == '\0') {
urec->pid = 0;
return S_INTERRUPT;
}
if (paddr) *paddr = (LONG)htonl(INADDR_LOOPBACK);
if (pport) *pport = urec->flags;
urec->flags = 0;
return S_OK;
}
_get_answer_socket(addr, port, psock)
LONG addr;
SHORT port;
LONG *psock;
{
int s;
struct sockaddr_in sin;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
return S_SYSERR;
}
memset(&sin, 0, sizeof sin);
sin.sin_family = PF_INET;
sin.sin_port = ntohs((unsigned short)port);
sin.sin_addr.s_addr = (unsigned long)addr;
if (connect(s, (struct sockaddr *)&sin, sizeof sin) == -1) {
close(s);
return S_SYSERR;
}
*psock = (LONG)s;
return S_OK;
}
local_bbs_refuse_page(addr, port)
LONG addr;
SHORT port;
{
int rc, sock;
char shaddup = 'n';
rc = _get_answer_socket(addr, port, &sock);
if (rc != S_OK) return rc;
if (send(sock, &shaddup, 1, 0) != 1) {
close(sock);
return S_SYSERR;
}
close(sock);
return S_OK;
}
local_bbs_accept_page(addr, port, psock)
LONG addr;
SHORT port;
LONG *psock;
{
int rc, sock;
char talktome = 'y';
SHORT mymode;
mymode = my_real_mode();
if (mymode == M_CHAT || mymode == M_TALK) {
return S_MODEVIOLATION;
}
rc = _get_answer_socket(addr, port, &sock);
if (rc != S_OK) return rc;
if (send(sock, &talktome, 1, 0) != 1) {
close(sock);
return S_SYSERR;
}
set_real_mode(M_TALK);
talkfd = sock;
/*
(maybe) TODO: get destpid and port, set in utable record.
Will have to do this before enum_users can return info about
who is talking to whom.
*/
*psock = (LONG)sock;
return S_OK;
}
/* Override list stuff */
#define OVERRIDE_FILE "overrides"
get_override_file(userid, buf)
char *userid;
char *buf;
{
get_home_directory(userid, buf);
strcat(buf, "/");
strcat(buf, OVERRIDE_FILE);
}
is_on_override_list(userid)
char *userid;
{
PATH buf;
get_override_file(userid, buf);
if (_record_find(buf, _match_full, my_userid(), NULL, NULL) == S_OK)
return 1;
else return 0;
}
local_bbs_set_overrides(list)
NAMELIST list;
{
PATH buf;
FILE *fp;
get_override_file(my_userid(), buf);
return (write_namelist(buf, list));
}
local_bbs_get_overrides(list)
NAMELIST *list;
{
PATH buf;
get_override_file(my_userid(), buf);
return (read_namelist(buf, list));
}
/* Returns 1 if current user can page user, 0 if not */
has_page_permission(udata)
USERDATA *udata;
{
int noovers, noothers;
if (!_they_have_access(C_TALKACCEPT, udata->perms)) return 0;
if (_has_perms(PERM_SYSOP)) return 1;
noovers = udata->u.flags & FLG_NOOVERRIDE;
noothers = udata->u.flags & FLG_NOPAGE;
if (noovers && noothers) return 0;
if (!noovers && !noothers) return 1;
if (is_on_override_list(udata->u.userid)) {
if (noovers) return 0;
}
else {
if (noothers) return 0;
}
return 1;
}
#if FULL_USER_DELETE
/* For fixing override files when users get deleted or their names changed */
/*ARGSUSED*/
fix_override_entry(indx, rec, ncs)
int indx;
char *rec;
struct namechange *ncs;
{
PATH overfile;
NAME userid;
/* Sleazy way to get the userid out of the passfile record */
memset(userid, 0, sizeof userid);
strncpy(userid, rec, NAMELEN);
strip_trailing_space(userid);
get_override_file(userid, overfile);
if (ncs->newname == NULL)
_record_delete(overfile, _match_full, ncs->oldname);
else _record_replace(overfile, _match_full, ncs->oldname,
_change_name, ncs->newname);
return S_OK;
}
#endif