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