2 * $Id: uams_pam.c,v 1.17 2005-04-28 20:49:50 bfernhomberg 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 */
18 #endif /* HAVE_UNISTD_H */
23 #else /* STDC_HEADERS */
27 #endif /* HAVE_STRCHR */
28 char *strchr (), *strrchr ();
30 #define memcpy(d,s,n) bcopy ((s), (d), (n))
31 #define memmove(d,s,n) bcopy ((s), (d), (n))
32 #endif /* ! HAVE_MEMCPY */
33 #endif /* STDC_HEADERS */
35 #include <atalk/logger.h>
37 #ifdef HAVE_SECURITY_PAM_APPL_H
38 #include <security/pam_appl.h>
40 #ifdef HAVE_PAM_PAM_APPL_H
41 #include <pam/pam_appl.h>
44 #include <atalk/afp.h>
45 #include <atalk/uam.h>
46 #include <atalk/util.h>
51 #define MIN(a,b) ((a) < (b) ? (a) : (b))
55 /* Static variables used to communicate between the conversation function
56 * and the server_login function
58 static pam_handle_t *pamh = NULL;
59 static char *hostname;
60 static char *PAM_username;
61 static char *PAM_password;
63 extern void append(void *, const char *, int);
65 /* PAM conversation function
66 * Here we assume (for now, at least) that echo on means login name, and
67 * echo off means password.
69 static int PAM_conv (int num_msg,
70 const struct pam_message **msg,
71 struct pam_response **resp,
72 void *appdata_ptr _U_)
74 struct pam_response *reply;
77 #define COPY_STRING(s) (s) ? strdup(s) : NULL
82 reply = (struct pam_response *)
83 calloc(num_msg, sizeof(struct pam_response));
88 for (count = 0; count < num_msg; count++) {
91 switch (msg[count]->msg_style) {
92 case PAM_PROMPT_ECHO_ON:
93 if (!(string = COPY_STRING(PAM_username)))
96 case PAM_PROMPT_ECHO_OFF:
97 if (!(string = COPY_STRING(PAM_password)))
101 #ifdef PAM_BINARY_PROMPT
102 case PAM_BINARY_PROMPT:
103 #endif /* PAM_BINARY_PROMPT */
112 reply[count].resp_retcode = 0;
113 reply[count].resp = string;
122 for (count = 0; count < num_msg; count++) {
123 if (!reply[count].resp)
125 switch (msg[count]->msg_style) {
126 case PAM_PROMPT_ECHO_OFF:
127 case PAM_PROMPT_ECHO_ON:
128 free(reply[count].resp);
136 static struct pam_conv PAM_conversation = {
141 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
142 char *ibuf, int ibuflen _U_,
143 char *rbuf _U_, int *rbuflen _U_)
148 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
149 (void *) &hostname, NULL) < 0)
151 LOG(log_info, logtype_uams, "uams_pam.c :PAM: unable to retrieve client hostname");
155 ibuf[ PASSWDLEN ] = '\0';
157 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
161 LOG(log_info, logtype_uams, "cleartext login: %s", username);
162 PAM_username = username;
163 PAM_password = ibuf; /* Set these things up for the conv function */
165 err = AFPERR_NOTAUTH;
166 PAM_error = pam_start("netatalk", username, &PAM_conversation,
168 if (PAM_error != PAM_SUCCESS)
171 pam_set_item(pamh, PAM_TTY, "afpd");
172 pam_set_item(pamh, PAM_RHOST, hostname);
173 /* use PAM_DISALLOW_NULL_AUTHTOK if passwdminlen > 0 */
174 PAM_error = pam_authenticate(pamh,0);
175 if (PAM_error != PAM_SUCCESS) {
176 if (PAM_error == PAM_MAXTRIES)
177 err = AFPERR_PWDEXPR;
181 PAM_error = pam_acct_mgmt(pamh, 0);
182 if (PAM_error != PAM_SUCCESS) {
183 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* Password change required */
184 err = AFPERR_PWDEXPR;
185 #ifdef PAM_AUTHTOKEN_REQD
186 else if (PAM_error == PAM_AUTHTOKEN_REQD)
187 err = AFPERR_PWDCHNG;
188 #endif /* PAM_AUTHTOKEN_REQD */
193 #ifndef PAM_CRED_ESTABLISH
194 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
195 #endif /* PAM_CRED_ESTABLISH */
196 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
197 if (PAM_error != PAM_SUCCESS)
200 PAM_error = pam_open_session(pamh, 0);
201 if (PAM_error != PAM_SUCCESS)
206 if (err == AFPERR_PWDEXPR)
212 pam_end(pamh, PAM_error);
217 /* --------------------------
220 static int pam_login(void *obj, struct passwd **uam_pwd,
221 char *ibuf, int ibuflen,
222 char *rbuf, int *rbuflen)
229 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
233 len = (unsigned char) *ibuf++;
235 return( AFPERR_PARAM );
238 memcpy(username, ibuf, len );
241 username[ len ] = '\0';
243 if ((unsigned long) ibuf & 1) /* pad character */
245 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
248 /* ----------------------------- */
249 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
250 char *ibuf, int ibuflen,
251 char *rbuf, int *rbuflen)
259 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0)
265 memcpy(&temp16, uname, sizeof(temp16));
268 if (!len || len > ulen ) {
269 return( AFPERR_PARAM );
271 memcpy(username, uname +2, len );
272 username[ len ] = '\0';
274 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
278 static void pam_logout() {
279 pam_close_session(pamh, 0);
285 static int pam_changepw(void *obj _U_, char *username,
286 struct passwd *pwd _U_, char *ibuf, int ibuflen _U_,
287 char *rbuf _U_, int *rbuflen _U_)
289 char pw[PASSWDLEN + 1];
295 memcpy(pw, ibuf, PASSWDLEN);
296 memset(ibuf, 0, PASSWDLEN);
297 pw[PASSWDLEN] = '\0';
299 /* let's do a quick check for the same password */
300 if (memcmp(pw, ibuf + PASSWDLEN, PASSWDLEN) == 0)
301 return AFPERR_PWDSAME;
303 /* Set these things up for the conv function */
304 PAM_username = username;
307 PAM_error = pam_start("netatalk", username, &PAM_conversation,
309 if (PAM_error != PAM_SUCCESS)
311 pam_set_item(lpamh, PAM_TTY, "afpd");
312 pam_set_item(lpamh, PAM_RHOST, hostname);
314 /* we might need to do this as root */
317 PAM_error = pam_authenticate(lpamh,0);
318 if (PAM_error != PAM_SUCCESS) {
320 pam_end(lpamh, PAM_error);
321 return AFPERR_NOTAUTH;
327 ibuf[PASSWDLEN] = '\0';
329 /* this really does need to be done as root */
330 PAM_error = pam_chauthtok(lpamh, 0);
331 seteuid(uid); /* un-root ourselves. */
332 memset(pw, 0, PASSWDLEN);
333 memset(ibuf, 0, PASSWDLEN);
334 if (PAM_error != PAM_SUCCESS) {
335 pam_end(lpamh, PAM_error);
336 return AFPERR_ACCESS;
344 /* Printer ClearTxtUAM login */
345 int pam_printer(start, stop, username, out)
346 char *start, *stop, *username;
351 char password[PASSWDLEN + 1] = "\0";
352 static const char *loginok = "0\r";
355 data = (char *)malloc(stop - start + 1);
357 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
361 strlcpy(data, start, stop - start + 1);
363 /* We are looking for the following format in data:
364 * (username) (password)
366 * Let's hope username doesn't contain ") ("!
369 /* Parse input for username in () */
370 if ((p = strchr(data, '(' )) == NULL) {
371 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
376 if ((q = strstr(p, ") (" )) == NULL) {
377 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
381 memcpy(username, p, MIN(UAM_USERNAMELEN, q - p) );
383 /* Parse input for password in next () */
385 if ((q = strrchr(p, ')' )) == NULL) {
386 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
390 memcpy(password, p, MIN(PASSWDLEN, (q - p)) );
392 /* Done copying username and password, clean up */
395 if (( pwd = uam_getname(NULL, username, strlen(username))) == NULL ) {
396 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
401 if (uam_checkuser(pwd) < 0) {
402 /* syslog of error happens in uam_checkuser */
406 PAM_username = username;
407 PAM_password = password;
409 PAM_error = pam_start("netatalk", username, &PAM_conversation,
411 if (PAM_error != PAM_SUCCESS) {
412 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
413 username, pam_strerror(pamh, PAM_error));
414 pam_end(pamh, PAM_error);
419 pam_set_item(pamh, PAM_TTY, "papd");
420 pam_set_item(pamh, PAM_RHOST, hostname);
421 PAM_error = pam_authenticate(pamh,0);
422 if (PAM_error != PAM_SUCCESS) {
423 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
424 username, pam_strerror(pamh, PAM_error));
425 pam_end(pamh, PAM_error);
430 PAM_error = pam_acct_mgmt(pamh, 0);
431 if (PAM_error != PAM_SUCCESS) {
432 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
433 username, pam_strerror(pamh, PAM_error));
434 pam_end(pamh, PAM_error);
439 PAM_error = pam_open_session(pamh, 0);
440 if (PAM_error != PAM_SUCCESS) {
441 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
442 username, pam_strerror(pamh, PAM_error));
443 pam_end(pamh, PAM_error);
448 /* Login successful, but no need to hang onto it,
449 so logout immediately */
450 append(out, loginok, strlen(loginok));
451 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
452 pam_close_session(pamh, 0);
460 static int uam_setup(const char *path)
462 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
463 pam_login, NULL, pam_logout, pam_login_ext) < 0)
466 if (uam_register(UAM_SERVER_CHANGEPW, path, "Cleartxt Passwrd",
468 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
472 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
480 static void uam_cleanup(void)
482 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
483 uam_unregister(UAM_SERVER_CHANGEPW, "Cleartxt Passwrd");
484 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
487 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
490 uam_setup, uam_cleanup
493 UAM_MODULE_EXPORT struct uam_export uams_pam = {
496 uam_setup, uam_cleanup