2 ** Copyright 2002 Double Precision, Inc. See COPYING for
3 ** distribution information.
7 #include "courierauthdebug.h"
15 /* for internal use */
17 static int courier_authdebug( const char *ofmt
, const char *fmt
, va_list ap
);
22 ** 2 - debug auth + write out passwords
25 int courier_authdebug_login_level
= 0;
28 ** purpose: initialize debugging
29 ** function: read environment variable DEBUG_LOGIN
30 ** and set up debugging according to it
34 void courier_authdebug_login_init( void )
36 const char *p
=getenv(DEBUG_LOGIN_ENV
);
38 courier_authdebug_login_level
= atoi( p
? p
:"0" );
42 ** purpose: print debug message to logger
43 ** does nothing if debug level is zero
44 ** adds prefix "DEBUG: " and suffix "\n"
45 ** Since we have a return value, we can use the convenient production
46 ** courier_authdebug_login_level && courier_authdebug_printf(...)
47 ** (as a macro, saves function calls when debugging is disabled)
50 int courier_authdebug_printf( const char *fmt
, ... ) {
55 if (courier_authdebug_login_level
== 0) return 0;
57 rc
= courier_authdebug( "DEBUG: %s\n", fmt
, ap
);
63 * Print a string to stderr, replacing any control characters with dot and
64 * limiting the total message size, followed by a newline
67 int courier_safe_printf(const char *fmt
, ...)
73 rc
= courier_authdebug( "%s\n", fmt
, ap
);
78 /** Print error log message **/
80 int courier_auth_err( const char *fmt
, ... ) {
86 rc
= courier_authdebug( "ERR: %s\n", fmt
, ap
);
92 ** purpose: print debug messages to logger - handy use
93 ** function: take message with logging level and drop
94 ** messages with too high level.
95 ** also include into the message the IP address
97 ** * level - level to be compared with DEBUG_LOGIN env var.
98 ** * fmt - message format as like in printf().
99 ** * ... - and "arguments" for fmt
102 void courier_authdebug_login( int level
, const char *fmt
, ... ) {
107 /* logging severity */
109 if( level
> courier_authdebug_login_level
)
112 snprintf( ofmt
, sizeof ofmt
, "DEBUG: LOGIN: ip=[%s], %%s\n", getenv("TCPREMOTEIP") );
114 courier_authdebug( ofmt
, fmt
, ap
);
119 ** purpose: print debug messages to logger - general use
120 ** function: read format string and arguments
121 ** and convert them to suitable form for output.
123 ** ofmt- printf() format string for output, where %s = the assembled text
124 ** fmt - printf() format string for arguments
125 ** ... - variable arguments
128 static int courier_authdebug( const char *ofmt
, const char *fmt
, va_list ap
)
131 char buf
[DEBUG_MESSAGE_SIZE
];
135 /* print into buffer to be able to replace control and other unwanted chars. */
136 vsnprintf( buf
, DEBUG_MESSAGE_SIZE
, fmt
, ap
);
139 /* replace nonprintable chars by dot */
140 for( i
=0 ; i
<len
; i
++ )
141 if( !isprint(buf
[i
]) )
146 return fprintf( stderr
, ofmt
, buf
);
150 * Print the information retrieved from the database into struct authinfo.
152 * The structure members 'clearpasswd' and 'passwd' are not always set at
153 * the point where this function is called, so we take separate values
157 int courier_authdebug_authinfo(const char *pfx
, const struct authinfo
*auth
,
158 const char *clearpasswd
, const char *passwd
)
160 char uidstr
[32] = "<null>";
162 if (!courier_authdebug_login_level
) return 0;
165 snprintf(uidstr
, sizeof uidstr
, "%ld", (long)*auth
->sysuserid
);
166 fprintf(stderr
, "%ssysusername=%s, sysuserid=%s, "
167 "sysgroupid=%ld, homedir=%s, address=%s, fullname=%s, "
168 "maildir=%s, quota=%s, options=%s\n", pfx
,
169 auth
->sysusername
? auth
->sysusername
: "<null>",
170 uidstr
, (long)auth
->sysgroupid
,
171 auth
->homedir
? auth
->homedir
: "<null>",
172 auth
->address
? auth
->address
: "<null>",
173 auth
->fullname
? auth
->fullname
: "<null>",
174 auth
->maildir
? auth
->maildir
: "<null>",
175 auth
->quota
? auth
->quota
: "<null>",
176 auth
->options
? auth
->options
: "<null>");
177 if (courier_authdebug_login_level
>= 2)
178 fprintf(stderr
, "%sclearpasswd=%s, passwd=%s\n", pfx
,
179 clearpasswd
? clearpasswd
: "<null>",
180 passwd
? passwd
: "<null>");