summaryrefslogtreecommitdiffstats
path: root/src/global/debug_peer.c
blob: ae57a8f51f083ff720668c9947c5c4fb55bf997d (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*++
/* NAME
/*	debug_peer 3
/* SUMMARY
/*	increase verbose logging for specific peers
/* SYNOPSIS
/*	#include <debug_peer.h>
/*
/*	void	debug_peer_init(void)
/*
/*	int	debug_peer_check(peer_name, peer_addr)
/*	const char *peer_name;
/*	const char *peer_addr;
/*
/*	void	debug_peer_restore()
/* DESCRIPTION
/*	This module implements increased verbose logging for specific
/*	network peers.
/*
/*	The \fIdebug_peer_list\fR configuration parameter
/*	specifies what peers receive this special treatment; see
/*	namadr_list(3) for a description of the matching process.
/*
/*	The \fIdebug_peer_level\fR configuration parameter specifies
/*	by what amount the verbose logging level should increase when
/*	a peer is listed in \fIdebug_peer_list\fR.
/*
/*	debug_peer_init() performs initializations that must be
/*	performed once at the start of the program.
/*
/*	debug_peer_check() increases the verbose logging level when the
/*	client name or address matches the debug_peer_list pattern.
/*	The result is non-zero when the noise leven was increased.
/*
/*	debug_peer_restore() restores the verbose logging level.
/*	This routine has no effect when debug_peer_check() had no
/*	effect; this routine can safely be called multiple times.
/* DIAGNOSTICS
/*	Panic: interface violations.
/*	Fatal errors: unable to access a peer_list file; invalid
/*	peer_list pattern; invalid verbosity level increment.
/* SEE ALSO
/*	msg(3) the msg_verbose variable
/*	namadr_list(3) match host by name or by address
/* CONFIG PARAMETERS
/*	debug_peer_list, patterns as described in namadr_list(3)
/*	debug_peer_level, verbose logging level
/* 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>

/* Utility library. */

#include <msg.h>

/* Global library. */

#include <mail_params.h>
#include <namadr_list.h>
#include <debug_peer.h>
#include <match_parent_style.h>

/* Application-specific. */

#define UNUSED_SAVED_LEVEL	(-1)

static NAMADR_LIST *debug_peer_list;
static int saved_level = UNUSED_SAVED_LEVEL;

/* debug_peer_init - initialize */

void    debug_peer_init(void)
{
    const char *myname = "debug_peer_init";

    /*
     * Sanity check.
     */
    if (debug_peer_list)
	msg_panic("%s: repeated call", myname);
    if (var_debug_peer_list == 0)
	msg_panic("%s: uninitialized %s", myname, VAR_DEBUG_PEER_LIST);
    if (var_debug_peer_level <= 0)
	msg_fatal("%s: %s <= 0", myname, VAR_DEBUG_PEER_LEVEL);

    /*
     * Finally.
     */
    if (*var_debug_peer_list)
	debug_peer_list =
	    namadr_list_init(VAR_DEBUG_PEER_LIST, MATCH_FLAG_RETURN
			     | match_parent_style(VAR_DEBUG_PEER_LIST),
			     var_debug_peer_list);
}

/* debug_peer_check - see if this peer needs verbose logging */

int     debug_peer_check(const char *name, const char *addr)
{

    /*
     * Crank up the noise when this peer is listed.
     */
    if (debug_peer_list != 0
	&& saved_level == UNUSED_SAVED_LEVEL
	&& namadr_list_match(debug_peer_list, name, addr) != 0) {
	saved_level = msg_verbose;
	msg_verbose += var_debug_peer_level;
	return (1);
    }
    return (0);
}

/* debug_peer_restore - restore logging level */

void    debug_peer_restore(void)
{
    if (saved_level != UNUSED_SAVED_LEVEL) {
	msg_verbose = saved_level;
	saved_level = UNUSED_SAVED_LEVEL;
    }
}