2 * Copyright (c) 1990,1993 Regents of The University of Michigan.
3 * Copyright (c) 1999 Adrian Sun (asun@u.washington.edu)
4 * All Rights Reserved. See COPYRIGHT.
9 #endif /* HAVE_CONFIG_H */
11 #include <sys/types.h>
12 /* crypt needs _XOPEN_SOURCE (500) at least on BSD, but that breaks Solaris compile */
14 #define _XOPEN_SOURCE 500 /* for crypt() */
17 #define _XOPEN_SOURCE /* for crypt() */
26 #endif /* ! HAVE_CRYPT_H */
34 #include <atalk/afp.h>
35 #include <atalk/logger.h>
36 #include <atalk/uam.h>
37 #include <atalk/util.h>
42 #define MIN(a,b) ((a) < (b) ? (a) : (b))
50 static const char *clientname;
53 /*XXX in etc/papd/file.h */
55 extern UAM_MODULE_EXPORT void append(struct papfile *, const char *, int);
57 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
58 char *ibuf, size_t ibuflen,
59 char *rbuf _U_, size_t *rbuflen _U_)
69 if( uam_afpserver_option( obj, UAM_OPTION_CLIENTNAME,
70 (void *) &clientname, NULL ) < 0 )
74 if (ibuflen < PASSWDLEN) {
75 return( AFPERR_PARAM );
77 ibuf[ PASSWDLEN ] = '\0';
79 if (( pwd = uam_getname(obj, username, ulen)) == NULL ) {
80 return AFPERR_NOTAUTH;
83 LOG(log_info, logtype_uams, "cleartext login: %s", username);
85 if (uam_checkuser(pwd) < 0) {
86 LOG(log_info, logtype_uams, "not a valid user");
87 return AFPERR_NOTAUTH;
91 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
92 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
93 return AFPERR_NOTAUTH;
95 pwd->pw_passwd = sp->sp_pwdp;
97 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
98 time_t now = time(NULL) / (60*60*24);
99 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
100 if ( expire_days < 0 ) {
101 LOG(log_info, logtype_uams, "Password for user %s expired", username);
102 err = AFPERR_PWDEXPR;
105 #endif /* SHADOWPW */
107 if (!pwd->pw_passwd) {
108 return AFPERR_NOTAUTH;
119 uam_afp_getcmdline( &ac, &av );
120 sprintf( hostname, "%s@%s", username, clientname );
122 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
123 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
124 return AFPERR_NOTAUTH;
129 p = crypt( ibuf, pwd->pw_passwd );
130 if ( strcmp( p, pwd->pw_passwd ) == 0 )
134 return AFPERR_NOTAUTH;
139 static int passwd_login(void *obj, struct passwd **uam_pwd,
140 char *ibuf, size_t ibuflen,
141 char *rbuf, size_t *rbuflen)
148 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
149 (void *) &username, &ulen) < 0)
153 return( AFPERR_PARAM );
156 len = (unsigned char) *ibuf++;
158 if (!len || len > ibuflen || len > ulen ) {
159 return( AFPERR_PARAM );
161 memcpy(username, ibuf, len );
164 username[ len ] = '\0';
166 if ((unsigned long) ibuf & 1) { /* pad character */
170 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
174 /* cleartxt login ext
177 2 bytes len (network order)
178 len bytes unicode name
180 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
181 char *ibuf, size_t ibuflen,
182 char *rbuf, size_t *rbuflen)
190 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
191 (void *) &username, &ulen) < 0)
197 memcpy(&temp16, uname, sizeof(temp16));
199 if (!len || len > ulen ) {
200 return( AFPERR_PARAM );
202 memcpy(username, uname +2, len );
203 username[ len ] = '\0';
204 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
210 static int passwd_changepw(void *obj, char *username,
211 struct passwd *pwd, char *ibuf,
212 size_t ibuflen, char *rbuf, size_t *rbuflen)
216 #endif /* SHADOWPW */
217 char pw[PASSWDLEN + 1], *p;
218 uid_t uid = geteuid();
220 if (uam_checkuser(pwd) < 0)
221 return AFPERR_ACCESS;
224 memcpy(pw, ibuf, PASSWDLEN);
225 memset(ibuf, 0, PASSWDLEN);
226 pw[PASSWDLEN] = '\0';
229 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
230 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
233 pwd->pw_passwd = sp->sp_pwdp;
234 #endif /* SHADOWPW */
236 p = crypt(pw, pwd->pw_passwd );
237 if (strcmp( p, pwd->pw_passwd )) {
238 memset(pw, 0, sizeof(pw));
239 return AFPERR_NOTAUTH;
244 ibuf[PASSWDLEN] = '\0';
248 #endif /* SHADOWPW */
254 /* Printer ClearTxtUAM login */
255 static int passwd_printer(char *start, char *stop, char *username, struct papfile *out)
260 #endif /* SHADOWPW */
262 char password[PASSWDLEN + 1] = "\0";
263 static const char *loginok = "0\r";
266 data = (char *)malloc(stop - start + 1);
268 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
271 strlcpy(data, start, stop - start + 1);
273 /* We are looking for the following format in data:
274 * (username) (password)
276 * Let's hope username doesn't contain ") ("!
279 /* Parse input for username in () */
280 if ((p = strchr(data, '(' )) == NULL) {
281 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
286 if ((q = strstr(p, ") (" )) == NULL) {
287 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
291 memcpy(username, p, MIN( UAM_USERNAMELEN, q - p ));
293 /* Parse input for password in next () */
295 if ((q = strrchr(p , ')' )) == NULL) {
296 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
300 memcpy(password, p, MIN(PASSWDLEN, q - p) );
302 /* Done copying username and password, clean up */
305 ulen = strlen(username);
307 if (( pwd = uam_getname(NULL, username, ulen)) == NULL ) {
308 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
313 if (uam_checkuser(pwd) < 0) {
314 /* syslog of error happens in uam_checkuser */
319 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
320 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
324 pwd->pw_passwd = sp->sp_pwdp;
326 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
327 time_t now = time(NULL) / (60*60*24);
328 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
329 if ( expire_days < 0 ) {
330 LOG(log_info, logtype_uams, "Password for user %s expired", username);
335 #endif /* SHADOWPW */
337 if (!pwd->pw_passwd) {
338 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
344 if ( kcheckuser( pwd, password) == 0)
348 p = crypt(password, pwd->pw_passwd);
349 if (strcmp(p, pwd->pw_passwd) != 0) {
350 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
354 /* Login successful */
355 append(out, loginok, strlen(loginok));
356 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
360 static int uam_setup(const char *path)
362 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
363 passwd_login, NULL, NULL, passwd_login_ext) < 0)
365 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
372 static void uam_cleanup(void)
374 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
375 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
378 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
381 uam_setup, uam_cleanup
384 UAM_MODULE_EXPORT struct uam_export uams_passwd = {
387 uam_setup, uam_cleanup