2 * $Id: uams_pam.c,v 1.15.2.1 2003-09-03 20:40:50 didg 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 /* PAM conversation function
58 * Here we assume (for now, at least) that echo on means login name, and
59 * echo off means password.
61 static int PAM_conv (int num_msg,
62 const struct pam_message **msg,
63 struct pam_response **resp,
66 struct pam_response *reply;
69 #define COPY_STRING(s) (s) ? strdup(s) : NULL
74 reply = (struct pam_response *)
75 calloc(num_msg, sizeof(struct pam_response));
80 for (count = 0; count < num_msg; count++) {
83 switch (msg[count]->msg_style) {
84 case PAM_PROMPT_ECHO_ON:
85 if (!(string = COPY_STRING(PAM_username)))
88 case PAM_PROMPT_ECHO_OFF:
89 if (!(string = COPY_STRING(PAM_password)))
93 #ifdef PAM_BINARY_PROMPT
94 case PAM_BINARY_PROMPT:
95 #endif /* PAM_BINARY_PROMPT */
104 reply[count].resp_retcode = 0;
105 reply[count].resp = string;
114 for (count = 0; count < num_msg; count++) {
115 if (!reply[count].resp)
117 switch (msg[count]->msg_style) {
118 case PAM_PROMPT_ECHO_OFF:
119 case PAM_PROMPT_ECHO_ON:
120 free(reply[count].resp);
128 static struct pam_conv PAM_conversation = {
133 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
134 char *ibuf, int ibuflen,
135 char *rbuf, int *rbuflen)
140 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
141 (void *) &hostname, NULL) < 0)
143 LOG(log_info, logtype_uams, "uams_pam.c :PAM: unable to retrieve client hostname");
147 ibuf[ PASSWDLEN ] = '\0';
149 if (( pwd = uam_getname(username, ulen)) == NULL ) {
153 LOG(log_info, logtype_uams, "cleartext login: %s", username);
154 PAM_username = username;
155 PAM_password = ibuf; /* Set these things up for the conv function */
157 err = AFPERR_NOTAUTH;
158 PAM_error = pam_start("netatalk", username, &PAM_conversation,
160 if (PAM_error != PAM_SUCCESS)
163 pam_set_item(pamh, PAM_TTY, "afpd");
164 pam_set_item(pamh, PAM_RHOST, hostname);
165 /* use PAM_DISALLOW_NULL_AUTHTOK if passwdminlen > 0 */
166 PAM_error = pam_authenticate(pamh,0);
167 if (PAM_error != PAM_SUCCESS) {
168 if (PAM_error == PAM_MAXTRIES)
169 err = AFPERR_PWDEXPR;
173 PAM_error = pam_acct_mgmt(pamh, 0);
174 if (PAM_error != PAM_SUCCESS) {
175 if (PAM_error == PAM_ACCT_EXPIRED)
176 err = AFPERR_PWDEXPR;
177 #ifdef PAM_AUTHTOKEN_REQD
178 else if (PAM_error == PAM_AUTHTOKEN_REQD)
179 err = AFPERR_PWDCHNG;
180 #endif /* PAM_AUTHTOKEN_REQD */
184 #ifndef PAM_CRED_ESTABLISH
185 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
186 #endif /* PAM_CRED_ESTABLISH */
187 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
188 if (PAM_error != PAM_SUCCESS)
191 PAM_error = pam_open_session(pamh, 0);
192 if (PAM_error != PAM_SUCCESS)
199 pam_end(pamh, PAM_error);
204 /* --------------------------
207 static int pam_login(void *obj, struct passwd **uam_pwd,
208 char *ibuf, int ibuflen,
209 char *rbuf, int *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';
229 if ((unsigned long) ibuf & 1) /* pad character */
231 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
234 /* ----------------------------- */
235 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
236 char *ibuf, int ibuflen,
237 char *rbuf, int *rbuflen)
245 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0)
251 memcpy(&temp16, uname, sizeof(temp16));
254 if (!len || len > ulen ) {
255 return( AFPERR_PARAM );
257 memcpy(username, uname +2, len );
258 username[ len ] = '\0';
260 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
264 static void pam_logout() {
265 pam_close_session(pamh, 0);
271 static int pam_changepw(void *obj, char *username,
272 struct passwd *pwd, char *ibuf, int ibuflen,
273 char *rbuf, int *rbuflen)
275 char pw[PASSWDLEN + 1];
281 memcpy(pw, ibuf, PASSWDLEN);
282 memset(ibuf, 0, PASSWDLEN);
283 pw[PASSWDLEN] = '\0';
285 /* let's do a quick check for the same password */
286 if (memcmp(pw, ibuf + PASSWDLEN, PASSWDLEN) == 0)
287 return AFPERR_PWDSAME;
289 /* Set these things up for the conv function */
290 PAM_username = username;
293 PAM_error = pam_start("netatalk", username, &PAM_conversation,
295 if (PAM_error != PAM_SUCCESS)
297 pam_set_item(lpamh, PAM_TTY, "afpd");
298 pam_set_item(lpamh, PAM_RHOST, hostname);
300 /* we might need to do this as root */
303 PAM_error = pam_authenticate(lpamh,0);
304 if (PAM_error != PAM_SUCCESS) {
306 pam_end(lpamh, PAM_error);
307 return AFPERR_NOTAUTH;
313 ibuf[PASSWDLEN] = '\0';
315 /* this really does need to be done as root */
316 PAM_error = pam_chauthtok(lpamh, 0);
317 seteuid(uid); /* un-root ourselves. */
318 memset(pw, 0, PASSWDLEN);
319 memset(ibuf, 0, PASSWDLEN);
320 if (PAM_error != PAM_SUCCESS) {
321 pam_end(lpamh, PAM_error);
322 return AFPERR_ACCESS;
330 /* Printer ClearTxtUAM login */
331 int pam_printer(start, stop, username, out)
332 char *start, *stop, *username;
337 char password[PASSWDLEN + 1] = "\0";
338 static const char *loginok = "0\r";
341 data = (char *)malloc(stop - start + 2);
343 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
347 strncpy(data, start, stop - start + 1);
348 data[stop - start + 2] = 0;
350 /* We are looking for the following format in data:
351 * (username) (password)
353 * Let's hope username doesn't contain ") ("!
356 /* Parse input for username in () */
357 if ((p = strchr(data, '(' )) == NULL) {
358 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
363 if ((q = strstr(p, ") (" )) == NULL) {
364 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
368 strncpy(username, p, MIN(UAM_USERNAMELEN, q - p) );
369 username[ UAM_USERNAMELEN +1] = '\0';
371 /* Parse input for password in next () */
373 if ((q = strrchr(p, ')' )) == NULL) {
374 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
378 strncpy(password, p, MIN(PASSWDLEN, (q - p)) );
379 password[ PASSWDLEN + 1] = '\0';
381 /* Done copying username and password, clean up */
384 if (( pwd = uam_getname(username, strlen(username))) == NULL ) {
385 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
390 if (uam_checkuser(pwd) < 0) {
391 /* syslog of error happens in uam_checkuser */
395 PAM_username = username;
396 PAM_password = password;
398 PAM_error = pam_start("netatalk", username, &PAM_conversation,
400 if (PAM_error != PAM_SUCCESS) {
401 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
402 username, pam_strerror(pamh, PAM_error));
403 pam_end(pamh, PAM_error);
408 pam_set_item(pamh, PAM_TTY, "papd");
409 pam_set_item(pamh, PAM_RHOST, hostname);
410 PAM_error = pam_authenticate(pamh,0);
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_error = pam_acct_mgmt(pamh, 0);
420 if (PAM_error != PAM_SUCCESS) {
421 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
422 username, pam_strerror(pamh, PAM_error));
423 pam_end(pamh, PAM_error);
428 PAM_error = pam_open_session(pamh, 0);
429 if (PAM_error != PAM_SUCCESS) {
430 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
431 username, pam_strerror(pamh, PAM_error));
432 pam_end(pamh, PAM_error);
437 /* Login successful, but no need to hang onto it,
438 so logout immediately */
439 append(out, loginok, strlen(loginok));
440 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
441 pam_close_session(pamh, 0);
449 static int uam_setup(const char *path)
451 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
452 pam_login, NULL, pam_logout, pam_login_ext) < 0)
455 if (uam_register(UAM_SERVER_CHANGEPW, path, "Cleartxt Passwrd",
457 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
461 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
469 static void uam_cleanup(void)
471 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
472 uam_unregister(UAM_SERVER_CHANGEPW, "Cleartxt Passwrd");
473 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
476 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
479 uam_setup, uam_cleanup
482 UAM_MODULE_EXPORT struct uam_export uams_pam = {
485 uam_setup, uam_cleanup