2 * $Id: uams_passwd.c,v 1.17 2002-02-13 18:26:31 srittau Exp $
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.
11 #endif /* HAVE_CONFIG_H */
19 #else /* STDC_HEADERS */
23 #endif /* HAVE_STRCHR */
24 char *strchr (), *strrchr ();
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 */
33 #endif /* HAVE_UNISTD_H */
36 #endif /* ! NO_CRYPT_H */
38 #include <atalk/logger.h>
48 #include <atalk/afp.h>
49 #include <atalk/uam.h>
57 static char *clientname;
61 static int passwd_login(void *obj, struct passwd **uam_pwd,
62 char *ibuf, int ibuflen,
63 char *rbuf, int *rbuflen)
74 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
75 (void *) &username, &ulen) < 0)
79 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
80 (void *) &clientname, NULL ) < 0 )
85 return( AFPERR_PARAM );
88 len = (unsigned char) *ibuf++;
90 if (!len || len > ibuflen || len > ulen ) {
91 return( AFPERR_PARAM );
94 memcpy(username, ibuf, len );
97 username[ len ] = '\0';
99 if ((unsigned long) ibuf & 1) { /* pad character */
103 if (ibuflen < PASSWDLEN) {
104 return( AFPERR_PARAM );
106 ibuf[ PASSWDLEN ] = '\0';
108 if (( pwd = uam_getname(username, ulen)) == NULL ) {
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;
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;
123 pwd->pw_passwd = sp->sp_pwdp;
124 #endif /* SHADOWPW */
126 if (!pwd->pw_passwd) {
127 return AFPERR_NOTAUTH;
138 uam_afp_getcmdline( &ac, &av );
139 sprintf( hostname, "%s@%s", username, clientname );
141 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
142 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
143 return AFPERR_NOTAUTH;
148 p = crypt( ibuf, pwd->pw_passwd );
149 if ( strcmp( p, pwd->pw_passwd ) == 0 )
153 return AFPERR_NOTAUTH;
159 static int passwd_changepw(void *obj, char *username,
160 struct passwd *pwd, char *ibuf,
161 int ibuflen, char *rbuf, int *rbuflen)
165 #endif /* SHADOWPW */
166 char pw[PASSWDLEN + 1], *p;
167 uid_t uid = geteuid();
169 if (uam_checkuser(pwd) < 0)
170 return AFPERR_ACCESS;
173 memcpy(pw, ibuf, PASSWDLEN);
174 memset(ibuf, 0, PASSWDLEN);
175 pw[PASSWDLEN] = '\0';
178 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
179 LOG(log_info, logtype_default, "no shadow passwd entry for %s", username);
182 pwd->pw_passwd = sp->sp_pwdp;
183 #endif /* SHADOWPW */
185 p = crypt(pw, pwd->pw_passwd );
186 if (strcmp( p, pwd->pw_passwd )) {
187 memset(pw, 0, sizeof(pw));
188 return AFPERR_NOTAUTH;
193 ibuf[PASSWDLEN] = '\0';
197 #endif /* SHADOWPW */
203 /* Printer ClearTxtUAM login */
204 static int passwd_printer(start, stop, username, out)
205 char *start, *stop, *username;
211 #endif /* SHADOWPW */
213 char password[PASSWDLEN + 1] = "\0";
214 static const char *loginok = "0\r";
217 data = (char *)malloc(stop - start + 1);
218 strncpy(data, start, stop - start + 1);
220 /* We are looking for the following format in data:
221 * (username) (password)
223 * Let's hope username doesn't contain ") ("!
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");
233 if ((q = strstr(data, ") (" )) == NULL) {
234 LOG(log_info, logtype_default,"Bad Login ClearTxtUAM: username not found in string");
238 strncpy(username, p, q - p);
240 /* Parse input for password in next () */
242 if ((q = strrchr(data, ')' )) == NULL) {
243 LOG(log_info, logtype_default,"Bad Login ClearTxtUAM: password not found in string");
247 strncpy(password, p, q - p);
249 /* Done copying username and password, clean up */
252 ulen = strlen(username);
254 if (( pwd = uam_getname(username, ulen)) == NULL ) {
255 LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: ( %s ) not found ",
260 if (uam_checkuser(pwd) < 0) {
261 /* syslog of error happens in uam_checkuser */
266 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
267 LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
271 pwd->pw_passwd = sp->sp_pwdp;
272 #endif /* SHADOWPW */
274 if (!pwd->pw_passwd) {
275 LOG(log_info, logtype_default, "Bad Login ClearTxtUAM: no password for %s",
281 if ( kcheckuser( pwd, password) == 0)
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);
291 /* Login successful */
292 append(out, loginok, strlen(loginok));
293 LOG(log_info, logtype_default, "Login ClearTxtUAM: %s", username);
298 static int uam_setup(const char *path)
300 if (uam_register(UAM_SERVER_LOGIN, path, "Cleartxt Passwrd",
301 passwd_login, NULL, NULL) < 0)
303 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
310 static void uam_cleanup(void)
312 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
313 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
316 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
319 uam_setup, uam_cleanup