payload: Use common prefixes for all payload type identifiers
[strongswan.git] / src / libcharon / plugins / xauth_pam / xauth_pam.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "xauth_pam.h"
17
18 #include <daemon.h>
19 #include <library.h>
20
21 #include <security/pam_appl.h>
22
23 typedef struct private_xauth_pam_t private_xauth_pam_t;
24
25 /**
26 * Private data of an xauth_pam_t object.
27 */
28 struct private_xauth_pam_t {
29
30 /**
31 * Public interface.
32 */
33 xauth_pam_t public;
34
35 /**
36 * ID of the peer
37 */
38 identification_t *peer;
39 };
40
41 METHOD(xauth_method_t, initiate, status_t,
42 private_xauth_pam_t *this, cp_payload_t **out)
43 {
44 cp_payload_t *cp;
45
46 cp = cp_payload_create_type(PLV1_CONFIGURATION, CFG_REQUEST);
47 cp->add_attribute(cp, configuration_attribute_create_chunk(
48 PLV1_CONFIGURATION_ATTRIBUTE, XAUTH_USER_NAME, chunk_empty));
49 cp->add_attribute(cp, configuration_attribute_create_chunk(
50 PLV1_CONFIGURATION_ATTRIBUTE, XAUTH_USER_PASSWORD, chunk_empty));
51 *out = cp;
52 return NEED_MORE;
53 }
54
55 /**
56 * PAM conv callback function
57 */
58 static int auth_conv(int num_msg, const struct pam_message **msg,
59 struct pam_response **resp, char *password)
60 {
61 struct pam_response *response;
62
63 if (num_msg != 1)
64 {
65 return PAM_CONV_ERR;
66 }
67 response = malloc(sizeof(struct pam_response));
68 response->resp = strdup(password);
69 response->resp_retcode = 0;
70 *resp = response;
71 return PAM_SUCCESS;
72 }
73
74 /**
75 * Authenticate a username/password using PAM
76 */
77 static bool authenticate(char *service, char *user, char *password)
78 {
79 pam_handle_t *pamh = NULL;
80 static struct pam_conv conv;
81 int ret;
82
83 conv.conv = (void*)auth_conv;
84 conv.appdata_ptr = password;
85
86 ret = pam_start(service, user, &conv, &pamh);
87 if (ret != PAM_SUCCESS)
88 {
89 DBG1(DBG_IKE, "XAuth pam_start for '%s' failed: %s",
90 user, pam_strerror(pamh, ret));
91 return FALSE;
92 }
93 ret = pam_authenticate(pamh, 0);
94 if (ret == PAM_SUCCESS)
95 {
96 ret = pam_acct_mgmt(pamh, 0);
97 if (ret != PAM_SUCCESS)
98 {
99 DBG1(DBG_IKE, "XAuth pam_acct_mgmt for '%s' failed: %s",
100 user, pam_strerror(pamh, ret));
101 }
102 }
103 else
104 {
105 DBG1(DBG_IKE, "XAuth pam_authenticate for '%s' failed: %s",
106 user, pam_strerror(pamh, ret));
107 }
108 pam_end(pamh, ret);
109 return ret == PAM_SUCCESS;
110 }
111
112 /**
113 * Convert configuration attribute content to a null-terminated string
114 */
115 static void attr2string(char *buf, size_t len, chunk_t chunk)
116 {
117 if (chunk.len && chunk.len < len)
118 {
119 chunk_t sane;
120
121 chunk_printable(chunk, &sane, '?');
122 snprintf(buf, len, "%.*s", (int)sane.len, sane.ptr);
123 chunk_clear(&sane);
124 }
125 }
126
127 METHOD(xauth_method_t, process, status_t,
128 private_xauth_pam_t *this, cp_payload_t *in, cp_payload_t **out)
129 {
130 char *service, user[128] = "", pass[128] = "", *pos;
131 configuration_attribute_t *attr;
132 enumerator_t *enumerator;
133 chunk_t chunk;
134
135 enumerator = in->create_attribute_enumerator(in);
136 while (enumerator->enumerate(enumerator, &attr))
137 {
138 switch (attr->get_type(attr))
139 {
140 case XAUTH_USER_NAME:
141 chunk = attr->get_chunk(attr);
142 /* trim to username part if email address given */
143 if (lib->settings->get_bool(lib->settings,
144 "%s.plugins.xauth-pam.trim_email",
145 TRUE, lib->ns))
146 {
147 pos = memchr(chunk.ptr, '@', chunk.len);
148 if (pos)
149 {
150 chunk.len = (u_char*)pos - chunk.ptr;
151 }
152 }
153 attr2string(user, sizeof(user), chunk);
154 break;
155 case XAUTH_USER_PASSWORD:
156 attr2string(pass, sizeof(pass), attr->get_chunk(attr));
157 break;
158 default:
159 break;
160 }
161 }
162 enumerator->destroy(enumerator);
163
164 if (!user[0] || !pass[0])
165 {
166 DBG1(DBG_IKE, "peer did not respond to our XAuth request");
167 return FAILED;
168 }
169
170 this->peer->destroy(this->peer);
171 this->peer = identification_create_from_string(user);
172
173 /* Look for PAM service, with a legacy fallback for the eap-gtc plugin.
174 * Default to "login". */
175 service = lib->settings->get_str(lib->settings,
176 "%s.plugins.xauth-pam.pam_service",
177 lib->settings->get_str(lib->settings,
178 "%s.plugins.eap-gtc.pam_service", "login", lib->ns),
179 lib->ns);
180
181 if (authenticate(service, user, pass))
182 {
183 DBG1(DBG_IKE, "PAM authentication of '%s' successful", user);
184 return SUCCESS;
185 }
186 return FAILED;
187 }
188
189 METHOD(xauth_method_t, get_identity, identification_t*,
190 private_xauth_pam_t *this)
191 {
192 return this->peer;
193 }
194
195 METHOD(xauth_method_t, destroy, void,
196 private_xauth_pam_t *this)
197 {
198 this->peer->destroy(this->peer);
199 free(this);
200 }
201
202 /*
203 * Described in header.
204 */
205 xauth_pam_t *xauth_pam_create_server(identification_t *server,
206 identification_t *peer, char *profile)
207 {
208 private_xauth_pam_t *this;
209
210 INIT(this,
211 .public = {
212 .xauth_method = {
213 .initiate = _initiate,
214 .process = _process,
215 .get_identity = _get_identity,
216 .destroy = _destroy,
217 },
218 },
219 .peer = peer->clone(peer),
220 );
221
222 return &this->public;
223 }