2 * $Id: uams_passwd.c,v 1.20 2003-05-14 15:13:50 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>
54 #define MIN(a,b) ((a) < (b) ? (a) : (b))
62 static char *clientname;
65 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
66 char *ibuf, int ibuflen,
67 char *rbuf, int *rbuflen)
76 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
77 (void *) &clientname, NULL ) < 0 )
81 if (ibuflen < PASSWDLEN) {
82 return( AFPERR_PARAM );
84 ibuf[ PASSWDLEN ] = '\0';
86 if (( pwd = uam_getname(username, ulen)) == NULL ) {
90 LOG(log_info, logtype_uams, "cleartext login: %s", username);
92 if (uam_checkuser(pwd) < 0) {
93 LOG(log_info, logtype_uams, "not a valid user");
94 return AFPERR_NOTAUTH;
98 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
99 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
100 return AFPERR_NOTAUTH;
102 pwd->pw_passwd = sp->sp_pwdp;
103 #endif /* SHADOWPW */
105 if (!pwd->pw_passwd) {
106 return AFPERR_NOTAUTH;
117 uam_afp_getcmdline( &ac, &av );
118 sprintf( hostname, "%s@%s", username, clientname );
120 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
121 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
122 return AFPERR_NOTAUTH;
127 p = crypt( ibuf, pwd->pw_passwd );
128 if ( strcmp( p, pwd->pw_passwd ) == 0 )
132 return AFPERR_NOTAUTH;
137 static int passwd_login(void *obj, struct passwd **uam_pwd,
138 char *ibuf, int ibuflen,
139 char *rbuf, int *rbuflen)
146 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
147 (void *) &username, &ulen) < 0)
151 return( AFPERR_PARAM );
154 len = (unsigned char) *ibuf++;
156 if (!len || len > ibuflen || len > ulen ) {
157 return( AFPERR_PARAM );
159 memcpy(username, ibuf, len );
162 username[ len ] = '\0';
164 if ((unsigned long) ibuf & 1) { /* pad character */
168 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
172 /* cleartxt login ext
175 2 bytes len (network order)
176 len bytes unicode name
178 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
179 char *ibuf, int ibuflen,
180 char *rbuf, int *rbuflen)
188 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
189 (void *) &username, &ulen) < 0)
195 memcpy(&temp16, uname, sizeof(temp16));
197 if (!len || len > ulen ) {
198 return( AFPERR_PARAM );
200 memcpy(username, uname +2, len );
201 username[ len ] = '\0';
202 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
208 static int passwd_changepw(void *obj, char *username,
209 struct passwd *pwd, char *ibuf,
210 int ibuflen, char *rbuf, int *rbuflen)
214 #endif /* SHADOWPW */
215 char pw[PASSWDLEN + 1], *p;
216 uid_t uid = geteuid();
218 if (uam_checkuser(pwd) < 0)
219 return AFPERR_ACCESS;
222 memcpy(pw, ibuf, PASSWDLEN);
223 memset(ibuf, 0, PASSWDLEN);
224 pw[PASSWDLEN] = '\0';
227 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
228 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
231 pwd->pw_passwd = sp->sp_pwdp;
232 #endif /* SHADOWPW */
234 p = crypt(pw, pwd->pw_passwd );
235 if (strcmp( p, pwd->pw_passwd )) {
236 memset(pw, 0, sizeof(pw));
237 return AFPERR_NOTAUTH;
242 ibuf[PASSWDLEN] = '\0';
246 #endif /* SHADOWPW */
252 /* Printer ClearTxtUAM login */
253 static int passwd_printer(start, stop, username, out)
254 char *start, *stop, *username;
260 #endif /* SHADOWPW */
262 char password[PASSWDLEN + 1] = "\0";
263 static const char *loginok = "0\r";
266 data = (char *)malloc(stop - start + 2);
268 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
271 strncpy(data, start, stop - start + 1);
272 data[stop - start + 2] = 0;
274 /* We are looking for the following format in data:
275 * (username) (password)
277 * Let's hope username doesn't contain ") ("!
280 /* Parse input for username in () */
281 if ((p = strchr(data, '(' )) == NULL) {
282 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
287 if ((q = strstr(data, ") (" )) == NULL) {
288 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
292 strncpy(username, p, MIN( UAM_USERNAMELEN, (q - p)) );
293 username[ UAM_USERNAMELEN+1] = '\0';
296 /* Parse input for password in next () */
298 if ((q = strrchr(data, ')' )) == NULL) {
299 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
303 strncpy(password, p, MIN(PASSWDLEN, q - p) );
304 password[ PASSWDLEN+1] = '\0';
307 /* Done copying username and password, clean up */
310 ulen = strlen(username);
312 if (( pwd = uam_getname(username, ulen)) == NULL ) {
313 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
318 if (uam_checkuser(pwd) < 0) {
319 /* syslog of error happens in uam_checkuser */
324 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
325 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
329 pwd->pw_passwd = sp->sp_pwdp;
330 #endif /* SHADOWPW */
332 if (!pwd->pw_passwd) {
333 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
339 if ( kcheckuser( pwd, password) == 0)
343 p = crypt(password, pwd->pw_passwd);
344 if (strcmp(p, pwd->pw_passwd) != 0) {
345 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
349 /* Login successful */
350 append(out, loginok, strlen(loginok));
351 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
356 int uam_setup(const char *path)
358 if (uam_register_fn(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
359 passwd_login, NULL, NULL, passwd_login_ext) < 0)
361 if (uam_register_fn(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
368 static int uam_setup(const char *path)
370 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
371 passwd_login, NULL, NULL, passwd_login_ext) < 0)
373 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
382 static void uam_cleanup(void)
384 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
385 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
388 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
391 uam_setup, uam_cleanup