]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_passwd.c
Sanity check this code more since it is run by root. Also clean up the style
[netatalk.git] / etc / uams / uams_passwd.c
1 /*
2  * $Id: uams_passwd.c,v 1.15 2002-01-24 16:31:20 jmarcus 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 <stdio.h>
14 #include <stdlib.h>
15
16 /* STDC check */
17 #if STDC_HEADERS
18 #include <string.h>
19 #else /* STDC_HEADERS */
20 #ifndef HAVE_STRCHR
21 #define strchr index
22 #define strrchr index
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
25 #ifndef HAVE_MEMCPY
26 #define memcpy(d,s,n) bcopy ((s), (d), (n))
27 #define memmove(d,s,n) bcopy ((s), (d), (n))
28 #endif /* ! HAVE_MEMCPY */
29 #endif /* STDC_HEADERS */
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34 #ifndef NO_CRYPT_H
35 #include <crypt.h>
36 #endif /* ! NO_CRYPT_H */
37 #include <pwd.h>
38 #include <atalk/logger.h>
39
40 #ifdef SOLARIS
41 #define SHADOWPW
42 #endif /* SOLARIS */
43
44 #ifdef SHADOWPW
45 #include <shadow.h>
46 #endif /* SHADOWPW */
47
48 #include <atalk/afp.h>
49 #include <atalk/uam.h>
50
51 #define PASSWDLEN 8
52
53 #ifdef TRU64
54 #include <sia.h>
55 #include <siad.h>
56
57 static char *clientname;
58 #endif /* TRU64 */
59
60 /* cleartxt login */
61 static int passwd_login(void *obj, struct passwd **uam_pwd,
62                         char *ibuf, int ibuflen,
63                         char *rbuf, int *rbuflen)
64 {
65     struct passwd *pwd;
66 #ifdef SHADOWPW
67     struct spwd *sp;
68 #endif /* SHADOWPW */
69     char *username, *p;
70     int len, ulen;
71
72     *rbuflen = 0;
73
74     if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
75                              (void *) &username, &ulen) < 0)
76         return AFPERR_MISC;
77
78 #ifdef TRU64
79     if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
80                               (void *) &clientname, NULL ) < 0 )
81         return AFPERR_MISC;
82 #endif /* TRU64 */
83
84     if (ibuflen <= 1) {
85         return( AFPERR_PARAM );
86     }
87
88     len = (unsigned char) *ibuf++;
89     ibuflen--;
90     if (!len || len > ibuflen || len > ulen ) {
91         return( AFPERR_PARAM );
92     }
93
94     memcpy(username, ibuf, len );
95     ibuf += len;
96     ibuflen -=len;
97     username[ len ] = '\0';
98
99     if ((unsigned long) ibuf & 1) { /* pad character */
100         ++ibuf;
101         ibuflen--;
102     }
103     if (ibuflen < PASSWDLEN) {
104         return( AFPERR_PARAM );
105     }
106     ibuf[ PASSWDLEN ] = '\0';
107
108     if (( pwd = uam_getname(username, ulen)) == NULL ) {
109         return AFPERR_PARAM;
110     }
111
112     LOG(log_info, logtype_default, "cleartext login: %s", username);
113     if (uam_checkuser(pwd) < 0) {
114         LOG(log_info, logtype_default, "not a valid user");
115         return AFPERR_NOTAUTH;
116     }
117
118 #ifdef SHADOWPW
119     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
120         LOG(log_info, logtype_default, "no shadow passwd entry for %s", username);
121         return AFPERR_NOTAUTH;
122     }
123     pwd->pw_passwd = sp->sp_pwdp;
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( sia_validate_user( NULL, ac, av, hostname, username,
142                                NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
143             return AFPERR_NOTAUTH;
144
145         return AFP_OK;
146     }
147 #else /* TRU64 */
148     p = crypt( ibuf, pwd->pw_passwd );
149     if ( strcmp( p, pwd->pw_passwd ) == 0 )
150         return AFP_OK;
151 #endif /* TRU64 */
152
153     return AFPERR_NOTAUTH;
154 }
155
156
157 #if 0
158 /* change passwd */
159 static int passwd_changepw(void *obj, char *username,
160                            struct passwd *pwd, char *ibuf,
161                            int ibuflen, char *rbuf, int *rbuflen)
162 {
163 #ifdef SHADOWPW
164     struct spwd *sp;
165 #endif /* SHADOWPW */
166     char pw[PASSWDLEN + 1], *p;
167     uid_t uid = geteuid();
168
169     if (uam_checkuser(pwd) < 0)
170         return AFPERR_ACCESS;
171
172     /* old password */
173     memcpy(pw, ibuf, PASSWDLEN);
174     memset(ibuf, 0, PASSWDLEN);
175     pw[PASSWDLEN] = '\0';
176
177 #ifdef SHADOWPW
178     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
179         LOG(log_info, logtype_default, "no shadow passwd entry for %s", username);
180         return AFPERR_PARAM;
181     }
182     pwd->pw_passwd = sp->sp_pwdp;
183 #endif /* SHADOWPW */
184
185     p = crypt(pw, pwd->pw_passwd );
186     if (strcmp( p, pwd->pw_passwd )) {
187         memset(pw, 0, sizeof(pw));
188         return AFPERR_NOTAUTH;
189     }
190
191     /* new password */
192     ibuf += PASSWDLEN;
193     ibuf[PASSWDLEN] = '\0';
194
195 #ifdef SHADOWPW
196 #else /* SHADOWPW */
197 #endif /* SHADOWPW */
198     return AFP_OK;
199 }
200 #endif /* 0 */
201
202
203 /* Printer ClearTxtUAM login */
204 static int passwd_printer(start, stop, username, out)
205 char    *start, *stop, *username;
206 struct papfile  *out;
207 {
208     struct passwd *pwd;
209 #ifdef SHADOWPW
210     struct spwd *sp;
211 #endif /* SHADOWPW */
212     char *data, *p, *q;
213     char        password[PASSWDLEN + 1] = "\0";
214     static const char *loginok = "0\r";
215     int ulen;
216
217     data = (char *)malloc(stop - start + 1);
218     strncpy(data, start, stop - start + 1);
219
220     /* We are looking for the following format in data:
221      * (username) (password)
222      *
223      * Let's hope username doesn't contain ") ("!
224      */
225
226     /* Parse input for username in () */
227     if ((p = strchr(data, '(' )) == NULL) {
228         LOG(log_info, logtype_default,"Bad Login ClearTxtUAM: username not found in string");
229         free(data);
230         return(-1);
231     }
232     p++;
233     if ((q = strstr(data, ") (" )) == NULL) {
234         LOG(log_info, logtype_default,"Bad Login ClearTxtUAM: username not found in string");
235         free(data);
236         return(-1);
237     }
238     strncpy(username, p, q - p);
239
240     /* Parse input for password in next () */
241     p = q + 3;
242     if ((q = strrchr(data, ')' )) == NULL) {
243         LOG(log_info, logtype_default,"Bad Login ClearTxtUAM: password not found in string");
244         free(data);
245         return(-1);
246     }
247     strncpy(password, p, q - p);
248
249     /* Done copying username and password, clean up */
250     free(data);
251
252     ulen = strlen(username);
253
254     if (( pwd = uam_getname(username, ulen)) == NULL ) {
255         LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: ( %s ) not found ",
256             username);
257         return(-1);
258     }
259
260     if (uam_checkuser(pwd) < 0) {
261         /* syslog of error happens in uam_checkuser */
262         return(-1);
263     }
264
265 #ifdef SHADOWPW
266     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
267         LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
268             username);
269         return(-1);
270     }
271     pwd->pw_passwd = sp->sp_pwdp;
272 #endif /* SHADOWPW */
273
274     if (!pwd->pw_passwd) {
275         LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: no password for %s",
276             username);
277         return(-1);
278     }
279
280 #ifdef AFS
281     if ( kcheckuser( pwd, password) == 0)
282         return(0);
283 #endif /* AFS */
284
285     p = crypt(password, pwd->pw_passwd);
286     if (strcmp(p, pwd->pw_passwd) != 0) {
287         LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: %s: bad password", username);
288         return(-1);
289     }
290
291     /* Login successful */
292     append(out, loginok, strlen(loginok));
293     LOG(log_info, logtype_default, "Login ClearTxtUAM: %s", username);
294     return(0);
295 }
296
297
298 static int uam_setup(const char *path)
299 {
300     if (uam_register(UAM_SERVER_LOGIN, path, "Cleartxt Passwrd",
301                      passwd_login, NULL, NULL) < 0)
302         return -1;
303     if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
304                      passwd_printer) < 0)
305         return -1;
306
307     return 0;
308 }
309
310 static void uam_cleanup(void)
311 {
312     uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
313     uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
314 }
315
316 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
317             UAM_MODULE_SERVER,
318             UAM_MODULE_VERSION,
319             uam_setup, uam_cleanup
320         };