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 $
6 * $DragonFly: src/contrib/libpam/libpam/Attic/pam_log.c,v 1.2 2003/06/17 04:24:03 dillon Exp $
15 #include "pam_private.h"
25 # define VA_LOCAL_DECL va_list ap;
26 # define VA_START(f) va_start(ap, f)
27 # define VA_END va_end(ap)
33 # define VA_LOCAL_DECL va_list ap;
34 # define VA_START(f) va_start(ap)
35 # define VA_END va_end(ap)
36 # endif /* __STDC__ */
37 /**************************************************************
38 * Patrick Powell Tue Apr 11 09:48:21 PDT 1995
39 * A bombproof version of doprnt (dopr) included.
40 * Sigh. This sort of thing is always nasty do deal with. Note that
41 * the version here does not include floating point...
43 * snprintf() is used instead of sprintf() as it does limit checks
44 * for string length. This covers a nasty loophole.
46 * The other functions are there to prevent NULL pointers from
47 * causing nast effects.
48 **************************************************************/
56 snprintf(char *str, size_t count, const char *fmt, ...)
58 snprintf(str, count, fmt, va_alist)
63 # endif /* __STDC__ */
69 len = vsnprintf(str, count, fmt, ap);
77 vsnprintf(char *str, size_t count, const char *fmt, va_list args)
79 vsnprintf(str, count, fmt, args)
84 # endif /* __STDC__ */
87 end = str + count - 1;
88 dopr( str, fmt, args );
95 * dopr(): poor man's version of doprintf
98 static void fmtstr __P((char *value, int ljust, int len, int zpad,
100 static void fmtnum __P((long value, int base, int dosign, int ljust, int len,
102 static void dostr __P(( char * , int ));
104 static void dopr_outch __P(( int c ));
108 dopr(char * buffer, const char * format, va_list args )
109 # else /* __STDC__ */
110 dopr( buffer, format, args )
114 # endif /* __STDC__ */
127 while( (ch = *format++) ){
130 ljust = len = zpad = maxwidth = 0;
131 longflag = pointflag = 0;
136 dostr( "**end of format**" , 0);
138 case '-': ljust = 1; goto nextch;
139 case '0': /* set zero padding if len not set */
140 if(len==0 && !pointflag) zpad = '0';
141 case '1': case '2': case '3':
142 case '4': case '5': case '6':
143 case '7': case '8': case '9':
145 maxwidth = maxwidth*10 + ch - '0';
147 len = len*10 + ch - '0';
151 maxwidth = va_arg( args, int );
153 len = va_arg( args, int );
155 case '.': pointflag = 1; goto nextch;
156 case 'l': longflag = 1; goto nextch;
158 /*fmtnum(value,base,dosign,ljust,len,zpad) */
160 value = va_arg( args, long );
162 value = va_arg( args, int );
164 fmtnum( value, 10,0, ljust, len, zpad ); break;
166 /*fmtnum(value,base,dosign,ljust,len,zpad) */
168 value = va_arg( args, long );
170 value = va_arg( args, int );
172 fmtnum( value, 8,0, ljust, len, zpad ); break;
175 value = va_arg( args, long );
177 value = va_arg( args, int );
179 fmtnum( value, 10,1, ljust, len, zpad ); break;
182 value = va_arg( args, long );
184 value = va_arg( args, int );
186 fmtnum( value, 16,0, ljust, len, zpad ); break;
189 value = va_arg( args, long );
191 value = va_arg( args, int );
193 fmtnum( value,-16,0, ljust, len, zpad ); break;
195 strvalue = va_arg( args, char *);
196 if (maxwidth > 0 || !pointflag) {
197 if (pointflag && len > maxwidth)
198 len = maxwidth; /* Adjust padding */
199 fmtstr( strvalue,ljust,len,zpad, maxwidth);
203 ch = va_arg( args, int );
204 dopr_outch( ch ); break;
205 case '%': dopr_outch( ch ); continue;
207 dostr( "???????" , 0);
219 fmtstr( value, ljust, len, zpad, maxwidth )
221 int ljust, len, zpad, maxwidth;
223 int padlen, strlen; /* amount to pad */
228 for( strlen = 0; value[strlen]; ++ strlen ); /* strlen */
229 if (strlen > maxwidth && maxwidth)
231 padlen = len - strlen;
232 if( padlen < 0 ) padlen = 0;
233 if( ljust ) padlen = -padlen;
234 while( padlen > 0 ) {
238 dostr( value, maxwidth );
239 while( padlen < 0 ) {
246 fmtnum( value, base, dosign, ljust, len, zpad )
248 int base, dosign, ljust, len, zpad;
251 unsigned long uvalue;
254 int padlen = 0; /* amount to pad */
257 /* DEBUGP(("value 0x%x, base %d, dosign %d, ljust %d, len %d, zpad %d\n",
258 value, base, dosign, ljust, len, zpad )); */
272 (caps? "0123456789ABCDEF":"0123456789abcdef")
273 [uvalue % (unsigned)base ];
274 uvalue = (uvalue / (unsigned)base );
277 padlen = len - place;
278 if( padlen < 0 ) padlen = 0;
279 if( ljust ) padlen = -padlen;
280 /* DEBUGP(( "str '%s', place %d, sign %c, padlen %d\n",
281 convert,place,signvalue,padlen)); */
282 if( zpad && padlen > 0 ){
284 dopr_outch( signvalue );
293 while( padlen > 0 ) {
297 if( signvalue ) dopr_outch( signvalue );
298 while( place > 0 ) dopr_outch( convert[--place] );
311 while(*str && cut-- > 0) dopr_outch(*str++);
313 while(*str) dopr_outch(*str++);
321 if( end == 0 || output < end )
327 vsyslog(int priority, const char *fmt, ...)
328 # else /* __STDC__ */
329 vsyslog(priority, fmt, va_alist)
333 # endif /* __STDC__ */
340 vsnprintf(logbuf, BUFSIZ, fmt, ap);
341 syslog(priority, "%s", logbuf);
347 void pam_vsystem_log(const pam_handle_t *pamh,
348 const struct pam_log_state *log_state,
349 int priority, const char *format, va_list args)
352 int option, facility;
354 D(("pam_vsystem_log called"));
357 /* make sure we have a log state to use */
358 if (NULL == log_state) {
359 if (NULL != pamh && NULL != pamh->pam_default_log.ident) {
360 ident = pamh->pam_default_log.ident;
361 option = pamh->pam_default_log.option;
362 facility = pamh->pam_default_log.facility;
364 ident = PAM_LOG_STATE_IDENT;
365 option = PAM_LOG_STATE_OPTION;
366 facility = PAM_LOG_STATE_FACILITY;
368 openlog(ident, option, facility);
370 openlog(log_state->ident, log_state->option, log_state->facility);
374 vsyslog(priority, format, args);
382 void pam_system_log(const pam_handle_t *pamh,
383 const struct pam_log_state *log_state,
384 int priority, const char *format, ... )
387 int option, facility;
390 D(("pam_system_log called"));
393 /* make sure we have a log state to use */
394 if (NULL == log_state) {
395 if (NULL != pamh && NULL != pamh->pam_default_log.ident) {
396 ident = pamh->pam_default_log.ident;
397 option = pamh->pam_default_log.option;
398 facility = pamh->pam_default_log.facility;
400 ident = PAM_LOG_STATE_IDENT;
401 option = PAM_LOG_STATE_OPTION;
402 facility = PAM_LOG_STATE_FACILITY;
404 openlog(ident, option, facility);
406 openlog(log_state->ident, log_state->option, log_state->facility);
410 va_start(args, format);
411 vsyslog(priority, format, args);
421 * Recommended #defines to make porting legacy apps easier [Ed. at this
422 * point, the syslog() #define is breoken -- suggestions?]
424 * #ifdef PAM_LOG_STATE
425 * # define openlog(ident, option, facility) { \
426 * struct pam_log_state tmp_state; \
427 * tmp_state.ident = ident; \
428 * tmp_state.option = option; \
429 * tmp_state.facility = facility; \
430 * (void) pam_set_item(pamh, PAM_LOG_STATE, &tmp_state); \
432 * # define syslog pam_system_log
433 * # define closelog()