process.c (9e522f7a18a64d3b9ea901ed70d0eee78342f0c6) process.c (a846453c5ebbf21be4af27ac1e1679a3c00d8103)
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $Id$
34 */
35
36#ifndef lint
32 */
33
34#ifndef lint
35#if 0
37static char sccsid[] = "@(#)process.c 8.2 (Berkeley) 11/16/93";
36static char sccsid[] = "@(#)process.c 8.2 (Berkeley) 11/16/93";
37#endif
38static const char rcsid[] =
39 "$Id$";
38#endif /* not lint */
39
40/*
41 * process.c handles the requests, which can be of three types:
42 * ANNOUNCE - announce to a user that a talk is wanted
43 * LEAVE_INVITE - insert the request into the table
44 * LOOK_UP - look up to see if a request is waiting in
45 * in the table for the local user
46 * DELETE - delete invitation
47 */
48#include <sys/param.h>
49#include <sys/stat.h>
50#include <sys/socket.h>
51#include <netinet/in.h>
52#include <protocols/talkd.h>
40#endif /* not lint */
41
42/*
43 * process.c handles the requests, which can be of three types:
44 * ANNOUNCE - announce to a user that a talk is wanted
45 * LEAVE_INVITE - insert the request into the table
46 * LOOK_UP - look up to see if a request is waiting in
47 * in the table for the local user
48 * DELETE - delete invitation
49 */
50#include <sys/param.h>
51#include <sys/stat.h>
52#include <sys/socket.h>
53#include <netinet/in.h>
54#include <protocols/talkd.h>
55#include <ctype.h>
56#include <err.h>
53#include <netdb.h>
57#include <netdb.h>
54#include <syslog.h>
58#include <paths.h>
55#include <stdio.h>
56#include <string.h>
59#include <stdio.h>
60#include <string.h>
57#include <ctype.h>
58#include <paths.h>
61#include <syslog.h>
59
62
63int announce __P((CTL_MSG *, char *));
64int delete_invite __P((int));
65void do_announce __P((CTL_MSG *, CTL_RESPONSE *));
60CTL_MSG *find_request();
61CTL_MSG *find_match();
66CTL_MSG *find_request();
67CTL_MSG *find_match();
68int find_user __P((char *, char *));
69void insert_table __P((CTL_MSG *, CTL_RESPONSE *));
70int new_id __P((void));
71void print_request __P((char *, CTL_MSG *));
72void print_response __P((char *, CTL_RESPONSE *));
62
73
74void
63process_request(mp, rp)
64 register CTL_MSG *mp;
65 register CTL_RESPONSE *rp;
66{
67 register CTL_MSG *ptr;
68 extern int debug;
69 char *s;
70
71 rp->vers = TALK_VERSION;
72 rp->type = mp->type;
73 rp->id_num = htonl(0);
74 if (mp->vers != TALK_VERSION) {
75process_request(mp, rp)
76 register CTL_MSG *mp;
77 register CTL_RESPONSE *rp;
78{
79 register CTL_MSG *ptr;
80 extern int debug;
81 char *s;
82
83 rp->vers = TALK_VERSION;
84 rp->type = mp->type;
85 rp->id_num = htonl(0);
86 if (mp->vers != TALK_VERSION) {
75 syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
87 syslog(LOG_WARNING, "bad protocol version %d", mp->vers);
76 rp->answer = BADVERSION;
77 return;
78 }
79 mp->id_num = ntohl(mp->id_num);
80 mp->addr.sa_family = ntohs(mp->addr.sa_family);
81 if (mp->addr.sa_family != AF_INET) {
88 rp->answer = BADVERSION;
89 return;
90 }
91 mp->id_num = ntohl(mp->id_num);
92 mp->addr.sa_family = ntohs(mp->addr.sa_family);
93 if (mp->addr.sa_family != AF_INET) {
82 syslog(LOG_WARNING, "Bad address, family %d",
94 syslog(LOG_WARNING, "bad address, family %d",
83 mp->addr.sa_family);
84 rp->answer = BADADDR;
85 return;
86 }
87 mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
88 if (mp->ctl_addr.sa_family != AF_INET) {
95 mp->addr.sa_family);
96 rp->answer = BADADDR;
97 return;
98 }
99 mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
100 if (mp->ctl_addr.sa_family != AF_INET) {
89 syslog(LOG_WARNING, "Bad control address, family %d",
101 syslog(LOG_WARNING, "bad control address, family %d",
90 mp->ctl_addr.sa_family);
91 rp->answer = BADCTLADDR;
92 return;
93 }
94 for (s = mp->l_name; *s; s++)
95 if (!isprint(*s)) {
102 mp->ctl_addr.sa_family);
103 rp->answer = BADCTLADDR;
104 return;
105 }
106 for (s = mp->l_name; *s; s++)
107 if (!isprint(*s)) {
96 syslog(LOG_NOTICE, "Illegal user name. Aborting");
108 syslog(LOG_NOTICE, "illegal user name. Aborting");
97 rp->answer = FAILED;
98 return;
99 }
100 mp->pid = ntohl(mp->pid);
101 if (debug)
102 print_request("process_request", mp);
103 switch (mp->type) {
104

--- 28 unchanged lines hidden (view full) ---

133 default:
134 rp->answer = UNKNOWN_REQUEST;
135 break;
136 }
137 if (debug)
138 print_response("process_request", rp);
139}
140
109 rp->answer = FAILED;
110 return;
111 }
112 mp->pid = ntohl(mp->pid);
113 if (debug)
114 print_request("process_request", mp);
115 switch (mp->type) {
116

--- 28 unchanged lines hidden (view full) ---

145 default:
146 rp->answer = UNKNOWN_REQUEST;
147 break;
148 }
149 if (debug)
150 print_response("process_request", rp);
151}
152
153void
141do_announce(mp, rp)
142 register CTL_MSG *mp;
143 CTL_RESPONSE *rp;
144{
145 struct hostent *hp;
146 CTL_MSG *ptr;
147 int result;
148

--- 31 unchanged lines hidden (view full) ---

180 }
181}
182
183#include <utmp.h>
184
185/*
186 * Search utmp for the local user
187 */
154do_announce(mp, rp)
155 register CTL_MSG *mp;
156 CTL_RESPONSE *rp;
157{
158 struct hostent *hp;
159 CTL_MSG *ptr;
160 int result;
161

--- 31 unchanged lines hidden (view full) ---

193 }
194}
195
196#include <utmp.h>
197
198/*
199 * Search utmp for the local user
200 */
201int
188find_user(name, tty)
189 char *name, *tty;
190{
191 struct utmp ubuf;
192 int status;
193 FILE *fd;
194 struct stat statb;
195 time_t best = 0;
196 char line[sizeof(ubuf.ut_line) + 1];
197 char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
198
199 if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
202find_user(name, tty)
203 char *name, *tty;
204{
205 struct utmp ubuf;
206 int status;
207 FILE *fd;
208 struct stat statb;
209 time_t best = 0;
210 char line[sizeof(ubuf.ut_line) + 1];
211 char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
212
213 if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
200 fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
214 warnx("can't read %s", _PATH_UTMP);
201 return (FAILED);
202 }
203#define SCMPN(a, b) strncmp(a, b, sizeof (a))
204 status = NOT_HERE;
205 (void) strcpy(ftty, _PATH_DEV);
206 while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
207 if (SCMPN(ubuf.ut_name, name) == 0) {
208 strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));

--- 26 unchanged lines hidden ---
215 return (FAILED);
216 }
217#define SCMPN(a, b) strncmp(a, b, sizeof (a))
218 status = NOT_HERE;
219 (void) strcpy(ftty, _PATH_DEV);
220 while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
221 if (SCMPN(ubuf.ut_name, name) == 0) {
222 strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));

--- 26 unchanged lines hidden ---