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