2 * $Id: uams_pam.c,v 1.24 2010-03-30 10:25:49 franklahm Exp $
4 * Copyright (c) 1990,1993 Regents of The University of Michigan.
5 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
6 * All Rights Reserved. See COPYRIGHT.
11 #endif /* HAVE_CONFIG_H */
17 #endif /* HAVE_UNISTD_H */
19 #ifdef HAVE_SECURITY_PAM_APPL_H
20 #include <security/pam_appl.h>
22 #ifdef HAVE_PAM_PAM_APPL_H
23 #include <pam/pam_appl.h>
25 #include <arpa/inet.h>
27 #include <atalk/afp.h>
28 #include <atalk/uam.h>
29 #include <atalk/util.h>
30 #include <atalk/logger.h>
31 #include <atalk/compat.h>
36 #define MIN(a,b) ((a) < (b) ? (a) : (b))
40 /* Static variables used to communicate between the conversation function
41 * and the server_login function
43 static pam_handle_t *pamh = NULL;
44 static const char *hostname;
45 static char *PAM_username;
46 static char *PAM_password;
48 /*XXX in etc/papd/file.h */
50 extern UAM_MODULE_EXPORT void append(struct papfile *, const char *, int);
52 /* PAM conversation function
53 * Here we assume (for now, at least) that echo on means login name, and
54 * echo off means password.
56 static int PAM_conv (int num_msg,
57 const struct pam_message **msg,
58 struct pam_response **resp,
59 void *appdata_ptr _U_)
61 struct pam_response *reply;
64 #define COPY_STRING(s) (s) ? strdup(s) : NULL
69 reply = (struct pam_response *)
70 calloc(num_msg, sizeof(struct pam_response));
75 for (count = 0; count < num_msg; count++) {
78 switch (msg[count]->msg_style) {
79 case PAM_PROMPT_ECHO_ON:
80 if (!(string = COPY_STRING(PAM_username)))
83 case PAM_PROMPT_ECHO_OFF:
84 if (!(string = COPY_STRING(PAM_password)))
88 #ifdef PAM_BINARY_PROMPT
89 case PAM_BINARY_PROMPT:
90 #endif /* PAM_BINARY_PROMPT */
99 reply[count].resp_retcode = 0;
100 reply[count].resp = string;
109 for (count = 0; count < num_msg; count++) {
110 if (!reply[count].resp)
112 switch (msg[count]->msg_style) {
113 case PAM_PROMPT_ECHO_OFF:
114 case PAM_PROMPT_ECHO_ON:
115 free(reply[count].resp);
123 static struct pam_conv PAM_conversation = {
128 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
129 char *ibuf, size_t ibuflen _U_,
130 char *rbuf _U_, size_t *rbuflen _U_)
135 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
136 (void *) &hostname, NULL) < 0)
138 LOG(log_info, logtype_uams, "uams_pam.c :PAM: unable to retrieve client hostname");
142 ibuf[ PASSWDLEN ] = '\0';
144 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
145 return AFPERR_NOTAUTH;
148 LOG(log_info, logtype_uams, "cleartext login: %s", username);
149 PAM_username = username;
150 PAM_password = ibuf; /* Set these things up for the conv function */
152 err = AFPERR_NOTAUTH;
153 PAM_error = pam_start("netatalk", username, &PAM_conversation,
155 if (PAM_error != PAM_SUCCESS)
158 pam_set_item(pamh, PAM_TTY, "afpd");
159 pam_set_item(pamh, PAM_RHOST, hostname);
160 /* use PAM_DISALLOW_NULL_AUTHTOK if passwdminlen > 0 */
161 PAM_error = pam_authenticate(pamh,0);
162 if (PAM_error != PAM_SUCCESS) {
163 if (PAM_error == PAM_MAXTRIES)
164 err = AFPERR_PWDEXPR;
168 PAM_error = pam_acct_mgmt(pamh, 0);
169 if (PAM_error != PAM_SUCCESS) {
170 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* Password change required */
171 err = AFPERR_PWDEXPR;
172 #ifdef PAM_AUTHTOKEN_REQD
173 else if (PAM_error == PAM_AUTHTOKEN_REQD)
174 err = AFPERR_PWDCHNG;
175 #endif /* PAM_AUTHTOKEN_REQD */
180 #ifndef PAM_CRED_ESTABLISH
181 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
182 #endif /* PAM_CRED_ESTABLISH */
183 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
184 if (PAM_error != PAM_SUCCESS)
187 PAM_error = pam_open_session(pamh, 0);
188 if (PAM_error != PAM_SUCCESS)
193 if (err == AFPERR_PWDEXPR)
199 pam_end(pamh, PAM_error);
204 /* --------------------------
207 static int pam_login(void *obj, struct passwd **uam_pwd,
208 char *ibuf, size_t ibuflen,
209 char *rbuf, size_t *rbuflen)
216 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
220 len = (unsigned char) *ibuf++;
222 return( AFPERR_PARAM );
225 memcpy(username, ibuf, len );
228 username[ len ] = '\0';
230 if ((unsigned long) ibuf & 1) /* pad character */
232 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
235 /* ----------------------------- */
236 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
237 char *ibuf, size_t ibuflen,
238 char *rbuf, size_t *rbuflen)
246 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0)
252 memcpy(&temp16, uname, sizeof(temp16));
255 if (!len || len > ulen ) {
256 return( AFPERR_PARAM );
258 memcpy(username, uname +2, len );
259 username[ len ] = '\0';
261 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
265 static void pam_logout(void) {
266 pam_close_session(pamh, 0);
272 static int pam_changepw(void *obj _U_, char *username,
273 struct passwd *pwd _U_, char *ibuf, size_t ibuflen _U_,
274 char *rbuf _U_, size_t *rbuflen _U_)
276 char pw[PASSWDLEN + 1];
282 memcpy(pw, ibuf, PASSWDLEN);
283 memset(ibuf, 0, PASSWDLEN);
284 pw[PASSWDLEN] = '\0';
286 /* let's do a quick check for the same password */
287 if (memcmp(pw, ibuf + PASSWDLEN, PASSWDLEN) == 0)
288 return AFPERR_PWDSAME;
290 /* Set these things up for the conv function */
291 PAM_username = username;
294 PAM_error = pam_start("netatalk", username, &PAM_conversation,
296 if (PAM_error != PAM_SUCCESS)
298 pam_set_item(lpamh, PAM_TTY, "afpd");
299 pam_set_item(lpamh, PAM_RHOST, hostname);
301 /* we might need to do this as root */
304 PAM_error = pam_authenticate(lpamh,0);
305 if (PAM_error != PAM_SUCCESS) {
307 pam_end(lpamh, PAM_error);
308 return AFPERR_NOTAUTH;
314 ibuf[PASSWDLEN] = '\0';
316 /* this really does need to be done as root */
317 PAM_error = pam_chauthtok(lpamh, 0);
318 seteuid(uid); /* un-root ourselves. */
319 memset(pw, 0, PASSWDLEN);
320 memset(ibuf, 0, PASSWDLEN);
321 if (PAM_error != PAM_SUCCESS) {
322 pam_end(lpamh, PAM_error);
323 return AFPERR_ACCESS;
331 /* Printer ClearTxtUAM login */
332 static int pam_printer(char *start, char *stop, char *username, struct papfile *out)
336 char password[PASSWDLEN + 1] = "\0";
337 static const char *loginok = "0\r";
340 data = (char *)malloc(stop - start + 1);
342 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
346 strlcpy(data, start, stop - start + 1);
348 /* We are looking for the following format in data:
349 * (username) (password)
351 * Let's hope username doesn't contain ") ("!
354 /* Parse input for username in () */
355 if ((p = strchr(data, '(' )) == NULL) {
356 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
361 if ((q = strstr(p, ") (" )) == NULL) {
362 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
366 memcpy(username, p, MIN(UAM_USERNAMELEN, q - p) );
368 /* Parse input for password in next () */
370 if ((q = strrchr(p, ')' )) == NULL) {
371 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
375 memcpy(password, p, MIN(PASSWDLEN, (q - p)) );
377 /* Done copying username and password, clean up */
380 if (( pwd = uam_getname(NULL, username, strlen(username))) == NULL ) {
381 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
386 if (uam_checkuser(pwd) < 0) {
387 /* syslog of error happens in uam_checkuser */
391 PAM_username = username;
392 PAM_password = password;
394 PAM_error = pam_start("netatalk", username, &PAM_conversation,
396 if (PAM_error != PAM_SUCCESS) {
397 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
398 username, pam_strerror(pamh, PAM_error));
399 pam_end(pamh, PAM_error);
404 pam_set_item(pamh, PAM_TTY, "papd");
405 pam_set_item(pamh, PAM_RHOST, hostname);
406 PAM_error = pam_authenticate(pamh,0);
407 if (PAM_error != PAM_SUCCESS) {
408 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
409 username, pam_strerror(pamh, PAM_error));
410 pam_end(pamh, PAM_error);
415 PAM_error = pam_acct_mgmt(pamh, 0);
416 if (PAM_error != PAM_SUCCESS) {
417 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
418 username, pam_strerror(pamh, PAM_error));
419 pam_end(pamh, PAM_error);
424 PAM_error = pam_open_session(pamh, 0);
425 if (PAM_error != PAM_SUCCESS) {
426 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
427 username, pam_strerror(pamh, PAM_error));
428 pam_end(pamh, PAM_error);
433 /* Login successful, but no need to hang onto it,
434 so logout immediately */
435 append(out, loginok, strlen(loginok));
436 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
437 pam_close_session(pamh, 0);
445 static int uam_setup(const char *path)
447 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
448 pam_login, NULL, pam_logout, pam_login_ext) < 0)
451 if (uam_register(UAM_SERVER_CHANGEPW, path, "Cleartxt Passwrd",
453 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
457 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
465 static void uam_cleanup(void)
467 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
468 uam_unregister(UAM_SERVER_CHANGEPW, "Cleartxt Passwrd");
469 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
472 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
475 uam_setup, uam_cleanup
478 UAM_MODULE_EXPORT struct uam_export uams_pam = {
481 uam_setup, uam_cleanup