1 #define BITLBEE_CORE
2 #include "bitlbee.h"
3 
4 #ifdef WITH_PAM
5 extern auth_backend_t auth_pam;
6 #endif
7 #ifdef WITH_LDAP
8 extern auth_backend_t auth_ldap;
9 #endif
10 
auth_init(const char * backend)11 GList *auth_init(const char *backend)
12 {
13 	GList *gl = NULL;
14 	int ok = backend ? 0 : 1;
15 #ifdef WITH_PAM
16 	gl = g_list_append(gl, &auth_pam);
17 	if (backend && !strcmp(backend, "pam")) {
18 		ok = 1;
19 	}
20 #endif
21 #ifdef WITH_LDAP
22 	gl = g_list_append(gl, &auth_ldap);
23 	if (backend && !strcmp(backend, "ldap")) {
24 		ok = 1;
25 	}
26 #endif
27 
28 	return ok ? gl : NULL;
29 }
30 
auth_check_pass(irc_t * irc,const char * nick,const char * password)31 storage_status_t auth_check_pass(irc_t *irc, const char *nick, const char *password)
32 {
33 	GList *gl;
34 	storage_status_t status = storage_check_pass(irc, nick, password);
35 
36 	if (status == STORAGE_CHECK_BACKEND) {
37 		for (gl = global.auth; gl; gl = gl->next) {
38 			auth_backend_t *be = gl->data;
39 			if (!strcmp(be->name, irc->auth_backend)) {
40 				status = be->check_pass(nick, password);
41 				break;
42 			}
43 		}
44 	} else if (status == STORAGE_NO_SUCH_USER && global.conf->auth_backend) {
45 		for (gl = global.auth; gl; gl = gl->next) {
46 			auth_backend_t *be = gl->data;
47 			if (!strcmp(be->name, global.conf->auth_backend)) {
48 				status = be->check_pass(nick, password);
49 				/* Save the user so storage_load will pick them up, similar to
50 				 * what the register command would do */
51 				if (status == STORAGE_OK) {
52 					irc->auth_backend = g_strdup(global.conf->auth_backend);
53 					storage_save(irc, (char *)password, 0);
54 				}
55 				break;
56 			}
57 		}
58 	}
59 
60 	if (status == STORAGE_OK) {
61 		irc_setpass(irc, password);
62 	}
63 
64 	return status;
65 }
66