]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/messages.c
Replace relevant direct seteuid() calls with calls to (un)become_root()
[netatalk.git] / etc / afpd / messages.c
index 164bb200ab1c69e9b10f9ab55d1023c43af679b2..3c85f6a3a505c7fe9ae4c5ade65a5c44f997e708 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id: messages.c,v 1.22 2009-10-29 13:38:15 didg Exp $
- *
  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
  * All Rights Reserved.  See COPYRIGHT.
  */
@@ -9,9 +7,7 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif /* HAVE_UNISTD_H */
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
 #include <atalk/afp.h>
 #include <atalk/dsi.h>
 #include <atalk/util.h>
+#include <atalk/unix.h>
 #include <atalk/logger.h>
-#include "globals.h"
-#include "misc.h"
+#include <atalk/globals.h>
 
+#include "misc.h"
 
 #define MAXMESGSIZE 199
 
@@ -44,10 +41,9 @@ void readmessage(AFPObj *obj)
     unsigned int i; 
     int rc;
     static int c;
-    uid_t euid;
-    u_int32_t maxmsgsize;
+    uint32_t maxmsgsize;
 
-    maxmsgsize = (obj->proto == AFPPROTO_DSI)?MIN(MAX(((DSI*)obj->handle)->attn_quantum, MAXMESGSIZE),MAXPATHLEN):MAXMESGSIZE;
+    maxmsgsize = MIN(MAX(obj->dsi->attn_quantum, MAXMESGSIZE), MAXPATHLEN);
 
     i=0;
     /* Construct file name SERVERTEXT/message.[pid] */
@@ -64,7 +60,7 @@ void readmessage(AFPObj *obj)
 
     message=fopen(filename, "r");
     if (message==NULL) {
-        LOG(log_info, logtype_afpd, "Unable to open file %s", filename);
+        /* try without the process id */
         sprintf(filename, "%s/message", SERVERTEXT);
         message=fopen(filename, "r");
     }
@@ -81,22 +77,12 @@ void readmessage(AFPObj *obj)
         /* cleanup */
         fclose(message);
 
-        /* Save effective uid and switch to root to delete file. */
-        /* Delete will probably fail otherwise, but let's try anyways */
-        euid = geteuid();
-        if (seteuid(0) < 0) {
-            LOG(log_error, logtype_afpd, "Could not switch back to root: %s",
-                               strerror(errno));
-        }
+        become_root();
 
-        if ( 0 < (rc = unlink(filename)) )
+        if ((rc = unlink(filename)) != 0)
            LOG(log_error, logtype_afpd, "File '%s' could not be deleted", strerror(errno));
 
-        /* Drop privs again, failing this is very bad */
-        if (seteuid(euid) < 0) {
-            LOG(log_error, logtype_afpd, "Could not switch back to uid %d: %s", euid, strerror(errno));
-            exit(EXITERR_SYS);
-        }
+        unbecome_root();
 
         if (rc < 0) {
             LOG(log_error, logtype_afpd, "Error deleting %s: %s", filename, strerror(rc));
@@ -116,25 +102,34 @@ void readmessage(AFPObj *obj)
 int afp_getsrvrmesg(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, size_t *rbuflen)
 {
     char *message;
-    u_int16_t type, bitmap;
-    u_int16_t msgsize;
+    uint16_t type, bitmap;
+    uint16_t msgsize;
     size_t outlen = 0;
     size_t msglen = 0;
     int utf8 = 0;
 
     *rbuflen = 0;
 
-    msgsize = (obj->proto == AFPPROTO_DSI)?MAX(((DSI*)obj->handle)->attn_quantum, MAXMESGSIZE):MAXMESGSIZE;
+    msgsize = MAX(obj->dsi->attn_quantum, MAXMESGSIZE);
 
     memcpy(&type, ibuf + 2, sizeof(type));
     memcpy(&bitmap, ibuf + 4, sizeof(bitmap));
 
+    message = servermesg;
     switch (ntohs(type)) {
     case AFPMESG_LOGIN: /* login */
-        message = obj->options.loginmesg;
+        /* at least TIGER loses server messages
+         * if it receives a server msg attention before
+         * it has asked the login msg...
+         * Workaround: concatenate the two if any, ugly.
+         */
+        if (obj->options.loginmesg) {
+            if (*message)
+                strlcat(message, " - ", MAXMESGSIZE);
+            strlcat(message, obj->options.loginmesg, MAXMESGSIZE);
+        }
         break;
     case AFPMESG_SERVER: /* server */
-        message = servermesg;
         break;
     default:
         return AFPERR_BITMAP;
@@ -182,6 +177,6 @@ int afp_getsrvrmesg(AFPObj *obj, char *ibuf, size_t ibuflen _U_, char *rbuf, siz
        *rbuflen += 1;
     }
     *rbuflen += outlen;
-
+    *message = 0;
     return AFP_OK;
 }