2 * pam_log.c -- PAM system logging
8 #include "pam_private.h"
22 # define VA_LOCAL_DECL va_list ap;
23 # define VA_START(f) va_start(ap, f)
24 # define VA_END va_end(ap)
30 # define VA_LOCAL_DECL va_list ap;
31 # define VA_START(f) va_start(ap)
32 # define VA_END va_end(ap)
33 # endif /* __STDC__ */
34 /**************************************************************
35 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
36 * A bombproof version of doprnt (dopr) included.
37 * Sigh. This sort of thing is always nasty do deal with. Note that
38 * the version here does not include floating point...
40 * snprintf() is used instead of sprintf() as it does limit checks
41 * for string length. This covers a nasty loophole.
43 * The other functions are there to prevent NULL pointers from
44 * causing nast effects.
45 **************************************************************/
53 snprintf(char *str, size_t count, const char *fmt, ...)
55 snprintf(str, count, fmt, va_alist)
60 # endif /* __STDC__ */
66 len = vsnprintf(str, count, fmt, ap);
74 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
76 vsnprintf(str, count, fmt, args)
81 # endif /* __STDC__ */
84 end = str + count - 1;
85 dopr( str, fmt, args );
92 * dopr(): poor man's version of doprintf
95 static void fmtstr __P((char *value, int ljust, int len, int zpad,
97 static void fmtnum __P((long value, int base, int dosign, int ljust, int len,
99 static void dostr __P(( char * , int ));
101 static void dopr_outch __P(( int c ));
105 dopr(char * buffer, const char * format, va_list args )
106 # else /* __STDC__ */
107 dopr( buffer, format, args )
111 # endif /* __STDC__ */
124 while( (ch = *format++) ){
127 ljust = len = zpad = maxwidth = 0;
128 longflag = pointflag = 0;
133 dostr( "**end of format**" , 0);
135 case '-': ljust = 1; goto nextch;
136 case '0': /* set zero padding if len not set */
137 if(len==0 && !pointflag) zpad = '0';
138 case '1': case '2': case '3':
139 case '4': case '5': case '6':
140 case '7': case '8': case '9':
142 maxwidth = maxwidth*10 + ch - '0';
144 len = len*10 + ch - '0';
148 maxwidth = va_arg( args, int );
150 len = va_arg( args, int );
152 case '.': pointflag = 1; goto nextch;
153 case 'l': longflag = 1; goto nextch;
155 /*fmtnum(value,base,dosign,ljust,len,zpad) */
157 value = va_arg( args, long );
159 value = va_arg( args, int );
161 fmtnum( value, 10,0, ljust, len, zpad ); break;
163 /*fmtnum(value,base,dosign,ljust,len,zpad) */
165 value = va_arg( args, long );
167 value = va_arg( args, int );
169 fmtnum( value, 8,0, ljust, len, zpad ); break;
172 value = va_arg( args, long );
174 value = va_arg( args, int );
176 fmtnum( value, 10,1, ljust, len, zpad ); break;
179 value = va_arg( args, long );
181 value = va_arg( args, int );
183 fmtnum( value, 16,0, 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;
192 strvalue = va_arg( args, char *);
193 if (maxwidth > 0 || !pointflag) {
194 if (pointflag && len > maxwidth)
195 len = maxwidth; /* Adjust padding */
196 fmtstr( strvalue,ljust,len,zpad, maxwidth);
200 ch = va_arg( args, int );
201 dopr_outch( ch ); break;
202 case '%': dopr_outch( ch ); continue;
204 dostr( "???????" , 0);
216 fmtstr( value, ljust, len, zpad, maxwidth )
218 int ljust, len, zpad, maxwidth;
220 int padlen, strlen; /* amount to pad */
225 for( strlen = 0; value[strlen]; ++ strlen ); /* strlen */
226 if (strlen > maxwidth && maxwidth)
228 padlen = len - strlen;
229 if( padlen < 0 ) padlen = 0;
230 if( ljust ) padlen = -padlen;
231 while( padlen > 0 ) {
235 dostr( value, maxwidth );
236 while( padlen < 0 ) {
243 fmtnum( value, base, dosign, ljust, len, zpad )
245 int base, dosign, ljust, len, zpad;
248 unsigned long uvalue;
251 int padlen = 0; /* amount to pad */
254 /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
255 value, base, dosign, ljust, len, zpad )); */
269 (caps? "0123456789ABCDEF":"0123456789abcdef")
270 [uvalue % (unsigned)base ];
271 uvalue = (uvalue / (unsigned)base );
274 padlen = len - place;
275 if( padlen < 0 ) padlen = 0;
276 if( ljust ) padlen = -padlen;
277 /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
278 convert,place,signvalue,padlen)); */
279 if( zpad && padlen > 0 ){
281 dopr_outch( signvalue );
290 while( padlen > 0 ) {
294 if( signvalue ) dopr_outch( signvalue );
295 while( place > 0 ) dopr_outch( convert[--place] );
308 while(*str && cut-- > 0) dopr_outch(*str++);
310 while(*str) dopr_outch(*str++);
318 if( end == 0 || output < end )
324 vsyslog(int priority, const char *fmt, ...)
325 # else /* __STDC__ */
326 vsyslog(priority, fmt, va_alist)
330 # endif /* __STDC__ */
337 vsnprintf(logbuf, BUFSIZ, fmt, ap);
338 syslog(priority, "%s", logbuf);
344 /* internal logging function */
346 void _pam_system_log(int priority, const char *format, ... )
351 D(("pam_system_log called"));
353 if (format == NULL) {
354 D(("NULL format to _pam_system_log() call"));
358 va_start(args, format);
360 eformat = malloc(sizeof(_PAM_SYSTEM_LOG_PREFIX)+strlen(format));
361 if (eformat != NULL) {
362 strcpy(eformat, _PAM_SYSTEM_LOG_PREFIX);
363 strcpy(eformat + sizeof(_PAM_SYSTEM_LOG_PREFIX) - 1, format);
364 vsyslog(priority, eformat, args);
365 _pam_overwrite(eformat);
368 vsyslog(priority, format, args);