]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_passwd.c
fixes for papd authentication.
[netatalk.git] / etc / uams / uams_passwd.c
1 /*
2  * $Id: uams_passwd.c,v 1.19.2.1.2.6 2004-02-14 02:47:15 didg 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 #define _XOPEN_SOURCE 500 /* for crypt() */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 /* STDC check */
18 #if STDC_HEADERS
19 #include <string.h>
20 #else /* STDC_HEADERS */
21 #ifndef HAVE_STRCHR
22 #define strchr index
23 #define strrchr index
24 #endif /* HAVE_STRCHR */
25 char *strchr (), *strrchr ();
26 #ifndef HAVE_MEMCPY
27 #define memcpy(d,s,n) bcopy ((s), (d), (n))
28 #define memmove(d,s,n) bcopy ((s), (d), (n))
29 #endif /* ! HAVE_MEMCPY */
30 #endif /* STDC_HEADERS */
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34 #ifdef HAVE_CRYPT_H
35 #include <crypt.h>
36 #endif /* ! HAVE_CRYPT_H */
37 #include <pwd.h>
38 #ifdef HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #ifdef HAVE_TIME_H
42 #include <time.h>
43 #endif
44 #ifdef SHADOWPW
45 #include <shadow.h>
46 #endif /* SHADOWPW */
47
48 #include <atalk/afp.h>
49 #include <atalk/logger.h>
50 #include <atalk/uam.h>
51 #include <atalk/util.h>
52
53 #define PASSWDLEN 8
54
55 #ifndef MIN
56 #define MIN(a,b) ((a) < (b) ? (a) : (b))
57 #endif /* MIN */
58
59
60 #ifdef TRU64
61 #include <sia.h>
62 #include <siad.h>
63
64 static char *clientname;
65 #endif /* TRU64 */
66
67 extern void append(void *, const char *, int);
68
69 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
70                         char *ibuf, int ibuflen,
71                         char *rbuf, int *rbuflen)
72 {
73     char  *p;
74     struct passwd *pwd;
75     int err = AFP_OK;
76 #ifdef SHADOWPW
77     struct spwd *sp;
78 #endif /* SHADOWPW */
79
80 #ifdef TRU64
81     if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
82                               (void *) &clientname, NULL ) < 0 )
83         return AFPERR_MISC;
84 #endif /* TRU64 */
85
86     if (ibuflen < PASSWDLEN) {
87         return( AFPERR_PARAM );
88     }
89     ibuf[ PASSWDLEN ] = '\0';
90
91     if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
92         return AFPERR_PARAM;
93     }
94
95     LOG(log_info, logtype_uams, "cleartext login: %s", username);
96
97     if (uam_checkuser(pwd) < 0) {
98         LOG(log_info, logtype_uams, "not a valid user");
99         return AFPERR_NOTAUTH;
100     }
101
102 #ifdef SHADOWPW
103     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
104         LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
105         return AFPERR_NOTAUTH;
106     }
107     pwd->pw_passwd = sp->sp_pwdp;
108
109     if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
110         time_t now = time(NULL) / (60*60*24);
111         int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
112         if ( expire_days < 0 ) {
113                 LOG(log_info, logtype_uams, "Password for user %s expired", username);
114                 err = AFPERR_PWDEXPR;
115         }
116     }
117 #endif /* SHADOWPW */
118
119     if (!pwd->pw_passwd) {
120         return AFPERR_NOTAUTH;
121     }
122
123     *uam_pwd = pwd;
124
125 #ifdef TRU64
126     {
127         int ac;
128         char **av;
129         char hostname[256];
130
131         uam_afp_getcmdline( &ac, &av );
132         sprintf( hostname, "%s@%s", username, clientname );
133
134         if( uam_sia_validate_user( NULL, ac, av, hostname, username,
135                                    NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
136             return AFPERR_NOTAUTH;
137
138         return err;
139     }
140 #else /* TRU64 */
141     p = crypt( ibuf, pwd->pw_passwd );
142     if ( strcmp( p, pwd->pw_passwd ) == 0 )
143         return err;
144 #endif /* TRU64 */
145
146     return AFPERR_NOTAUTH;
147
148 }
149
150 /* cleartxt login */
151 static int passwd_login(void *obj, struct passwd **uam_pwd,
152                         char *ibuf, int ibuflen,
153                         char *rbuf, int *rbuflen)
154 {
155     char *username;
156     int len, ulen;
157
158     *rbuflen = 0;
159
160     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
161                              (void *) &username, &ulen) < 0)
162         return AFPERR_MISC;
163
164     if (ibuflen <= 1) {
165         return( AFPERR_PARAM );
166     }
167
168     len = (unsigned char) *ibuf++;
169     ibuflen--;
170     if (!len || len > ibuflen || len > ulen ) {
171         return( AFPERR_PARAM );
172     }
173     memcpy(username, ibuf, len );
174     ibuf += len;
175     ibuflen -=len;
176     username[ len ] = '\0';
177
178     if ((unsigned long) ibuf & 1) { /* pad character */
179         ++ibuf;
180         ibuflen--;
181     }
182     return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
183     
184 }
185
186 /* cleartxt login ext 
187  * uname format :
188     byte      3
189     2 bytes   len (network order)
190     len bytes unicode name
191 */
192 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
193                         char *ibuf, int ibuflen,
194                         char *rbuf, int *rbuflen)
195 {
196     char       *username;
197     int        len, ulen;
198     u_int16_t  temp16;
199
200     *rbuflen = 0;
201
202     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
203                              (void *) &username, &ulen) < 0)
204         return AFPERR_MISC;
205
206     if (*uname != 3)
207         return AFPERR_PARAM;
208     uname++;
209     memcpy(&temp16, uname, sizeof(temp16));
210     len = ntohs(temp16);
211     if (!len || len > ulen ) {
212         return( AFPERR_PARAM );
213     }
214     memcpy(username, uname +2, len );
215     username[ len ] = '\0';
216     return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
217 }
218                         
219
220 #if 0
221 /* change passwd */
222 static int passwd_changepw(void *obj, char *username,
223                            struct passwd *pwd, char *ibuf,
224                            int ibuflen, char *rbuf, int *rbuflen)
225 {
226 #ifdef SHADOWPW
227     struct spwd *sp;
228 #endif /* SHADOWPW */
229     char pw[PASSWDLEN + 1], *p;
230     uid_t uid = geteuid();
231
232     if (uam_checkuser(pwd) < 0)
233         return AFPERR_ACCESS;
234
235     /* old password */
236     memcpy(pw, ibuf, PASSWDLEN);
237     memset(ibuf, 0, PASSWDLEN);
238     pw[PASSWDLEN] = '\0';
239
240 #ifdef SHADOWPW
241     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
242         LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
243         return AFPERR_PARAM;
244     }
245     pwd->pw_passwd = sp->sp_pwdp;
246 #endif /* SHADOWPW */
247
248     p = crypt(pw, pwd->pw_passwd );
249     if (strcmp( p, pwd->pw_passwd )) {
250         memset(pw, 0, sizeof(pw));
251         return AFPERR_NOTAUTH;
252     }
253
254     /* new password */
255     ibuf += PASSWDLEN;
256     ibuf[PASSWDLEN] = '\0';
257
258 #ifdef SHADOWPW
259 #else /* SHADOWPW */
260 #endif /* SHADOWPW */
261     return AFP_OK;
262 }
263 #endif /* 0 */
264
265
266 /* Printer ClearTxtUAM login */
267 static int passwd_printer(start, stop, username, out)
268 char    *start, *stop, *username;
269 struct papfile  *out;
270 {
271     struct passwd *pwd;
272 #ifdef SHADOWPW
273     struct spwd *sp;
274 #endif /* SHADOWPW */
275     char *data, *p, *q;
276     char        password[PASSWDLEN + 1] = "\0";
277     static const char *loginok = "0\r";
278     int ulen;
279
280     data = (char *)malloc(stop - start + 1);
281     if (!data) {
282         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
283         return(-1);
284     }
285     strlcpy(data, start, stop - start + 1);
286
287     /* We are looking for the following format in data:
288      * (username) (password)
289      *
290      * Let's hope username doesn't contain ") ("!
291      */
292
293     /* Parse input for username in () */
294     if ((p = strchr(data, '(' )) == NULL) {
295         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
296         free(data);
297         return(-1);
298     }
299     p++;
300     if ((q = strstr(p, ") (" )) == NULL) {
301         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
302         free(data);
303         return(-1);
304     }
305     memcpy(username, p,  MIN( UAM_USERNAMELEN, q - p ));
306
307     /* Parse input for password in next () */
308     p = q + 3;
309     if ((q = strrchr(p , ')' )) == NULL) {
310         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
311         free(data);
312         return(-1);
313     }
314     memcpy(password, p, MIN(PASSWDLEN, q - p) );
315
316     /* Done copying username and password, clean up */
317     free(data);
318
319     ulen = strlen(username);
320
321     if (( pwd = uam_getname(NULL, username, ulen)) == NULL ) {
322         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
323             username);
324         return(-1);
325     }
326
327     if (uam_checkuser(pwd) < 0) {
328         /* syslog of error happens in uam_checkuser */
329         return(-1);
330     }
331
332 #ifdef SHADOWPW
333     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
334         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
335             username);
336         return(-1);
337     }
338     pwd->pw_passwd = sp->sp_pwdp;
339
340     if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
341         time_t now = time(NULL) / (60*60*24);
342         int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
343         if ( expire_days < 0 ) {
344                 LOG(log_info, logtype_uams, "Password for user %s expired", username);
345                 return (-1);
346         }
347     }
348
349 #endif /* SHADOWPW */
350
351     if (!pwd->pw_passwd) {
352         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
353             username);
354         return(-1);
355     }
356
357 #ifdef AFS
358     if ( kcheckuser( pwd, password) == 0)
359         return(0);
360 #endif /* AFS */
361
362     p = crypt(password, pwd->pw_passwd);
363     if (strcmp(p, pwd->pw_passwd) != 0) {
364         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
365         return(-1);
366     }
367
368     /* Login successful */
369     append(out, loginok, strlen(loginok));
370     LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
371     return(0);
372 }
373
374 #ifdef ATACC
375 int uam_setup(const char *path)
376 {
377     if (uam_register_fn(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
378                      passwd_login, NULL, NULL, passwd_login_ext) < 0)
379         return -1;
380     if (uam_register_fn(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
381                      passwd_printer) < 0)
382         return -1;
383
384     return 0;
385 }
386 #else 
387 static int uam_setup(const char *path)
388 {
389     if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
390                      passwd_login, NULL, NULL, passwd_login_ext) < 0)
391         return -1;
392     if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
393                      passwd_printer) < 0)
394         return -1;
395
396     return 0;
397 }
398
399 #endif
400
401 static void uam_cleanup(void)
402 {
403     uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
404     uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
405 }
406
407 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
408             UAM_MODULE_SERVER,
409             UAM_MODULE_VERSION,
410             uam_setup, uam_cleanup
411         };