2 * $Id: uams_pam.c,v 1.15.2.1.2.3 2003-10-30 00:21:47 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 #include <security/pam_appl.h>
39 #include <atalk/afp.h>
40 #include <atalk/uam.h>
45 #define MIN(a,b) ((a) < (b) ? (a) : (b))
49 /* Static variables used to communicate between the conversation function
50 * and the server_login function
52 static pam_handle_t *pamh = NULL;
53 static char *hostname;
54 static char *PAM_username;
55 static char *PAM_password;
57 extern void append(void *, const char *, int);
59 /* PAM conversation function
60 * Here we assume (for now, at least) that echo on means login name, and
61 * echo off means password.
63 static int PAM_conv (int num_msg,
64 const struct pam_message **msg,
65 struct pam_response **resp,
68 struct pam_response *reply;
71 #define COPY_STRING(s) (s) ? strdup(s) : NULL
76 reply = (struct pam_response *)
77 calloc(num_msg, sizeof(struct pam_response));
82 for (count = 0; count < num_msg; count++) {
85 switch (msg[count]->msg_style) {
86 case PAM_PROMPT_ECHO_ON:
87 if (!(string = COPY_STRING(PAM_username)))
90 case PAM_PROMPT_ECHO_OFF:
91 if (!(string = COPY_STRING(PAM_password)))
95 #ifdef PAM_BINARY_PROMPT
96 case PAM_BINARY_PROMPT:
97 #endif /* PAM_BINARY_PROMPT */
106 reply[count].resp_retcode = 0;
107 reply[count].resp = string;
116 for (count = 0; count < num_msg; count++) {
117 if (!reply[count].resp)
119 switch (msg[count]->msg_style) {
120 case PAM_PROMPT_ECHO_OFF:
121 case PAM_PROMPT_ECHO_ON:
122 free(reply[count].resp);
130 static struct pam_conv PAM_conversation = {
135 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
136 char *ibuf, int ibuflen,
137 char *rbuf, int *rbuflen)
142 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
143 (void *) &hostname, NULL) < 0)
145 LOG(log_info, logtype_uams, "uams_pam.c :PAM: unable to retrieve client hostname");
149 ibuf[ PASSWDLEN ] = '\0';
151 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
155 LOG(log_info, logtype_uams, "cleartext login: %s", username);
156 PAM_username = username;
157 PAM_password = ibuf; /* Set these things up for the conv function */
159 err = AFPERR_NOTAUTH;
160 PAM_error = pam_start("netatalk", username, &PAM_conversation,
162 if (PAM_error != PAM_SUCCESS)
165 pam_set_item(pamh, PAM_TTY, "afpd");
166 pam_set_item(pamh, PAM_RHOST, hostname);
167 /* use PAM_DISALLOW_NULL_AUTHTOK if passwdminlen > 0 */
168 PAM_error = pam_authenticate(pamh,0);
169 if (PAM_error != PAM_SUCCESS) {
170 if (PAM_error == PAM_MAXTRIES)
171 err = AFPERR_PWDEXPR;
175 PAM_error = pam_acct_mgmt(pamh, 0);
176 if (PAM_error != PAM_SUCCESS) {
177 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* Password change required */
178 err = AFPERR_PWDEXPR;
179 #ifdef PAM_AUTHTOKEN_REQD
180 else if (PAM_error == PAM_AUTHTOKEN_REQD)
181 err = AFPERR_PWDCHNG;
182 #endif /* PAM_AUTHTOKEN_REQD */
187 #ifndef PAM_CRED_ESTABLISH
188 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
189 #endif /* PAM_CRED_ESTABLISH */
190 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
191 if (PAM_error != PAM_SUCCESS)
194 PAM_error = pam_open_session(pamh, 0);
195 if (PAM_error != PAM_SUCCESS)
200 if (err == AFPERR_PWDEXPR)
206 pam_end(pamh, PAM_error);
211 /* --------------------------
214 static int pam_login(void *obj, struct passwd **uam_pwd,
215 char *ibuf, int ibuflen,
216 char *rbuf, int *rbuflen)
223 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
227 len = (unsigned char) *ibuf++;
229 return( AFPERR_PARAM );
232 memcpy(username, ibuf, len );
235 username[ len ] = '\0';
237 if ((unsigned long) ibuf & 1) /* pad character */
239 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
242 /* ----------------------------- */
243 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
244 char *ibuf, int ibuflen,
245 char *rbuf, int *rbuflen)
253 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0)
259 memcpy(&temp16, uname, sizeof(temp16));
262 if (!len || len > ulen ) {
263 return( AFPERR_PARAM );
265 memcpy(username, uname +2, len );
266 username[ len ] = '\0';
268 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
272 static void pam_logout() {
273 pam_close_session(pamh, 0);
279 static int pam_changepw(void *obj, char *username,
280 struct passwd *pwd, char *ibuf, int ibuflen,
281 char *rbuf, int *rbuflen)
283 char pw[PASSWDLEN + 1];
289 memcpy(pw, ibuf, PASSWDLEN);
290 memset(ibuf, 0, PASSWDLEN);
291 pw[PASSWDLEN] = '\0';
293 /* let's do a quick check for the same password */
294 if (memcmp(pw, ibuf + PASSWDLEN, PASSWDLEN) == 0)
295 return AFPERR_PWDSAME;
297 /* Set these things up for the conv function */
298 PAM_username = username;
301 PAM_error = pam_start("netatalk", username, &PAM_conversation,
303 if (PAM_error != PAM_SUCCESS)
305 pam_set_item(lpamh, PAM_TTY, "afpd");
306 pam_set_item(lpamh, PAM_RHOST, hostname);
308 /* we might need to do this as root */
311 PAM_error = pam_authenticate(lpamh,0);
312 if (PAM_error != PAM_SUCCESS) {
314 pam_end(lpamh, PAM_error);
315 return AFPERR_NOTAUTH;
321 ibuf[PASSWDLEN] = '\0';
323 /* this really does need to be done as root */
324 PAM_error = pam_chauthtok(lpamh, 0);
325 seteuid(uid); /* un-root ourselves. */
326 memset(pw, 0, PASSWDLEN);
327 memset(ibuf, 0, PASSWDLEN);
328 if (PAM_error != PAM_SUCCESS) {
329 pam_end(lpamh, PAM_error);
330 return AFPERR_ACCESS;
338 /* Printer ClearTxtUAM login */
339 int pam_printer(start, stop, username, out)
340 char *start, *stop, *username;
345 char password[PASSWDLEN + 1] = "\0";
346 static const char *loginok = "0\r";
349 data = (char *)malloc(stop - start + 2);
351 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
355 strncpy(data, start, stop - start + 1);
356 data[stop - start + 2] = 0;
358 /* We are looking for the following format in data:
359 * (username) (password)
361 * Let's hope username doesn't contain ") ("!
364 /* Parse input for username in () */
365 if ((p = strchr(data, '(' )) == NULL) {
366 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
371 if ((q = strstr(p, ") (" )) == NULL) {
372 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
376 strncpy(username, p, MIN(UAM_USERNAMELEN, q - p) );
377 username[ UAM_USERNAMELEN +1] = '\0';
379 /* Parse input for password in next () */
381 if ((q = strrchr(p, ')' )) == NULL) {
382 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
386 strncpy(password, p, MIN(PASSWDLEN, (q - p)) );
387 password[ PASSWDLEN + 1] = '\0';
389 /* Done copying username and password, clean up */
392 if (( pwd = uam_getname(NULL, username, strlen(username))) == NULL ) {
393 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
398 if (uam_checkuser(pwd) < 0) {
399 /* syslog of error happens in uam_checkuser */
403 PAM_username = username;
404 PAM_password = password;
406 PAM_error = pam_start("netatalk", username, &PAM_conversation,
408 if (PAM_error != PAM_SUCCESS) {
409 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
410 username, pam_strerror(pamh, PAM_error));
411 pam_end(pamh, PAM_error);
416 pam_set_item(pamh, PAM_TTY, "papd");
417 pam_set_item(pamh, PAM_RHOST, hostname);
418 PAM_error = pam_authenticate(pamh,0);
419 if (PAM_error != PAM_SUCCESS) {
420 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
421 username, pam_strerror(pamh, PAM_error));
422 pam_end(pamh, PAM_error);
427 PAM_error = pam_acct_mgmt(pamh, 0);
428 if (PAM_error != PAM_SUCCESS) {
429 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
430 username, pam_strerror(pamh, PAM_error));
431 pam_end(pamh, PAM_error);
436 PAM_error = pam_open_session(pamh, 0);
437 if (PAM_error != PAM_SUCCESS) {
438 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
439 username, pam_strerror(pamh, PAM_error));
440 pam_end(pamh, PAM_error);
445 /* Login successful, but no need to hang onto it,
446 so logout immediately */
447 append(out, loginok, strlen(loginok));
448 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
449 pam_close_session(pamh, 0);
457 static int uam_setup(const char *path)
459 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
460 pam_login, NULL, pam_logout, pam_login_ext) < 0)
463 if (uam_register(UAM_SERVER_CHANGEPW, path, "Cleartxt Passwrd",
465 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
469 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
477 static void uam_cleanup(void)
479 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
480 uam_unregister(UAM_SERVER_CHANGEPW, "Cleartxt Passwrd");
481 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
484 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
487 uam_setup, uam_cleanup
490 UAM_MODULE_EXPORT struct uam_export uams_pam = {
493 uam_setup, uam_cleanup