1 /*	$NetBSD: debug_peer.c,v 1.3 2022/10/08 16:12:45 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	debug_peer 3
6 /* SUMMARY
7 /*	increase verbose logging for specific peers
8 /* SYNOPSIS
9 /*	#include <debug_peer.h>
10 /*
11 /*	void	debug_peer_init(void)
12 /*
13 /*	int	debug_peer_check(peer_name, peer_addr)
14 /*	const char *peer_name;
15 /*	const char *peer_addr;
16 /*
17 /*	void	debug_peer_restore()
18 /* DESCRIPTION
19 /*	This module implements increased verbose logging for specific
20 /*	network peers.
21 /*
22 /*	The \fIdebug_peer_list\fR configuration parameter
23 /*	specifies what peers receive this special treatment; see
24 /*	namadr_list(3) for a description of the matching process.
25 /*
26 /*	The \fIdebug_peer_level\fR configuration parameter specifies
27 /*	by what amount the verbose logging level should increase when
28 /*	a peer is listed in \fIdebug_peer_list\fR.
29 /*
30 /*	debug_peer_init() performs initializations that must be
31 /*	performed once at the start of the program.
32 /*
33 /*	debug_peer_check() increases the verbose logging level when the
34 /*	client name or address matches the debug_peer_list pattern.
35 /*	The result is non-zero when the noise leven was increased.
36 /*
37 /*	debug_peer_restore() restores the verbose logging level.
38 /*	This routine has no effect when debug_peer_check() had no
39 /*	effect; this routine can safely be called multiple times.
40 /* DIAGNOSTICS
41 /*	Panic: interface violations.
42 /*	Fatal errors: unable to access a peer_list file; invalid
43 /*	peer_list pattern; invalid verbosity level increment.
44 /* SEE ALSO
45 /*	msg(3) the msg_verbose variable
46 /*	namadr_list(3) match host by name or by address
47 /* CONFIG PARAMETERS
48 /*	debug_peer_list, patterns as described in namadr_list(3)
49 /*	debug_peer_level, verbose logging level
50 /* LICENSE
51 /* .ad
52 /* .fi
53 /*	The Secure Mailer license must be distributed with this software.
54 /* AUTHOR(S)
55 /*	Wietse Venema
56 /*	IBM T.J. Watson Research
57 /*	P.O. Box 704
58 /*	Yorktown Heights, NY 10598, USA
59 /*--*/
60 
61 /* System library. */
62 
63 #include <sys_defs.h>
64 
65 /* Utility library. */
66 
67 #include <msg.h>
68 
69 /* Global library. */
70 
71 #include <mail_params.h>
72 #include <namadr_list.h>
73 #include <debug_peer.h>
74 #include <match_parent_style.h>
75 
76 /* Application-specific. */
77 
78 #define UNUSED_SAVED_LEVEL	(-1)
79 
80 static NAMADR_LIST *debug_peer_list;
81 static int saved_level = UNUSED_SAVED_LEVEL;
82 
83 /* debug_peer_init - initialize */
84 
debug_peer_init(void)85 void    debug_peer_init(void)
86 {
87     const char *myname = "debug_peer_init";
88 
89     /*
90      * Sanity check.
91      */
92     if (debug_peer_list)
93 	msg_panic("%s: repeated call", myname);
94     if (var_debug_peer_list == 0)
95 	msg_panic("%s: uninitialized %s", myname, VAR_DEBUG_PEER_LIST);
96     if (var_debug_peer_level <= 0)
97 	msg_fatal("%s: %s <= 0", myname, VAR_DEBUG_PEER_LEVEL);
98 
99     /*
100      * Finally.
101      */
102     if (*var_debug_peer_list)
103 	debug_peer_list =
104 	    namadr_list_init(VAR_DEBUG_PEER_LIST, MATCH_FLAG_RETURN
105 			     | match_parent_style(VAR_DEBUG_PEER_LIST),
106 			     var_debug_peer_list);
107 }
108 
109 /* debug_peer_check - see if this peer needs verbose logging */
110 
debug_peer_check(const char * name,const char * addr)111 int     debug_peer_check(const char *name, const char *addr)
112 {
113 
114     /*
115      * Crank up the noise when this peer is listed.
116      */
117     if (debug_peer_list != 0
118 	&& saved_level == UNUSED_SAVED_LEVEL
119 	&& namadr_list_match(debug_peer_list, name, addr) != 0) {
120 	saved_level = msg_verbose;
121 	msg_verbose += var_debug_peer_level;
122 	return (1);
123     }
124     return (0);
125 }
126 
127 /* debug_peer_restore - restore logging level */
128 
debug_peer_restore(void)129 void    debug_peer_restore(void)
130 {
131     if (saved_level != UNUSED_SAVED_LEVEL) {
132 	msg_verbose = saved_level;
133 	saved_level = UNUSED_SAVED_LEVEL;
134     }
135 }
136