aboutsummaryrefslogtreecommitdiffstats
path: root/ll-auth-binding/src/ll-auth-binding.c
blob: 4a9bf11c45f11ee601523e602971dcb62fb1271b (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
#include <security/pam_appl.h>
#include <security/pam_misc.h>
#include <libudev.h>
#include <pthread.h>
#include <poll.h>

#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>

// Defines
#define PAM_RULE													"agl"
#define UDEV_MONITOR_POLLING_TIMEOUT								5000

#define UDEV_ACTION_UNSUPPORTED										0
#define UDEV_ACTION_ADD												1
#define UDEV_ACTION_REMOVE											2

#define LOGIN_SUCCESS												0
#define LOGIN_ERROR_USER_LOGGED										1
#define LOGIN_ERROR_PAM_START										2
#define LOGIN_ERROR_PAM_PUTENV										3
#define LOGIN_ERROR_PAM_AUTHENTICATE								4
#define LOGIN_ERROR_PAM_ACCT_MGMT									5
#define LOGIN_ERROR_PAM_NO_USER										6
#define LOGIN_ERROR_PAM_END											7

// Globals
static const char* error_messages[] =
{
	"",
	"The current user must be logged out first!",
	"PAM start failed!",
	"PAM putenv failed!",
	"PAM authenticate failed!",
	"PAM acct_mgmt failed!",
	"No user provided by the PAM module!",
	"PAM end failed!"
};

static char*					current_device						= NULL;
static char*					current_user						= NULL;
static struct pam_conv			conv								= { misc_conv, NULL };
static struct udev*				udev_context						= NULL;
static struct udev_monitor*		udev_mon							= NULL;
static pthread_t				udev_monitoring_thread_handle;
static struct afb_event			evt_login, evt_logout, evt_failed;

/**
 * @brief Free the memory associated to the specified string and nullify the pointer.
 * @param[in] string A pointer to the string.
 */
static inline void free_string(char** string)
{
	if (string)
	{
		if (*string) free(*string);
		*string = NULL;
	}
}

/**
 * @brief Free the memory associated to the specified UDev's context and nullify the pointer.
 * @param[in] ctx UDev's context.
 */
static inline void free_udev_context(struct udev** ctx)
{
	if (ctx)
	{
		if (*ctx) udev_unref(*ctx);
		*ctx = NULL;
	}
}

/**
 * @brief Free the memory associated to the specified UDev's monitor and nullify the pointer.
 * @param[in] mon UDev's monitor.
 */
static inline void free_udev_monitor(struct udev_monitor** mon)
{
	if (mon)
	{
		if (*mon) udev_monitor_unref(*mon);
		*mon = NULL;
	}
}

/**
 * @brief Print UDev infos for the specified device.
 * @param[in] dev The device.
 */
static inline void print_udev_device_info(struct udev_device* dev)
{
	AFB_INFO("    Action: %s", udev_device_get_action(dev));
	AFB_INFO("    Node: %s", udev_device_get_devnode(dev));
	AFB_INFO("    Subsystem: %s", udev_device_get_subsystem(dev));
	AFB_INFO("    Devtype: %s", udev_device_get_devtype(dev));
	AFB_INFO("    DevNum: %lu", udev_device_get_devnum(dev));
	AFB_INFO("    DevPath: %s", udev_device_get_devpath(dev));
	AFB_INFO("    Driver: %s", udev_device_get_driver(dev));
	AFB_INFO("    SeqNum: %llu", udev_device_get_seqnum(dev));
	AFB_INFO("    SysName: %s", udev_device_get_sysname(dev));
	AFB_INFO("    SysNum: %s", udev_device_get_sysnum(dev));
	AFB_INFO("    SysPath: %s", udev_device_get_syspath(dev));
}

/**
 * @brief Get the UDev's action as an int to allow switch condition.
 * @param[in] dev The device.
 */
static inline int udev_device_get_action_int(struct udev_device* dev)
{
	const char* action = udev_device_get_action(dev);
	return
		strcmp(action, "add")
		? (strcmp(action, "remove") ? UDEV_ACTION_UNSUPPORTED : UDEV_ACTION_REMOVE)
		: UDEV_ACTION_ADD;
}

/**
 * @brief PAM authentication process.
 * @param[in] pamh The handle to the PAM context.
 * @param[in] device The device to login.
 */
static int pam_process(pam_handle_t* pamh, const char* device)
{
	int r;
	
	if (!pamh) return LOGIN_ERROR_PAM_START;
	
	char pam_variable[4096] = "DEVICE=";
	strcat(pam_variable, device);
	
	if ((r = pam_putenv(pamh, pam_variable)) != PAM_SUCCESS)
		return LOGIN_ERROR_PAM_PUTENV;

	if ((r = pam_authenticate(pamh, 0)) != PAM_SUCCESS)
		return LOGIN_ERROR_PAM_AUTHENTICATE;

	if ((r = pam_acct_mgmt(pamh, 0)) != PAM_SUCCESS)
			return LOGIN_ERROR_PAM_ACCT_MGMT;

	const char* pam_user;
	pam_get_item(pamh, PAM_USER, (const void**)&pam_user);
	if (!pam_user)
		return LOGIN_ERROR_PAM_NO_USER;

	current_device = strdup(device);
	current_user = strdup(pam_user);
	
	return LOGIN_SUCCESS;
}

/**
 * @brief Login using PAM.
 * @param[in] device The device to use.
 * @return Exit code, @c LOGIN_SUCCESS on success.
 */
static int login_pam(const char* device)
{
	int r;
	pam_handle_t* pamh;
	
	if (current_user)
		return LOGIN_ERROR_USER_LOGGED;

	if ((r = pam_start("agl", NULL, &conv, &pamh)) != PAM_SUCCESS)
		return LOGIN_ERROR_PAM_START;

	r = pam_process(pamh, device);
	if (r != LOGIN_SUCCESS)
	{
		pam_end(pamh, r);
		return r;
	}

	if ((r = pam_end(pamh, r)) != PAM_SUCCESS)
		return LOGIN_ERROR_PAM_END;
	
	return LOGIN_SUCCESS;
}

/**
 * @brief Try to login a user using a device.
 * @param[in] device The device to use.
 * @return Exit code, @c LOGIN_SUCCESS if success.
 */
static int login(const char* device)
{
	int ret;
	struct json_object* result;
	
	result = json_object_new_object();
	
	ret = login_pam(device);
	switch(ret)
	{
		case LOGIN_SUCCESS:
			json_object_object_add(result, "device", json_object_new_string(current_device));
			json_object_object_add(result, "user", json_object_new_string(current_user));
			afb_event_broadcast(evt_login, result);
			break;
		default:
			json_object_object_add(result, "message", json_object_new_string(error_messages[ret]));
			afb_event_broadcast(evt_failed, result);
	}
	
	json_object_put(result);
	return ret;
}

/// @brief Try to logout a user using a device.
/// @param[in] device The device to logout.
static void logout(const char* device)
{
	struct json_object* result;
	
	result = json_object_new_object();
	
	if (current_device && !strcmp(device, current_device))
	{
		json_object_object_add(result, "device", json_object_new_string(current_device));
		json_object_object_add(result, "user", json_object_new_string(current_user));
		AFB_INFO("[logout] device: %s", device);
		afb_event_broadcast(evt_logout, NULL);
		
		free_string(&current_device);
		free_string(&current_user);
	}
	else
	{
		json_object_object_add(result, "message", json_object_new_string("The unplugged device wasn't the user key!"));
		AFB_INFO("The unplugged device wasn't the user key!");
		afb_event_broadcast(evt_failed, result);
	}
	json_object_put(result);
}

/**
 * @brief UDev's monitoring thread.
 */
void* udev_monitoring_thread(void* arg)
{
	struct udev_device* dev;
	struct pollfd pfd;
	int action;
	
	pfd.fd = udev_monitor_get_fd(udev_mon);
	pfd.events = POLLIN;
	
	while(1)
	{
		if (poll(&pfd, 1, UDEV_MONITOR_POLLING_TIMEOUT))
		{
			dev = udev_monitor_receive_device(udev_mon);
			if (dev)
			{
				if (!strcmp(udev_device_get_devtype(dev), "disk"))
				{
					action = udev_device_get_action_int(dev);
					switch(action)
					{
						case UDEV_ACTION_ADD:
							AFB_INFO("A device is plugged-in");
							print_udev_device_info(dev);
							login(udev_device_get_devnode(dev));
							break;
						case UDEV_ACTION_REMOVE:
							AFB_INFO("A device is plugged-out");
							print_udev_device_info(dev);
							logout(udev_device_get_devnode(dev));
							break;
						default:
							AFB_DEBUG("Unsupported udev action");
							break;
					}
				}
				udev_device_unref(dev);
			}
			else
			{
				AFB_ERROR("No Device from udev_monitor_receive_device().");
			}
		}
		else
		{
			AFB_DEBUG("Udev polling timeout");
		}
	}
	return NULL;
}

/**
 * @brief API's verb 'getuser'. Try to get user informations.
 * @param[in] req The request object.
 */
static void verb_getuser(struct afb_req req)
{
	if (!current_device || !current_user)
	{
		afb_req_fail(req, "there is no logged user!", NULL);
		return;
	}

	json_object* result = json_object_new_object();
	json_object_object_add(result, "user", json_object_new_string(current_user));
	json_object_object_add(result, "device", json_object_new_string(current_device));

	afb_req_success(req, result, NULL);
}

/**
 * @brief Do the cleanup when init fails.
 * @param[in] error Error message.
 * @param[in] retcode Error code to return.
 * @return An exit code equals to @c retcode.
 */
static inline int ll_auth_init_cleanup(const char* error, int retcode)
{
	AFB_ERROR_V2("%s", error);
	free_string(&current_user);
	free_string(&current_device);
	
	free_udev_monitor(&udev_mon);
	free_udev_context(&udev_context);
	return retcode;
}

/**
 * @brief Initialize the binding.
 */
int ll_auth_init()
{
	evt_login = afb_daemon_make_event("login");
	evt_logout = afb_daemon_make_event("logout");
	evt_failed = afb_daemon_make_event("failed");

	if (!afb_event_is_valid(evt_login) || !afb_event_is_valid(evt_logout) || !afb_event_is_valid(evt_failed))
		return ll_auth_init_cleanup("Can't create events", -1);
		
	udev_context = udev_new();
	if (!udev_context)
		return ll_auth_init_cleanup("Can't initialize udev's context", -1);
	
	udev_mon = udev_monitor_new_from_netlink(udev_context, "udev");
	if (!udev_mon)
		return ll_auth_init_cleanup("Can't initialize udev's monitor", -1);
	
	udev_monitor_filter_add_match_subsystem_devtype(udev_mon, "block", NULL);
	udev_monitor_enable_receiving(udev_mon);
	
	if (pthread_create(&udev_monitoring_thread_handle, NULL, udev_monitoring_thread, NULL))
		return ll_auth_init_cleanup("Can't start the udev's monitoring thread", -1);
	
	AFB_INFO("ll-auth-binding is ready");
	return 0;
}

static const afb_verb_v2 _ll_auth_binding_verbs[]= {
	{
		.verb = "getuser",
		.callback = verb_getuser,
		.auth = NULL,
		.info = NULL,
		.session = AFB_SESSION_NONE_V2
	},
	{ .verb=NULL}
};

const struct afb_binding_v2 afbBindingV2 = {
		.api = "ll-auth",
		.specification = NULL,
		.verbs = _ll_auth_binding_verbs,
		.preinit = NULL,
		.init = ll_auth_init,
		.onevent = NULL,
		.noconcurrency = 0
};