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