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