2 * $Id: uams_passwd.c,v 1.31 2010-03-30 12:44:35 franklahm 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 */
13 #include <sys/types.h>
14 /* crypt needs _XOPEN_SOURCE (500) at least on BSD, but that breaks Solaris compile */
16 #define _XOPEN_SOURCE 500 /* for crypt() */
19 #define _XOPEN_SOURCE /* for crypt() */
27 #endif /* HAVE_UNISTD_H */
30 #endif /* ! HAVE_CRYPT_H */
32 #ifdef HAVE_SYS_TIME_H
42 #include <atalk/afp.h>
43 #include <atalk/logger.h>
44 #include <atalk/uam.h>
45 #include <atalk/util.h>
50 #define MIN(a,b) ((a) < (b) ? (a) : (b))
58 static const char *clientname;
61 /*XXX in etc/papd/file.h */
63 extern UAM_MODULE_EXPORT void append(struct papfile *, const char *, int);
65 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
66 char *ibuf, size_t ibuflen,
67 char *rbuf _U_, size_t *rbuflen _U_)
77 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
78 (void *) &clientname, NULL ) < 0 )
82 if (ibuflen < PASSWDLEN) {
83 return( AFPERR_PARAM );
85 ibuf[ PASSWDLEN ] = '\0';
87 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
88 return AFPERR_NOTAUTH;
91 LOG(log_info, logtype_uams, "cleartext login: %s", username);
93 if (uam_checkuser(pwd) < 0) {
94 LOG(log_info, logtype_uams, "not a valid user");
95 return AFPERR_NOTAUTH;
99 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
100 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
101 return AFPERR_NOTAUTH;
103 pwd->pw_passwd = sp->sp_pwdp;
105 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
106 time_t now = time(NULL) / (60*60*24);
107 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
108 if ( expire_days < 0 ) {
109 LOG(log_info, logtype_uams, "Password for user %s expired", username);
110 err = AFPERR_PWDEXPR;
113 #endif /* SHADOWPW */
115 if (!pwd->pw_passwd) {
116 return AFPERR_NOTAUTH;
127 uam_afp_getcmdline( &ac, &av );
128 sprintf( hostname, "%s@%s", username, clientname );
130 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
131 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
132 return AFPERR_NOTAUTH;
137 p = crypt( ibuf, pwd->pw_passwd );
138 if ( strcmp( p, pwd->pw_passwd ) == 0 )
142 return AFPERR_NOTAUTH;
147 static int passwd_login(void *obj, struct passwd **uam_pwd,
148 char *ibuf, size_t ibuflen,
149 char *rbuf, size_t *rbuflen)
156 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
157 (void *) &username, &ulen) < 0)
161 return( AFPERR_PARAM );
164 len = (unsigned char) *ibuf++;
166 if (!len || len > ibuflen || len > ulen ) {
167 return( AFPERR_PARAM );
169 memcpy(username, ibuf, len );
172 username[ len ] = '\0';
174 if ((unsigned long) ibuf & 1) { /* pad character */
178 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
182 /* cleartxt login ext
185 2 bytes len (network order)
186 len bytes unicode name
188 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
189 char *ibuf, size_t ibuflen,
190 char *rbuf, size_t *rbuflen)
198 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
199 (void *) &username, &ulen) < 0)
205 memcpy(&temp16, uname, sizeof(temp16));
207 if (!len || len > ulen ) {
208 return( AFPERR_PARAM );
210 memcpy(username, uname +2, len );
211 username[ len ] = '\0';
212 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
218 static int passwd_changepw(void *obj, char *username,
219 struct passwd *pwd, char *ibuf,
220 size_t ibuflen, char *rbuf, size_t *rbuflen)
224 #endif /* SHADOWPW */
225 char pw[PASSWDLEN + 1], *p;
226 uid_t uid = geteuid();
228 if (uam_checkuser(pwd) < 0)
229 return AFPERR_ACCESS;
232 memcpy(pw, ibuf, PASSWDLEN);
233 memset(ibuf, 0, PASSWDLEN);
234 pw[PASSWDLEN] = '\0';
237 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
238 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
241 pwd->pw_passwd = sp->sp_pwdp;
242 #endif /* SHADOWPW */
244 p = crypt(pw, pwd->pw_passwd );
245 if (strcmp( p, pwd->pw_passwd )) {
246 memset(pw, 0, sizeof(pw));
247 return AFPERR_NOTAUTH;
252 ibuf[PASSWDLEN] = '\0';
256 #endif /* SHADOWPW */
262 /* Printer ClearTxtUAM login */
263 static int passwd_printer(char *start, char *stop, char *username, struct papfile *out)
268 #endif /* SHADOWPW */
270 char password[PASSWDLEN + 1] = "\0";
271 static const char *loginok = "0\r";
274 data = (char *)malloc(stop - start + 1);
276 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
279 strlcpy(data, start, stop - start + 1);
281 /* We are looking for the following format in data:
282 * (username) (password)
284 * Let's hope username doesn't contain ") ("!
287 /* Parse input for username in () */
288 if ((p = strchr(data, '(' )) == NULL) {
289 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
294 if ((q = strstr(p, ") (" )) == NULL) {
295 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
299 memcpy(username, p, MIN( UAM_USERNAMELEN, q - p ));
301 /* Parse input for password in next () */
303 if ((q = strrchr(p , ')' )) == NULL) {
304 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
308 memcpy(password, p, MIN(PASSWDLEN, q - p) );
310 /* Done copying username and password, clean up */
313 ulen = strlen(username);
315 if (( pwd = uam_getname(NULL, username, ulen)) == NULL ) {
316 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
321 if (uam_checkuser(pwd) < 0) {
322 /* syslog of error happens in uam_checkuser */
327 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
328 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
332 pwd->pw_passwd = sp->sp_pwdp;
334 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
335 time_t now = time(NULL) / (60*60*24);
336 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
337 if ( expire_days < 0 ) {
338 LOG(log_info, logtype_uams, "Password for user %s expired", username);
343 #endif /* SHADOWPW */
345 if (!pwd->pw_passwd) {
346 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
352 if ( kcheckuser( pwd, password) == 0)
356 p = crypt(password, pwd->pw_passwd);
357 if (strcmp(p, pwd->pw_passwd) != 0) {
358 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
362 /* Login successful */
363 append(out, loginok, strlen(loginok));
364 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
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",
380 static void uam_cleanup(void)
382 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
383 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
386 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
389 uam_setup, uam_cleanup
392 UAM_MODULE_EXPORT struct uam_export uams_passwd = {
395 uam_setup, uam_cleanup