1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 */ 12 13 #ifndef lint 14 static char sccsid[] = "@(#)process.c 5.5 (Berkeley) 05/20/88"; 15 #endif /* not lint */ 16 17 /* 18 * process.c handles the requests, which can be of three types: 19 * ANNOUNCE - announce to a user that a talk is wanted 20 * LEAVE_INVITE - insert the request into the table 21 * LOOK_UP - look up to see if a request is waiting in 22 * in the table for the local user 23 * DELETE - delete invitation 24 */ 25 #include <sys/types.h> 26 #include <sys/stat.h> 27 #include <stdio.h> 28 #include <syslog.h> 29 #include <netdb.h> 30 #include <netinet/in.h> 31 32 #include <protocols/talkd.h> 33 34 char *strcpy(); 35 CTL_MSG *find_request(); 36 CTL_MSG *find_match(); 37 38 process_request(mp, rp) 39 register CTL_MSG *mp; 40 register CTL_RESPONSE *rp; 41 { 42 register CTL_MSG *ptr; 43 extern int debug; 44 45 rp->vers = TALK_VERSION; 46 rp->type = mp->type; 47 rp->id_num = htonl(0); 48 if (mp->vers != TALK_VERSION) { 49 syslog(LOG_WARNING, "Bad protocol version %d", mp->vers); 50 rp->answer = BADVERSION; 51 return; 52 } 53 mp->id_num = ntohl(mp->id_num); 54 mp->addr.sa_family = ntohs(mp->addr.sa_family); 55 if (mp->addr.sa_family != AF_INET) { 56 syslog(LOG_WARNING, "Bad address, family %d", 57 mp->addr.sa_family); 58 rp->answer = BADADDR; 59 return; 60 } 61 mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family); 62 if (mp->ctl_addr.sa_family != AF_INET) { 63 syslog(LOG_WARNING, "Bad control address, family %d", 64 mp->ctl_addr.sa_family); 65 rp->answer = BADCTLADDR; 66 return; 67 } 68 mp->pid = ntohl(mp->pid); 69 if (debug) 70 print_request("process_request", mp); 71 switch (mp->type) { 72 73 case ANNOUNCE: 74 do_announce(mp, rp); 75 break; 76 77 case LEAVE_INVITE: 78 ptr = find_request(mp); 79 if (ptr != (CTL_MSG *)0) { 80 rp->id_num = htonl(ptr->id_num); 81 rp->answer = SUCCESS; 82 } else 83 insert_table(mp, rp); 84 break; 85 86 case LOOK_UP: 87 ptr = find_match(mp); 88 if (ptr != (CTL_MSG *)0) { 89 rp->id_num = htonl(ptr->id_num); 90 rp->addr = ptr->addr; 91 rp->addr.sa_family = htons(ptr->addr.sa_family); 92 rp->answer = SUCCESS; 93 } else 94 rp->answer = NOT_HERE; 95 break; 96 97 case DELETE: 98 rp->answer = delete_invite(mp->id_num); 99 break; 100 101 default: 102 rp->answer = UNKNOWN_REQUEST; 103 break; 104 } 105 if (debug) 106 print_response("process_request", rp); 107 } 108 109 do_announce(mp, rp) 110 register CTL_MSG *mp; 111 CTL_RESPONSE *rp; 112 { 113 struct hostent *hp; 114 CTL_MSG *ptr; 115 int result; 116 117 /* see if the user is logged */ 118 result = find_user(mp->r_name, mp->r_tty); 119 if (result != SUCCESS) { 120 rp->answer = result; 121 return; 122 } 123 #define satosin(sa) ((struct sockaddr_in *)(sa)) 124 hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr, 125 sizeof (struct in_addr), AF_INET); 126 if (hp == (struct hostent *)0) { 127 rp->answer = MACHINE_UNKNOWN; 128 return; 129 } 130 ptr = find_request(mp); 131 if (ptr == (CTL_MSG *) 0) { 132 insert_table(mp, rp); 133 rp->answer = announce(mp, hp->h_name); 134 return; 135 } 136 if (mp->id_num > ptr->id_num) { 137 /* 138 * This is an explicit re-announce, so update the id_num 139 * field to avoid duplicates and re-announce the talk. 140 */ 141 ptr->id_num = new_id(); 142 rp->id_num = htonl(ptr->id_num); 143 rp->answer = announce(mp, hp->h_name); 144 } else { 145 /* a duplicated request, so ignore it */ 146 rp->id_num = htonl(ptr->id_num); 147 rp->answer = SUCCESS; 148 } 149 } 150 151 #include <utmp.h> 152 153 /* 154 * Search utmp for the local user 155 */ 156 find_user(name, tty) 157 char *name, *tty; 158 { 159 struct utmp ubuf; 160 int status; 161 FILE *fd; 162 struct stat statb; 163 char ftty[20]; 164 165 if ((fd = fopen("/etc/utmp", "r")) == NULL) { 166 perror("Can't open /etc/utmp"); 167 return (FAILED); 168 } 169 #define SCMPN(a, b) strncmp(a, b, sizeof (a)) 170 status = NOT_HERE; 171 (void) strcpy(ftty, "/dev/"); 172 while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) 173 if (SCMPN(ubuf.ut_name, name) == 0) { 174 if (*tty == '\0') { 175 status = PERMISSION_DENIED; 176 /* no particular tty was requested */ 177 (void) strcpy(ftty+5, ubuf.ut_line); 178 if (stat(ftty,&statb) == 0) { 179 if (!(statb.st_mode & 020)) 180 continue; 181 (void) strcpy(tty, ubuf.ut_line); 182 status = SUCCESS; 183 break; 184 } 185 } 186 if (strcmp(ubuf.ut_line, tty) == 0) { 187 status = SUCCESS; 188 break; 189 } 190 } 191 fclose(fd); 192 return (status); 193 } 194