2 * $Id: uams_pam.c,v 1.15 2003-01-08 22:16:25 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>
44 /* Static variables used to communicate between the conversation function
45 * and the server_login function
47 static pam_handle_t *pamh = NULL;
48 static char *hostname;
49 static char *PAM_username;
50 static char *PAM_password;
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,
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, int ibuflen,
130 char *rbuf, int *rbuflen)
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(username, ulen)) == NULL ) {
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_ACCT_EXPIRED)
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 */
179 #ifndef PAM_CRED_ESTABLISH
180 #define PAM_CRED_ESTABLISH PAM_ESTABLISH_CRED
181 #endif /* PAM_CRED_ESTABLISH */
182 PAM_error = pam_setcred(pamh, PAM_CRED_ESTABLISH);
183 if (PAM_error != PAM_SUCCESS)
186 PAM_error = pam_open_session(pamh, 0);
187 if (PAM_error != PAM_SUCCESS)
194 pam_end(pamh, PAM_error);
199 /* --------------------------
202 static int pam_login(void *obj, struct passwd **uam_pwd,
203 char *ibuf, int ibuflen,
204 char *rbuf, int *rbuflen)
211 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0) {
215 len = (unsigned char) *ibuf++;
217 return( AFPERR_PARAM );
220 memcpy(username, ibuf, len );
223 username[ len ] = '\0';
224 if ((unsigned long) ibuf & 1) /* pad character */
226 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
229 /* ----------------------------- */
230 static int pam_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
231 char *ibuf, int ibuflen,
232 char *rbuf, int *rbuflen)
240 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME, (void *) &username, &ulen) < 0)
246 memcpy(&temp16, uname, sizeof(temp16));
249 if (!len || len > ulen ) {
250 return( AFPERR_PARAM );
252 memcpy(username, uname +2, len );
253 username[ len ] = '\0';
255 return (login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
259 static void pam_logout() {
260 pam_close_session(pamh, 0);
266 static int pam_changepw(void *obj, char *username,
267 struct passwd *pwd, char *ibuf, int ibuflen,
268 char *rbuf, int *rbuflen)
270 char pw[PASSWDLEN + 1];
276 memcpy(pw, ibuf, PASSWDLEN);
277 memset(ibuf, 0, PASSWDLEN);
278 pw[PASSWDLEN] = '\0';
280 /* let's do a quick check for the same password */
281 if (memcmp(pw, ibuf + PASSWDLEN, PASSWDLEN) == 0)
282 return AFPERR_PWDSAME;
284 /* Set these things up for the conv function */
285 PAM_username = username;
288 PAM_error = pam_start("netatalk", username, &PAM_conversation,
290 if (PAM_error != PAM_SUCCESS)
292 pam_set_item(lpamh, PAM_TTY, "afpd");
293 pam_set_item(lpamh, PAM_RHOST, hostname);
295 /* we might need to do this as root */
298 PAM_error = pam_authenticate(lpamh,0);
299 if (PAM_error != PAM_SUCCESS) {
301 pam_end(lpamh, PAM_error);
302 return AFPERR_NOTAUTH;
308 ibuf[PASSWDLEN] = '\0';
310 /* this really does need to be done as root */
311 PAM_error = pam_chauthtok(lpamh, 0);
312 seteuid(uid); /* un-root ourselves. */
313 memset(pw, 0, PASSWDLEN);
314 memset(ibuf, 0, PASSWDLEN);
315 if (PAM_error != PAM_SUCCESS) {
316 pam_end(lpamh, PAM_error);
317 return AFPERR_ACCESS;
325 /* Printer ClearTxtUAM login */
326 int pam_printer(start, stop, username, out)
327 char *start, *stop, *username;
332 char password[PASSWDLEN + 1] = "\0";
333 static const char *loginok = "0\r";
335 data = (char *)malloc(stop - start + 1);
336 strncpy(data, start, stop - start + 1);
338 /* We are looking for the following format in data:
339 * (username) (password)
341 * Let's hope username doesn't contain ") ("!
344 /* Parse input for username in () */
345 if ((p = strchr(data, '(' )) == NULL) {
346 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
351 if ((q = strstr(data, ") (" )) == NULL) {
352 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
356 strncpy(username, p, q - p);
358 /* Parse input for password in next () */
360 if ((q = strrchr(data, ')' )) == NULL) {
361 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
365 strncpy(password, p, q - p);
367 /* Done copying username and password, clean up */
370 PAM_username = username;
371 PAM_password = password;
373 PAM_error = pam_start("netatalk", username, &PAM_conversation,
375 if (PAM_error != PAM_SUCCESS) {
376 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
377 username, pam_strerror(pamh, PAM_error));
378 pam_end(pamh, PAM_error);
383 pam_set_item(pamh, PAM_TTY, "papd");
384 pam_set_item(pamh, PAM_RHOST, hostname);
385 PAM_error = pam_authenticate(pamh,0);
386 if (PAM_error != PAM_SUCCESS) {
387 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
388 username, pam_strerror(pamh, PAM_error));
389 pam_end(pamh, PAM_error);
394 PAM_error = pam_acct_mgmt(pamh, 0);
395 if (PAM_error != PAM_SUCCESS) {
396 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
397 username, pam_strerror(pamh, PAM_error));
398 pam_end(pamh, PAM_error);
403 PAM_error = pam_open_session(pamh, 0);
404 if (PAM_error != PAM_SUCCESS) {
405 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: %s",
406 username, pam_strerror(pamh, PAM_error));
407 pam_end(pamh, PAM_error);
412 /* Login successful, but no need to hang onto it,
413 so logout immediately */
414 append(out, loginok, strlen(loginok));
415 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
416 pam_close_session(pamh, 0);
424 static int uam_setup(const char *path)
426 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
427 pam_login, NULL, pam_logout, pam_login_ext) < 0)
430 if (uam_register(UAM_SERVER_CHANGEPW, path, "Cleartxt Passwrd",
432 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
436 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
444 static void uam_cleanup(void)
446 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
447 uam_unregister(UAM_SERVER_CHANGEPW, "Cleartxt Passwrd");
448 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
451 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
454 uam_setup, uam_cleanup
457 UAM_MODULE_EXPORT struct uam_export uams_pam = {
460 uam_setup, uam_cleanup