]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_passwd.c
Trunk-BP: validity checking.
[netatalk.git] / etc / uams / uams_passwd.c
1 /*
2  * $Id: uams_passwd.c,v 1.13.2.1 2002-02-08 00:00:01 srittau Exp $
3  *
4  * Copyright (c) 1990,1993 Regents of The University of Michigan.
5  * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu) 
6  * All Rights Reserved.  See COPYRIGHT.
7  */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif /* HAVE_CONFIG_H */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 /* STDC check */
17 #if STDC_HEADERS
18 #include <string.h>
19 #else /* STDC_HEADERS */
20 #ifndef HAVE_STRCHR
21 #define strchr index
22 #define strrchr index
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
25 #ifndef HAVE_MEMCPY
26 #define memcpy(d,s,n) bcopy ((s), (d), (n))
27 #define memmove(d,s,n) bcopy ((s), (d), (n))
28 #endif /* ! HAVE_MEMCPY */
29 #endif /* STDC_HEADERS */
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34 #ifndef NO_CRYPT_H
35 #include <crypt.h>
36 #endif /* ! NO_CRYPT_H */
37 #include <pwd.h>
38 #include <syslog.h>
39
40 #ifdef SOLARIS
41 #define SHADOWPW
42 #endif /* SOLARIS */
43
44 #ifdef SHADOWPW
45 #include <shadow.h>
46 #endif /* SHADOWPW */
47
48 #include <atalk/afp.h>
49 #include <atalk/uam.h>
50
51 #define PASSWDLEN 8
52
53 #ifdef TRU64
54 #include <sia.h>
55 #include <siad.h>
56
57 static char *clientname;
58 #endif /* TRU64 */
59
60 /* cleartxt login */
61 static int passwd_login(void *obj, struct passwd **uam_pwd,
62                         char *ibuf, int ibuflen,
63                         char *rbuf, int *rbuflen)
64 {
65     struct passwd *pwd;
66 #ifdef SHADOWPW
67     struct spwd *sp;
68 #endif /* SHADOWPW */
69     char *username, *p;
70     int len, ulen;
71
72     *rbuflen = 0;
73
74     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
75                              (void *) &username, &ulen) < 0)
76         return AFPERR_MISC;
77
78 #ifdef TRU64
79     if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
80                               (void *) &clientname, NULL ) < 0 )
81         return AFPERR_MISC;
82 #endif /* TRU64 */
83
84     if (ibuflen <= 1) {
85         return( AFPERR_PARAM );
86     }
87
88     len = (unsigned char) *ibuf++;
89     ibuflen--;
90     if (!len || len > ibuflen || len > ulen ) {
91         return( AFPERR_PARAM );
92     }
93
94     memcpy(username, ibuf, len );
95     ibuf += len;
96     ibuflen -=len;
97     username[ len ] = '\0';
98
99     if ((unsigned long) ibuf & 1) { /* pad character */
100         ++ibuf;
101         ibuflen--;
102     }
103     if (ibuflen < PASSWDLEN) {
104         return( AFPERR_PARAM );
105     }
106     ibuf[ PASSWDLEN ] = '\0';
107
108     if (( pwd = uam_getname(username, ulen)) == NULL ) {
109         return AFPERR_PARAM;
110     }
111
112     syslog(LOG_INFO, "cleartext login: %s", username);
113     if (uam_checkuser(pwd) < 0) {
114         return AFPERR_NOTAUTH;
115     }
116
117 #ifdef SHADOWPW
118     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
119         syslog( LOG_INFO, "no shadow passwd entry for %s", username);
120         return AFPERR_NOTAUTH;
121     }
122     pwd->pw_passwd = sp->sp_pwdp;
123 #endif /* SHADOWPW */
124
125     if (!pwd->pw_passwd) {
126         return AFPERR_NOTAUTH;
127     }
128
129     *uam_pwd = pwd;
130
131 #ifdef TRU64
132     {
133         int ac;
134         char **av;
135         char hostname[256];
136
137         uam_afp_getcmdline( &ac, &av );
138         sprintf( hostname, "%s@%s", username, clientname );
139
140         if( sia_validate_user( NULL, ac, av, hostname, username,
141                                NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
142             return AFPERR_NOTAUTH;
143
144         return AFP_OK;
145     }
146 #else /* TRU64 */
147     p = crypt( ibuf, pwd->pw_passwd );
148     if ( strcmp( p, pwd->pw_passwd ) == 0 )
149         return AFP_OK;
150 #endif /* TRU64 */
151
152     return AFPERR_NOTAUTH;
153 }
154
155
156 #if 0
157 /* change passwd */
158 static int passwd_changepw(void *obj, char *username,
159                            struct passwd *pwd, char *ibuf,
160                            int ibuflen, char *rbuf, int *rbuflen)
161 {
162 #ifdef SHADOWPW
163     struct spwd *sp;
164 #endif /* SHADOWPW */
165     char pw[PASSWDLEN + 1], *p;
166     uid_t uid = geteuid();
167
168     if (uam_checkuser(pwd) < 0)
169         return AFPERR_ACCESS;
170
171     /* old password */
172     memcpy(pw, ibuf, PASSWDLEN);
173     memset(ibuf, 0, PASSWDLEN);
174     pw[PASSWDLEN] = '\0';
175
176 #ifdef SHADOWPW
177     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
178         syslog( LOG_INFO, "no shadow passwd entry for %s", username);
179         return AFPERR_PARAM;
180     }
181     pwd->pw_passwd = sp->sp_pwdp;
182 #endif /* SHADOWPW */
183
184     p = crypt(pw, pwd->pw_passwd );
185     if (strcmp( p, pwd->pw_passwd )) {
186         memset(pw, 0, sizeof(pw));
187         return AFPERR_NOTAUTH;
188     }
189
190     /* new password */
191     ibuf += PASSWDLEN;
192     ibuf[PASSWDLEN] = '\0';
193
194 #ifdef SHADOWPW
195 #else /* SHADOWPW */
196 #endif /* SHADOWPW */
197     return AFP_OK;
198 }
199 #endif /* 0 */
200
201
202 /* Printer ClearTxtUAM login */
203 static int passwd_printer(start, stop, username, out)
204         char    *start, *stop, *username;
205         struct papfile  *out;
206 {
207     struct passwd *pwd;
208 #ifdef SHADOWPW
209     struct spwd *sp;
210 #endif /* SHADOWPW */
211     char *data, *p, *q;
212     char        password[PASSWDLEN + 1] = "\0";
213     static const char *loginok = "0\r";
214     int ulen;
215
216     data = (char *)malloc(stop - start + 1);
217     strncpy(data, start, stop - start + 1);
218
219     /* We are looking for the following format in data:
220      * (username) (password)
221      *
222      * Let's hope username doesn't contain ") ("!
223      */
224
225     /* Parse input for username in () */
226     if ((p = strchr(data, '(' )) == NULL) {
227         syslog(LOG_INFO,"Bad Login ClearTxtUAM: username not found in string");
228         free(data);
229         return(-1);
230     }
231     p++;
232     if ((q = strstr(data, ") (" )) == NULL) {
233         syslog(LOG_INFO,"Bad Login ClearTxtUAM: username not found in string");
234         free(data);
235         return(-1);
236     }
237     strncpy(username, p, q - p);
238
239     /* Parse input for password in next () */
240     p = q + 3;
241     if ((q = strrchr(data, ')' )) == NULL) {
242         syslog(LOG_INFO,"Bad Login ClearTxtUAM: password not found in string");
243         free(data);
244         return(-1);
245     }
246     strncpy(password, p, q - p);
247
248     /* Done copying username and password, clean up */
249     free(data);
250
251     ulen = strlen(username);
252
253     if (( pwd = uam_getname(username, ulen)) == NULL ) {
254         syslog(LOG_INFO, "Bad Login ClearTxtUAM: ( %s ) not found ",
255                         username);
256         return(-1);
257     }
258
259     if (uam_checkuser(pwd) < 0) {
260         /* syslog of error happens in uam_checkuser */
261         return(-1);
262     }
263
264 #ifdef SHADOWPW
265     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
266         syslog(LOG_INFO, "Bad Login ClearTxtUAM: no shadow passwd entry for %s", 
267                         username);
268         return(-1);
269     }
270     pwd->pw_passwd = sp->sp_pwdp;
271 #endif /* SHADOWPW */
272
273     if (!pwd->pw_passwd) {
274         syslog(LOG_INFO, "Bad Login ClearTxtUAM: no password for %s",
275                         username);
276         return(-1);
277     }
278
279 #ifdef AFS
280     if ( kcheckuser( pwd, password) == 0)
281         return(0);
282 #endif /* AFS */
283
284     p = crypt(password, pwd->pw_passwd);
285     if (strcmp(p, pwd->pw_passwd) != 0) {
286         syslog(LOG_INFO, "Bad Login ClearTxtUAM: %s: bad password", username);
287         return(-1);
288     }
289
290     /* Login successful */
291     append(out, loginok, strlen(loginok));
292     syslog(LOG_INFO, "Login ClearTxtUAM: %s", username);
293     return(0);
294 }
295
296
297 static int uam_setup(const char *path)
298 {
299     if (uam_register(UAM_SERVER_LOGIN, path, "Cleartxt Passwrd",
300                      passwd_login, NULL, NULL) < 0)
301         return -1;
302     if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
303                      passwd_printer) < 0)
304         return -1;
305
306     return 0;
307 }
308
309 static void uam_cleanup(void)
310 {
311     uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
312     uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
313 }
314
315 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
316             UAM_MODULE_SERVER,
317             UAM_MODULE_VERSION,
318             uam_setup, uam_cleanup
319         };