summaryrefslogtreecommitdiffstats
path: root/src/local/biff_notify.c
blob: a6a49258ee65e39fd02d5f3a3828e4052ccb63b6 (plain)
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
/*++
/* NAME
/*	biff_notify 3
/* SUMMARY
/*	send biff notification
/* SYNOPSIS
/*	#include <biff_notify.h>
/*
/*	void	biff_notify(text, len)
/*	const char *text;
/*	ssize_t	len;
/* DESCRIPTION
/*	biff_notify() sends a \fBBIFF\fR notification request to the
/*	\fBcomsat\fR daemon.
/*
/*	Arguments:
/* .IP text
/*	Null-terminated text (username@mailbox-offset).
/* .IP len
/*	Length of text, including null terminator.
/* BUGS
/*	The \fBBIFF\fR "service" can be a noticeable load for
/*	systems that have many logged-in users.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include "sys_defs.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>

/* Utility library. */

#include <msg.h>
#include <iostuff.h>

/* Application-specific. */

#include <biff_notify.h>

/* biff_notify - notify recipient via the biff "protocol" */

void    biff_notify(const char *text, ssize_t len)
{
    static struct sockaddr_in sin;
    static int sock = -1;
    struct hostent *hp;
    struct servent *sp;

    /*
     * Initialize a socket address structure, or re-use an existing one.
     */
    if (sin.sin_family == 0) {
	if ((sp = getservbyname("biff", "udp")) == 0) {
	    msg_warn("service not found: biff/udp");
	    return;
	}
	if ((hp = gethostbyname("localhost")) == 0) {
	    msg_warn("host not found: localhost");
	    return;
	}
	if ((int) hp->h_length > (int) sizeof(sin.sin_addr)) {
	    msg_warn("bad address size %d for localhost", hp->h_length);
	    return;
	}
	sin.sin_family = hp->h_addrtype;
	sin.sin_port = sp->s_port;
	memcpy((void *) &sin.sin_addr, hp->h_addr_list[0], hp->h_length);
    }

    /*
     * Open a socket, or re-use an existing one.
     */
    if (sock < 0) {
	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
	    msg_warn("socket: %m");
	    return;
	}
	close_on_exec(sock, CLOSE_ON_EXEC);
    }

    /*
     * Biff!
     */
    if (sendto(sock, text, len, 0, (struct sockaddr *) &sin, sizeof(sin)) != len)
	msg_warn("biff_notify: %m");
}