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