File: talkd.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 (188 lines) | stat: -rw-r--r-- 4,385 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
/*
  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 <argp.h>
#include <intalkd.h>
#include <signal.h>
#include <libinetutils.h>
#include <progname.h>
#include "attribute.h"

#ifndef LOG_FACILITY
# define LOG_FACILITY LOG_DAEMON
#endif

void talkd_init (void);
void talkd_run (int fd);

/* Configurable parameters: */
int debug;
int logging;
int strict_policy;
unsigned int timeout = 30;
time_t max_idle_time = 120;
time_t max_request_ttl = MAX_LIFE;

char *acl_file;
char *hostname;

const char args_doc[] = "";
const char doc[] = "Talk daemon, using service `ntalk'.";
const char *program_authors[] = {
  "Sergey Poznyakoff",
  NULL
};

static struct argp_option argp_options[] = {
#define GRP 0
  {"acl", 'a', "FILE", 0, "read site-wide ACLs from FILE", GRP + 1},
  {"debug", 'd', NULL, 0, "enable debugging", GRP + 1},
  {"idle-timeout", 'i', "SECONDS", 0, "set idle timeout value to SECONDS",
   GRP + 1},
  {"logging", 'l', NULL, 0, "enable more syslog reporting", GRP + 1},
  {"request-ttl", 'r', "SECONDS", 0, "set request time-to-live value to "
   "SECONDS", GRP + 1},
  {"strict-policy", 'S', NULL, 0, "apply strict ACL policy", GRP + 1},
  {"timeout", 't', "SECONDS", 0, "set timeout value to SECONDS", GRP + 1},
#undef GRP
  {NULL, 0, NULL, 0, NULL, 0}
};

static error_t
parse_opt (int key, char *arg, struct argp_state *state MAYBE_UNUSED)
{
  switch (key)
    {
    case 'a':
      acl_file = arg;
      break;

    case 'd':
      debug++;
      break;

    case 'i':
      max_idle_time = strtoul (arg, NULL, 0);
      break;

    case 'l':
      logging++;
      break;

    case 'r':
      max_request_ttl = strtoul (arg, NULL, 0);
      break;

    case 'S':
      strict_policy++;
      break;

    case 't':
      timeout = strtoul (arg, NULL, 0);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }

  return 0;
}

static struct argp argp =
  { argp_options, parse_opt, args_doc, doc, NULL, NULL, NULL };

int
main (int argc, char *argv[])
{
  set_program_name (argv[0]);
  /* Parse command line */
  iu_argp_init ("talkd", program_authors);
  argp_parse (&argp, argc, argv, 0, NULL, NULL);

  openlog ("talkd", LOG_PID, LOG_FACILITY);
  read_acl (acl_file, 1);	/* System wide ACL.  Can abort.  */
  talkd_init ();
  talkd_run (STDIN_FILENO);
  return 0;
}

void
talkd_init (void)
{
  hostname = localhost ();
  if (!hostname)
    {
      syslog (LOG_ERR, "Cannot determine my hostname: %m");
      exit (EXIT_FAILURE);
    }
}

time_t last_msg_time;

static void
alarm_handler (int err MAYBE_UNUSED)
{
  int oerrno = errno;

  if ((time (NULL) - last_msg_time) >= max_idle_time)
    exit (EXIT_SUCCESS);
  alarm (timeout);
  errno = oerrno;
}

void
talkd_run (int fd)
{
  struct sockaddr ctl_addr;

  signal (SIGALRM, alarm_handler);
  alarm (timeout);
  while (1)
    {
      int rc;
      struct sockaddr_in sa_in;
      CTL_MSG msg;
      CTL_RESPONSE resp;
      socklen_t len;

      len = sizeof sa_in;
      rc =
	recvfrom (fd, &msg, sizeof msg, 0, (struct sockaddr *) &sa_in, &len);
      if (rc != sizeof msg)
	{
	  if (rc < 0 && errno != EINTR && (logging || debug))
	    syslog (LOG_NOTICE, "recvfrom: %m");
	  continue;
	}
      last_msg_time = time (NULL);
      if (process_request (&msg, &sa_in, &resp) == 0)
	{
	  ctl_addr.sa_family = msg.ctl_addr.sa_family;
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
	  ctl_addr.sa_len = sizeof (struct sockaddr_in);
#endif
	  memcpy (&ctl_addr.sa_data, &msg.ctl_addr.sa_data,
		  sizeof (ctl_addr.sa_data));
	  rc = sendto (fd, &resp, sizeof resp, 0,
		       &ctl_addr, sizeof (ctl_addr));
	  if (rc != sizeof resp && (logging || debug))
	    syslog (LOG_NOTICE, "sendto: %m");
	}
    }
}