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