2 * pam_log.c -- PAM system logging
5 * $FreeBSD: src/contrib/libpam/libpam/pam_log.c,v 1.2.6.2 2001/06/11 15:28:12 markm Exp $
14 #include "pam_private.h"
24 # define VA_LOCAL_DECL va_list ap;
25 # define VA_START(f) va_start(ap, f)
26 # define VA_END va_end(ap)
32 # define VA_LOCAL_DECL va_list ap;
33 # define VA_START(f) va_start(ap)
34 # define VA_END va_end(ap)
35 # endif /* __STDC__ */
36 /**************************************************************
37 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
38 * A bombproof version of doprnt (dopr) included.
39 * Sigh. This sort of thing is always nasty do deal with. Note that
40 * the version here does not include floating point...
42 * snprintf() is used instead of sprintf() as it does limit checks
43 * for string length. This covers a nasty loophole.
45 * The other functions are there to prevent NULL pointers from
46 * causing nast effects.
47 **************************************************************/
55 snprintf(char *str, size_t count, const char *fmt, ...)
57 snprintf(str, count, fmt, va_alist)
62 # endif /* __STDC__ */
68 len = vsnprintf(str, count, fmt, ap);
76 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
78 vsnprintf(str, count, fmt, args)
83 # endif /* __STDC__ */
86 end = str + count - 1;
87 dopr( str, fmt, args );
94 * dopr(): poor man's version of doprintf
97 static void fmtstr __P((char *value, int ljust, int len, int zpad,
99 static void fmtnum __P((long value, int base, int dosign, int ljust, int len,
101 static void dostr __P(( char * , int ));
103 static void dopr_outch __P(( int c ));
107 dopr(char * buffer, const char * format, va_list args )
108 # else /* __STDC__ */
109 dopr( buffer, format, args )
113 # endif /* __STDC__ */
126 while( (ch = *format++) ){
129 ljust = len = zpad = maxwidth = 0;
130 longflag = pointflag = 0;
135 dostr( "**end of format**" , 0);
137 case '-': ljust = 1; goto nextch;
138 case '0': /* set zero padding if len not set */
139 if(len==0 && !pointflag) zpad = '0';
140 case '1': case '2': case '3':
141 case '4': case '5': case '6':
142 case '7': case '8': case '9':
144 maxwidth = maxwidth*10 + ch - '0';
146 len = len*10 + ch - '0';
150 maxwidth = va_arg( args, int );
152 len = va_arg( args, int );
154 case '.': pointflag = 1; goto nextch;
155 case 'l': longflag = 1; goto nextch;
157 /*fmtnum(value,base,dosign,ljust,len,zpad) */
159 value = va_arg( args, long );
161 value = va_arg( args, int );
163 fmtnum( value, 10,0, ljust, len, zpad ); break;
165 /*fmtnum(value,base,dosign,ljust,len,zpad) */
167 value = va_arg( args, long );
169 value = va_arg( args, int );
171 fmtnum( value, 8,0, ljust, len, zpad ); break;
174 value = va_arg( args, long );
176 value = va_arg( args, int );
178 fmtnum( value, 10,1, ljust, len, zpad ); break;
181 value = va_arg( args, long );
183 value = va_arg( args, int );
185 fmtnum( value, 16,0, ljust, len, zpad ); break;
188 value = va_arg( args, long );
190 value = va_arg( args, int );
192 fmtnum( value,-16,0, ljust, len, zpad ); break;
194 strvalue = va_arg( args, char *);
195 if (maxwidth > 0 || !pointflag) {
196 if (pointflag && len > maxwidth)
197 len = maxwidth; /* Adjust padding */
198 fmtstr( strvalue,ljust,len,zpad, maxwidth);
202 ch = va_arg( args, int );
203 dopr_outch( ch ); break;
204 case '%': dopr_outch( ch ); continue;
206 dostr( "???????" , 0);
218 fmtstr( value, ljust, len, zpad, maxwidth )
220 int ljust, len, zpad, maxwidth;
222 int padlen, strlen; /* amount to pad */
227 for( strlen = 0; value[strlen]; ++ strlen ); /* strlen */
228 if (strlen > maxwidth && maxwidth)
230 padlen = len - strlen;
231 if( padlen < 0 ) padlen = 0;
232 if( ljust ) padlen = -padlen;
233 while( padlen > 0 ) {
237 dostr( value, maxwidth );
238 while( padlen < 0 ) {
245 fmtnum( value, base, dosign, ljust, len, zpad )
247 int base, dosign, ljust, len, zpad;
250 unsigned long uvalue;
253 int padlen = 0; /* amount to pad */
256 /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
257 value, base, dosign, ljust, len, zpad )); */
271 (caps? "0123456789ABCDEF":"0123456789abcdef")
272 [uvalue % (unsigned)base ];
273 uvalue = (uvalue / (unsigned)base );
276 padlen = len - place;
277 if( padlen < 0 ) padlen = 0;
278 if( ljust ) padlen = -padlen;
279 /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
280 convert,place,signvalue,padlen)); */
281 if( zpad && padlen > 0 ){
283 dopr_outch( signvalue );
292 while( padlen > 0 ) {
296 if( signvalue ) dopr_outch( signvalue );
297 while( place > 0 ) dopr_outch( convert[--place] );
310 while(*str && cut-- > 0) dopr_outch(*str++);
312 while(*str) dopr_outch(*str++);
320 if( end == 0 || output < end )
326 vsyslog(int priority, const char *fmt, ...)
327 # else /* __STDC__ */
328 vsyslog(priority, fmt, va_alist)
332 # endif /* __STDC__ */
339 vsnprintf(logbuf, BUFSIZ, fmt, ap);
340 syslog(priority, "%s", logbuf);
346 void pam_vsystem_log(const pam_handle_t *pamh,
347 const struct pam_log_state *log_state,
348 int priority, const char *format, va_list args)
351 int option, facility;
353 D(("pam_vsystem_log called"));
356 /* make sure we have a log state to use */
357 if (NULL == log_state) {
358 if (NULL != pamh && NULL != pamh->pam_default_log.ident) {
359 ident = pamh->pam_default_log.ident;
360 option = pamh->pam_default_log.option;
361 facility = pamh->pam_default_log.facility;
363 ident = PAM_LOG_STATE_IDENT;
364 option = PAM_LOG_STATE_OPTION;
365 facility = PAM_LOG_STATE_FACILITY;
367 openlog(ident, option, facility);
369 openlog(log_state->ident, log_state->option, log_state->facility);
373 vsyslog(priority, format, args);
381 void pam_system_log(const pam_handle_t *pamh,
382 const struct pam_log_state *log_state,
383 int priority, const char *format, ... )
386 int option, facility;
389 D(("pam_system_log called"));
392 /* make sure we have a log state to use */
393 if (NULL == log_state) {
394 if (NULL != pamh && NULL != pamh->pam_default_log.ident) {
395 ident = pamh->pam_default_log.ident;
396 option = pamh->pam_default_log.option;
397 facility = pamh->pam_default_log.facility;
399 ident = PAM_LOG_STATE_IDENT;
400 option = PAM_LOG_STATE_OPTION;
401 facility = PAM_LOG_STATE_FACILITY;
403 openlog(ident, option, facility);
405 openlog(log_state->ident, log_state->option, log_state->facility);
409 va_start(args, format);
410 vsyslog(priority, format, args);
420 * Recommended #defines to make porting legacy apps easier [Ed. at this
421 * point, the syslog() #define is breoken -- suggestions?]
423 * #ifdef PAM_LOG_STATE
424 * # define openlog(ident, option, facility) { \
425 * struct pam_log_state tmp_state; \
426 * tmp_state.ident = ident; \
427 * tmp_state.option = option; \
428 * tmp_state.facility = facility; \
429 * (void) pam_set_item(pamh, PAM_LOG_STATE, &tmp_state); \
431 * # define syslog pam_system_log
432 * # define closelog()