Debian Codegraph @

talkd/talkd.c

raw
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 #include <argp.h>
21 #include <intalkd.h>
22 #include <signal.h>
23 #include <libinetutils.h>
24 #include <progname.h>
25 #include "attribute.h"
26
27 #ifndef LOG_FACILITY
28 # define LOG_FACILITY LOG_DAEMON
29 #endif
30
31 void talkd_init (void);
32 void talkd_run (int fd);
33
34 /* Configurable parameters: */
35 int debug;
36 int logging;
37 int strict_policy;
38 unsigned int timeout = 30;
39 time_t max_idle_time = 120;
40 time_t max_request_ttl = MAX_LIFE;
41
42 char *acl_file;
43 char *hostname;
44
45 const char args_doc[] = "";
46 const char doc[] = "Talk daemon, using service `ntalk'.";
47 const char *program_authors[] = {
48 "Sergey Poznyakoff",
49 NULL
50 };
51
52 static struct argp_option argp_options[] = {
53 #define GRP 0
54 {"acl", 'a', "FILE", 0, "read site-wide ACLs from FILE", GRP + 1},
55 {"debug", 'd', NULL, 0, "enable debugging", GRP + 1},
56 {"idle-timeout", 'i', "SECONDS", 0, "set idle timeout value to SECONDS",
57 GRP + 1},
58 {"logging", 'l', NULL, 0, "enable more syslog reporting", GRP + 1},
59 {"request-ttl", 'r', "SECONDS", 0, "set request time-to-live value to "
60 "SECONDS", GRP + 1},
61 {"strict-policy", 'S', NULL, 0, "apply strict ACL policy", GRP + 1},
62 {"timeout", 't', "SECONDS", 0, "set timeout value to SECONDS", GRP + 1},
63 #undef GRP
64 {NULL, 0, NULL, 0, NULL, 0}
65 };
66
67 static error_t
68 parse_opt (int key, char *arg, struct argp_state *state MAYBE_UNUSED)
69 {
70 switch (key)
71 {
72 case 'a':
73 acl_file = arg;
74 break;
75
76 case 'd':
77 debug++;
78 break;
79
80 case 'i':
81 max_idle_time = strtoul (arg, NULL, 0);
82 break;
83
84 case 'l':
85 logging++;
86 break;
87
88 case 'r':
89 max_request_ttl = strtoul (arg, NULL, 0);
90 break;
91
92 case 'S':
93 strict_policy++;
94 break;
95
96 case 't':
97 timeout = strtoul (arg, NULL, 0);
98 break;
99
100 default:
101 return ARGP_ERR_UNKNOWN;
102 }
103
104 return 0;
105 }
106
107 static struct argp argp =
108 { argp_options, parse_opt, args_doc, doc, NULL, NULL, NULL };
109
110 int
111 main (int argc, char *argv[])
112 {
113 set_program_name (argv[0]);
114 /* Parse command line */
115 iu_argp_init ("talkd", program_authors);
116 argp_parse (&argp, argc, argv, 0, NULL, NULL);
117
118 openlog ("talkd", LOG_PID, LOG_FACILITY);
119 read_acl (acl_file, 1); /* System wide ACL. Can abort. */
120 talkd_init ();
121 talkd_run (STDIN_FILENO);
122 return 0;
123 }
124
125 void
126 talkd_init (void)
127 {
128 hostname = localhost ();
129 if (!hostname)
130 {
131 syslog (LOG_ERR, "Cannot determine my hostname: %m");
132 exit (EXIT_FAILURE);
133 }
134 }
135
136 time_t last_msg_time;
137
138 static void
139 alarm_handler (int err MAYBE_UNUSED)
140 {
141 int oerrno = errno;
142
143 if ((time (NULL) - last_msg_time) >= max_idle_time)
144 exit (EXIT_SUCCESS);
145 alarm (timeout);
146 errno = oerrno;
147 }
148
149 void
150 talkd_run (int fd)
151 {
152 struct sockaddr ctl_addr;
153
154 signal (SIGALRM, alarm_handler);
155 alarm (timeout);
156 while (1)
157 {
158 int rc;
159 struct sockaddr_in sa_in;
160 CTL_MSG msg;
161 CTL_RESPONSE resp;
162 socklen_t len;
163
164 len = sizeof sa_in;
165 rc =
166 recvfrom (fd, &msg, sizeof msg, 0, (struct sockaddr *) &sa_in, &len);
167 if (rc != sizeof msg)
168 {
169 if (rc < 0 && errno != EINTR && (logging || debug))
170 syslog (LOG_NOTICE, "recvfrom: %m");
171 continue;
172 }
173 last_msg_time = time (NULL);
174 if (process_request (&msg, &sa_in, &resp) == 0)
175 {
176 ctl_addr.sa_family = msg.ctl_addr.sa_family;
177 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
178 ctl_addr.sa_len = sizeof (struct sockaddr_in);
179 #endif
180 memcpy (&ctl_addr.sa_data, &msg.ctl_addr.sa_data,
181 sizeof (ctl_addr.sa_data));
182 rc = sendto (fd, &resp, sizeof resp, 0,
183 &ctl_addr, sizeof (ctl_addr));
184 if (rc != sizeof resp && (logging || debug))
185 syslog (LOG_NOTICE, "sendto: %m");
186 }
187 }
188 }