]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/afp_asp.c
Merge symlink branch
[netatalk.git] / etc / afpd / afp_asp.c
index 20db4b255447d5703b1885f7eb854b044c45fb21..ea40e58f8a9e45b027fe52a14a5f4101dbfcf2be 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: afp_asp.c,v 1.16 2002-03-24 01:23:40 sibaz Exp $
+ * $Id: afp_asp.c,v 1.28 2010-02-10 14:05:37 franklahm Exp $
  *
  * Copyright (c) 1997 Adrian Sun (asun@zoology.washington.edu)
  * Copyright (c) 1990,1993 Regents of The University of Michigan.
 #include "auth.h"
 #include "fork.h"
 
-extern struct oforks   *writtenfork;
+#ifdef FORCE_UIDGID
+#warning UIDGID
+#include "uid.h"
+#endif /* FORCE_UIDGID */
 
 static AFPObj *child;
 
-static __inline__ void afp_authprint_remove(AFPObj *);
+static void afp_authprint_remove(AFPObj *);
 
-static __inline__ void afp_asp_close(AFPObj *obj)
+static void afp_asp_close(AFPObj *obj)
 {
     ASP asp = obj->handle;
 
+    if (seteuid( obj->uid ) < 0) {
+        LOG(log_error, logtype_afpd, "can't seteuid back %s", strerror(errno));
+        exit(EXITERR_SYS);
+    }
+    close_all_vol();
     if (obj->options.authprintdir) afp_authprint_remove(obj);
 
     if (obj->logout)
@@ -59,7 +67,7 @@ static __inline__ void afp_asp_close(AFPObj *obj)
 }
 
 /* removes the authprint trailing when appropriate */
-static __inline__ void afp_authprint_remove(AFPObj *obj)
+static void afp_authprint_remove(AFPObj *obj)
 {
     ASP asp = obj->handle;
     char addr_filename[256];
@@ -73,7 +81,7 @@ static __inline__ void afp_authprint_remove(AFPObj *obj)
 
     memset( addr_filename_buff, 0, 256 );
 
-    if(stat(addr_filename, &cap_st) == 0) {
+    if(lstat(addr_filename, &cap_st) == 0) {
        if( S_ISREG(cap_st.st_mode) ) {
            int len;
            int capfd = open( addr_filename, O_RDONLY );
@@ -117,6 +125,9 @@ static __inline__ void afp_authprint_remove(AFPObj *obj)
     }
 }
 
+/* ------------------------
+ * SIGTERM
+*/
 static void afp_asp_die(const int sig)
 {
     ASP asp = child->handle;
@@ -133,7 +144,10 @@ static void afp_asp_die(const int sig)
         exit(sig);
 }
 
-static void afp_asp_timedown()
+/* -----------------------------
+ * SIGUSR1
+ */
+static void afp_asp_timedown(int sig _U_)
 {
     struct sigaction   sv;
     struct itimerval   it;
@@ -146,26 +160,60 @@ static void afp_asp_timedown()
     it.it_interval.tv_usec = 0;
     it.it_value.tv_sec = 300;
     it.it_value.tv_usec = 0;
-    if ( setitimer( ITIMER_REAL, &it, 0 ) < 0 ) {
+    if ( setitimer( ITIMER_REAL, &it, NULL ) < 0 ) {
         LOG(log_error, logtype_afpd, "afp_timedown: setitimer: %s", strerror(errno) );
-        afp_asp_die(1);
+        afp_asp_die(EXITERR_SYS);
     }
 
     memset(&sv, 0, sizeof(sv));
     sv.sa_handler = afp_asp_die;
     sigemptyset( &sv.sa_mask );
+    sigaddset(&sv.sa_mask, SIGHUP);
+    sigaddset(&sv.sa_mask, SIGTERM);
     sv.sa_flags = SA_RESTART;
-    if ( sigaction( SIGALRM, &sv, 0 ) < 0 ) {
+    if ( sigaction( SIGALRM, &sv, NULL ) < 0 ) {
         LOG(log_error, logtype_afpd, "afp_timedown: sigaction: %s", strerror(errno) );
-        afp_asp_die(1);
+        afp_asp_die(EXITERR_SYS);
+    }
+
+    /* ignore myself */
+    sv.sa_handler = SIG_IGN;
+    sigemptyset( &sv.sa_mask );
+    sv.sa_flags = SA_RESTART;
+    if ( sigaction( SIGUSR1, &sv, NULL ) < 0 ) {
+        LOG(log_error, logtype_afpd, "afp_timedown: sigaction SIGUSR1: %s", strerror(errno) );
+        afp_asp_die(EXITERR_SYS);
     }
 }
 
+/* ---------------------------------
+ * SIGHUP reload configuration file
+*/
+extern volatile int reload_request;
+
+static void afp_asp_reload(int sig _U_)
+{
+    reload_request = 1;
+}
+
+/* ---------------------- */
+#ifdef SERVERTEXT
+static void afp_asp_getmesg (int sig _U_)
+{
+    readmessage(child);
+    asp_attention(child->handle, AFPATTN_MESG | AFPATTN_TIME(5));
+}
+#endif /* SERVERTEXT */
+
+/* ---------------------- */
 void afp_over_asp(AFPObj *obj)
 {
     ASP asp;
     struct sigaction  action;
-    int                func, ccnt = 0, reply = 0;
+    int                func,  reply = 0;
+#ifdef DEBUG1
+    int ccnt = 0;
+#endif    
 
     obj->exit = afp_asp_die;
     obj->reply = (int (*)()) asp_cmdreply;
@@ -173,22 +221,65 @@ void afp_over_asp(AFPObj *obj)
     child = obj;
     asp = (ASP) obj->handle;
 
-    /* install signal handlers */
+    /* install signal handlers 
+     * With ASP tickle handler is done in the parent process
+    */
     memset(&action, 0, sizeof(action));
-    action.sa_handler = afp_asp_timedown;
+
+    /* install SIGHUP */
+    action.sa_handler = afp_asp_reload; 
     sigemptyset( &action.sa_mask );
+    sigaddset(&action.sa_mask, SIGTERM);
+    sigaddset(&action.sa_mask, SIGUSR1);
+#ifdef SERVERTEXT
+    sigaddset(&action.sa_mask, SIGUSR2);
+#endif    
     action.sa_flags = SA_RESTART;
-    if ( sigaction( SIGHUP, &action, 0 ) < 0 ) {
+    if ( sigaction( SIGHUP, &action, NULL ) < 0 ) {
         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
-        afp_asp_die(1);
+        afp_asp_die(EXITERR_SYS);
     }
 
+    /*  install SIGTERM */
     action.sa_handler = afp_asp_die;
     sigemptyset( &action.sa_mask );
+    sigaddset(&action.sa_mask, SIGHUP);
+    sigaddset(&action.sa_mask, SIGUSR1);
+#ifdef SERVERTEXT
+    sigaddset(&action.sa_mask, SIGUSR2);
+#endif    
+    action.sa_flags = SA_RESTART;
+    if ( sigaction( SIGTERM, &action, NULL ) < 0 ) {
+        LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
+        afp_asp_die(EXITERR_SYS);
+    }
+
+#ifdef SERVERTEXT
+    /* Added for server message support */
+    action.sa_handler = afp_asp_getmesg;
+    sigemptyset( &action.sa_mask );
+    sigaddset(&action.sa_mask, SIGTERM);
+    sigaddset(&action.sa_mask, SIGUSR1);
+    sigaddset(&action.sa_mask, SIGHUP);
+    action.sa_flags = SA_RESTART;
+    if ( sigaction( SIGUSR2, &action, NULL) < 0 ) {
+        LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
+        afp_asp_die(EXITERR_SYS);
+    }
+#endif /* SERVERTEXT */
+
+    /*  SIGUSR1 - set down in 5 minutes  */
+    action.sa_handler = afp_asp_timedown; 
+    sigemptyset( &action.sa_mask );
+    sigaddset(&action.sa_mask, SIGHUP);
+    sigaddset(&action.sa_mask, SIGTERM);
+#ifdef SERVERTEXT
+    sigaddset(&action.sa_mask, SIGUSR2);
+#endif    
     action.sa_flags = SA_RESTART;
-    if ( sigaction( SIGTERM, &action, 0 ) < 0 ) {
+    if ( sigaction( SIGUSR1, &action, NULL ) < 0 ) {
         LOG(log_error, logtype_afpd, "afp_over_asp: sigaction: %s", strerror(errno) );
-        afp_asp_die(1);
+        afp_asp_die(EXITERR_SYS);
     }
 
     LOG(log_info, logtype_afpd, "session from %u.%u:%u on %u.%u:%u",
@@ -199,14 +290,20 @@ void afp_over_asp(AFPObj *obj)
         atp_sockaddr( asp->asp_atp )->sat_port );
 
     while ((reply = asp_getrequest(asp))) {
+        if (reload_request) {
+            reload_request = 0;
+            load_volumes(child);
+        }
         switch (reply) {
         case ASPFUNC_CLOSE :
             afp_asp_close(obj);
             LOG(log_info, logtype_afpd, "done" );
 
+#ifdef DEBUG1
             if ( obj->options.flags & OPTION_DEBUG ) {
                 printf( "done\n" );
             }
+#endif
             return;
             break;
 
@@ -221,10 +318,12 @@ void afp_over_asp(AFPObj *obj)
             }
 #endif /* AFS */
             func = (u_char) asp->commands[0];
+#ifdef DEBUG1
             if ( obj->options.flags & OPTION_DEBUG ) {
                 printf("command: %d (%s)\n", func, AfpNum2name(func));
                 bprint( asp->commands, asp->cmdlen );
             }
+#endif            
             if ( afp_switch[ func ] != NULL ) {
                 /*
                  * The function called from afp_switch is expected to
@@ -236,45 +335,60 @@ void afp_over_asp(AFPObj *obj)
                 reply = (*afp_switch[ func ])(obj,
                                               asp->commands, asp->cmdlen,
                                               asp->data, &asp->datalen);
+#ifdef FORCE_UIDGID
+               /* bring everything back to old euid, egid */
+               if (obj->force_uid)
+                   restore_uidgid ( &obj->uidgid );
+#endif /* FORCE_UIDGID */
             } else {
                 LOG(log_error, logtype_afpd, "bad function %X", func );
                 asp->datalen = 0;
                 reply = AFPERR_NOOP;
             }
+#ifdef DEBUG1
             if ( obj->options.flags & OPTION_DEBUG ) {
                 printf( "reply: %d, %d\n", reply, ccnt++ );
                 bprint( asp->data, asp->datalen );
             }
-
+#endif
             if ( asp_cmdreply( asp, reply ) < 0 ) {
                 LOG(log_error, logtype_afpd, "asp_cmdreply: %s", strerror(errno) );
-                afp_asp_die(1);
+                afp_asp_die(EXITERR_CLNT);
             }
             break;
 
         case ASPFUNC_WRITE :
             func = (u_char) asp->commands[0];
+#ifdef DEBUG1
             if ( obj->options.flags & OPTION_DEBUG ) {
                 printf( "(write) command: %d\n", func );
                 bprint( asp->commands, asp->cmdlen );
             }
+#endif
             if ( afp_switch[ func ] != NULL ) {
                 asp->datalen = ASP_DATASIZ;
                 reply = (*afp_switch[ func ])(obj,
                                               asp->commands, asp->cmdlen,
                                               asp->data, &asp->datalen);
+#ifdef FORCE_UIDGID
+               /* bring everything back to old euid, egid */
+               if (obj->force_uid)
+                   restore_uidgid ( &obj->uidgid );
+#endif /* FORCE_UIDGID */
             } else {
                 LOG(log_error, logtype_afpd, "(write) bad function %X", func );
                 asp->datalen = 0;
                 reply = AFPERR_NOOP;
             }
+#ifdef DEBUG1
             if ( obj->options.flags & OPTION_DEBUG ) {
                 printf( "(write) reply code: %d, %d\n", reply, ccnt++ );
                 bprint( asp->data, asp->datalen );
             }
+#endif
             if ( asp_wrtreply( asp, reply ) < 0 ) {
                 LOG(log_error, logtype_afpd, "asp_wrtreply: %s", strerror(errno) );
-                afp_asp_die(1);
+                afp_asp_die(EXITERR_CLNT);
             }
             break;
         default:
@@ -285,7 +399,7 @@ void afp_over_asp(AFPObj *obj)
             LOG(log_info, logtype_afpd, "main: asp_getrequest: %d", reply );
             break;
         }
-
+#ifdef DEBUG1
         if ( obj->options.flags & OPTION_DEBUG ) {
 #ifdef notdef
             pdesc( stdout );
@@ -293,6 +407,7 @@ void afp_over_asp(AFPObj *obj)
             of_pforkdesc( stdout );
             fflush( stdout );
         }
+#endif
     }
 }