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 */
33 #include <arpa/inet.h>
35 #include <atalk/afp.h>
36 #include <atalk/logger.h>
37 #include <atalk/uam.h>
38 #include <atalk/util.h>
39 #include <atalk/compat.h>
44 #define MIN(a,b) ((a) < (b) ? (a) : (b))
52 static const char *clientname;
55 /*XXX in etc/papd/file.h */
57 extern UAM_MODULE_EXPORT void append(struct papfile *, const char *, int);
59 static int pwd_login(void *obj, char *username, int ulen, struct passwd **uam_pwd,
60 char *ibuf, size_t ibuflen,
61 char *rbuf _U_, size_t *rbuflen _U_)
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(obj, username, ulen)) == NULL ) {
82 return AFPERR_NOTAUTH;
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;
99 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
100 time_t now = time(NULL) / (60*60*24);
101 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
102 if ( expire_days < 0 ) {
103 LOG(log_info, logtype_uams, "Password for user %s expired", username);
104 err = AFPERR_PWDEXPR;
107 #endif /* SHADOWPW */
109 if (!pwd->pw_passwd) {
110 return AFPERR_NOTAUTH;
121 uam_afp_getcmdline( &ac, &av );
122 sprintf( hostname, "%s@%s", username, clientname );
124 if( uam_sia_validate_user( NULL, ac, av, hostname, username,
125 NULL, FALSE, NULL, ibuf ) != SIASUCCESS )
126 return AFPERR_NOTAUTH;
131 p = crypt( ibuf, pwd->pw_passwd );
132 if ( strcmp( p, pwd->pw_passwd ) == 0 )
136 return AFPERR_NOTAUTH;
141 static int passwd_login(void *obj, struct passwd **uam_pwd,
142 char *ibuf, size_t ibuflen,
143 char *rbuf, size_t *rbuflen)
150 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
151 (void *) &username, &ulen) < 0)
155 return( AFPERR_PARAM );
158 len = (unsigned char) *ibuf++;
160 if (!len || len > ibuflen || len > ulen ) {
161 return( AFPERR_PARAM );
163 memcpy(username, ibuf, len );
166 username[ len ] = '\0';
168 if ((unsigned long) ibuf & 1) { /* pad character */
172 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
176 /* cleartxt login ext
179 2 bytes len (network order)
180 len bytes unicode name
182 static int passwd_login_ext(void *obj, char *uname, struct passwd **uam_pwd,
183 char *ibuf, size_t ibuflen,
184 char *rbuf, size_t *rbuflen)
192 if (uam_afpserver_option(obj, UAM_OPTION_USERNAME,
193 (void *) &username, &ulen) < 0)
199 memcpy(&temp16, uname, sizeof(temp16));
201 if (!len || len > ulen ) {
202 return( AFPERR_PARAM );
204 memcpy(username, uname +2, len );
205 username[ len ] = '\0';
206 return (pwd_login(obj, username, ulen, uam_pwd, ibuf, ibuflen, rbuf, rbuflen));
212 static int passwd_changepw(void *obj, char *username,
213 struct passwd *pwd, char *ibuf,
214 size_t ibuflen, char *rbuf, size_t *rbuflen)
218 #endif /* SHADOWPW */
219 char pw[PASSWDLEN + 1], *p;
220 uid_t uid = geteuid();
222 if (uam_checkuser(pwd) < 0)
223 return AFPERR_ACCESS;
226 memcpy(pw, ibuf, PASSWDLEN);
227 memset(ibuf, 0, PASSWDLEN);
228 pw[PASSWDLEN] = '\0';
231 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
232 LOG(log_info, logtype_uams, "no shadow passwd entry for %s", username);
235 pwd->pw_passwd = sp->sp_pwdp;
236 #endif /* SHADOWPW */
238 p = crypt(pw, pwd->pw_passwd );
239 if (strcmp( p, pwd->pw_passwd )) {
240 memset(pw, 0, sizeof(pw));
241 return AFPERR_NOTAUTH;
246 ibuf[PASSWDLEN] = '\0';
250 #endif /* SHADOWPW */
256 /* Printer ClearTxtUAM login */
257 static int passwd_printer(char *start, char *stop, char *username, struct papfile *out)
262 #endif /* SHADOWPW */
264 char password[PASSWDLEN + 1] = "\0";
265 static const char *loginok = "0\r";
268 data = (char *)malloc(stop - start + 1);
270 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: malloc");
273 strlcpy(data, start, stop - start + 1);
275 /* We are looking for the following format in data:
276 * (username) (password)
278 * Let's hope username doesn't contain ") ("!
281 /* Parse input for username in () */
282 if ((p = strchr(data, '(' )) == NULL) {
283 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
288 if ((q = strstr(p, ") (" )) == NULL) {
289 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: username not found in string");
293 memcpy(username, p, MIN( UAM_USERNAMELEN, q - p ));
295 /* Parse input for password in next () */
297 if ((q = strrchr(p , ')' )) == NULL) {
298 LOG(log_info, logtype_uams,"Bad Login ClearTxtUAM: password not found in string");
302 memcpy(password, p, MIN(PASSWDLEN, q - p) );
304 /* Done copying username and password, clean up */
307 ulen = strlen(username);
309 if (( pwd = uam_getname(NULL, username, ulen)) == NULL ) {
310 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: ( %s ) not found ",
315 if (uam_checkuser(pwd) < 0) {
316 /* syslog of error happens in uam_checkuser */
321 if (( sp = getspnam( pwd->pw_name )) == NULL ) {
322 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no shadow passwd entry for %s",
326 pwd->pw_passwd = sp->sp_pwdp;
328 if (sp && sp->sp_max != -1 && sp->sp_lstchg) {
329 time_t now = time(NULL) / (60*60*24);
330 int32_t expire_days = sp->sp_lstchg - now + sp->sp_max;
331 if ( expire_days < 0 ) {
332 LOG(log_info, logtype_uams, "Password for user %s expired", username);
337 #endif /* SHADOWPW */
339 if (!pwd->pw_passwd) {
340 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: no password for %s",
346 if ( kcheckuser( pwd, password) == 0)
350 p = crypt(password, pwd->pw_passwd);
351 if (strcmp(p, pwd->pw_passwd) != 0) {
352 LOG(log_info, logtype_uams, "Bad Login ClearTxtUAM: %s: bad password", username);
356 /* Login successful */
357 append(out, loginok, strlen(loginok));
358 LOG(log_info, logtype_uams, "Login ClearTxtUAM: %s", username);
362 static int uam_setup(const char *path)
364 if (uam_register(UAM_SERVER_LOGIN_EXT, path, "Cleartxt Passwrd",
365 passwd_login, NULL, NULL, passwd_login_ext) < 0)
367 if (uam_register(UAM_SERVER_PRINTAUTH, path, "ClearTxtUAM",
374 static void uam_cleanup(void)
376 uam_unregister(UAM_SERVER_LOGIN, "Cleartxt Passwrd");
377 uam_unregister(UAM_SERVER_PRINTAUTH, "ClearTxtUAM");
380 UAM_MODULE_EXPORT struct uam_export uams_clrtxt = {
383 uam_setup, uam_cleanup
386 UAM_MODULE_EXPORT struct uam_export uams_passwd = {
389 uam_setup, uam_cleanup