1 /*	$NetBSD: biff_notify.c,v 1.2 2017/02/14 01:16:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	biff_notify 3
6 /* SUMMARY
7 /*	send biff notification
8 /* SYNOPSIS
9 /*	#include <biff_notify.h>
10 /*
11 /*	void	biff_notify(text, len)
12 /*	const char *text;
13 /*	ssize_t	len;
14 /* DESCRIPTION
15 /*	biff_notify() sends a \fBBIFF\fR notification request to the
16 /*	\fBcomsat\fR daemon.
17 /*
18 /*	Arguments:
19 /* .IP text
20 /*	Null-terminated text (username@mailbox-offset).
21 /* .IP len
22 /*	Length of text, including null terminator.
23 /* BUGS
24 /*	The \fBBIFF\fR "service" can be a noticeable load for
25 /*	systems that have many logged-in users.
26 /* LICENSE
27 /* .ad
28 /* .fi
29 /*	The Secure Mailer license must be distributed with this software.
30 /* AUTHOR(S)
31 /*	Wietse Venema
32 /*	IBM T.J. Watson Research
33 /*	P.O. Box 704
34 /*	Yorktown Heights, NY 10598, USA
35 /*--*/
36 
37 /* System library. */
38 
39 #include "sys_defs.h"
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netdb.h>
43 #include <string.h>
44 
45 /* Utility library. */
46 
47 #include <msg.h>
48 #include <iostuff.h>
49 
50 /* Application-specific. */
51 
52 #include <biff_notify.h>
53 
54 /* biff_notify - notify recipient via the biff "protocol" */
55 
biff_notify(const char * text,ssize_t len)56 void    biff_notify(const char *text, ssize_t len)
57 {
58     static struct sockaddr_in sin;
59     static int sock = -1;
60     struct hostent *hp;
61     struct servent *sp;
62 
63     /*
64      * Initialize a socket address structure, or re-use an existing one.
65      */
66     if (sin.sin_family == 0) {
67 	if ((sp = getservbyname("biff", "udp")) == 0) {
68 	    msg_warn("service not found: biff/udp");
69 	    return;
70 	}
71 	if ((hp = gethostbyname("localhost")) == 0) {
72 	    msg_warn("host not found: localhost");
73 	    return;
74 	}
75 	if ((int) hp->h_length > (int) sizeof(sin.sin_addr)) {
76 	    msg_warn("bad address size %d for localhost", hp->h_length);
77 	    return;
78 	}
79 	sin.sin_family = hp->h_addrtype;
80 	sin.sin_port = sp->s_port;
81 	memcpy((void *) &sin.sin_addr, hp->h_addr_list[0], hp->h_length);
82     }
83 
84     /*
85      * Open a socket, or re-use an existing one.
86      */
87     if (sock < 0) {
88 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
89 	    msg_warn("socket: %m");
90 	    return;
91 	}
92 	close_on_exec(sock, CLOSE_ON_EXEC);
93     }
94 
95     /*
96      * Biff!
97      */
98     if (sendto(sock, text, len, 0, (struct sockaddr *) &sin, sizeof(sin)) != len)
99 	msg_warn("biff_notify: %m");
100 }
101