1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
10 /* This module contains functions that call the PAM authentication mechanism
11 defined by Sun for Solaris and also available for Linux and other OS.
13 We can't just compile this code and allow the library mechanism to omit the
14 functions if they are not wanted, because we need to have the PAM headers
15 available for compiling. Therefore, compile these functions only if SUPPORT_PAM
16 is defined. However, some compilers don't like compiling empty modules, so keep
17 them happy with a dummy when skipping the rest. Make it reference itself to
18 stop picky compilers complaining that it is unused, and put in a dummy argument
19 to stop even pickier compilers complaining about infinite loops. */
22 static void dummy(int x) { dummy(x-1); }
23 #else /* SUPPORT_PAM */
26 #include <pam/pam_appl.h>
28 #include <security/pam_appl.h>
31 /* According to the specification, it should be possible to have an application
32 data pointer passed to the conversation function. However, I was unable to get
33 this to work on Solaris 2.6, so static variables are used instead. */
35 static int pam_conv_had_error;
36 static uschar *pam_args;
37 static BOOL pam_arg_ended;
41 /*************************************************
42 * PAM conversation function *
43 *************************************************/
45 /* This function is passed to the PAM authentication function, and it calls it
46 back when it wants data from the client. The string list is in pam_args. When
47 we reach the end, we pass back an empty string once. If this function is called
48 again, it will give an error response. This is protection against something
52 num_msg number of messages associated with the call
53 msg points to an array of length num_msg of pam_message structures
54 resp set to point to the response block, which has to be got by
56 appdata_ptr the application data pointer - not used because in Solaris
57 2.6 it always arrived in pam_converse() as NULL
59 Returns: a PAM return code
63 pam_converse (int num_msg, PAM_CONVERSE_ARG2_TYPE **msg,
64 struct pam_response **resp, void *appdata_ptr)
68 struct pam_response *reply;
70 if (pam_arg_ended) return PAM_CONV_ERR;
72 reply = malloc(sizeof(struct pam_response) * num_msg);
74 if (reply == NULL) return PAM_CONV_ERR;
76 for (i = 0; i < num_msg; i++)
79 switch (msg[i]->msg_style)
81 case PAM_PROMPT_ECHO_ON:
82 case PAM_PROMPT_ECHO_OFF:
83 arg = string_nextinlist(&pam_args, &sep, big_buffer, big_buffer_size);
89 reply[i].resp = CS string_copy_malloc(arg); /* PAM frees resp */
90 reply[i].resp_retcode = PAM_SUCCESS;
93 case PAM_TEXT_INFO: /* Just acknowledge messages */
95 reply[i].resp_retcode = PAM_SUCCESS;
99 default: /* Must be an error of some sort... */
101 pam_conv_had_error = TRUE;
112 /*************************************************
113 * Perform PAM authentication *
114 *************************************************/
116 /* This function calls the PAM authentication mechanism, passing over one or
120 s a colon-separated list of strings
121 errptr where to point an error message
123 Returns: OK if authentication succeeded
124 FAIL if authentication failed
125 ERROR some other error condition
129 auth_call_pam(uschar *s, uschar **errptr)
131 pam_handle_t *pamh = NULL;
132 struct pam_conv pamc;
137 /* Set up the input data structure: the address of the conversation function,
138 and a pointer to application data, which we don't use because I couldn't get it
139 to work under Solaris 2.6 - it always arrived in pam_converse() as NULL. */
141 pamc.conv = pam_converse;
142 pamc.appdata_ptr = NULL;
144 /* Initialize the static data - the current input data, the error flag, and the
145 flag for data end. */
148 pam_conv_had_error = FALSE;
149 pam_arg_ended = FALSE;
151 /* The first string in the list is the user. If this is an empty string, we
152 fail. PAM doesn't support authentication with an empty user (it prompts for it,
153 causing a potential mis-interpretation). */
155 user = string_nextinlist(&pam_args, &sep, big_buffer, big_buffer_size);
156 if (user == NULL || user[0] == 0) return FAIL;
158 /* Start off PAM interaction */
161 debug_printf("Running PAM authentication for user \"%s\"\n", user);
163 pam_error = pam_start ("exim", CS user, &pamc, &pamh);
165 /* Do the authentication - the pam_authenticate() will call pam_converse() to
166 get the data it wants. After successful authentication we call pam_acct_mgmt()
167 to apply any other restrictions (e.g. only some times of day). */
169 if (pam_error == PAM_SUCCESS)
171 pam_error = pam_authenticate (pamh, PAM_SILENT);
172 if (pam_error == PAM_SUCCESS && !pam_conv_had_error)
173 pam_error = pam_acct_mgmt (pamh, PAM_SILENT);
176 /* Finish the PAM interaction - this causes it to clean up store etc. Unclear
177 what should be passed as the second argument. */
179 pam_end(pamh, PAM_SUCCESS);
181 /* Sort out the return code. If not success, set the error message. */
183 if (pam_error == PAM_SUCCESS)
185 DEBUG(D_auth) debug_printf("PAM success\n");
189 *errptr = (uschar *)pam_strerror(pamh, pam_error);
190 DEBUG(D_auth) debug_printf("PAM error: %s\n", *errptr);
192 if (pam_error == PAM_USER_UNKNOWN ||
193 pam_error == PAM_AUTH_ERR ||
194 pam_error == PAM_ACCT_EXPIRED)
200 #endif /* SUPPORT_PAM */
202 /* End of call_pam.c */