2 * pam_log.c -- PAM system logging
10 # include <features.h>
19 #include "pam_private.h"
29 # define VA_LOCAL_DECL va_list ap;
30 # define VA_START(f) va_start(ap, f)
31 # define VA_END va_end(ap)
37 # define VA_LOCAL_DECL va_list ap;
38 # define VA_START(f) va_start(ap)
39 # define VA_END va_end(ap)
40 # endif /* __STDC__ */
41 /**************************************************************
42 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
43 * A bombproof version of doprnt (dopr) included.
44 * Sigh. This sort of thing is always nasty do deal with. Note that
45 * the version here does not include floating point...
47 * snprintf() is used instead of sprintf() as it does limit checks
48 * for string length. This covers a nasty loophole.
50 * The other functions are there to prevent NULL pointers from
51 * causing nast effects.
52 **************************************************************/
60 snprintf(char *str, size_t count, const char *fmt, ...)
62 snprintf(str, count, fmt, va_alist)
67 # endif /* __STDC__ */
73 len = vsnprintf(str, count, fmt, ap);
81 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
83 vsnprintf(str, count, fmt, args)
88 # endif /* __STDC__ */
91 end = str + count - 1;
92 dopr( str, fmt, args );
99 * dopr(): poor man's version of doprintf
102 static void fmtstr __P((char *value, int ljust, int len, int zpad,
104 static void fmtnum __P((long value, int base, int dosign, int ljust, int len,
106 static void dostr __P(( char * , int ));
108 static void dopr_outch __P(( int c ));
112 dopr(char * buffer, const char * format, va_list args )
113 # else /* __STDC__ */
114 dopr( buffer, format, args )
118 # endif /* __STDC__ */
131 while( (ch = *format++) ){
134 ljust = len = zpad = maxwidth = 0;
135 longflag = pointflag = 0;
140 dostr( "**end of format**" , 0);
142 case '-': ljust = 1; goto nextch;
143 case '0': /* set zero padding if len not set */
144 if(len==0 && !pointflag) zpad = '0';
145 case '1': case '2': case '3':
146 case '4': case '5': case '6':
147 case '7': case '8': case '9':
149 maxwidth = maxwidth*10 + ch - '0';
151 len = len*10 + ch - '0';
155 maxwidth = va_arg( args, int );
157 len = va_arg( args, int );
159 case '.': pointflag = 1; goto nextch;
160 case 'l': longflag = 1; goto nextch;
162 /*fmtnum(value,base,dosign,ljust,len,zpad) */
164 value = va_arg( args, long );
166 value = va_arg( args, int );
168 fmtnum( value, 10,0, ljust, len, zpad ); break;
170 /*fmtnum(value,base,dosign,ljust,len,zpad) */
172 value = va_arg( args, long );
174 value = va_arg( args, int );
176 fmtnum( value, 8,0, ljust, len, zpad ); break;
179 value = va_arg( args, long );
181 value = va_arg( args, int );
183 fmtnum( value, 10,1, ljust, len, zpad ); break;
186 value = va_arg( args, long );
188 value = va_arg( args, int );
190 fmtnum( value, 16,0, ljust, len, zpad ); break;
193 value = va_arg( args, long );
195 value = va_arg( args, int );
197 fmtnum( value,-16,0, ljust, len, zpad ); break;
199 strvalue = va_arg( args, char *);
200 if (maxwidth > 0 || !pointflag) {
201 if (pointflag && len > maxwidth)
202 len = maxwidth; /* Adjust padding */
203 fmtstr( strvalue,ljust,len,zpad, maxwidth);
207 ch = va_arg( args, int );
208 dopr_outch( ch ); break;
209 case '%': dopr_outch( ch ); continue;
211 dostr( "???????" , 0);
223 fmtstr( value, ljust, len, zpad, maxwidth )
225 int ljust, len, zpad, maxwidth;
227 int padlen, strlen; /* amount to pad */
232 for( strlen = 0; value[strlen]; ++ strlen ); /* strlen */
233 if (strlen > maxwidth && maxwidth)
235 padlen = len - strlen;
236 if( padlen < 0 ) padlen = 0;
237 if( ljust ) padlen = -padlen;
238 while( padlen > 0 ) {
242 dostr( value, maxwidth );
243 while( padlen < 0 ) {
250 fmtnum( value, base, dosign, ljust, len, zpad )
252 int base, dosign, ljust, len, zpad;
255 unsigned long uvalue;
258 int padlen = 0; /* amount to pad */
261 /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
262 value, base, dosign, ljust, len, zpad )); */
276 (caps? "0123456789ABCDEF":"0123456789abcdef")
277 [uvalue % (unsigned)base ];
278 uvalue = (uvalue / (unsigned)base );
281 padlen = len - place;
282 if( padlen < 0 ) padlen = 0;
283 if( ljust ) padlen = -padlen;
284 /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
285 convert,place,signvalue,padlen)); */
286 if( zpad && padlen > 0 ){
288 dopr_outch( signvalue );
297 while( padlen > 0 ) {
301 if( signvalue ) dopr_outch( signvalue );
302 while( place > 0 ) dopr_outch( convert[--place] );
315 while(*str && cut-- > 0) dopr_outch(*str++);
317 while(*str) dopr_outch(*str++);
325 if( end == 0 || output < end )
331 vsyslog(int priority, const char *fmt, ...)
332 # else /* __STDC__ */
333 vsyslog(priority, fmt, va_alist)
337 # endif /* __STDC__ */
344 vsnprintf(logbuf, BUFSIZ, fmt, ap);
345 syslog(priority, "%s", logbuf);
351 /* internal logging function */
353 void _pam_system_log(int priority, const char *format, ... )
358 D(("pam_system_log called"));
360 if (format == NULL) {
361 D(("NULL format to _pam_system_log() call"));
365 va_start(args, format);
367 eformat = malloc(sizeof(_PAM_SYSTEM_LOG_PREFIX)+strlen(format));
368 if (eformat != NULL) {
369 strcpy(eformat, _PAM_SYSTEM_LOG_PREFIX);
370 strcpy(eformat + sizeof(_PAM_SYSTEM_LOG_PREFIX) - 1, format);
371 vsyslog(priority, eformat, args);
372 _pam_overwrite(eformat);
375 vsyslog(priority, format, args);