Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libpam / libpam / include / security / _pam_macros.h
1 #ifndef PAM_MACROS_H
2 #define PAM_MACROS_H
3
4 /*
5  * All kind of macros used by PAM, but usable in some other
6  * programs too.
7  * Organized by Cristian Gafton <gafton@redhat.com>
8  * $FreeBSD: src/contrib/libpam/libpam/include/security/_pam_macros.h,v 1.1.1.1.6.2 2001/06/11 15:28:14 markm Exp $
9  */
10
11 /* a 'safe' version of strdup */
12
13 extern char *strdup(const char *s);
14 #define  x_strdup(s)  ( (s) ? strdup(s):NULL )
15
16 /* Good policy to strike out passwords with some characters not just
17    free the memory */
18
19 #define _pam_overwrite(x)        \
20 do {                             \
21      register char *__xx__;      \
22      if ((__xx__=(x)))           \
23           while (*__xx__)        \
24                *__xx__++ = '\0'; \
25 } while (0)
26
27 /*
28  * Don't just free it, forget it too.
29  */
30
31 #define _pam_drop(X) \
32 do {                 \
33     if (X) {         \
34         free(X);     \
35         X=NULL;      \
36     }                \
37 } while (0)
38
39 #define _pam_drop_reply(/* struct pam_response * */ reply, /* int */ replies) \
40 do {                                              \
41     int reply_i;                                  \
42                                                   \
43     for (reply_i=0; reply_i<replies; ++reply_i) { \
44         if (reply[reply_i].resp) {                \
45             _pam_overwrite(reply[reply_i].resp);  \
46             free(reply[reply_i].resp);            \
47         }                                         \
48     }                                             \
49     if (reply)                                    \
50         free(reply);                              \
51 } while (0)
52
53 /* some debugging code */
54
55 #ifdef DEBUG
56
57 /*
58  * This provides the necessary function to do debugging in PAM.
59  * Cristian Gafton <gafton@redhat.com>
60  */
61
62 #include <stdio.h>
63 #include <sys/types.h>
64 #include <stdarg.h>
65 #include <stdlib.h>
66 #include <errno.h>
67
68 /*
69  * This is for debugging purposes ONLY. DO NOT use on live systems !!!
70  * You have been warned :-) - CG
71  *
72  * to get automated debugging to the log file, it must be created manually.
73  * _PAM_LOGFILE must exist, mode 666
74  */
75
76 #ifndef _PAM_LOGFILE
77 #define _PAM_LOGFILE "/tmp/pam-debug.log"
78 #endif
79
80 static void _pam_output_debug_info(const char *file, const char *fn
81                                    , const int line)
82 {
83     FILE *logfile;
84     int must_close = 1;
85     
86     if (!(logfile = fopen(_PAM_LOGFILE,"a"))) {
87         logfile = stderr;
88         must_close = 0;
89     }
90     fprintf(logfile,"[%s:%s(%d)] ",file, fn, line);
91     if (must_close) {
92         fflush(logfile);
93         fclose(logfile);
94     }
95 }
96
97 static void _pam_output_debug(const char *format, ...)
98 {
99     va_list args;
100     FILE *logfile;
101     int must_close = 1;
102     
103     va_start(args, format);
104
105     if (!(logfile = fopen(_PAM_LOGFILE,"a"))) {
106         logfile = stderr;
107         must_close = 0;
108     }
109     vfprintf(logfile, format, args);
110     fprintf(logfile, "\n");
111     if (must_close) {
112         fflush(logfile);
113         fclose(logfile);
114     }
115
116     va_end(args);
117 }
118
119 #define D(x) do { \
120     _pam_output_debug_info(__FILE__, __FUNCTION__, __LINE__); \
121     _pam_output_debug x ; \
122 } while (0)
123
124 #define _pam_show_mem(X,XS) do {                                      \
125       int i;                                                          \
126       register unsigned char *x;                                      \
127       x = (unsigned char *)X;                                         \
128       fprintf(stderr, "  <start at %p>\n", X);                        \
129       for (i = 0; i < XS ; ++x, ++i) {                                \
130           fprintf(stderr, "    %02X. <%p:%02X>\n", i, x, *x);         \
131       }                                                               \
132       fprintf(stderr, "  <end for %p after %d bytes>\n", X, XS);      \
133 } while (0)
134
135 #define _pam_show_reply(/* struct pam_response * */reply, /* int */replies) \
136 do {                                                                        \
137     int reply_i;                                                            \
138     setbuf(stderr, NULL);                                                   \
139     fprintf(stderr, "array at %p of size %d\n",reply,replies);              \
140     fflush(stderr);                                                         \
141     if (reply) {                                                            \
142         for (reply_i = 0; reply_i < replies; reply_i++) {                   \
143             fprintf(stderr, "  elem# %d at %p: resp = %p, retcode = %d\n",  \
144                     reply_i, reply+reply_i, reply[reply_i].resp,            \
145                     reply[reply_i].resp, _retcode);                         \
146             fflush(stderr);                                                 \
147             if (reply[reply_i].resp) {                                      \
148                 fprintf(stderr, "    resp[%d] = '%s'\n",                    \
149                         strlen(reply[reply_i].resp), reply[reply_i].resp);  \
150                 fflush(stderr);                                             \
151             }                                                               \
152         }                                                                   \
153     }                                                                       \
154     fprintf(stderr, "done here\n");                                         \
155     fflush(stderr);                                                         \
156 } while (0)
157
158 #else
159
160 #define D(x)                             do { } while (0)
161 #define _pam_show_mem(X,XS)              do { } while (0)
162 #define _pam_show_reply(reply, replies)  do { } while (0)
163
164 #endif /* DEBUG */
165
166 #endif  /* PAM_MACROS_H */