2 * $Id: uams_passwd.c,v 1.19 2002-10-17 18:01:55 didg 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;
60 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
61 char *ibuf, int ibuflen,
62 char *rbuf, int *rbuflen)
71 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
72 (void *) &clientname, NULL ) < 0 )
76 if (ibuflen < PASSWDLEN) {
77 return( AFPERR_PARAM );
79 ibuf[ PASSWDLEN ] = '\0';
81 if (( pwd = uam_getname(username, ulen)) == NULL ) {
85 LOG(log_info, logtype_uams, "cleartext login: %s", username);
87 if (uam_checkuser(pwd) < 0) {
88 LOG(log_info, logtype_uams, "not a valid user");
89 return AFPERR_NOTAUTH;
93 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
94 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
95 return AFPERR_NOTAUTH;
97 pwd->pw_passwd = sp->sp_pwdp;
100 if (!pwd->pw_passwd) {
101 return AFPERR_NOTAUTH;
112 uam_afp_getcmdline( &ac, &av );
113 sprintf( hostname, "%s@%s", username, clientname );
115 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
116 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
117 return AFPERR_NOTAUTH;
122 p = crypt( ibuf, pwd->pw_passwd );
123 if ( strcmp( p, pwd->pw_passwd ) == 0 )
127 return AFPERR_NOTAUTH;
132 static int passwd_login(void *obj, struct passwd **uam_pwd,
133 char *ibuf, int ibuflen,
134 char *rbuf, int *rbuflen)
141 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
142 (void *) &username, &ulen) < 0)
146 return( AFPERR_PARAM );
149 len = (unsigned char) *ibuf++;
151 if (!len || len > ibuflen || len > ulen ) {
152 return( AFPERR_PARAM );
154 memcpy(username, ibuf, len );
157 username[ len ] = '\0';
159 if ((unsigned long) ibuf & 1) { /* pad character */
163 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
167 /* cleartxt login ext
170 2 bytes len (network order)
171 len bytes unicode name
173 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
174 char *ibuf, int ibuflen,
175 char *rbuf, int *rbuflen)
183 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
184 (void *) &username, &ulen) < 0)
190 memcpy(&temp16, uname, sizeof(temp16));
192 if (!len || len > ulen ) {
193 return( AFPERR_PARAM );
195 memcpy(username, uname +2, len );
196 username[ len ] = '\0';
197 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
203 static int passwd_changepw(void *obj, char *username,
204 struct passwd *pwd, char *ibuf,
205 int ibuflen, char *rbuf, int *rbuflen)
209 #endif /* SHADOWPW */
210 char pw[PASSWDLEN + 1], *p;
211 uid_t uid = geteuid();
213 if (uam_checkuser(pwd) < 0)
214 return AFPERR_ACCESS;
217 memcpy(pw, ibuf, PASSWDLEN);
218 memset(ibuf, 0, PASSWDLEN);
219 pw[PASSWDLEN] = '\0';
222 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
223 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
226 pwd->pw_passwd = sp->sp_pwdp;
227 #endif /* SHADOWPW */
229 p = crypt(pw, pwd->pw_passwd );
230 if (strcmp( p, pwd->pw_passwd )) {
231 memset(pw, 0, sizeof(pw));
232 return AFPERR_NOTAUTH;
237 ibuf[PASSWDLEN] = '\0';
241 #endif /* SHADOWPW */
247 /* Printer ClearTxtUAM login */
248 static int passwd_printer(start, stop, username, out)
249 char *start, *stop, *username;
255 #endif /* SHADOWPW */
257 char password[PASSWDLEN + 1] = "\0";
258 static const char *loginok = "0\r";
261 data = (char *)malloc(stop - start + 1);
262 strncpy(data, start, stop - start + 1);
264 /* We are looking for the following format in data:
265 * (username) (password)
267 * Let's hope username doesn't contain ") ("!
270 /* Parse input for username in () */
271 if ((p = strchr(data, '(' )) == NULL) {
272 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
277 if ((q = strstr(data, ") (" )) == NULL) {
278 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
282 strncpy(username, p, q - p);
284 /* Parse input for password in next () */
286 if ((q = strrchr(data, ')' )) == NULL) {
287 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
291 strncpy(password, p, q - p);
293 /* Done copying username and password, clean up */
296 ulen = strlen(username);
298 if (( pwd = uam_getname(username, ulen)) == NULL ) {
299 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
304 if (uam_checkuser(pwd) < 0) {
305 /* syslog of error happens in uam_checkuser */
310 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
311 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
315 pwd->pw_passwd = sp->sp_pwdp;
316 #endif /* SHADOWPW */
318 if (!pwd->pw_passwd) {
319 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
325 if ( kcheckuser( pwd, password) == 0)
329 p = crypt(password, pwd->pw_passwd);
330 if (strcmp(p, pwd->pw_passwd) != 0) {
331 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
335 /* Login successful */
336 append(out, loginok, strlen(loginok));
337 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
342 int uam_setup(const char *path)
344 if (uam_register_fn(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
345 passwd_login, NULL, NULL, passwd_login_ext) < 0)
347 if (uam_register_fn(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
354 static int uam_setup(const char *path)
356 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
357 passwd_login, NULL, NULL, passwd_login_ext) < 0)
359 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
368 static void uam_cleanup(void)
370 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
371 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
374 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
377 uam_setup, uam_cleanup