Annotation of src/libexec/talkd/process.c, Revision 1.4
1.4 ! christos 1: /* $NetBSD: process.c,v 1.3 1997/06/29 18:01:14 christos Exp $ */
1.3 christos 2:
1.1 cgd 3: /*
1.3 christos 4: * Copyright (c) 1983, 1993
5: * The Regents of the University of California. All rights reserved.
1.1 cgd 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. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by the University of
18: * California, Berkeley and its contributors.
19: * 4. Neither the name of the University nor the names of its contributors
20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.3 christos 36: #include <sys/cdefs.h>
1.1 cgd 37: #ifndef lint
1.3 christos 38: #if 0
39: static char sccsid[] = "@(#)process.c 8.2 (Berkeley) 11/16/93";
40: #else
1.4 ! christos 41: __RCSID("$NetBSD: process.c,v 1.3 1997/06/29 18:01:14 christos Exp $");
1.3 christos 42: #endif
1.1 cgd 43: #endif /* not lint */
44:
45: /*
46: * process.c handles the requests, which can be of three types:
47: * ANNOUNCE - announce to a user that a talk is wanted
48: * LEAVE_INVITE - insert the request into the table
49: * LOOK_UP - look up to see if a request is waiting in
50: * in the table for the local user
51: * DELETE - delete invitation
52: */
53: #include <sys/param.h>
54: #include <sys/stat.h>
55: #include <sys/socket.h>
56: #include <netinet/in.h>
57: #include <protocols/talkd.h>
58: #include <netdb.h>
59: #include <syslog.h>
60: #include <stdio.h>
61: #include <string.h>
62: #include <paths.h>
1.4 ! christos 63: #include "extern.h"
1.1 cgd 64:
1.4 ! christos 65: void
1.1 cgd 66: process_request(mp, rp)
1.4 ! christos 67: CTL_MSG *mp;
! 68: CTL_RESPONSE *rp;
1.1 cgd 69: {
1.4 ! christos 70: CTL_MSG *ptr;
1.1 cgd 71: extern int debug;
72:
73: rp->vers = TALK_VERSION;
74: rp->type = mp->type;
75: rp->id_num = htonl(0);
76: if (mp->vers != TALK_VERSION) {
77: syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
78: rp->answer = BADVERSION;
79: return;
80: }
81: mp->id_num = ntohl(mp->id_num);
82: mp->addr.sa_family = ntohs(mp->addr.sa_family);
83: if (mp->addr.sa_family != AF_INET) {
84: syslog(LOG_WARNING, "Bad address, family %d",
85: mp->addr.sa_family);
86: rp->answer = BADADDR;
87: return;
88: }
89: mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
90: if (mp->ctl_addr.sa_family != AF_INET) {
91: syslog(LOG_WARNING, "Bad control address, family %d",
92: mp->ctl_addr.sa_family);
93: rp->answer = BADCTLADDR;
94: return;
95: }
96: mp->pid = ntohl(mp->pid);
97: if (debug)
98: print_request("process_request", mp);
99: switch (mp->type) {
100:
101: case ANNOUNCE:
102: do_announce(mp, rp);
103: break;
104:
105: case LEAVE_INVITE:
106: ptr = find_request(mp);
107: if (ptr != (CTL_MSG *)0) {
108: rp->id_num = htonl(ptr->id_num);
109: rp->answer = SUCCESS;
110: } else
111: insert_table(mp, rp);
112: break;
113:
114: case LOOK_UP:
115: ptr = find_match(mp);
116: if (ptr != (CTL_MSG *)0) {
117: rp->id_num = htonl(ptr->id_num);
118: rp->addr = ptr->addr;
119: rp->addr.sa_family = htons(ptr->addr.sa_family);
120: rp->answer = SUCCESS;
121: } else
122: rp->answer = NOT_HERE;
123: break;
124:
125: case DELETE:
126: rp->answer = delete_invite(mp->id_num);
127: break;
128:
129: default:
130: rp->answer = UNKNOWN_REQUEST;
131: break;
132: }
133: if (debug)
134: print_response("process_request", rp);
135: }
136:
1.4 ! christos 137: void
1.1 cgd 138: do_announce(mp, rp)
1.4 ! christos 139: CTL_MSG *mp;
1.1 cgd 140: CTL_RESPONSE *rp;
141: {
142: struct hostent *hp;
143: CTL_MSG *ptr;
144: int result;
145:
146: /* see if the user is logged */
147: result = find_user(mp->r_name, mp->r_tty);
148: if (result != SUCCESS) {
149: rp->answer = result;
150: return;
151: }
152: #define satosin(sa) ((struct sockaddr_in *)(sa))
153: hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
154: sizeof (struct in_addr), AF_INET);
155: if (hp == (struct hostent *)0) {
156: rp->answer = MACHINE_UNKNOWN;
157: return;
158: }
159: ptr = find_request(mp);
160: if (ptr == (CTL_MSG *) 0) {
161: insert_table(mp, rp);
162: rp->answer = announce(mp, hp->h_name);
163: return;
164: }
165: if (mp->id_num > ptr->id_num) {
166: /*
167: * This is an explicit re-announce, so update the id_num
168: * field to avoid duplicates and re-announce the talk.
169: */
170: ptr->id_num = new_id();
171: rp->id_num = htonl(ptr->id_num);
172: rp->answer = announce(mp, hp->h_name);
173: } else {
174: /* a duplicated request, so ignore it */
175: rp->id_num = htonl(ptr->id_num);
176: rp->answer = SUCCESS;
177: }
178: }
179:
180: #include <utmp.h>
181:
182: /*
183: * Search utmp for the local user
184: */
1.4 ! christos 185: int
1.1 cgd 186: find_user(name, tty)
187: char *name, *tty;
188: {
189: struct utmp ubuf;
190: int status;
191: FILE *fd;
192: struct stat statb;
1.3 christos 193: char line[sizeof(ubuf.ut_line) + 1];
194: char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
1.4 ! christos 195: time_t atime = 0;
! 196: int anytty = 0;
1.1 cgd 197:
198: if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
199: fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
200: return (FAILED);
201: }
202: #define SCMPN(a, b) strncmp(a, b, sizeof (a))
203: status = NOT_HERE;
204: (void) strcpy(ftty, _PATH_DEV);
1.4 ! christos 205:
! 206: if (*tty == '\0')
! 207: anytty = 1;
! 208:
! 209: while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
! 210: if (SCMPN(ubuf.ut_name, name) != 0)
! 211: continue;
! 212: (void)strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
! 213: line[sizeof(ubuf.ut_line)] = '\0';
! 214: if (anytty) {
! 215: /* no particular tty was requested */
! 216: (void)strcpy(ftty + sizeof(_PATH_DEV) - 1, line);
! 217: if (stat(ftty, &statb) == 0) {
! 218: if (!(statb.st_mode & 020)) {
! 219: if (status != SUCCESS)
! 220: status = PERMISSION_DENIED;
! 221: continue;
! 222: }
! 223: if (statb.st_atime > atime) {
! 224: atime = statb.st_atime;
1.3 christos 225: (void) strcpy(tty, line);
1.1 cgd 226: status = SUCCESS;
227: }
228: }
1.4 ! christos 229: } else if (strcmp(line, tty) == 0) {
! 230: status = SUCCESS;
! 231: break;
1.1 cgd 232: }
1.4 ! christos 233: }
! 234: (void)fclose(fd);
1.1 cgd 235: return (status);
236: }
CVSweb <webmaster@jp.NetBSD.org>