Merge pull request #62 from firefields/master
[matches/MCTX3420.git] / testing / login / ldap / ldap_test.c
1 // Build with gcc -std=c99 -lldap
2 // Requires libldap2-dev
3
4 #define _BSD_SOURCE
5 #define _XOPEN_SOURCE
6 #define LDAP_DEPRECATED 1 // Needed for ldap_simple_bind_s
7
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ldap.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <stdbool.h>
15
16
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <pwd.h>
20 #include <assert.h>
21
22
23 bool BindTheDemon(const char * username, const char * passwd)
24 {
25         static LDAP * ld = NULL;
26         
27
28         static int auth_method = LDAP_AUTH_SIMPLE;
29         static int version = LDAP_VERSION3;
30         static char uri[] = "ldaps://ldap.pheme.uwa.edu.au";
31         char dn[BUFSIZ]; // The "dn" is essentially the username plus a bunch of cruft that for some (presumably good) reason LDAP requires
32
33         if (ld == NULL)
34         {
35                 ldap_initialize(&ld, uri); // This is deprecated.
36                 if (ld == NULL)
37                 {
38                         fprintf(stderr, "ldap_init failed - %s\n", strerror(errno));
39                         return false;
40                 }
41
42                 printf("ldap_init succeeded\n");
43                 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version) != LDAP_OPT_SUCCESS) // This is also deprecated.
44                 {
45                         fprintf(stderr, "ldap_set_option failed - %s\n", strerror(errno));
46                         return false;
47                 }
48
49                 printf("ldap_set_option succeeded\n");
50         }
51
52         char * user_type = "Students";
53         if (username[0] == '0')
54                 user_type = "Staff";
55
56         if (sprintf(dn, "cn=%s,ou=%s,ou=Users,ou=UWA,dc=uwads,dc=uwa,dc=edu,dc=au", username, user_type) >= BUFSIZ)
57         {
58                 fprintf(stderr, "LDAP DN string too long!\n");
59                 return false;
60         }
61
62         printf("ldap_bind_s ...\n");
63
64         //printf("dn = %s\npasswd = %s\n", dn, passwd);
65
66         struct berval creds;
67         creds.bv_val = (char*)passwd;
68         if (ldap_simple_bind_s(ld, dn, passwd) != LDAP_SUCCESS) // Yep. Deprecated.
69         //if (ldap_sasl_bind_s(ld, dn, LDAP_SASL_SIMPLE , &creds, NULL, NULL, NULL) != LDAP_SUCCESS) // Doesn't work
70         {
71                 fprintf(stderr,"ldap_bind_s failed - %s", strerror(errno));
72                 return false;
73         }
74         return true;
75
76 }
77
78 int main(int argc, char ** argv)
79 {
80         
81         // Get the username and password
82         // Need to get these passed through HTTPS at some point
83         printf("Username: ");
84         char username[BUFSIZ];
85         if (fgets(username, BUFSIZ, stdin) != username)
86         {
87                 fprintf(stderr, "Username too long!\n");
88                 exit(EXIT_FAILURE);
89         }
90
91         username[strlen(username)-1] = '\0';
92
93         char * password = getpass("Password: "); //NOTE: getpass is deprecated. Just here for testing.
94         
95         printf("Could we bind the demon? %d\n", BindTheDemon(username, password));
96         return 0;
97 }

UCC git Repository :: git.ucc.asn.au