Annotation of src/libexec/talkd/process.c, Revision 1.10
1.10 ! agc 1: /* $NetBSD: process.c,v 1.9 2003/05/17 22:50:29 itojun 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.
1.10 ! agc 15: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 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:
1.3 christos 32: #include <sys/cdefs.h>
1.1 cgd 33: #ifndef lint
1.3 christos 34: #if 0
35: static char sccsid[] = "@(#)process.c 8.2 (Berkeley) 11/16/93";
36: #else
1.10 ! agc 37: __RCSID("$NetBSD: process.c,v 1.9 2003/05/17 22:50:29 itojun Exp $");
1.3 christos 38: #endif
1.1 cgd 39: #endif /* not lint */
40:
41: /*
42: * process.c handles the requests, which can be of three types:
43: * ANNOUNCE - announce to a user that a talk is wanted
44: * LEAVE_INVITE - insert the request into the table
45: * LOOK_UP - look up to see if a request is waiting in
46: * in the table for the local user
47: * DELETE - delete invitation
48: */
1.5 mrg 49:
1.1 cgd 50: #include <sys/param.h>
51: #include <sys/stat.h>
52: #include <sys/socket.h>
1.5 mrg 53:
1.1 cgd 54: #include <netinet/in.h>
1.5 mrg 55:
1.1 cgd 56: #include <protocols/talkd.h>
1.5 mrg 57:
1.1 cgd 58: #include <netdb.h>
59: #include <syslog.h>
60: #include <stdio.h>
61: #include <string.h>
62: #include <paths.h>
1.5 mrg 63:
1.4 christos 64: #include "extern.h"
1.1 cgd 65:
1.7 christos 66: #include "utmpentry.h"
67:
1.4 christos 68: void
1.1 cgd 69: process_request(mp, rp)
1.4 christos 70: CTL_MSG *mp;
71: CTL_RESPONSE *rp;
1.1 cgd 72: {
1.4 christos 73: CTL_MSG *ptr;
1.1 cgd 74:
75: rp->vers = TALK_VERSION;
76: rp->type = mp->type;
77: rp->id_num = htonl(0);
1.8 itojun 78: mp->id_num = ntohl(mp->id_num);
79: mp->addr.sa_family = ntohs(mp->addr.sa_family);
80: mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
81: mp->pid = ntohl(mp->pid);
1.1 cgd 82: if (mp->vers != TALK_VERSION) {
83: syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
84: rp->answer = BADVERSION;
85: return;
86: }
87: if (mp->addr.sa_family != AF_INET) {
88: syslog(LOG_WARNING, "Bad address, family %d",
89: mp->addr.sa_family);
90: rp->answer = BADADDR;
91: return;
92: }
93: if (mp->ctl_addr.sa_family != AF_INET) {
94: syslog(LOG_WARNING, "Bad control address, family %d",
95: mp->ctl_addr.sa_family);
96: rp->answer = BADCTLADDR;
97: return;
98: }
1.6 mrg 99: if (debug || logging)
100: print_request("request", mp);
1.1 cgd 101: switch (mp->type) {
102:
103: case ANNOUNCE:
104: do_announce(mp, rp);
105: break;
106:
107: case LEAVE_INVITE:
108: ptr = find_request(mp);
109: if (ptr != (CTL_MSG *)0) {
110: rp->id_num = htonl(ptr->id_num);
111: rp->answer = SUCCESS;
112: } else
113: insert_table(mp, rp);
114: break;
115:
116: case LOOK_UP:
117: ptr = find_match(mp);
118: if (ptr != (CTL_MSG *)0) {
119: rp->id_num = htonl(ptr->id_num);
120: rp->addr = ptr->addr;
121: rp->addr.sa_family = htons(ptr->addr.sa_family);
122: rp->answer = SUCCESS;
123: } else
124: rp->answer = NOT_HERE;
125: break;
126:
127: case DELETE:
128: rp->answer = delete_invite(mp->id_num);
129: break;
130:
131: default:
132: rp->answer = UNKNOWN_REQUEST;
133: break;
134: }
135: if (debug)
1.6 mrg 136: print_response("process_request done", rp);
1.1 cgd 137: }
138:
1.4 christos 139: void
1.1 cgd 140: do_announce(mp, rp)
1.4 christos 141: CTL_MSG *mp;
1.1 cgd 142: CTL_RESPONSE *rp;
143: {
144: struct hostent *hp;
145: CTL_MSG *ptr;
146: int result;
147:
148: /* see if the user is logged */
1.8 itojun 149: result = find_user(mp->r_name, mp->r_tty, sizeof(mp->r_tty));
1.1 cgd 150: if (result != SUCCESS) {
151: rp->answer = result;
152: return;
153: }
154: #define satosin(sa) ((struct sockaddr_in *)(sa))
155: hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
156: sizeof (struct in_addr), AF_INET);
157: if (hp == (struct hostent *)0) {
158: rp->answer = MACHINE_UNKNOWN;
159: return;
160: }
161: ptr = find_request(mp);
162: if (ptr == (CTL_MSG *) 0) {
163: insert_table(mp, rp);
164: rp->answer = announce(mp, hp->h_name);
165: return;
166: }
167: if (mp->id_num > ptr->id_num) {
168: /*
169: * This is an explicit re-announce, so update the id_num
170: * field to avoid duplicates and re-announce the talk.
171: */
172: ptr->id_num = new_id();
173: rp->id_num = htonl(ptr->id_num);
174: rp->answer = announce(mp, hp->h_name);
175: } else {
176: /* a duplicated request, so ignore it */
177: rp->id_num = htonl(ptr->id_num);
178: rp->answer = SUCCESS;
179: }
180: }
181:
182: /*
183: * Search utmp for the local user
184: */
1.4 christos 185: int
1.8 itojun 186: find_user(name, tty, ttysize)
1.1 cgd 187: char *name, *tty;
1.8 itojun 188: size_t ttysize;
1.1 cgd 189: {
190: int status;
191: struct stat statb;
1.7 christos 192: struct utmpentry *ep;
193: char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)];
1.4 christos 194: time_t atime = 0;
195: int anytty = 0;
1.1 cgd 196:
1.7 christos 197: (void)getutentries(NULL, &ep);
198:
1.1 cgd 199: status = NOT_HERE;
1.8 itojun 200: (void) strlcpy(ftty, _PATH_DEV, sizeof(ftty));
1.4 christos 201:
202: if (*tty == '\0')
203: anytty = 1;
204:
1.7 christos 205: for (; ep; ep = ep->next) {
206: if (strcmp(ep->name, name) != 0)
1.4 christos 207: continue;
208: if (anytty) {
209: /* no particular tty was requested */
1.9 itojun 210: (void)strlcat(ftty, ep->line, sizeof(ftty));
1.8 itojun 211: if (stat(ftty, &statb) != 0)
212: continue;
213:
214: if (!(statb.st_mode & S_IWGRP)) {
215: if (status != SUCCESS)
216: status = PERMISSION_DENIED;
217: continue;
218: }
219: if (statb.st_atime > atime &&
220: strlcpy(tty, ep->line, ttysize) < ttysize) {
221: atime = statb.st_atime;
222: status = SUCCESS;
1.1 cgd 223: }
1.7 christos 224: } else if (strcmp(ep->line, tty) == 0) {
1.4 christos 225: status = SUCCESS;
226: break;
1.1 cgd 227: }
1.4 christos 228: }
1.1 cgd 229: return (status);
230: }
CVSweb <webmaster@jp.NetBSD.org>