]> err.no Git - sope/blob - sope-ldap/samples/pwd-check.m
more code directory reorganizations
[sope] / sope-ldap / samples / pwd-check.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 /* pwcheck_ldap.c -- check passwords using LDAP
22  *
23  * Author: Clayton Donley <donley@cig.mot.com>
24  *         http://www.wwa.com/~donley/
25  * Version: 1.01
26  *
27  * Note: This works by finding a DN that matches an entered UID and
28  * binding to the LDAP server using this UID.  This uses clear-text
29  * passwords.  A better approach with servers that support SSL and
30  * new LDAPv3 servers that support SASL bind methods like CRAM-MD5
31  * and TSL.
32  *
33  * This version should work with both University of Michigan and Netscape
34  * LDAP libraries.  It also gets rid of the requirement for userPassword
35  * attribute readability.
36  *
37  */
38
39 //#include <lber.h>
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <lber.h>
43 #include <ldap.h>
44 //#include <libio.h>
45
46 /* Set These to your Local Environment */
47
48 #define MY_LDAP_SERVER  "imap.mdlink.de"
49 #define MY_LDAP_BASEDN  "ou=people,o=mdlink.de"
50 #define MY_LDAP_UIDATTR "uid"
51
52 char *pwcheck(char *userid, char *password) {
53     LDAP *ld;
54     LDAPMessage *result;
55     LDAPMessage *entry;
56     char *attrs[2];
57     char filter[200]; 
58     char *dn;
59     int ldbind_res;
60     char **vals;
61
62 /* If the password is NULL, reject the login...Otherwise the bind will
63    succeed as a reference bind.  Not good... */
64
65     if (strcmp(password,"") == 0)
66     {
67        return "Null Password";
68     }
69     
70 /* Open the LDAP connection.  Change the second argument if your LDAP
71    server is not on port 389. */
72
73     if ((ld = ldap_open(MY_LDAP_SERVER,LDAP_PORT)) == NULL)
74     {
75        return "Init Failed";
76     }
77
78 /* Bind anonymously so that you can find the DN of the appropriate user. */
79
80     if (ldap_simple_bind_s(ld,"","") != LDAP_SUCCESS)
81     {
82         ldap_unbind(ld);
83         return "Bind Failed";
84     }
85
86 /* Generate a filter that will return the entry with a matching UID */
87
88     sprintf(filter,"(%s=%s)",MY_LDAP_UIDATTR,userid);
89
90 /* Just return country...This doesn't actually matter, since we will
91    not read the attributes and values, only the DN */
92
93     attrs[0] = "c";
94     attrs[1] = NULL;
95
96 /* Perform the search... */
97
98     if (ldap_search_s(ld,MY_LDAP_BASEDN,LDAP_SCOPE_SUBTREE,filter,attrs,1,&result)
99 != LDAP_SUCCESS)
100     {
101        ldap_unbind(ld);
102        return "Search Failed";
103     }
104
105 /* If the entry count is not equal to one, either the UID was not unique or
106    there was no match */
107
108     if ((ldap_count_entries(ld,result)) != 1)
109     {
110        ldap_unbind(ld);
111        return "UserID Unknown";
112     }
113
114 /* Get the first entry */
115
116     if ((entry = ldap_first_entry(ld,result)) == NULL)
117     {
118        ldap_unbind(ld);
119        return "UserID Unknown";
120     }
121
122 /* Get the DN of the entry */
123
124     if ((dn = ldap_get_dn(ld,entry)) == NULL)
125     {
126        ldap_unbind(ld);
127        return "DN Not Found";
128     }
129
130 /* Now bind as the DN with the password supplied earlier...
131    Successful bind means the password was correct, otherwise the
132    password is invalid. */
133
134     printf("dn: %s\npassword: %s\n", dn, password);
135     
136     if (ldap_simple_bind_s(ld,dn,password) != LDAP_SUCCESS)
137     {
138        ldap_unbind(ld);
139        return "Invalid Login or Password";
140     }
141
142     ldap_unbind(ld);
143     return "OK";
144 }
145
146 #include <Foundation/Foundation.h>
147 #include "NGLdapConnection.h"
148
149 int main(int argc, char **argv, char **env) {
150   NSArray        *args;
151   NSUserDefaults *ud;
152   char *uid, *pwd;
153
154 #if LIB_FOUNDATION_LIBRARY
155   [NSProcessInfo initializeWithArguments:argv count:argc environment:env];
156 #endif
157
158   args = [[NSProcessInfo processInfo] arguments];
159   ud   = [NSUserDefaults standardUserDefaults];
160   
161   if (argc < 3)
162     exit(10);
163
164 #if 0
165   uid = argv[1];
166   pwd = argv[2];
167   
168   printf("pwcheck('%s', '%s'): %s\n", uid, pwd,
169          pwcheck(uid, pwd));
170 #else
171   
172   if ([NGLdapConnection checkPassword:[ud stringForKey:@"LDAPPassword"]
173                         ofLogin:[ud stringForKey:@"LDAPBindDN"]
174                         atBaseDN:[ud stringForKey:@"LDAPRootDN"]
175                         onHost:[ud stringForKey:@"LDAPHost"]
176                         port:0]) {
177     NSLog(@"OK: user %@ is authorized.", [ud stringForKey:@"LDAPBindDN"]);
178   }
179   else {
180     NSLog(@"FAIL: user %@ is not authorized.", [ud stringForKey:@"LDAPBindDN"]);
181   }
182   
183 #endif
184   return 0;
185 }