3 * Copyright (c) 1990,1993 Regents of The University of Michigan.
4 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
10 #endif /* HAVE_CONFIG_H */
16 #endif /* HAVE_UNISTD_H */
18 #ifdef HAVE_SECURITY_PAM_APPL_H
19 #include <security/pam_appl.h>
21 #ifdef HAVE_PAM_PAM_APPL_H
22 #include <pam/pam_appl.h>
24 #include <arpa/inet.h>
26 #include <atalk/afp.h>
27 #include <atalk/uam.h>
28 #include <atalk/util.h>
29 #include <atalk/logger.h>
30 #include <atalk/compat.h>
35 #define MIN(a,b) ((a) < (b) ? (a) : (b))
39 /* Static variables used to communicate between the conversation function
40 * and the server_login function
42 static pam_handle_t *pamh = NULL;
43 static const char *hostname;
44 static char *PAM_username;
45 static char *PAM_password;
47 /*XXX in etc/papd/file.h */
49 extern UAM_MODULE_EXPORT void append(struct papfile *, const char *, int);
51 /* PAM conversation function
52 * Here we assume (for now, at least) that echo on means login name, and
53 * echo off means password.
55 static int PAM_conv (int num_msg,
57 const struct pam_message **msg,
59 struct pam_message **msg,
61 struct pam_response **resp,
62 void *appdata_ptr _U_)
64 struct pam_response *reply;
67 #define COPY_STRING(s) (s) ? strdup(s) : NULL
72 reply = (struct pam_response *)
73 calloc(num_msg, sizeof(struct pam_response));
78 for (count = 0; count < num_msg; count++) {
81 switch (msg[count]->msg_style) {
82 case PAM_PROMPT_ECHO_ON:
83 if (!(string = COPY_STRING(PAM_username)))
86 case PAM_PROMPT_ECHO_OFF:
87 if (!(string = COPY_STRING(PAM_password)))
91 #ifdef PAM_BINARY_PROMPT
92 case PAM_BINARY_PROMPT:
93 #endif /* PAM_BINARY_PROMPT */
102 reply[count].resp_retcode = 0;
103 reply[count].resp = string;
112 for (count = 0; count < num_msg; count++) {
113 if (!reply[count].resp)
115 switch (msg[count]->msg_style) {
116 case PAM_PROMPT_ECHO_OFF:
117 case PAM_PROMPT_ECHO_ON:
118 free(reply[count].resp);
126 static struct pam_conv PAM_conversation = {
131 static int login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
132 char *ibuf, size_t ibuflen _U_,
133 char *rbuf _U_, size_t *rbuflen _U_)
138 if (uam_afpserver_option(obj, UAM_OPTION_CLIENTNAME,
139 (void *) &hostname, NULL) < 0)
141 LOG(log_info, logtype_uams, "uams_pam.c :PAM: unable to retrieve client hostname");
145 ibuf[ PASSWDLEN ] = '\0';
147 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
148 return AFPERR_NOTAUTH;
151 LOG(log_info, logtype_uams, "cleartext login: %s", username);
152 PAM_username = username;
153 PAM_password = ibuf; /* Set these things up for the conv function */
155 err = AFPERR_NOTAUTH;
156 PAM_error = pam_start("netatalk", username, &PAM_conversation,
158 if (PAM_error != PAM_SUCCESS)
161 pam_set_item(pamh, PAM_TTY, "afpd");
162 pam_set_item(pamh, PAM_RHOST, hostname);
163 /* use PAM_DISALLOW_NULL_AUTHTOK if passwdminlen > 0 */
164 PAM_error = pam_authenticate(pamh,0);
165 if (PAM_error != PAM_SUCCESS) {
166 if (PAM_error == PAM_MAXTRIES)
167 err = AFPERR_PWDEXPR;
171 PAM_error = pam_acct_mgmt(pamh, 0);
172 if (PAM_error != PAM_SUCCESS) {
173 if (PAM_error == PAM_NEW_AUTHTOK_REQD) /* Password change required */
174 err = AFPERR_PWDEXPR;
175 #ifdef PAM_AUTHTOKEN_REQD
176 else if (PAM_error == PAM_AUTHTOKEN_REQD)
177 err = AFPERR_PWDCHNG;
178 #endif /* PAM_AUTHTOKEN_REQD */
183 #ifndef PAM_CRED_ESTABLISH
184 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
185 #endif /* PAM_CRED_ESTABLISH */
186 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
187 if (PAM_error != PAM_SUCCESS)
190 PAM_error = pam_open_session(pamh, 0);
191 if (PAM_error != PAM_SUCCESS)
196 if (err == AFPERR_PWDEXPR)
202 pam_end(pamh, PAM_error);
207 /* --------------------------
210 static int pam_login(void *obj, struct passwd **uam_pwd,
211 char *ibuf, size_t ibuflen,
212 char *rbuf, size_t *rbuflen)
219 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
223 len = (unsigned char) *ibuf++;
225 return( AFPERR_PARAM );
228 memcpy(username, ibuf, len );
231 username[ len ] = '\0';
233 if ((unsigned long) ibuf & 1) /* pad character */
235 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
238 /* ----------------------------- */
239 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
240 char *ibuf, size_t ibuflen,
241 char *rbuf, size_t *rbuflen)
249 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0)
255 memcpy(&temp16, uname, sizeof(temp16));
258 if (!len || len > ulen ) {
259 return( AFPERR_PARAM );
261 memcpy(username, uname +2, len );
262 username[ len ] = '\0';
264 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
268 static void pam_logout(void) {
269 pam_close_session(pamh, 0);
275 static int pam_changepw(void *obj _U_, char *username,
276 struct passwd *pwd _U_, char *ibuf, size_t ibuflen _U_,
277 char *rbuf _U_, size_t *rbuflen _U_)
279 char pw[PASSWDLEN + 1];
285 memcpy(pw, ibuf, PASSWDLEN);
286 memset(ibuf, 0, PASSWDLEN);
287 pw[PASSWDLEN] = '\0';
289 /* let's do a quick check for the same password */
290 if (memcmp(pw, ibuf + PASSWDLEN, PASSWDLEN) == 0)
291 return AFPERR_PWDSAME;
293 /* Set these things up for the conv function */
294 PAM_username = username;
297 PAM_error = pam_start("netatalk", username, &PAM_conversation,
299 if (PAM_error != PAM_SUCCESS)
301 pam_set_item(lpamh, PAM_TTY, "afpd");
302 pam_set_item(lpamh, PAM_RHOST, hostname);
304 /* we might need to do this as root */
307 PAM_error = pam_authenticate(lpamh,0);
308 if (PAM_error != PAM_SUCCESS) {
310 pam_end(lpamh, PAM_error);
311 return AFPERR_NOTAUTH;
317 ibuf[PASSWDLEN] = '\0';
319 /* this really does need to be done as root */
320 PAM_error = pam_chauthtok(lpamh, 0);
321 seteuid(uid); /* un-root ourselves. */
322 memset(pw, 0, PASSWDLEN);
323 memset(ibuf, 0, PASSWDLEN);
324 if (PAM_error != PAM_SUCCESS) {
325 pam_end(lpamh, PAM_error);
326 return AFPERR_ACCESS;
334 /* Printer ClearTxtUAM login */
335 static int pam_printer(char *start, char *stop, char *username, struct papfile *out)
339 char password[PASSWDLEN + 1] = "\0";
340 static const char *loginok = "0\r";
343 data = (char *)malloc(stop - start + 1);
345 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
349 strlcpy(data, start, stop - start + 1);
351 /* We are looking for the following format in data:
352 * (username) (password)
354 * Let's hope username doesn't contain ") ("!
357 /* Parse input for username in () */
358 if ((p = strchr(data, '(' )) == NULL) {
359 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
364 if ((q = strstr(p, ") (" )) == NULL) {
365 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
369 memcpy(username, p, MIN(UAM_USERNAMELEN, q - p) );
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 memcpy(password, p, MIN(PASSWDLEN, (q - p)) );
380 /* Done copying username and password, clean up */
383 if (( pwd = uam_getname(NULL, username, strlen(username))) == NULL ) {
384 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
389 if (uam_checkuser(pwd) < 0) {
390 /* syslog of error happens in uam_checkuser */
394 PAM_username = username;
395 PAM_password = password;
397 PAM_error = pam_start("netatalk", username, &PAM_conversation,
399 if (PAM_error != PAM_SUCCESS) {
400 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
401 username, pam_strerror(pamh, PAM_error));
402 pam_end(pamh, PAM_error);
407 pam_set_item(pamh, PAM_TTY, "papd");
408 pam_set_item(pamh, PAM_RHOST, hostname);
409 PAM_error = pam_authenticate(pamh,0);
410 if (PAM_error != PAM_SUCCESS) {
411 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
412 username, pam_strerror(pamh, PAM_error));
413 pam_end(pamh, PAM_error);
418 PAM_error = pam_acct_mgmt(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_open_session(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 /* Login successful, but no need to hang onto it,
437 so logout immediately */
438 append(out, loginok, strlen(loginok));
439 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
440 pam_close_session(pamh, 0);
448 static int uam_setup(const char *path)
450 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
451 pam_login, NULL, pam_logout, pam_login_ext) < 0)
454 if (uam_register(UAM_SERVER_CHANGEPW, path, "Cleartxt Passwrd",
456 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
460 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
468 static void uam_cleanup(void)
470 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
471 uam_unregister(UAM_SERVER_CHANGEPW, "Cleartxt Passwrd");
472 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
475 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
478 uam_setup, uam_cleanup
481 UAM_MODULE_EXPORT struct uam_export uams_pam = {
484 uam_setup, uam_cleanup