]> arthur.barton.de Git - netatalk.git/blob - etc/uams/uams_passwd.c
apply parts of the netbsd build patch by Patrick Welche <prlw1@newn.cam.ac.uk>, mostl...
[netatalk.git] / etc / uams / uams_passwd.c
1 /*
2  * $Id: uams_passwd.c,v 1.22 2004-01-14 16:10:29 bfernhomberg 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 #define _XOPEN_SOURCE /* for crypt() */
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif /* HAVE_CONFIG_H */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 /* STDC check */
19 #if STDC_HEADERS
20 #include <string.h>
21 #else /* STDC_HEADERS */
22 #ifndef HAVE_STRCHR
23 #define strchr index
24 #define strrchr index
25 #endif /* HAVE_STRCHR */
26 char *strchr (), *strrchr ();
27 #ifndef HAVE_MEMCPY
28 #define memcpy(d,s,n) bcopy ((s), (d), (n))
29 #define memmove(d,s,n) bcopy ((s), (d), (n))
30 #endif /* ! HAVE_MEMCPY */
31 #endif /* STDC_HEADERS */
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif /* HAVE_UNISTD_H */
36 #ifdef HAVE_CRYPT_H
37 #include <crypt.h>
38 #endif /* HAVE_CRYPT_H */
39 #include <pwd.h>
40 #include <atalk/logger.h>
41
42 #ifdef SOLARIS
43 #define SHADOWPW
44 #endif /* SOLARIS */
45
46 #ifdef SHADOWPW
47 #include <shadow.h>
48 #endif /* SHADOWPW */
49
50 #include <atalk/afp.h>
51 #include <atalk/uam.h>
52
53 #define PASSWDLEN 8
54
55 #ifndef MIN
56 #define MIN(a,b) ((a) < (b) ? (a) : (b))
57 #endif /* MIN */
58
59
60 #ifdef TRU64
61 #include <sia.h>
62 #include <siad.h>
63
64 static char *clientname;
65 #endif /* TRU64 */
66
67 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
68                         char *ibuf, int ibuflen,
69                         char *rbuf, int *rbuflen)
70 {
71     char  *p;
72     struct passwd *pwd;
73 #ifdef SHADOWPW
74     struct spwd *sp;
75 #endif /* SHADOWPW */
76
77 #ifdef TRU64
78     if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
79                               (void *) &clientname, NULL ) < 0 )
80         return AFPERR_MISC;
81 #endif /* TRU64 */
82
83     if (ibuflen < PASSWDLEN) {
84         return( AFPERR_PARAM );
85     }
86     ibuf[ PASSWDLEN ] = '\0';
87
88     if (( pwd = uam_getname(username, ulen)) == NULL ) {
89         return AFPERR_PARAM;
90     }
91
92     LOG(log_info, logtype_uams, "cleartext login: %s", username);
93
94     if (uam_checkuser(pwd) < 0) {
95         LOG(log_info, logtype_uams, "not a valid user");
96         return AFPERR_NOTAUTH;
97     }
98
99 #ifdef SHADOWPW
100     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
101         LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
102         return AFPERR_NOTAUTH;
103     }
104     pwd->pw_passwd = sp->sp_pwdp;
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 AFP_OK;
127     }
128 #else /* TRU64 */
129     p = crypt( ibuf, pwd->pw_passwd );
130     if ( strcmp( p, pwd->pw_passwd ) == 0 )
131         return AFP_OK;
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, int ibuflen,
141                         char *rbuf, int *rbuflen)
142 {
143     char *username;
144     int 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 <= 1) {
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, int ibuflen,
182                         char *rbuf, int *rbuflen)
183 {
184     char       *username;
185     int        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                            int ibuflen, char *rbuf, int *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(start, stop, username, out)
256 char    *start, *stop, *username;
257 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 + 2);
269     if (!data) {
270         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
271         return(-1);
272     }
273     strncpy(data, start, stop - start + 1);
274     data[stop - start + 2] = 0;
275
276     /* We are looking for the following format in data:
277      * (username) (password)
278      *
279      * Let's hope username doesn't contain ") ("!
280      */
281
282     /* Parse input for username in () */
283     if ((p = strchr(data, '(' )) == NULL) {
284         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
285         free(data);
286         return(-1);
287     }
288     p++;
289     if ((q = strstr(data, ") (" )) == NULL) {
290         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
291         free(data);
292         return(-1);
293     }
294     strncpy(username, p, MIN( UAM_USERNAMELEN, (q - p)) );
295     username[ UAM_USERNAMELEN+1] = '\0';
296
297
298     /* Parse input for password in next () */
299     p = q + 3;
300     if ((q = strrchr(data, ')' )) == NULL) {
301         LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
302         free(data);
303         return(-1);
304     }
305     strncpy(password, p, MIN(PASSWDLEN, q - p) );
306     password[ PASSWDLEN+1] = '\0';
307
308
309     /* Done copying username and password, clean up */
310     free(data);
311
312     ulen = strlen(username);
313
314     if (( pwd = uam_getname(username, ulen)) == NULL ) {
315         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
316             username);
317         return(-1);
318     }
319
320     if (uam_checkuser(pwd) < 0) {
321         /* syslog of error happens in uam_checkuser */
322         return(-1);
323     }
324
325 #ifdef SHADOWPW
326     if (( sp = getspnam( pwd->pw_name )) == NULL ) {
327         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
328             username);
329         return(-1);
330     }
331     pwd->pw_passwd = sp->sp_pwdp;
332 #endif /* SHADOWPW */
333
334     if (!pwd->pw_passwd) {
335         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
336             username);
337         return(-1);
338     }
339
340 #ifdef AFS
341     if ( kcheckuser( pwd, password) == 0)
342         return(0);
343 #endif /* AFS */
344
345     p = crypt(password, pwd->pw_passwd);
346     if (strcmp(p, pwd->pw_passwd) != 0) {
347         LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
348         return(-1);
349     }
350
351     /* Login successful */
352     append(out, loginok, strlen(loginok));
353     LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
354     return(0);
355 }
356
357 #ifdef ATACC
358 int uam_setup(const char *path)
359 {
360     if (uam_register_fn(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
361                      passwd_login, NULL, NULL, passwd_login_ext) < 0)
362         return -1;
363     if (uam_register_fn(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
364                      passwd_printer) < 0)
365         return -1;
366
367     return 0;
368 }
369 #else 
370 static int uam_setup(const char *path)
371 {
372     if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
373                      passwd_login, NULL, NULL, passwd_login_ext) < 0)
374         return -1;
375     if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
376                      passwd_printer) < 0)
377         return -1;
378
379     return 0;
380 }
381
382 #endif
383
384 static void uam_cleanup(void)
385 {
386     uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
387     uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
388 }
389
390 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
391             UAM_MODULE_SERVER,
392             UAM_MODULE_VERSION,
393             uam_setup, uam_cleanup
394         };