File: process.c

package info (click to toggle)
inetutils 2%3A2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 17,312 kB
  • sloc: ansic: 125,847; sh: 12,435; yacc: 1,651; makefile: 697; perl: 72
file content (268 lines) | stat: -rw-r--r-- 6,435 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
  Copyright (C) 1996-2025 Free Software Foundation, Inc.

  This file is part of GNU Inetutils.

  GNU Inetutils is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or (at
  your option) any later version.

  GNU Inetutils is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see `http://www.gnu.org/licenses/'. */

#include <config.h>

#include <intalkd.h>
#include <readutmp.h>

int find_user (char *name, char *tty);
void do_announce (CTL_MSG * mp, CTL_RESPONSE * rp);

int
process_request (CTL_MSG *msg, struct sockaddr_in *sa_in, CTL_RESPONSE *rp)
{
  CTL_MSG *ptr;

  rp->vers = TALK_VERSION;
  rp->type = msg->type;
  rp->id_num = htonl (0);
  if (msg->vers != TALK_VERSION)
    {
      if (logging || debug)
	syslog (LOG_NOTICE, "Bad protocol version %d", msg->vers);
      rp->answer = BADVERSION;
      return 0;
    }

  /* Convert the machine independent represention
   * of the talk protocol to the present architecture.
   * In particular, `msg->addr.sa_family' will be
   * valid for socket initialization.
   */
  msg->id_num = ntohl (msg->id_num);
  msg->addr.sa_family = ntohs (msg->addr.sa_family);
  if (msg->addr.sa_family != AF_INET)
    {
      if (logging || debug)
	syslog (LOG_NOTICE, "Bad address, family %d", msg->addr.sa_family);
      rp->answer = BADADDR;
      return 0;
    }
  /* Convert to valid socket address for this architecture.  */
  msg->ctl_addr.sa_family = ntohs (msg->ctl_addr.sa_family);
  if (msg->ctl_addr.sa_family != AF_INET)
    {
      if (logging || debug)
	syslog (LOG_NOTICE, "Bad control address, family %d",
		msg->ctl_addr.sa_family);
      rp->answer = BADCTLADDR;
      return 0;
    }
  /* FIXME: compare address and sa_in? */

  if (acl_match (msg, sa_in) == ACL_DENY)
    {
      /* This denial happens for each of LOOK_UP,
       * ANNOUNCE, and DELETE, in this order.
       * Make a syslog note only for the first of them.
       */
      if ((logging || debug) && msg->type == LOOK_UP)
	syslog (LOG_NOTICE, "dropping request: %s@%s",
		msg->l_name, inet_ntoa (sa_in->sin_addr));

      /* The answer FAILED is returned to minimize the amount
       * of information disclosure, since ACL has denied access.
       */
      rp->answer = FAILED;
      return 0;
    }

  if (debug)
    {
      print_request ("process_request", msg);
    }

  msg->pid = ntohl (msg->pid);

  switch (msg->type)
    {
    case ANNOUNCE:
      do_announce (msg, rp);
      if (logging && rp->answer == SUCCESS)
	syslog (LOG_INFO, "%s@%s called by %s@%s",
		msg->r_name,
		(msg->r_tty[0]
		 ? msg->r_tty
		 : inet_ntoa (sa_in->sin_addr)),
		msg->l_name, inet_ntoa (os2sin_addr (msg->addr)));
      break;

    case LEAVE_INVITE:
      ptr = find_request (msg);
      if (ptr)
	{
	  rp->id_num = htonl (ptr->id_num);
	  rp->answer = SUCCESS;
	}
      else
	insert_table (msg, rp);
      break;

    case LOOK_UP:
      ptr = find_match (msg);
      if (ptr)
	{
	  rp->id_num = htonl (ptr->id_num);
	  rp->addr = ptr->addr;
	  rp->addr.sa_family = htons (ptr->addr.sa_family);
	  rp->answer = SUCCESS;
	  if (logging)
	    syslog (LOG_INFO, "%s talks to %s@%s",
		    msg->r_name, msg->l_name, inet_ntoa (sa_in->sin_addr));
	}
      else
	rp->answer = NOT_HERE;
      break;

    case DELETE:
      rp->answer = delete_invite (msg->id_num);
      break;

    default:
      rp->answer = UNKNOWN_REQUEST;
      break;
    }

  if (debug)
    print_response ("process_request response", rp);

  return 0;
}

void
do_announce (CTL_MSG *mp, CTL_RESPONSE *rp)
{
  struct hostent *hp;
  CTL_MSG *ptr;
  int result;

  result = find_user (mp->r_name, mp->r_tty);
  if (result != SUCCESS)
    {
      rp->answer = result;
      return;
    }

  hp = gethostbyaddr ((char *) &os2sin_addr (mp->ctl_addr),
		      sizeof (struct in_addr), AF_INET);
  if (!hp)
    {
      rp->answer = MACHINE_UNKNOWN;
      return;
    }
  ptr = find_request (mp);
  if (!ptr)
    {
      insert_table (mp, rp);
      rp->answer = announce (mp, hp->h_name);
      return;
    }
  if (mp->id_num > ptr->id_num)
    {
      /* Explicit re-announce: update the id_num to avoid duplicates
         and re-announce the talk. */
      ptr->id_num = new_id ();
      rp->id_num = htonl (ptr->id_num);
      rp->answer = announce (mp, hp->h_name);
    }
  else
    {
      /* a duplicated request, so ignore it */
      rp->id_num = htonl (ptr->id_num);
      rp->answer = SUCCESS;
    }
}

/* Search utmp for the local user.  */
int
find_user (char *name, char *tty)
{
  STRUCT_UTMP *uptr;
#ifndef HAVE_GETUTXUSER
  STRUCT_UTMP *utmpbuf;
  size_t utmp_count;
#endif /* HAVE_GETUTXUSER */
  int status;
  struct stat statb;
  char ftty[sizeof (PATH_TTY_PFX) + sizeof (uptr->ut_line)];
  time_t last_time = 0;
  int notty;

  notty = (*tty == '\0');

  status = NOT_HERE;
  strcpy (ftty, PATH_TTY_PFX);

/* *INDENT-OFF* */
#ifdef HAVE_GETUTXUSER
  setutxent ();

  while ((uptr = getutxuser (name)))
#else /* !HAVE_GETUTXUSER */
  if (read_utmp (UTMP_FILE, &utmp_count, &utmpbuf,
		 READ_UTMP_USER_PROCESS | READ_UTMP_CHECK_PIDS) < 0)
    return FAILED;

  for (uptr = utmpbuf; uptr < utmpbuf + utmp_count; uptr++)
    {
      if (!strncmp (UT_USER (uptr), name, sizeof (UT_USER (uptr))))
#endif /* !HAVE_GETUTXUSER */
/* *INDENT-ON* */
  {
    if (notty)
      {
	/* no particular tty was requested */
	strncpy (ftty + sizeof (PATH_TTY_PFX) - 1,
		 uptr->ut_line, sizeof (ftty) - sizeof (PATH_TTY_PFX) - 1);
	ftty[sizeof (ftty) - 1] = 0;

	if (stat (ftty, &statb) == 0)
	  {
	    if (!(statb.st_mode & S_IWGRP))
	      {
		if (status != SUCCESS)
		  status = PERMISSION_DENIED;
		continue;
	      }
	    if (statb.st_atime > last_time)
	      {
		last_time = statb.st_atime;
		strcpy (tty, uptr->ut_line);
		status = SUCCESS;
	      }
	    continue;
	  }
      }
    if (!strcmp (uptr->ut_line, tty))
      {
	status = SUCCESS;
	break;
      }
  }
/* *INDENT-OFF* */
#ifndef HAVE_GETUTXUSER
    }
  free (utmpbuf);
#else /* HAVE_GETUTXUSER */
  endutxent ();
#endif
/* *INDENT-ON* */

  return status;
}