]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/auth.c
Merge branch-2-1
[netatalk.git] / etc / afpd / auth.c
index eee06dfe66f9bbba4449bad708988ff6480b25bd..0a5727fad0304babb30cb3f7a2fd23504adb5eb4 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: auth.c,v 1.67 2009-10-13 22:55:36 didg Exp $
- *
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
  * All Rights Reserved.  See COPYRIGHT.
  */
@@ -48,7 +46,7 @@ extern void afp_get_cmdline( int *ac, char ***av );
 #include "status.h"
 #include "fork.h"
 #include "extattrs.h"
-#ifdef HAVE_NFSv4_ACLS
+#ifdef HAVE_ACLS
 #include "acls.h"
 #endif
 
@@ -74,15 +72,17 @@ int ngroups;
  * These numbers are scattered throughout the code.
  */
 static struct afp_versions  afp_versions[] = {
+#ifndef NO_DDP
     { "AFPVersion 1.1", 11 },
     { "AFPVersion 2.0", 20 },
     { "AFPVersion 2.1", 21 },
+#endif /* ! NO_DDP */
     { "AFP2.2", 22 },
 #ifdef AFP3x
     { "AFPX03", 30 },
     { "AFP3.1", 31 },
     { "AFP3.2", 32 }
-#endif
+#endif /* AFP3x */
 };
 
 static struct uam_mod uam_modules = {NULL, NULL, &uam_modules, &uam_modules};
@@ -94,17 +94,30 @@ static struct uam_obj uam_changepw = {"", "", 0, {{NULL, NULL, NULL, NULL}}, &ua
 static struct uam_obj *afp_uam = NULL;
 
 
-void status_versions( char *data)
+void status_versions( char *data, const ASP asp, const DSI *dsi)
 {
     char                *start = data;
     u_int16_t           status;
-    int         len, num, i;
+    int         len, num, i, count = 0;
 
     memcpy(&status, start + AFPSTATUS_VERSOFF, sizeof(status));
     num = sizeof( afp_versions ) / sizeof( afp_versions[ 0 ] );
+
+    for ( i = 0; i < num; i++ ) {
+#ifndef NO_DDP
+        if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
+#endif /* ! NO_DDP */
+        if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
+        count++;
+    }
     data += ntohs( status );
-    *data++ = num;
+    *data++ = count;
+
     for ( i = 0; i < num; i++ ) {
+#ifndef NO_DDP
+        if ( !asp && (afp_versions[ i ].av_number <= 21)) continue;
+#endif /* ! NO_DDP */
+        if ( !dsi && (afp_versions[ i ].av_number >= 22)) continue;
         len = strlen( afp_versions[ i ].av_name );
         *data++ = len;
         memcpy( data, afp_versions[ i ].av_name , len );
@@ -158,13 +171,15 @@ static int send_reply(const AFPObj *obj, const int err)
     return AFP_OK;
 }
 
-static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
+static int afp_errpwdexpired(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, 
+                             char *rbuf _U_, size_t *rbuflen)
 {
     *rbuflen = 0;
     return AFPERR_PWDEXPR;
 }
 
-static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
+static int afp_null_nolog(AFPObj *obj _U_, char *ibuf _U_, size_t ibuflen _U_, 
+                          char *rbuf _U_, size_t *rbuflen)
 {
     *rbuflen = 0;
     return( AFPERR_NOOP );
@@ -193,11 +208,11 @@ static int set_auth_switch(int expired)
         afp_switch = postauth_switch;
         switch (afp_version) {
         case 32:
-#ifdef HAVE_NFSv4_ACLS
+#ifdef HAVE_ACLS
             uam_afpserver_action(AFP_GETACL, UAM_AFPSERVER_POSTAUTH, afp_getacl, NULL);
             uam_afpserver_action(AFP_SETACL, UAM_AFPSERVER_POSTAUTH, afp_setacl, NULL);
             uam_afpserver_action(AFP_ACCESS, UAM_AFPSERVER_POSTAUTH, afp_access, NULL);
-#endif
+#endif /* HAVE_ACLS */
             uam_afpserver_action(AFP_GETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_getextattr, NULL);
             uam_afpserver_action(AFP_SETEXTATTR, UAM_AFPSERVER_POSTAUTH, afp_setextattr, NULL);
             uam_afpserver_action(AFP_REMOVEATTR, UAM_AFPSERVER_POSTAUTH, afp_remextattr, NULL);
@@ -246,8 +261,8 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expi
         return AFPERR_NOTAUTH;
     }
 
-    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);
+    LOG(log_note, logtype_afpd, "%s Login by %s",
+        afp_versions[afp_version_index].av_name, pwd->pw_name);
 
 #ifndef NO_DDP
     if (obj->proto == AFPPROTO_ASP) {
@@ -372,17 +387,40 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expi
     }
 #endif /* TRU64 */
 
-    /* There's probably a better way to do this, but for now, we just
-       play root */
+    if (ngroups > 0) {
+        #define GROUPSTR_BUFSIZE 1024
+        char groupsstr[GROUPSTR_BUFSIZE];
+        char *s = groupsstr;
+        int j = GROUPSTR_BUFSIZE;
+
+        int n = snprintf(groupsstr, GROUPSTR_BUFSIZE, "%u", groups[0]);
+        j -= n;
+        s += n;
+
+        for (int i = 1; i < ngroups; i++) {
+            n = snprintf(s, j, ", %u", groups[i]);
+            if (n == j) {
+                /* Buffer full */
+                LOG(log_debug, logtype_afpd, "login: group string buffer overflow");
+                break;
+            }
+            j -= n;
+            s += n;
+        }
+        LOG(log_debug, logtype_afpd, "login: %u supplementary groups: %s", ngroups, groupsstr);
+    }
 
+    /* There's probably a better way to do this, but for now, we just play root */
 #ifdef ADMIN_GRP
-    if (admin) uuid = 0;
+    if (admin)
+        uuid = 0;
     else
 #endif /* ADMIN_GRP */
         uuid = pwd->pw_uid;
 
     set_auth_switch(expired);
-
+    /* save our euid, we need it for preexec_close */
+    obj->uid = geteuid();
     obj->logout = logout;
 
 #ifdef FORCE_UIDGID
@@ -396,8 +434,8 @@ static int login(AFPObj *obj, struct passwd *pwd, void (*logout)(void), int expi
 /* ---------------------- */
 int afp_zzz ( /* Function 122 */
     AFPObj       *obj,
-    char         *ibuf _U_, unsigned int ibuflen _U_, 
-    char *rbuf, unsigned int *rbuflen)
+    char         *ibuf _U_, size_t ibuflen _U_, 
+    char *rbuf, size_t *rbuflen)
 {
     u_int32_t   retdata;
 
@@ -454,8 +492,8 @@ static int create_session_key(AFPObj *obj)
 /* ---------------------- */
 int afp_getsession(
     AFPObj *obj,
-    char   *ibuf, unsigned int ibuflen, 
-    char   *rbuf, unsigned int *rbuflen)
+    char   *ibuf, size_t ibuflen, 
+    char   *rbuf, size_t *rbuflen)
 {
     u_int16_t           type;
     u_int32_t           idlen = 0;
@@ -467,6 +505,10 @@ int afp_getsession(
     *rbuflen = 0;
     tklen = 0;
 
+    if (ibuflen < 2 + sizeof(type)) {
+        return AFPERR_PARAM;
+    }
+
     ibuf += 2;
     ibuflen -= 2;
 
@@ -546,7 +588,7 @@ int afp_getsession(
 }
 
 /* ---------------------- */
-int afp_disconnect(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf _U_, int *rbuflen)
+int afp_disconnect(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf _U_, size_t *rbuflen)
 {
     u_int16_t           type;
 
@@ -598,7 +640,7 @@ int afp_disconnect(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf _U_,
 }
 
 /* ---------------------- */
-static int get_version(AFPObj *obj, char *ibuf, int ibuflen, int len)
+static int get_version(AFPObj *obj, char *ibuf, size_t ibuflen, size_t len)
 {
     int num,i;
 
@@ -629,17 +671,18 @@ static int get_version(AFPObj *obj, char *ibuf, int ibuflen, int len)
 }
 
 /* ---------------------- */
-int afp_login(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
+int afp_login(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
 {
     struct passwd *pwd = NULL;
-    int     len, i;
+    size_t len;
+    int     i;
 
     *rbuflen = 0;
 
     if ( nologin & 1)
         return send_reply(obj, AFPERR_SHUTDOWN );
 
-    if (ibuflen <= 1)
+    if (ibuflen < 2)
         return send_reply(obj, AFPERR_BADVERS );
 
     ibuf++;
@@ -650,12 +693,12 @@ int afp_login(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
     if (i)
         return send_reply(obj, i );
 
+    if (ibuflen <= len)
+        return send_reply(obj, AFPERR_BADUAM);
+
     ibuf += len;
     ibuflen -= len;
 
-    if (ibuflen <= 1)
-        return send_reply(obj, AFPERR_BADUAM);
-
     len = (unsigned char) *ibuf++;
     ibuflen--;
 
@@ -679,10 +722,10 @@ int afp_login(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
 }
 
 /* ---------------------- */
-int afp_login_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen, char *rbuf, unsigned int *rbuflen)
+int afp_login_ext(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
 {
     struct passwd *pwd = NULL;
-    unsigned int  len;
+    size_t  len;
     int     i;
     char        type;
     u_int16_t   len16;
@@ -693,7 +736,7 @@ int afp_login_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen, char *rbuf, uns
     if ( nologin & 1)
         return send_reply(obj, AFPERR_SHUTDOWN );
 
-    if (ibuflen <= 4)
+    if (ibuflen < 5)
         return send_reply(obj, AFPERR_BADVERS );
 
     ibuf++;
@@ -708,12 +751,12 @@ int afp_login_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen, char *rbuf, uns
     if (i)
         return send_reply(obj, i );
 
+    if (ibuflen <= len)
+        return send_reply(obj, AFPERR_BADUAM);
+
     ibuf    += len;
     ibuflen -= len;
 
-    if (ibuflen <= 1)
-        return send_reply(obj, AFPERR_BADUAM);
-
     len = (unsigned char) *ibuf;
     ibuf++;
     ibuflen--;
@@ -731,7 +774,7 @@ int afp_login_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen, char *rbuf, uns
         return send_reply(obj, AFPERR_BADUAM);
     }
     /* user name */
-    if (len <= 1 +sizeof(len16))
+    if (ibuflen <= 1 +sizeof(len16))
         return send_reply(obj, AFPERR_PARAM);
     type = *ibuf;
     username = ibuf;
@@ -806,12 +849,12 @@ int afp_login_ext(AFPObj *obj, char *ibuf, unsigned int ibuflen, char *rbuf, uns
 }
 
 /* ---------------------- */
-int afp_logincont(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
+int afp_logincont(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
 {
     struct passwd *pwd = NULL;
     int err;
 
-    if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL ) {
+    if ( afp_uam == NULL || afp_uam->u.uam_login.logincont == NULL || ibuflen < 2 ) {
         *rbuflen = 0;
         return send_reply(obj, AFPERR_NOTAUTH );
     }
@@ -826,9 +869,9 @@ int afp_logincont(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen
 }
 
 
-int afp_logout(AFPObj *obj, char *ibuf _U_, int ibuflen  _U_, char *rbuf  _U_, int *rbuflen  _U_)
+int afp_logout(AFPObj *obj, char *ibuf _U_, size_t ibuflen  _U_, char *rbuf  _U_, size_t *rbuflen  _U_)
 {
-    LOG(log_info, logtype_afpd, "logout %s", obj->username);
+    LOG(log_note, logtype_afpd, "AFP logout by %s", obj->username);
     close_all_vol();
     obj->exit(0);
     return AFP_OK;
@@ -841,7 +884,7 @@ int afp_logout(AFPObj *obj, char *ibuf _U_, int ibuflen  _U_, char *rbuf  _U_, i
  *       to work. this also does a little pre-processing before it hands
  *       it off to the uam.
  */
-int afp_changepw(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
+int afp_changepw(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_t *rbuflen)
 {
     char username[MACFILELEN + 1], *start = ibuf;
     struct uam_obj *uam;
@@ -894,6 +937,9 @@ int afp_changepw(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
         return AFPERR_PARAM;
 
     /* send it off to the uam. we really don't use ibuflen right now. */
+    if (ibuflen < (size_t)(ibuf - start)) 
+        return AFPERR_PARAM;
+    
     ibuflen -= (ibuf - start);
     ret = uam->u.uam_changepw(obj, username, pwd, ibuf, ibuflen,
                               rbuf, rbuflen);
@@ -908,7 +954,7 @@ int afp_changepw(AFPObj *obj, char *ibuf, int ibuflen, char *rbuf, int *rbuflen)
 
 
 /* FPGetUserInfo */
-int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, int *rbuflen)
+int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     u_int8_t  thisuser;
     u_int32_t id;
@@ -951,10 +997,10 @@ int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, in
         *rbuflen += sizeof(id);
     }
 
-#ifdef HAVE_NFSv4_ACLS
+#ifdef HAVE_ACLS
     if (bitmap & USERIBIT_UUID) {
         int ret;
-        uuid_t uuid;
+        atalk_uuid_t uuid;
         char *uuidstring;
 
         if ( ! (obj->options.flags & OPTION_UUID))
@@ -973,7 +1019,7 @@ int afp_getuserinfo(AFPObj *obj _U_, char *ibuf, int ibuflen _U_, char *rbuf, in
         rbuf += UUID_BINSIZE;
         *rbuflen += UUID_BINSIZE;
     }
-#endif
+#endif /* HAVE_ACLS */
 
     LOG(log_debug, logtype_afpd, "END afp_getuserinfo:");
     return AFP_OK;
@@ -1045,9 +1091,9 @@ int auth_load(const char *path, const char *list)
         if (stat(name, &st) == 0) {
             if ((mod = uam_load(name, p))) {
                 uam_attach(&uam_modules, mod);
-                LOG(log_info, logtype_afpd, "uam: %s loaded", p);
+                LOG(log_debug, logtype_afpd, "uam: %s loaded", p);
             } else {
-                LOG(log_info, logtype_afpd, "uam: %s load failure",p);
+                LOG(log_error, logtype_afpd, "uam: %s load failure",p);
             }
         } else {
             LOG(log_info, logtype_afpd, "uam: uam not found (status=%d)", stat(name, &st));