1 // Build with gcc -std=c99 -lldap
2 // Requires libldap2-dev
6 #define LDAP_DEPRECATED 1 // Needed for ldap_simple_bind_s
18 #include <sys/types.h>
23 bool BindTheDemon(const char * username, const char * passwd)
25 static LDAP * ld = NULL;
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
35 ldap_initialize(&ld, uri); // This is deprecated.
38 fprintf(stderr, "ldap_init failed - %s\n", strerror(errno));
42 printf("ldap_init succeeded\n");
43 if (ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version) != LDAP_OPT_SUCCESS) // This is also deprecated.
45 fprintf(stderr, "ldap_set_option failed - %s\n", strerror(errno));
49 printf("ldap_set_option succeeded\n");
52 char * user_type = "Students";
53 if (username[0] == '0')
56 if (sprintf(dn, "cn=%s,ou=%s,ou=Users,ou=UWA,dc=uwads,dc=uwa,dc=edu,dc=au", username, user_type) >= BUFSIZ)
58 fprintf(stderr, "LDAP DN string too long!\n");
62 printf("ldap_bind_s ...\n");
64 //printf("dn = %s\npasswd = %s\n", dn, passwd);
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
71 fprintf(stderr,"ldap_bind_s failed - %s", strerror(errno));
78 int main(int argc, char ** argv)
81 // Get the username and password
82 // Need to get these passed through HTTPS at some point
84 char username[BUFSIZ];
85 if (fgets(username, BUFSIZ, stdin) != username)
87 fprintf(stderr, "Username too long!\n");
91 username[strlen(username)-1] = '\0';
93 char * password = getpass("Password: "); //NOTE: getpass is deprecated. Just here for testing.
95 printf("Could we bind the demon? %d\n", BindTheDemon(username, password));