GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: libexec/talkd/announce.c Lines: 0 56 0.0 %
Date: 2017-11-07 Branches: 0 26 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: announce.c,v 1.24 2016/02/01 07:25:51 mestre Exp $	*/
2
3
/*
4
 * Copyright (c) 1983 Regents of the University of California.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. Neither the name of the University nor the names of its contributors
16
 *    may be used to endorse or promote products derived from this software
17
 *    without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
 * SUCH DAMAGE.
30
 */
31
32
#include <sys/stat.h>
33
#include <protocols/talkd.h>
34
35
#include <limits.h>
36
#include <paths.h>
37
#include <stdio.h>
38
#include <string.h>
39
#include <unistd.h>
40
#include <vis.h>
41
42
#include "talkd.h"
43
44
static void	print_mesg(FILE *,CTL_MSG *,char *);
45
46
/*
47
 * Announce an invitation to talk.  If the user is
48
 * accepting messages, announce that a talk is requested.
49
 */
50
int
51
announce(CTL_MSG *request, char *remote_machine)
52
{
53
	char full_tty[PATH_MAX];
54
	FILE *tf;
55
	struct stat stbuf;
56
57
	(void)snprintf(full_tty, sizeof(full_tty), "%s/%s", _PATH_DEV,
58
	    request->r_tty);
59
	if (access(full_tty, 0) != 0)
60
		return (FAILED);
61
	if ((tf = fopen(full_tty, "w")) == NULL)
62
		return (PERMISSION_DENIED);
63
	if (fstat(fileno(tf), &stbuf) < 0) {
64
		fclose(tf);
65
		return (PERMISSION_DENIED);
66
	}
67
	if ((stbuf.st_mode & S_IWGRP) == 0) {
68
		fclose(tf);
69
		return (PERMISSION_DENIED);
70
	}
71
	print_mesg(tf, request, remote_machine);
72
	fclose(tf);
73
	return (SUCCESS);
74
}
75
76
#define max(a,b) ( (a) > (b) ? (a) : (b) )
77
#define N_LINES 5
78
#define N_CHARS 120
79
80
/*
81
 * Build a block of characters containing the message.
82
 * It is sent blank filled and in a single block to
83
 * try to keep the message in one piece if the recipient
84
 * is in vi at the time
85
 */
86
static void
87
print_mesg(FILE *tf, CTL_MSG *request, char *remote_machine)
88
{
89
	time_t clocktime;
90
	struct tm *localclock;
91
	char line_buf[N_LINES][N_CHARS];
92
	int sizes[N_LINES];
93
	char big_buf[(N_LINES + 1) * N_CHARS];
94
	char *bptr, *lptr, vis_user[sizeof(request->l_name) * 4];
95
	int i, j, max_size;
96
97
	i = 0;
98
	max_size = 0;
99
	time(&clocktime);
100
	localclock = localtime(&clocktime);
101
	(void)snprintf(line_buf[i], N_CHARS, " ");
102
	sizes[i] = strlen(line_buf[i]);
103
	max_size = max(max_size, sizes[i]);
104
	i++;
105
	(void)snprintf(line_buf[i], N_CHARS,
106
	    "Message from Talk_Daemon@%s at %d:%02d ...",
107
	    hostname, localclock->tm_hour , localclock->tm_min );
108
	sizes[i] = strlen(line_buf[i]);
109
	max_size = max(max_size, sizes[i]);
110
	i++;
111
	strvis(vis_user, request->l_name, VIS_CSTYLE);
112
	(void)snprintf(line_buf[i], N_CHARS,
113
	    "talk: connection requested by %s@%s.",
114
	    vis_user, remote_machine);
115
	sizes[i] = strlen(line_buf[i]);
116
	max_size = max(max_size, sizes[i]);
117
	i++;
118
	(void)snprintf(line_buf[i], N_CHARS, "talk: respond with:  talk %s@%s",
119
	    vis_user, remote_machine);
120
	sizes[i] = strlen(line_buf[i]);
121
	max_size = max(max_size, sizes[i]);
122
	i++;
123
	(void)snprintf(line_buf[i], N_CHARS, " ");
124
	sizes[i] = strlen(line_buf[i]);
125
	max_size = max(max_size, sizes[i]);
126
	i++;
127
	bptr = big_buf;
128
	*bptr++ = '\007'; /* send something to wake them up */
129
	*bptr++ = '\r';	/* add a \r in case of raw mode */
130
	*bptr++ = '\n';
131
	for (i = 0; i < N_LINES; i++) {
132
		/* copy the line into the big buffer */
133
		lptr = line_buf[i];
134
		while (*lptr != '\0')
135
			*(bptr++) = *(lptr++);
136
		/* pad out the rest of the lines with blanks */
137
		for (j = sizes[i]; j < max_size + 2; j++)
138
			*(bptr++) = ' ';
139
		*(bptr++) = '\r';	/* add a \r in case of raw mode */
140
		*(bptr++) = '\n';
141
	}
142
	*bptr = '\0';
143
	fprintf(tf, "%s", big_buf);
144
	fflush(tf);
145
}