Annotation of src/libexec/talkd/process.c, Revision 1.12
1.12 ! christos 1: /* $NetBSD: process.c,v 1.11 2004/10/01 12:27:09 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.
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.12 ! christos 37: __RCSID("$NetBSD: process.c,v 1.11 2004/10/01 12:27:09 christos 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: CTL_MSG *ptr;
145: int result;
1.12 ! christos 146: char hostname[NI_MAXHOST];
! 147: struct sockaddr sa;
! 148:
! 149: tsa2sa(&sa, &mp->ctl_addr);
1.1 cgd 150:
151: /* see if the user is logged */
1.8 itojun 152: result = find_user(mp->r_name, mp->r_tty, sizeof(mp->r_tty));
1.1 cgd 153: if (result != SUCCESS) {
154: rp->answer = result;
155: return;
156: }
1.12 ! christos 157: if (getnameinfo(&sa, sa.sa_len, hostname, sizeof(hostname), NULL,
! 158: 0, 0)) {
1.1 cgd 159: rp->answer = MACHINE_UNKNOWN;
160: return;
161: }
162: ptr = find_request(mp);
163: if (ptr == (CTL_MSG *) 0) {
164: insert_table(mp, rp);
1.12 ! christos 165: rp->answer = announce(mp, hostname);
1.1 cgd 166: return;
167: }
168: if (mp->id_num > ptr->id_num) {
169: /*
170: * This is an explicit re-announce, so update the id_num
171: * field to avoid duplicates and re-announce the talk.
172: */
173: ptr->id_num = new_id();
174: rp->id_num = htonl(ptr->id_num);
1.12 ! christos 175: rp->answer = announce(mp, hostname);
1.1 cgd 176: } else {
177: /* a duplicated request, so ignore it */
178: rp->id_num = htonl(ptr->id_num);
179: rp->answer = SUCCESS;
180: }
181: }
182:
183: /*
184: * Search utmp for the local user
185: */
1.4 christos 186: int
1.8 itojun 187: find_user(name, tty, ttysize)
1.1 cgd 188: char *name, *tty;
1.8 itojun 189: size_t ttysize;
1.1 cgd 190: {
191: int status;
192: struct stat statb;
1.7 christos 193: struct utmpentry *ep;
194: char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)];
1.4 christos 195: time_t atime = 0;
196: int anytty = 0;
1.1 cgd 197:
1.7 christos 198: (void)getutentries(NULL, &ep);
199:
1.1 cgd 200: status = NOT_HERE;
1.8 itojun 201: (void) strlcpy(ftty, _PATH_DEV, sizeof(ftty));
1.4 christos 202:
203: if (*tty == '\0')
204: anytty = 1;
205:
1.7 christos 206: for (; ep; ep = ep->next) {
207: if (strcmp(ep->name, name) != 0)
1.4 christos 208: continue;
209: if (anytty) {
210: /* no particular tty was requested */
1.11 christos 211: (void)strlcpy(ftty + sizeof(_PATH_DEV) - 1, ep->line,
212: sizeof(ftty) - sizeof(_PATH_DEV) + 1);
1.8 itojun 213: if (stat(ftty, &statb) != 0)
214: continue;
215:
216: if (!(statb.st_mode & S_IWGRP)) {
217: if (status != SUCCESS)
218: status = PERMISSION_DENIED;
219: continue;
220: }
221: if (statb.st_atime > atime &&
222: strlcpy(tty, ep->line, ttysize) < ttysize) {
223: atime = statb.st_atime;
224: status = SUCCESS;
1.1 cgd 225: }
1.7 christos 226: } else if (strcmp(ep->line, tty) == 0) {
1.4 christos 227: status = SUCCESS;
228: break;
1.1 cgd 229: }
1.4 christos 230: }
1.1 cgd 231: return (status);
232: }
CVSweb <webmaster@jp.NetBSD.org>