blob: 67b243c77713ff721e92bd5a524d87e8c6fef4e0 [file] [log] [blame]
Adam Tkaca0d90c92010-09-07 09:33:51 +00001/*
2 * Copyright (C) 2006 Martin Koegler
3 * Copyright (C) 2010 TigerVNC Team
4 *
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This software is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this software; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18 * USA.
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25#ifndef HAVE_PAM
26#error "This source should not be compiled when PAM is unsupported"
27#endif
28
29#include <stdlib.h>
30#include <string.h>
31#include <security/pam_appl.h>
32
33#include <rfb/pam.h>
34
35typedef struct
36{
37 const char *username;
38 const char *password;
39} AuthData;
40
41static int pam_callback(int count, const struct pam_message **in,
42 struct pam_response **out, void *ptr)
43{
44 int i;
45 AuthData *auth = (AuthData *) ptr;
46 struct pam_response *resp =
47 (struct pam_response *) malloc (sizeof (struct pam_response) * count);
48
49 if (!resp && count)
50 return PAM_CONV_ERR;
51
52 for (i = 0; i < count; i++) {
53 resp[i].resp_retcode = PAM_SUCCESS;
54 switch (in[i]->msg_style) {
55 case PAM_TEXT_INFO:
56 case PAM_ERROR_MSG:
57 resp[i].resp = 0;
58 break;
59 case PAM_PROMPT_ECHO_ON: /* Send Username */
60 resp[i].resp = strdup(auth->username);
61 break;
62 case PAM_PROMPT_ECHO_OFF: /* Send Password */
63 resp[i].resp = strdup(auth->password);
64 break;
65 default:
66 free(resp);
67 return PAM_CONV_ERR;
68 }
69 }
70
71 *out = resp;
72 return PAM_SUCCESS;
73}
74
75
76int do_pam_auth(const char *service, const char *username, const char *password)
77{
78 int ret;
79 AuthData auth = { username, password };
80 struct pam_conv conv = {
81 pam_callback,
82 &auth
83 };
84 pam_handle_t *h = 0;
85 ret = pam_start(service, username, &conv, &h);
86 if (ret == PAM_SUCCESS)
87 ret = pam_authenticate(h, 0);
88 if (ret == PAM_SUCCESS)
89 ret = pam_acct_mgmt(h, 0);
90 pam_end(h, ret);
91
92 return ret == PAM_SUCCESS ? 1 : 0;
93}
94