summaryrefslogtreecommitdiffstats
path: root/pam_wlan_helper.c
blob: 76db00a22296f76f1461d213b4836b8e5b44fcb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
  Copyright (c) 2015, Kristof Stahl <stahl@itsc.uni-luebeck.de>
  Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


/*
  See:

  https://developer.gnome.org/libnm-glib/0.9/
  https://developer.gnome.org/libnm-util/0.9/
*/

#include <stdio.h>
#include <string.h>

#include <security/pam_modules.h>

#include <nm-client.h>
#include <nm-remote-settings.h>
#include <nm-setting-connection.h>
#include <nm-setting-wireless.h>
#include <nm-setting-wireless-security.h>
#include <nm-setting-8021x.h>
#include <nm-setting-ip4-config.h>
#include <nm-setting-ip6-config.h>


#define TIMEOUT 10
#define CONNECTION_ID "UzL-Wlan"
#define CONNECTION_UUID "af7b7f0b-3e09-442e-8841-f7d7482fa7b0"

#define CONNECTION_SSID "UzL-Wlan"
#define CONNECTION_AUTH_ALG "open"
#define CONNECTION_KEY_MGMT "wpa-eap"
#define CONNECTION_EAP "ttls"
#define CONNECTION_PHASE2_AUTHEAP "mschapv2"


#define UNUSED __attribute__((unused))


static GByteArray * string_to_byte_array(const char *str) {
	size_t len = strlen(str);
	GByteArray *ba = g_byte_array_sized_new(len);

	g_byte_array_append(ba, (const unsigned char *)str, len);

	return ba;
}

static int setup_connection(NMConnection *con, const char *user, const char *pass) {
	NMSetting *setting;

	/* NMSettingConnection */
	setting = nm_setting_connection_new();
	g_object_set(G_OBJECT(setting),
		     NM_SETTING_CONNECTION_UUID, CONNECTION_UUID,
		     NM_SETTING_CONNECTION_ID, CONNECTION_ID,
		     NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
		     NULL);
	nm_connection_add_setting(con, setting);

	/* NMSettingWireless */
	setting = nm_setting_wireless_new();
	GByteArray *ssid = string_to_byte_array(CONNECTION_SSID);
	g_object_set(G_OBJECT(setting),
		     NM_SETTING_WIRELESS_SSID, ssid,
		     NM_SETTING_WIRELESS_MODE, NM_SETTING_WIRELESS_MODE_INFRA,
		     NULL);
	g_byte_array_unref(ssid);
	nm_connection_add_setting(con, setting);

	/* NMSettingWirelessSecurity */
	setting = nm_setting_wireless_security_new();
	g_object_set(G_OBJECT(setting),
		     NM_SETTING_WIRELESS_SECURITY_AUTH_ALG, CONNECTION_AUTH_ALG,
		     NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, CONNECTION_KEY_MGMT,
		     NULL);
	nm_connection_add_setting(con, setting);

	/* NMSetting8021x */
	setting = nm_setting_802_1x_new();
	/* TODO: Add certificate and subject check */
	nm_setting_802_1x_add_eap_method(NM_SETTING_802_1X(setting), CONNECTION_EAP);
	g_object_set(setting,
		     NM_SETTING_802_1X_IDENTITY, user,
		     NM_SETTING_802_1X_PASSWORD, pass,
		     NM_SETTING_802_1X_PHASE2_AUTHEAP, CONNECTION_PHASE2_AUTHEAP,
		     NULL);
	nm_connection_add_setting(con, setting);

	/* NMSettingIP4Config */
	setting = nm_setting_ip4_config_new();
	g_object_set(G_OBJECT(setting),
		     NM_SETTING_IP4_CONFIG_METHOD, "auto",
		     NULL);
	nm_connection_add_setting(con, setting);

	/* NMSettingIP6Config */
	setting = nm_setting_ip6_config_new();
	g_object_set(G_OBJECT(setting),
		     NM_SETTING_IP6_CONFIG_METHOD, "auto",
		     NULL);
	nm_connection_add_setting(con, setting);

	return PAM_SUCCESS;
}

typedef struct {
	int ret;
	GMainLoop *loop;
	NMClient *client;
} cb_arg;


static NMDevice * find_device(NMClient *client) {
	size_t i;
	const GPtrArray *devices = nm_client_get_devices(client);

	for (i = 0; i < devices->len; i++) {
		NMDevice *device = g_ptr_array_index(devices, i);

		if (nm_device_get_device_type(device) == NM_DEVICE_TYPE_WIFI)
			return device;
	}

	return NULL;
}

static void state_cb(NMDevice *device, UNUSED GParamSpec *pspec, gpointer user_data) {
	cb_arg *arg = user_data;

	NMActiveConnection *active_connection = nm_device_get_active_connection(device);
	if (!active_connection)
		return;

	NMActiveConnectionState state = nm_active_connection_get_state(active_connection);

	if (state != NM_ACTIVE_CONNECTION_STATE_ACTIVATING && state != NM_ACTIVE_CONNECTION_STATE_DEACTIVATING) {
		if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED)
			arg->ret = PAM_SUCCESS;

		g_main_loop_quit(arg->loop);
	}
}

static void activate_cb(UNUSED NMClient *client, UNUSED NMActiveConnection *active_connection, GError *error, gpointer user_data) {
	cb_arg *arg = user_data;

	if (error)
		goto error;

	return;

 error:
	g_main_loop_quit(arg->loop);
}

static void add_cb(UNUSED NMRemoteSettings *settings, NMRemoteConnection *con, GError *error, gpointer user_data) {
	cb_arg *arg = user_data;

	if (error)
		goto error;

	NMDevice *device = find_device(arg->client);
	if (!device)
		goto error;

	g_signal_connect(device, "notify::state", G_CALLBACK(state_cb), arg);
	nm_client_activate_connection(arg->client, NM_CONNECTION(con), device, NULL, activate_cb, arg);
	return;

 error:
	g_main_loop_quit(arg->loop);
}

static int authenticate(const char *user, const char *pass) {
	DBusGConnection *dbus = NULL;
	NMRemoteSettings *settings = NULL;
	NMConnection *con = NULL;
	cb_arg arg = {};

	arg.ret = PAM_SERVICE_ERR;
	arg.loop = g_main_loop_new(NULL, FALSE);

	arg.client = nm_client_new();
	if (!arg.client)
		goto end;

	dbus = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
	if (!dbus)
		goto end;

	settings = nm_remote_settings_new(dbus);
	if (!settings)
		goto end;

	con = nm_connection_new();
	setup_connection(con, user, pass);

	if (!nm_remote_settings_add_connection_unsaved(settings, con, add_cb, &arg))
		goto end;

	arg.ret = PAM_AUTH_ERR;

	g_main_loop_run(arg.loop);

 end:
	g_object_unref(con);

	g_object_unref(settings);
	dbus_g_connection_unref(dbus);
	g_object_unref(arg.client);
	g_main_loop_unref(arg.loop);

	return arg.ret;
}

int main() {
	char buf[1024];
	size_t len = 0;

	while (len < sizeof(buf)-1) {
		size_t r = fread(buf+len, 1, sizeof(buf)-len-1, stdin);
		if (!r)
			break;

		len += r;
	}

	buf[len] = 0;

	const char *user = buf;
	const char *pass = user + strlen(user) + 1;
	if (pass > buf+len)
		return PAM_SYSTEM_ERR;

	return authenticate(user, pass);
}