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