]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/auth.c
add missing ibuflen decrement
[netatalk.git] / etc / afpd / auth.c
index 4560b6bc4f6ed0e20e2c599d4bb2b30ed16acb44..150977351dd95c0631562c1553674f4e803078df 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: auth.c,v 1.44.2.3.2.3 2003-09-25 12:23:53 didg Exp $
+ * $Id: auth.c,v 1.44.2.3.2.14 2004-06-18 07:58:20 bfernhomberg Exp $
  *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
 #include <limits.h>
 #include <string.h>
 #include <ctype.h>
-
-#ifdef SHADOWPW
-#include <shadow.h>
-#endif /* SHADOWPW */
-
+#include <time.h>
 #include <pwd.h>
 #include <grp.h>
 #include <atalk/logger.h>
@@ -157,7 +153,66 @@ static int send_reply(const AFPObj *obj, const int err)
     return AFP_OK;
 }
 
-static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
+
+static int afp_errpwdexpired(obj, ibuf, ibuflen, rbuf, rbuflen )
+AFPObj      *obj;
+char   *ibuf, *rbuf;
+int            ibuflen, *rbuflen;
+{
+    *rbuflen = 0;
+    return AFPERR_PWDEXPR;
+}
+
+
+static int set_auth_switch(AFPObj *obj, int expired)
+{
+    int i;
+
+    if (expired) {
+       /* 
+        * BF: expired password handling
+        * to allow the user to change his/her password we have to allow login
+        * but every following call except for FPChangePassword will be thrown
+        * away with an AFPERR_PWDEXPR error. (thanks to Leland Wallace from Apple
+        * for clarifying this)
+         */
+
+       for (i=0; i<=0xff; i++) {
+           uam_afpserver_action(i, UAM_AFPSERVER_PREAUTH, afp_errpwdexpired, NULL); 
+       }
+        uam_afpserver_action(AFP_LOGOUT, UAM_AFPSERVER_PREAUTH, afp_logout, NULL); 
+       uam_afpserver_action(AFP_CHANGEPW, UAM_AFPSERVER_PREAUTH, afp_changepw, NULL);
+    }
+    else {
+        afp_switch = postauth_switch;
+        switch (afp_version) {
+        case 31:
+           uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL); 
+        case 30:
+           uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL); 
+           uam_afpserver_action(AFP_BYTELOCK_EXT,  UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL); 
+            /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
+               enumerate_ext */
+           uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL); 
+           uam_afpserver_action(AFP_GETSESSTOKEN,  UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL); 
+           uam_afpserver_action(AFP_READ_EXT,      UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL); 
+           uam_afpserver_action(AFP_WRITE_EXT,     UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL); 
+           uam_afpserver_action(AFP_DISCTOLDSESS,  UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL); 
+
+        case 22:
+            /*
+             * If first connection to a server is done in classic AFP2.2 version is used
+             * but OSX uses AFP3.x FPzzz command !
+            */
+           uam_afpserver_action(AFP_ZZZ,  UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL); 
+           break;
+        }
+    }
+
+    return AFP_OK;
+}
+
+static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expired)
 {
 #ifdef ADMIN_GRP
     int admin = 0;
@@ -175,6 +230,7 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
     LOG(log_info, logtype_afpd, "login %s (uid %d, gid %d) %s", pwd->pw_name,
         pwd->pw_uid, pwd->pw_gid , afp_versions[afp_version_index].av_name);
 
+#ifndef NO_DDP
     if (obj->proto == AFPPROTO_ASP) {
         ASP asp = obj->handle;
         int addr_net = ntohs( asp->asp_sat.sat_addr.s_net );
@@ -211,6 +267,7 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
             } /* if (addr_net && addr_node ) */
         } /* if (options->authprintdir) */
     } /* if (obj->proto == AFPPROTO_ASP) */
+#endif
 
     if (initgroups( pwd->pw_name, pwd->pw_gid ) < 0) {
 #ifdef RUN_AS_USER
@@ -225,9 +282,10 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
     /* Basically if the user is in the admin group, we stay root */
 
     if (( ngroups = getgroups( NGROUPS, groups )) < 0 ) {
-        LOG(log_error, logtype_afpd, "login: getgroups: %s", strerror(errno) );
+        LOG(log_error, logtype_afpd, "login: %s getgroups: %s", pwd->pw_name, strerror(errno) );
         return AFPERR_BADUAM;
     }
+
 #ifdef ADMIN_GRP
 #ifdef DEBUG
     LOG(log_info, logtype_afpd, "obj->options.admingid == %d", obj->options.admingid);
@@ -275,18 +333,15 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
             inet_ntoa( dsi->client.sin_addr ) );
 
         if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
-            LOG(log_error, logtype_afpd, "login: %s", strerror(errno) );
+            LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
             return AFPERR_BADUAM;
         }
     }
 #else /* TRU64 */
-#if 0
-        if (setregid(pwd->pw_gid, pwd->pw_gid ) < 0 || setreuid(pwd->pw_uid,pwd->pw_uid ) < 0) {
-#endif        
-        if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
-            LOG(log_error, logtype_afpd, "login: %s", strerror(errno) );
-            return AFPERR_BADUAM;
-        }
+    if (setegid( pwd->pw_gid ) < 0 || seteuid( pwd->pw_uid ) < 0) {
+        LOG(log_error, logtype_afpd, "login: %s %s", pwd->pw_name, strerror(errno) );
+        return AFPERR_BADUAM;
+    }
 #endif /* TRU64 */
 
     /* There's probably a better way to do this, but for now, we just
@@ -298,31 +353,15 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void))
 #endif /* ADMIN_GRP */
         uuid = pwd->pw_uid;
 
-    afp_switch = postauth_switch;
-    switch (afp_version) {
-    case 31:
-       uam_afpserver_action(AFP_ENUMERATE_EXT2, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext2, NULL); 
-    case 30:
-       uam_afpserver_action(AFP_ENUMERATE_EXT, UAM_AFPSERVER_POSTAUTH, afp_enumerate_ext, NULL); 
-       uam_afpserver_action(AFP_BYTELOCK_EXT,  UAM_AFPSERVER_POSTAUTH, afp_bytelock_ext, NULL); 
-        /* catsearch_ext uses the same packet as catsearch FIXME double check this, it wasn't true for enue
-           enumerate_ext */
-       uam_afpserver_action(AFP_CATSEARCH_EXT, UAM_AFPSERVER_POSTAUTH, afp_catsearch_ext, NULL); 
-       uam_afpserver_action(AFP_GETSESSTOKEN,  UAM_AFPSERVER_POSTAUTH, afp_getsession, NULL); 
-       uam_afpserver_action(AFP_READ_EXT,      UAM_AFPSERVER_POSTAUTH, afp_read_ext, NULL); 
-       uam_afpserver_action(AFP_WRITE_EXT,     UAM_AFPSERVER_POSTAUTH, afp_write_ext, NULL); 
-       uam_afpserver_action(AFP_DISCTOLDSESS,  UAM_AFPSERVER_POSTAUTH, afp_disconnect, NULL); 
-       uam_afpserver_action(AFP_ZZZ,  UAM_AFPSERVER_POSTAUTH, afp_zzz, NULL); 
+    set_auth_switch(obj, expired);
 
-       break;
-    }
     obj->logout = logout;
 
 #ifdef FORCE_UIDGID
     obj->force_uid = 1;
     save_uidgid ( &obj->uidgid );
-#endif                 
-
+#endif 
+       
     return( AFP_OK );
 }
 
@@ -348,6 +387,41 @@ unsigned int ibuflen, *rbuflen;
     rbuf += sizeof(retdata);
     return AFP_OK;
 }
+
+/* ---------------------- */
+static int create_session_token(AFPObj *obj)
+{
+    pid_t pid;
+
+    /* use 8 bytes for token as OSX, don't know if it helps */
+    if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
+       LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
+       return AFPERR_MISC;
+    }
+
+    if ( NULL == (obj->sinfo.sessiontoken = malloc(SESSIONTOKEN_LEN)) )
+       return AFPERR_MISC;
+
+    memset(obj->sinfo.sessiontoken, 0, SESSIONTOKEN_LEN);
+    obj->sinfo.sessiontoken_len = SESSIONTOKEN_LEN;
+    pid = getpid();
+    memcpy(obj->sinfo.sessiontoken, &pid, sizeof(pid_t));
+
+    return 0;
+}
+
+static int create_session_key(AFPObj *obj)
+{
+    /* create session key */
+    if (obj->sinfo.sessionkey == NULL) {
+        if (NULL == (obj->sinfo.sessionkey = malloc(SESSIONKEY_LEN)) )
+            return AFPERR_MISC;   
+        uam_random_string(obj, obj->sinfo.sessionkey, SESSIONKEY_LEN);
+        obj->sinfo.sessionkey_len = SESSIONKEY_LEN;
+    }
+    return AFP_OK;
+}
+
    
 /* ---------------------- */
 int afp_getsession(obj, ibuf, ibuflen, rbuf, rbuflen )
@@ -358,20 +432,26 @@ unsigned int ibuflen, *rbuflen;
     u_int16_t           type;
     u_int32_t           idlen = 0;
     u_int32_t          boottime;
-
-    u_int16_t           tklen, tp; /* FIXME: spec  u_int32_t? */
-    pid_t               token;
-    char               *p;
+    u_int32_t           tklen, tp;
+    char                *token;
+    char                *p;
 
     *rbuflen = 0;
+    tklen = 0;
 
     ibuf += 2;
     ibuflen -= 2;
 
-    memcpy(&type, ibuf, sizeof(type));    
+    memcpy(&type, ibuf, sizeof(type));
     type = ntohs(type);
     ibuf += sizeof(type);
     ibuflen -= sizeof(type);
+
+    if ( obj->sinfo.sessiontoken == NULL ) {
+        if ( create_session_token( obj ) )
+            return AFPERR_MISC;
+    }
+
     /*
      * 
     */
@@ -389,6 +469,8 @@ unsigned int ibuflen, *rbuflen;
                 return AFPERR_PARAM;
             }
             /* memcpy (id, ibuf, idlen) */
+            tklen = obj->sinfo.sessiontoken_len;
+            token = obj->sinfo.sessiontoken;
         }
         break;
     case 3: /* Jaguar */
@@ -404,31 +486,33 @@ unsigned int ibuflen, *rbuflen;
            if (ibuflen < idlen || idlen > (90-10)) {
                return AFPERR_PARAM;
            }
-           server_ipc_write(IPC_GETSESSION, idlen+8, p ); 
+           server_ipc_write(IPC_GETSESSION, idlen+8, p );
+           tklen = obj->sinfo.sessiontoken_len;
+           token = obj->sinfo.sessiontoken;
        }
        type = 0;
        break;
-    }
-    *rbuflen = sizeof(type);
-    type = htons(type);
-    memcpy(rbuf, &type, sizeof(type));
-    rbuf += sizeof(type);
+     case 8: /* Panther Kerberos Token */
+            tklen = obj->sinfo.cryptedkey_len;
+            token = obj->sinfo.cryptedkey;
+        break;
+     default:
+            return AFPERR_NOOP;
+        break;
 
-    *rbuflen += sizeof(tklen);
+    }
 
-    /* use at least 8 bytes for token as OSX, don't know if it helps */
-    tklen = sizeof(pid_t);
-    if (tklen < 8)
-        tklen = 8;
+    if (tklen == 0)
+        return AFPERR_MISC;
 
-    tp = htons(tklen);
+    tp = htonl(tklen);
     memcpy(rbuf, &tp, sizeof(tklen));
     rbuf += sizeof(tklen);
+    *rbuflen += sizeof(tklen);
 
+    memcpy(rbuf, token, tklen);
     *rbuflen += tklen;
-    memset(rbuf, 0, tklen);
-    token = getpid();
-    memcpy(rbuf, &token, sizeof(pid_t));
+
     return AFP_OK;
 }
 
@@ -442,10 +526,16 @@ int               ibuflen, *rbuflen;
 
     u_int32_t           tklen;
     pid_t               token;
+    int                 i;
 
     *rbuflen = 0;
     ibuf += 2;
 
+    /* check for guest user */
+    if ( 0 == (strcasecmp(obj->username, obj->options.guest)) ) {
+        return AFPERR_MISC;
+    }
+
     memcpy(&type, ibuf, sizeof(type));
     type = ntohs(type);
     ibuf += sizeof(type);
@@ -454,10 +544,24 @@ int               ibuflen, *rbuflen;
     tklen = ntohl(tklen);
     ibuf += sizeof(tklen);
 
-    if (tklen != sizeof(pid_t)) {
+    if ( sizeof(pid_t) > SESSIONTOKEN_LEN) {
+         LOG(log_error, logtype_afpd, "sizeof(pid_t) > %u", SESSIONTOKEN_LEN );
+         return AFPERR_MISC;
+    }
+    if (tklen != SESSIONTOKEN_LEN) {
         return AFPERR_MISC;
     }   
+    tklen = sizeof(pid_t);
     memcpy(&token, ibuf, tklen);
+
+    /* our stuff is pid + zero pad */
+    ibuf += tklen;
+    for (i = tklen; i < SESSIONTOKEN_LEN; i++, ibuf++) {
+         if (*ibuf != 0) {
+             return AFPERR_MISC;
+         }
+    }
+    
     /* killed old session, not easy */
     server_ipc_write(IPC_KILLTOKEN, tklen, &token);
     sleep(1);
@@ -542,11 +646,15 @@ int               ibuflen, *rbuflen;
     ibuf += len;
     ibuflen -= len;
 
+    if (AFP_OK != (i = create_session_key(obj)) )
+        return send_reply(obj, i);
+
     i = afp_uam->u.uam_login.login(obj, &pwd, ibuf, ibuflen, rbuf, rbuflen);
-    if (i || !pwd)
+
+    if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
         return send_reply(obj, i);
 
-    return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
+    return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
 }
 
 /* ---------------------- */
@@ -666,12 +774,17 @@ unsigned int      ibuflen, *rbuflen;
         ibuflen--;
     }
 
+    if (AFP_OK != (i = create_session_key(obj)) ) {
+        return send_reply(obj, i);
+    }
+
     /* FIXME user name are in UTF8 */    
     i = afp_uam->u.uam_login.login_ext(obj, username, &pwd, ibuf, ibuflen, rbuf, rbuflen);
-    if (i || !pwd)
+
+    if (!pwd || ( i != AFP_OK && i != AFPERR_PWDEXPR))
         return send_reply(obj, i);
 
-    return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
+    return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((i==AFPERR_PWDEXPR)?1:0)));
 }
 
 /* ---------------------- */
@@ -688,13 +801,13 @@ int               ibuflen, *rbuflen;
         return send_reply(obj, AFPERR_NOTAUTH );
     }
 
-    ibuf += 2;
+    ibuf += 2; ibuflen -= 2;
     err = afp_uam->u.uam_login.logincont(obj, &pwd, ibuf, ibuflen,
                                          rbuf, rbuflen);
-    if (err || !pwd)
+    if (!pwd || ( err != AFP_OK && err != AFPERR_PWDEXPR))
         return send_reply(obj, err);
 
-    return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout));
+    return send_reply(obj, login(obj, pwd, afp_uam->u.uam_login.logout, ((err==AFPERR_PWDEXPR)?1:0)));
 }
 
 
@@ -730,6 +843,12 @@ int                ibuflen, *rbuflen;
     *rbuflen = 0;
     ibuf += 2;
 
+    /* check if password change is allowed, OS-X ignores the flag.
+     * we shouldn't trust the client on this anyway.
+     * not sure about the "right" error code, NOOP for now */ 
+    if (!(obj->options.passwdbits & PASSWD_SET))
+         return AFPERR_NOOP;
+
     /* make sure we can deal w/ this uam */
     len = (unsigned char) *ibuf++;
     if ((uam = auth_uamfind(UAM_SERVER_CHANGEPW, ibuf, len)) == NULL)
@@ -772,6 +891,9 @@ int         ibuflen, *rbuflen;
     LOG(log_info, logtype_afpd, "password change %s.",
         (ret == AFPERR_AUTHCONT) ? "continued" :
         (ret ? "failed" : "succeeded"));
+    if ( ret == AFP_OK )
+       set_auth_switch(obj, 0);
+       
     return ret;
 }
 
@@ -854,7 +976,7 @@ int auth_register(const int type, struct uam_obj *uam)
         return -1;
 
     if (!(start = UAM_LIST(type)))
-        return 0; /* silently fail */
+        return 1; /* we don't know what to do with it, caller must free it */
 
     uam_attach(start, uam);
     return 0;
@@ -871,7 +993,7 @@ int auth_load(const char *path, const char *list)
     if (!path || !*path || !list || (len = strlen(path)) > sizeof(name) - 2)
         return -1;
 
-    strncpy(buf, list, sizeof(buf));
+    strlcpy(buf, list, sizeof(buf));
     if ((p = strtok(buf, ",")) == NULL)
         return -1;
 
@@ -882,7 +1004,7 @@ int auth_load(const char *path, const char *list)
     }
 
     while (p) {
-        strncpy(name + len, p, sizeof(name) - len);
+        strlcpy(name + len, p, sizeof(name) - len);
         LOG(log_debug, logtype_afpd, "uam: loading (%s)", name);
         /*
         if ((stat(name, &st) == 0) && (mod = uam_load(name, p))) {