]> jfr.im git - irc/evilnet/x3.git/blob - src/x3ldap.c
beginnings of LDAP authentication ability
[irc/evilnet/x3.git] / src / x3ldap.c
1 /*
2 *
3 * LDAP functionality for x3, by Rubin
4 *
5 * TODO:
6 * * get queries working in static existance, so i understand how it works
7 * * get ldap enabled in ./configure
8 * * x3.conf settings to enable/configure its use
9 * * generic functions to enable ldap
10 * * nickserv.c work to use said functions.
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <ldap.h>
16 //#include <sys/select.h>
17
18 #include "conf.h"
19 #include "config.h"
20 #include "global.h"
21 #include "x3ldap.h"
22
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
26 #ifdef HAVE_SYS_SELECT_H
27 #include <sys/select.h>
28 #endif
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
31 #endif
32
33 #ifdef WITH_LDAP
34
35 /* char dn[] = "uid=%s,ou=Users,dc=afternet,dc=org";
36 char password[] = "xxxxxxx";
37 char base[] = "ou=Users,dc=afternet,dc=org";
38 int ldap_version = 3;
39 */
40 extern struct nickserv_config nickserv_conf;
41
42
43 /* TODO: change all these printfs to proper debug statements */
44
45 LDAP *ld = NULL;
46
47 int ldap_do_init()
48 {
49 /* TODO: check here for all required config options and exit() out if not present */
50 ld = ldap_init(nickserv_conf.ldap_host, nickserv_conf.ldap_port);
51 if(ld == NULL) {
52 printf("Failed!\n");
53 exit(1);
54 }
55 ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &nickserv_conf.ldap_version);
56 printf("Success! ldap_init() was successfull in connecting to %s port %d\n", nickserv_conf.ldap_host, nickserv_conf.ldap_port );
57 return true;
58 }
59
60 /* Try to auth someone. If theres problems, try reconnecting
61 * once every 10 seconds for 1 minute.
62 * TODO: move this stuff to config file
63 */
64 unsigned int ldap_check_auth( char *account, char *pass)
65 {
66 char buff[MAXLEN];
67 int q;
68
69 memset(buff, 0, MAXLEN);
70 snprintf(buff, sizeof(buff)-1, nickserv_conf.ldap_dn_fmt /*"uid=%s,ou=Users,dc=afternet,dc=org"*/, account);
71 int n = 0;
72 while(1) {
73 q = ldap_simple_bind_s(ld, buff, pass);
74 if(q == LDAP_SUCCESS) {
75 return true;
76 }
77 else if(q == LDAP_INVALID_CREDENTIALS) {
78 return false;
79 }
80 else {
81 printf("Bind failed: %s/****** (%d)\n", buff, q);
82 ldap_perror(ld, "ldap");
83 /* Re-init to re-connect to ldap server if thats the problem */
84 sleep(10);
85 ldap_do_init(nickserv_conf);
86 }
87 if(n++ > 6) {
88 printf("Failing to reconnect to ldap server. Dieing.");
89 exit(1);
90 }
91 }
92 printf("bind() successfull! You are bound as %s\n", buff);
93 return true;
94
95 }
96
97 #ifdef notdef /* not used yet - will be used to pull email etc out of ldap */
98 LDAPMessage ldap_search_user(char uid)
99 {
100
101 char filter[] = "cn=admin";
102
103 struct timeval timeout;
104 /*
105 Now we do a search;
106 */
107 timeout.tv_usec = 0;
108 timeout.tv_sec = 5;
109 if( ldap_search_st(ld, base, LDAP_SCOPE_ONELEVEL, filter, NULL, 0, &timeout, &res) != LDAP_SUCCESS) {
110 printf("search failed: %s %s\n", base, filter);
111 exit(1);
112 }
113 printf("Search successfull! %s %s\n", base, filter);
114 printf("Got %d entries\n", ldap_count_entries(ld, res));
115 {
116 LDAPMessage *entry;
117 char **value;
118 entry = ldap_first_entry(ld, res);
119 value = ldap_get_values(ld, entry, "cn");
120 printf("cn: %s\n", value[0]);
121 value = ldap_get_values(ld, entry, "description");
122 printf("Description: %s\n", value[0]);
123 value = ldap_get_values(ld, entry, "userPassword");
124 printf("pass: %s\n", value ? value[0] : "error");
125 }
126 /*
127 ldap_result();
128 ldap_first_entry();
129 ldap_first_attribute();
130 for(;;) {
131 ldap_get_values();
132 ldap_next_attribute();
133 }
134
135 ldap_parse_result();
136
137 ldap_unbind_ext();
138
139 */
140 /* get errors with ldap_err2string(); */
141 }
142
143 #endif
144
145 void ldap_close()
146 {
147 ldap_unbind(ld);
148 }
149
150 #endif