2 ** Copyright 2002 Double Precision, Inc. See COPYING for
3 ** distribution information.
7 #include "courierauthdebug.h"
14 static const char rcsid
[]="$Id: debug.c,v 1.7 2004/11/21 03:14:58 mrsam Exp $";
16 /* for internal use */
18 static int courier_authdebug( const char *ofmt
, const char *fmt
, va_list ap
);
23 ** 2 - debug auth + write out passwords
26 int courier_authdebug_login_level
= 0;
29 ** purpose: initialize debugging
30 ** function: read environment variable DEBUG_LOGIN
31 ** and set up debugging according to it
35 void courier_authdebug_login_init( void )
37 const char *p
=getenv(DEBUG_LOGIN_ENV
);
39 courier_authdebug_login_level
= atoi( p
? p
:"0" );
43 ** purpose: print debug message to logger
44 ** does nothing if debug level is zero
45 ** adds prefix "DEBUG: " and suffix "\n"
46 ** Since we have a return value, we can use the convenient production
47 ** courier_authdebug_login_level && courier_authdebug_printf(...)
48 ** (as a macro, saves function calls when debugging is disabled)
51 int courier_authdebug_printf( const char *fmt
, ... ) {
56 if (courier_authdebug_login_level
== 0) return 0;
58 rc
= courier_authdebug( "DEBUG: %s\n", fmt
, ap
);
64 * Print a string to stderr, replacing any control characters with dot and
65 * limiting the total message size, followed by a newline
68 int courier_safe_printf(const char *fmt
, ...)
74 rc
= courier_authdebug( "%s\n", fmt
, ap
);
79 /** Print error log message **/
81 int courier_auth_err( const char *fmt
, ... ) {
87 rc
= courier_authdebug( "ERR: %s\n", fmt
, ap
);
93 ** purpose: print debug messages to logger - handy use
94 ** function: take message with logging level and drop
95 ** messages with too high level.
96 ** also include into the message the IP address
98 ** * level - level to be compared with DEBUG_LOGIN env var.
99 ** * fmt - message format as like in printf().
100 ** * ... - and "arguments" for fmt
103 void courier_authdebug_login( int level
, const char *fmt
, ... ) {
108 /* logging severity */
110 if( level
> courier_authdebug_login_level
)
113 snprintf( ofmt
, sizeof ofmt
, "DEBUG: LOGIN: ip=[%s], %%s\n", getenv("TCPREMOTEIP") );
115 courier_authdebug( ofmt
, fmt
, ap
);
120 ** purpose: print debug messages to logger - general use
121 ** function: read format string and arguments
122 ** and convert them to suitable form for output.
124 ** ofmt- printf() format string for output, where %s = the assembled text
125 ** fmt - printf() format string for arguments
126 ** ... - variable arguments
129 static int courier_authdebug( const char *ofmt
, const char *fmt
, va_list ap
)
132 char buf
[DEBUG_MESSAGE_SIZE
];
136 /* print into buffer to be able to replace control and other unwanted chars. */
137 vsnprintf( buf
, DEBUG_MESSAGE_SIZE
, fmt
, ap
);
140 /* replace nonprintable chars by dot */
141 for( i
=0 ; i
<len
; i
++ )
142 if( !isprint(buf
[i
]) )
147 return fprintf( stderr
, ofmt
, buf
);
151 * Print the information retrieved from the database into struct authinfo.
153 * The structure members 'clearpasswd' and 'passwd' are not always set at
154 * the point where this function is called, so we take separate values
158 int courier_authdebug_authinfo(const char *pfx
, const struct authinfo
*auth
,
159 const char *clearpasswd
, const char *passwd
)
161 char uidstr
[32] = "<null>";
163 if (!courier_authdebug_login_level
) return 0;
166 snprintf(uidstr
, sizeof uidstr
, "%ld", (long)*auth
->sysuserid
);
167 fprintf(stderr
, "%ssysusername=%s, sysuserid=%s, "
168 "sysgroupid=%ld, homedir=%s, address=%s, fullname=%s, "
169 "maildir=%s, quota=%s, options=%s\n", pfx
,
170 auth
->sysusername
? auth
->sysusername
: "<null>",
171 uidstr
, (long)auth
->sysgroupid
,
172 auth
->homedir
? auth
->homedir
: "<null>",
173 auth
->address
? auth
->address
: "<null>",
174 auth
->fullname
? auth
->fullname
: "<null>",
175 auth
->maildir
? auth
->maildir
: "<null>",
176 auth
->quota
? auth
->quota
: "<null>",
177 auth
->options
? auth
->options
: "<null>");
178 if (courier_authdebug_login_level
>= 2)
179 fprintf(stderr
, "%sclearpasswd=%s, passwd=%s\n", pfx
,
180 clearpasswd
? clearpasswd
: "<null>",
181 passwd
? passwd
: "<null>");