2 * $Id: uams_passwd.c,v 1.19.2.1.2.7 2004-02-20 20:53:14 bfernhomberg 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 /* crypt needs _XOPEN_SOURCE 500 at least on BSD, but that breaks Solaris compile */
15 #define _XOPEN_SOURCE 500 /* for crypt() */
23 #else /* STDC_HEADERS */
27 #endif /* HAVE_STRCHR */
28 char *strchr (), *strrchr ();
30 #define memcpy(d,s,n) bcopy ((s), (d), (n))
31 #define memmove(d,s,n) bcopy ((s), (d), (n))
32 #endif /* ! HAVE_MEMCPY */
33 #endif /* STDC_HEADERS */
36 #endif /* HAVE_UNISTD_H */
39 #endif /* ! HAVE_CRYPT_H */
41 #ifdef HAVE_SYS_TIME_H
51 #include <atalk/afp.h>
52 #include <atalk/logger.h>
53 #include <atalk/uam.h>
54 #include <atalk/util.h>
59 #define MIN(a,b) ((a) < (b) ? (a) : (b))
67 static char *clientname;
70 extern void append(void *, const char *, int);
72 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
73 char *ibuf, int ibuflen,
74 char *rbuf, int *rbuflen)
84 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
85 (void *) &clientname, NULL ) < 0 )
89 if (ibuflen < PASSWDLEN) {
90 return( AFPERR_PARAM );
92 ibuf[ PASSWDLEN ] = '\0';
94 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
98 LOG(log_info, logtype_uams, "cleartext login: %s", username);
100 if (uam_checkuser(pwd) < 0) {
101 LOG(log_info, logtype_uams, "not a valid user");
102 return AFPERR_NOTAUTH;
106 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
107 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
108 return AFPERR_NOTAUTH;
110 pwd->pw_passwd = sp->sp_pwdp;
112 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
113 time_t now = time(NULL) / (60*60*24);
114 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
115 if ( expire_days < 0 ) {
116 LOG(log_info, logtype_uams, "Password for user %s expired", username);
117 err = AFPERR_PWDEXPR;
120 #endif /* SHADOWPW */
122 if (!pwd->pw_passwd) {
123 return AFPERR_NOTAUTH;
134 uam_afp_getcmdline( &ac, &av );
135 sprintf( hostname, "%s@%s", username, clientname );
137 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
138 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
139 return AFPERR_NOTAUTH;
144 p = crypt( ibuf, pwd->pw_passwd );
145 if ( strcmp( p, pwd->pw_passwd ) == 0 )
149 return AFPERR_NOTAUTH;
154 static int passwd_login(void *obj, struct passwd **uam_pwd,
155 char *ibuf, int ibuflen,
156 char *rbuf, int *rbuflen)
163 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
164 (void *) &username, &ulen) < 0)
168 return( AFPERR_PARAM );
171 len = (unsigned char) *ibuf++;
173 if (!len || len > ibuflen || len > ulen ) {
174 return( AFPERR_PARAM );
176 memcpy(username, ibuf, len );
179 username[ len ] = '\0';
181 if ((unsigned long) ibuf & 1) { /* pad character */
185 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
189 /* cleartxt login ext
192 2 bytes len (network order)
193 len bytes unicode name
195 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
196 char *ibuf, int ibuflen,
197 char *rbuf, int *rbuflen)
205 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
206 (void *) &username, &ulen) < 0)
212 memcpy(&temp16, uname, sizeof(temp16));
214 if (!len || len > ulen ) {
215 return( AFPERR_PARAM );
217 memcpy(username, uname +2, len );
218 username[ len ] = '\0';
219 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
225 static int passwd_changepw(void *obj, char *username,
226 struct passwd *pwd, char *ibuf,
227 int ibuflen, char *rbuf, int *rbuflen)
231 #endif /* SHADOWPW */
232 char pw[PASSWDLEN + 1], *p;
233 uid_t uid = geteuid();
235 if (uam_checkuser(pwd) < 0)
236 return AFPERR_ACCESS;
239 memcpy(pw, ibuf, PASSWDLEN);
240 memset(ibuf, 0, PASSWDLEN);
241 pw[PASSWDLEN] = '\0';
244 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
245 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
248 pwd->pw_passwd = sp->sp_pwdp;
249 #endif /* SHADOWPW */
251 p = crypt(pw, pwd->pw_passwd );
252 if (strcmp( p, pwd->pw_passwd )) {
253 memset(pw, 0, sizeof(pw));
254 return AFPERR_NOTAUTH;
259 ibuf[PASSWDLEN] = '\0';
263 #endif /* SHADOWPW */
269 /* Printer ClearTxtUAM login */
270 static int passwd_printer(start, stop, username, out)
271 char *start, *stop, *username;
277 #endif /* SHADOWPW */
279 char password[PASSWDLEN + 1] = "\0";
280 static const char *loginok = "0\r";
283 data = (char *)malloc(stop - start + 1);
285 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
288 strlcpy(data, start, stop - start + 1);
290 /* We are looking for the following format in data:
291 * (username) (password)
293 * Let's hope username doesn't contain ") ("!
296 /* Parse input for username in () */
297 if ((p = strchr(data, '(' )) == NULL) {
298 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
303 if ((q = strstr(p, ") (" )) == NULL) {
304 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
308 memcpy(username, p, MIN( UAM_USERNAMELEN, q - p ));
310 /* Parse input for password in next () */
312 if ((q = strrchr(p , ')' )) == NULL) {
313 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
317 memcpy(password, p, MIN(PASSWDLEN, q - p) );
319 /* Done copying username and password, clean up */
322 ulen = strlen(username);
324 if (( pwd = uam_getname(NULL, username, ulen)) == NULL ) {
325 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
330 if (uam_checkuser(pwd) < 0) {
331 /* syslog of error happens in uam_checkuser */
336 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
337 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
341 pwd->pw_passwd = sp->sp_pwdp;
343 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
344 time_t now = time(NULL) / (60*60*24);
345 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
346 if ( expire_days < 0 ) {
347 LOG(log_info, logtype_uams, "Password for user %s expired", username);
352 #endif /* SHADOWPW */
354 if (!pwd->pw_passwd) {
355 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
361 if ( kcheckuser( pwd, password) == 0)
365 p = crypt(password, pwd->pw_passwd);
366 if (strcmp(p, pwd->pw_passwd) != 0) {
367 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
371 /* Login successful */
372 append(out, loginok, strlen(loginok));
373 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
378 int uam_setup(const char *path)
380 if (uam_register_fn(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
381 passwd_login, NULL, NULL, passwd_login_ext) < 0)
383 if (uam_register_fn(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
390 static int uam_setup(const char *path)
392 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
393 passwd_login, NULL, NULL, passwd_login_ext) < 0)
395 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
404 static void uam_cleanup(void)
406 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
407 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
410 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
413 uam_setup, uam_cleanup