| 1 |
/* |
| 2 |
Copyright (C) 1996-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <intalkd.h> |
| 22 |
#include <readutmp.h> |
| 23 |
|
| 24 |
int find_user (char *name, char *tty); |
| 25 |
void do_announce (CTL_MSG * mp, CTL_RESPONSE * rp); |
| 26 |
|
| 27 |
int |
| 28 |
process_request (CTL_MSG *msg, struct sockaddr_in *sa_in, CTL_RESPONSE *rp) |
| 29 |
{ |
| 30 |
CTL_MSG *ptr; |
| 31 |
|
| 32 |
rp->vers = TALK_VERSION; |
| 33 |
rp->type = msg->type; |
| 34 |
rp->id_num = htonl (0); |
| 35 |
if (msg->vers != TALK_VERSION) |
| 36 |
{ |
| 37 |
if (logging || debug) |
| 38 |
syslog (LOG_NOTICE, "Bad protocol version %d", msg->vers); |
| 39 |
rp->answer = BADVERSION; |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
/* Convert the machine independent representation |
| 44 |
* of the talk protocol to the present architecture. |
| 45 |
* In particular, `msg->addr.sa_family' will be |
| 46 |
* valid for socket initialization. |
| 47 |
*/ |
| 48 |
msg->id_num = ntohl (msg->id_num); |
| 49 |
msg->addr.sa_family = ntohs (msg->addr.sa_family); |
| 50 |
if (msg->addr.sa_family != AF_INET) |
| 51 |
{ |
| 52 |
if (logging || debug) |
| 53 |
syslog (LOG_NOTICE, "Bad address, family %d", msg->addr.sa_family); |
| 54 |
rp->answer = BADADDR; |
| 55 |
return 0; |
| 56 |
} |
| 57 |
/* Convert to valid socket address for this architecture. */ |
| 58 |
msg->ctl_addr.sa_family = ntohs (msg->ctl_addr.sa_family); |
| 59 |
if (msg->ctl_addr.sa_family != AF_INET) |
| 60 |
{ |
| 61 |
if (logging || debug) |
| 62 |
syslog (LOG_NOTICE, "Bad control address, family %d", |
| 63 |
msg->ctl_addr.sa_family); |
| 64 |
rp->answer = BADCTLADDR; |
| 65 |
return 0; |
| 66 |
} |
| 67 |
/* FIXME: compare address and sa_in? */ |
| 68 |
|
| 69 |
if (acl_match (msg, sa_in) == ACL_DENY) |
| 70 |
{ |
| 71 |
/* This denial happens for each of LOOK_UP, |
| 72 |
* ANNOUNCE, and DELETE, in this order. |
| 73 |
* Make a syslog note only for the first of them. |
| 74 |
*/ |
| 75 |
if ((logging || debug) && msg->type == LOOK_UP) |
| 76 |
syslog (LOG_NOTICE, "dropping request: %s@%s", |
| 77 |
msg->l_name, inet_ntoa (sa_in->sin_addr)); |
| 78 |
|
| 79 |
/* The answer FAILED is returned to minimize the amount |
| 80 |
* of information disclosure, since ACL has denied access. |
| 81 |
*/ |
| 82 |
rp->answer = FAILED; |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
if (debug) |
| 87 |
{ |
| 88 |
print_request ("process_request", msg); |
| 89 |
} |
| 90 |
|
| 91 |
msg->pid = ntohl (msg->pid); |
| 92 |
|
| 93 |
switch (msg->type) |
| 94 |
{ |
| 95 |
case ANNOUNCE: |
| 96 |
do_announce (msg, rp); |
| 97 |
if (logging && rp->answer == SUCCESS) |
| 98 |
syslog (LOG_INFO, "%s@%s called by %s@%s", |
| 99 |
msg->r_name, |
| 100 |
(msg->r_tty[0] |
| 101 |
? msg->r_tty |
| 102 |
: inet_ntoa (sa_in->sin_addr)), |
| 103 |
msg->l_name, inet_ntoa (os2sin_addr (msg->addr))); |
| 104 |
break; |
| 105 |
|
| 106 |
case LEAVE_INVITE: |
| 107 |
ptr = find_request (msg); |
| 108 |
if (ptr) |
| 109 |
{ |
| 110 |
rp->id_num = htonl (ptr->id_num); |
| 111 |
rp->answer = SUCCESS; |
| 112 |
} |
| 113 |
else |
| 114 |
insert_table (msg, rp); |
| 115 |
break; |
| 116 |
|
| 117 |
case LOOK_UP: |
| 118 |
ptr = find_match (msg); |
| 119 |
if (ptr) |
| 120 |
{ |
| 121 |
rp->id_num = htonl (ptr->id_num); |
| 122 |
rp->addr = ptr->addr; |
| 123 |
rp->addr.sa_family = htons (ptr->addr.sa_family); |
| 124 |
rp->answer = SUCCESS; |
| 125 |
if (logging) |
| 126 |
syslog (LOG_INFO, "%s talks to %s@%s", |
| 127 |
msg->r_name, msg->l_name, inet_ntoa (sa_in->sin_addr)); |
| 128 |
} |
| 129 |
else |
| 130 |
rp->answer = NOT_HERE; |
| 131 |
break; |
| 132 |
|
| 133 |
case DELETE: |
| 134 |
rp->answer = delete_invite (msg->id_num); |
| 135 |
break; |
| 136 |
|
| 137 |
default: |
| 138 |
rp->answer = UNKNOWN_REQUEST; |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
if (debug) |
| 143 |
print_response ("process_request response", rp); |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
do_announce (CTL_MSG *mp, CTL_RESPONSE *rp) |
| 150 |
{ |
| 151 |
struct hostent *hp; |
| 152 |
CTL_MSG *ptr; |
| 153 |
int result; |
| 154 |
|
| 155 |
result = find_user (mp->r_name, mp->r_tty); |
| 156 |
if (result != SUCCESS) |
| 157 |
{ |
| 158 |
rp->answer = result; |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
hp = gethostbyaddr ((char *) &os2sin_addr (mp->ctl_addr), |
| 163 |
sizeof (struct in_addr), AF_INET); |
| 164 |
if (!hp) |
| 165 |
{ |
| 166 |
rp->answer = MACHINE_UNKNOWN; |
| 167 |
return; |
| 168 |
} |
| 169 |
ptr = find_request (mp); |
| 170 |
if (!ptr) |
| 171 |
{ |
| 172 |
insert_table (mp, rp); |
| 173 |
rp->answer = announce (mp, hp->h_name); |
| 174 |
return; |
| 175 |
} |
| 176 |
if (mp->id_num > ptr->id_num) |
| 177 |
{ |
| 178 |
/* Explicit re-announce: update the id_num to avoid duplicates |
| 179 |
and re-announce the talk. */ |
| 180 |
ptr->id_num = new_id (); |
| 181 |
rp->id_num = htonl (ptr->id_num); |
| 182 |
rp->answer = announce (mp, hp->h_name); |
| 183 |
} |
| 184 |
else |
| 185 |
{ |
| 186 |
/* a duplicated request, so ignore it */ |
| 187 |
rp->id_num = htonl (ptr->id_num); |
| 188 |
rp->answer = SUCCESS; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/* Search utmp for the local user. */ |
| 193 |
int |
| 194 |
find_user (char *name, char *tty) |
| 195 |
{ |
| 196 |
STRUCT_UTMP *uptr; |
| 197 |
#ifndef HAVE_GETUTXUSER |
| 198 |
STRUCT_UTMP *utmpbuf; |
| 199 |
size_t utmp_count; |
| 200 |
#endif /* HAVE_GETUTXUSER */ |
| 201 |
int status; |
| 202 |
struct stat statb; |
| 203 |
char ftty[sizeof (PATH_TTY_PFX) + sizeof (uptr->ut_line)]; |
| 204 |
time_t last_time = 0; |
| 205 |
int notty; |
| 206 |
|
| 207 |
notty = (*tty == '\0'); |
| 208 |
|
| 209 |
status = NOT_HERE; |
| 210 |
strcpy (ftty, PATH_TTY_PFX); |
| 211 |
|
| 212 |
/* *INDENT-OFF* */ |
| 213 |
#ifdef HAVE_GETUTXUSER |
| 214 |
setutxent (); |
| 215 |
|
| 216 |
while ((uptr = getutxuser (name))) |
| 217 |
#else /* !HAVE_GETUTXUSER */ |
| 218 |
if (read_utmp (UTMP_FILE, &utmp_count, &utmpbuf, |
| 219 |
READ_UTMP_USER_PROCESS | READ_UTMP_CHECK_PIDS) < 0) |
| 220 |
return FAILED; |
| 221 |
|
| 222 |
for (uptr = utmpbuf; uptr < utmpbuf + utmp_count; uptr++) |
| 223 |
{ |
| 224 |
if (!strncmp (UT_USER (uptr), name, sizeof (UT_USER (uptr)))) |
| 225 |
#endif /* !HAVE_GETUTXUSER */ |
| 226 |
/* *INDENT-ON* */ |
| 227 |
{ |
| 228 |
if (notty) |
| 229 |
{ |
| 230 |
/* no particular tty was requested */ |
| 231 |
strncpy (ftty + sizeof (PATH_TTY_PFX) - 1, |
| 232 |
uptr->ut_line, sizeof (ftty) - sizeof (PATH_TTY_PFX) - 1); |
| 233 |
ftty[sizeof (ftty) - 1] = 0; |
| 234 |
|
| 235 |
if (stat (ftty, &statb) == 0) |
| 236 |
{ |
| 237 |
if (!(statb.st_mode & S_IWGRP)) |
| 238 |
{ |
| 239 |
if (status != SUCCESS) |
| 240 |
status = PERMISSION_DENIED; |
| 241 |
continue; |
| 242 |
} |
| 243 |
if (statb.st_atime > last_time) |
| 244 |
{ |
| 245 |
last_time = statb.st_atime; |
| 246 |
strcpy (tty, uptr->ut_line); |
| 247 |
status = SUCCESS; |
| 248 |
} |
| 249 |
continue; |
| 250 |
} |
| 251 |
} |
| 252 |
if (!strcmp (uptr->ut_line, tty)) |
| 253 |
{ |
| 254 |
status = SUCCESS; |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
/* *INDENT-OFF* */ |
| 259 |
#ifndef HAVE_GETUTXUSER |
| 260 |
} |
| 261 |
free (utmpbuf); |
| 262 |
#else /* HAVE_GETUTXUSER */ |
| 263 |
endutxent (); |
| 264 |
#endif |
| 265 |
/* *INDENT-ON* */ |
| 266 |
|
| 267 |
return status; |
| 268 |
} |