Remove extra whitespace at the end of some lines.
[dragonfly.git] / contrib / libpam / libpamc / include / security / pam_client.h
1 /*
2  * $Id: pam_client.h,v 1.4 2001/01/20 22:29:47 agmorgan Exp $
3  *
4  * Copyright (c) 1999 Andrew G. Morgan <morgan@linux.kernel.org>
5  *
6  * This header file provides the prototypes for the PAM client API
7  */
8
9 #ifndef PAM_CLIENT_H
10 #define PAM_CLIENT_H
11
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdio.h>
15
16 /* opaque agent handling structure */
17
18 typedef struct pamc_handle_s *pamc_handle_t;
19
20 /* binary prompt structure pointer */
21 #ifndef __u32
22 # define __u32  unsigned int
23 #endif
24 #ifndef __u8
25 # define __u8  unsigned char
26 #endif
27 typedef struct { __u32 length; __u8 control; } *pamc_bp_t;
28
29 /*
30  * functions provided by libpamc
31  */
32
33 /*
34  * Initialize the agent abstraction library
35  */
36
37 pamc_handle_t pamc_start(void);
38
39 /*
40  * Terminate the authentication process
41  */
42
43 int pamc_end(pamc_handle_t *pch);
44
45 /*
46  * force the loading of a specified agent
47  */
48
49 int pamc_load(pamc_handle_t pch, const char *agent_id);
50
51 /*
52  * Single conversation interface for binary prompts
53  */
54
55 int pamc_converse(pamc_handle_t pch, pamc_bp_t *prompt_p);
56
57 /*
58  * disable an agent
59  */
60
61 int pamc_disable(pamc_handle_t pch, const char *agent_id);
62
63 /*
64  * obtain a list of available agents
65  */
66
67 char **pamc_list_agents(pamc_handle_t pch);
68
69 /*
70  * PAM_BP_ MACROS for creating, destroying and manipulating binary prompts
71  */
72
73 #include <stdlib.h>
74 #include <stdio.h>
75 #include <unistd.h>
76
77 #ifndef PAM_BP_ASSERT
78 # define PAM_BP_ASSERT(x)   do { printf(__FILE__ "(%d): %s\n", \
79                                         __LINE__, x) ; exit(1); } while (0)
80 #endif /* PAM_BP_ASSERT */
81
82 #ifndef PAM_BP_CALLOC
83 # define PAM_BP_CALLOC      calloc
84 #endif /* PAM_BP_CALLOC */
85
86 #ifndef PAM_BP_FREE
87 # define PAM_BP_FREE        free
88 #endif /* PAM_BP_FREE */
89
90 #define __PAM_BP_WOCTET(x,y)  (*((y) + (__u8 *)(x)))
91 #define __PAM_BP_ROCTET(x,y)  (*((y) + (const __u8 *)(x)))
92
93 #define PAM_BP_MIN_SIZE       (sizeof(__u32) + sizeof(__u8))
94 #define PAM_BP_MAX_LENGTH     0x20000                   /* an advisory limit */
95 #define PAM_BP_WCONTROL(x)    (__PAM_BP_WOCTET(x,4))
96 #define PAM_BP_RCONTROL(x)    (__PAM_BP_ROCTET(x,4))
97 #define PAM_BP_SIZE(x)        ((__PAM_BP_ROCTET(x,0)<<24)+      \
98                                (__PAM_BP_ROCTET(x,1)<<16)+      \
99                                (__PAM_BP_ROCTET(x,2)<< 8)+      \
100                                (__PAM_BP_ROCTET(x,3)    ))
101 #define PAM_BP_LENGTH(x)      (PAM_BP_SIZE(x) - PAM_BP_MIN_SIZE)
102 #define PAM_BP_WDATA(x)       (PAM_BP_MIN_SIZE + (__u8 *) (x))
103 #define PAM_BP_RDATA(x)       (PAM_BP_MIN_SIZE + (const __u8 *) (x))
104
105 /* Note, this macro always '\0' terminates renewed packets */
106
107 #define PAM_BP_RENEW(old_p, cntrl, data_length)                            \
108 do {                                                                       \
109     if (old_p) {                                                           \
110         if (*(old_p)) {                                                    \
111             __u32 __size;                                                  \
112             __size = PAM_BP_SIZE(*(old_p));                                \
113             memset(*(old_p), 0, __size);                                   \
114             PAM_BP_FREE(*(old_p));                                         \
115         }                                                                  \
116         if (cntrl) {                                                       \
117             __u32 __size;                                                  \
118                                                                            \
119             __size = PAM_BP_MIN_SIZE + data_length;                        \
120             if ((*(old_p) = PAM_BP_CALLOC(1, 1+__size))) {                 \
121                 __PAM_BP_WOCTET(*(old_p), 3) =  __size      & 0xFF;        \
122                 __PAM_BP_WOCTET(*(old_p), 2) = (__size>>=8) & 0xFF;        \
123                 __PAM_BP_WOCTET(*(old_p), 1) = (__size>>=8) & 0xFF;        \
124                 __PAM_BP_WOCTET(*(old_p), 0) = (__size>>=8) & 0xFF;        \
125                 (*(old_p))->control = cntrl;                               \
126             } else {                                                       \
127                 PAM_BP_ASSERT("out of memory for binary prompt");          \
128             }                                                              \
129         } else {                                                           \
130             *old_p = NULL;                                                 \
131         }                                                                  \
132     } else {                                                               \
133         PAM_BP_ASSERT("programming error, invalid binary prompt pointer"); \
134     }                                                                      \
135 } while (0)
136
137 #define PAM_BP_FILL(prmpt, offset, length, data)                           \
138 do {                                                                       \
139     int bp_length;                                                         \
140     __u8 *prompt = (__u8 *) (prmpt);                                       \
141     bp_length = PAM_BP_LENGTH(prompt);                                     \
142     if (bp_length < ((length)+(offset))) {                                 \
143         PAM_BP_ASSERT("attempt to write over end of prompt");              \
144     }                                                                      \
145     memcpy((offset) + PAM_BP_WDATA(prompt), (data), (length));             \
146 } while (0)
147
148 #define PAM_BP_EXTRACT(prmpt, offset, length, data)                        \
149 do {                                                                       \
150     int __bp_length;                                                       \
151     const __u8 *__prompt = (const __u8 *) (prmpt);                         \
152     __bp_length = PAM_BP_LENGTH(__prompt);                                 \
153     if (((offset) < 0) || (__bp_length < ((length)+(offset)))              \
154         || ((length) < 0)) {                                               \
155         PAM_BP_ASSERT("invalid extraction from prompt");                   \
156     }                                                                      \
157     memcpy((data), (offset) + PAM_BP_RDATA(__prompt), (length));           \
158 } while (0)
159
160
161 /* Control types */
162
163 #define PAM_BPC_FALSE   0
164 #define PAM_BPC_TRUE    1
165
166 #define PAM_BPC_OK      0x01   /* continuation packet   */
167 #define PAM_BPC_SELECT  0x02   /* initialization packet */
168 #define PAM_BPC_DONE    0x03   /* termination packet    */
169 #define PAM_BPC_FAIL    0x04   /* unable to execute     */
170
171 /* The following control characters are only legal for echanges
172    between an agent and a client (it is the responsibility of the
173    client to enforce this rule in the face of a rogue server): */
174
175 #define PAM_BPC_GETENV  0x41   /* obtain client env.var */
176 #define PAM_BPC_PUTENV  0x42   /* set client env.var    */
177 #define PAM_BPC_TEXT    0x43   /* display message       */
178 #define PAM_BPC_ERROR   0x44   /* display error message */
179 #define PAM_BPC_PROMPT  0x45   /* echo'd text prompt    */
180 #define PAM_BPC_PASS    0x46   /* non-echo'd text prompt*/
181
182 /* quick check for prompts that are legal for the client (by
183    implication the server too) to send to libpamc */
184
185 #define PAM_BPC_FOR_CLIENT(/* pamc_bp_t */ prompt)                            \
186     (((prompt)->control <= PAM_BPC_FAIL && (prompt)->control >= PAM_BPC_OK)   \
187      ? PAM_BPC_TRUE:PAM_BPC_FALSE)
188
189
190 #endif /* PAM_CLIENT_H */