Debian Codegraph @

talkd/announce.c

raw
1 /*
2 Copyright (C) 1994-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 <stdarg.h>
23 #include <sys/uio.h>
24
25 #undef MAX
26 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
27 #define N_LINES 5
28 #define N_CHARS 256
29
30 #include <libinetutils.h>
31
32 typedef struct
33 {
34 int ind;
35 int max_size;
36 char line[N_LINES][N_CHARS];
37 int size[N_LINES];
38 char buf[N_LINES * N_CHARS + 3];
39 } LINE;
40
41 static void
42 init_line (LINE *lp)
43 {
44 memset (lp, 0, sizeof *lp);
45 }
46
47 static void
48 format_line (LINE *lp, const char *fmt, ...)
49 {
50 va_list ap;
51 int i = lp->ind;
52
53 if (lp->ind >= N_LINES)
54 return;
55 lp->ind++;
56 va_start (ap, fmt);
57 lp->size[i] = vsnprintf (lp->line[i], sizeof lp->line[i], fmt, ap);
58 lp->max_size = MAX (lp->max_size, lp->size[i]);
59 va_end (ap);
60 }
61
62 static char *
63 finish_line (LINE *lp)
64 {
65 int i;
66 char *p;
67
68 p = lp->buf;
69 *p++ = '\a';
70 *p++ = '\r';
71 *p++ = '\n';
72 for (i = 0; i < lp->ind; i++)
73 {
74 char *q;
75 int j;
76
77 for (q = lp->line[i]; *q; q++)
78 *p++ = *q;
79 for (j = lp->size[i]; j < lp->max_size + 2; j++)
80 *p++ = ' ';
81 *p++ = '\r';
82 *p++ = '\n';
83 }
84 *p = 0;
85 return lp->buf;
86 }
87
88 static int
89 print_mesg (char *tty, CTL_MSG *request, char *remote_machine)
90 {
91 time_t t;
92 LINE ln;
93 char *buf;
94 struct tm *tm;
95 struct iovec iovec;
96 char *cp;
97
98 time (&t);
99 tm = localtime (&t);
100 init_line (&ln);
101 format_line (&ln, "");
102 format_line (&ln, "Message from Talk_Daemon@%s at %d:%02d ...",
103 hostname, tm->tm_hour, tm->tm_min);
104 format_line (&ln, "talk: connection requested by %s@%s",
105 request->l_name, remote_machine);
106 format_line (&ln, "talk: respond with: talk %s@%s",
107 request->l_name, remote_machine);
108 format_line (&ln, "");
109 format_line (&ln, "");
110 buf = finish_line (&ln);
111
112 iovec.iov_base = buf;
113 iovec.iov_len = strlen (buf);
114
115 if ((cp = inetutils_ttymsg (&iovec, 1, tty, RING_WAIT - 5)) != NULL)
116 {
117 syslog (LOG_ERR, "%s", cp);
118 return FAILED;
119 }
120 return SUCCESS;
121 }
122
123 /* See if the user is accepting messages. If so, announce that
124 a talk is requested. */
125 int
126 announce (CTL_MSG *request, char *remote_machine)
127 {
128 char *ttypath;
129 int len;
130 struct stat st;
131 int rc;
132
133 len = sizeof (PATH_TTY_PFX) + strlen (request->r_tty) + 2;
134 ttypath = malloc (len);
135 if (!ttypath)
136 {
137 syslog (LOG_ERR, "Out of memory");
138 exit (EXIT_FAILURE);
139 }
140 sprintf (ttypath, "%s/%s", PATH_TTY_PFX, request->r_tty);
141 rc = stat (ttypath, &st);
142 free (ttypath);
143 if (rc < 0 || (st.st_mode & S_IWGRP) == 0)
144 return PERMISSION_DENIED;
145 return print_mesg (request->r_tty, request, remote_machine);
146 }