tidy up a load of lintian warnings
[freeradius.git] / src / main / process.c
index 927104e..14cc3b0 100644 (file)
@@ -285,6 +285,12 @@ static bool we_are_master(void)
 #  define ASSERT_MASTER
 #endif
 
+/*
+ *     Make state transitions simpler.
+ */
+#define FINAL_STATE(_x) NO_CHILD_THREAD; request->component = "<" #_x ">"; request->module = ""; request->child_state = _x
+
+
 static int event_new_fd(rad_listen_t *this);
 
 /*
@@ -613,8 +619,7 @@ static void request_done(REQUEST *request, int action)
         *      and wait for the master thread timer to clean us up.
         */
        if (!we_are_master()) {
-               NO_CHILD_THREAD;
-               request->child_state = REQUEST_DONE;
+               FINAL_STATE(REQUEST_DONE);
                return;
        }
 #endif
@@ -849,16 +854,20 @@ static void request_cleanup_delay_init(REQUEST *request)
                if (rad_debug_lvl) printf("(%u) ********\tNEXT-STATE %s -> %s\n", request->number, __FUNCTION__, "request_cleanup_delay");
 #endif
                request->process = request_cleanup_delay;
-               request->child_state = REQUEST_CLEANUP_DELAY;
+
+               if (!we_are_master()) {
+                       FINAL_STATE(REQUEST_CLEANUP_DELAY);
+                       return;
+               }
 
                /*
                 *      Update this if we can, otherwise let the timers pick it up.
                 */
-               if (we_are_master()) {
-                       STATE_MACHINE_TIMER(FR_ACTION_TIMER);
-               } else {
-                       NO_CHILD_THREAD;
-               }
+               request->child_state = REQUEST_CLEANUP_DELAY;
+#ifdef HAVE_PTHREAD_H
+               rad_assert(request->child_pid == NO_SUCH_CHILD_PID);
+#endif
+               STATE_MACHINE_TIMER(FR_ACTION_TIMER);
                return;
        }
 
@@ -873,7 +882,7 @@ done:
 /*
  *     Enforce max_request_time.
  */
-static void request_max_time(REQUEST *request)
+static bool request_max_time(REQUEST *request)
 {
        struct timeval now, when;
        rad_assert(request->magic == REQUEST_MAGIC);
@@ -896,7 +905,7 @@ static void request_max_time(REQUEST *request)
        if (request->child_state == REQUEST_DONE) {
        done:
                request_done(request, FR_ACTION_DONE);
-               return;
+               return true;
        }
 
        /*
@@ -939,6 +948,7 @@ static void request_max_time(REQUEST *request)
        tv_add(&when, request->delay);
        request->delay += request->delay >> 1;
        STATE_MACHINE_TIMER(FR_ACTION_TIMER);
+       return false;
 }
 
 static void request_queue_or_run(REQUEST *request,
@@ -1159,8 +1169,6 @@ static void request_response_delay(REQUEST *request, int action)
        case FR_ACTION_TIMER:
                fr_event_now(el, &now);
 
-               rad_assert(request->response_delay.tv_sec > 0);
-
                /*
                 *      See if it's time to send the reply.  If not,
                 *      we wait some more.
@@ -1337,8 +1345,7 @@ static void request_finish(REQUEST *request, int action)
         */
        if (request->packet->dst_port == 0) {
                RDEBUG("Finished internally proxied request.");
-               NO_CHILD_THREAD;
-               request->child_state = REQUEST_DONE;
+               FINAL_STATE(REQUEST_DONE);
                return;
        }
 
@@ -1401,6 +1408,27 @@ static void request_finish(REQUEST *request, int action)
            (request->root->reject_delay.tv_sec > 0)) {
                request->response_delay = request->root->reject_delay;
 
+               vp = fr_pair_find_by_num(request->reply->vps, PW_FREERADIUS_RESPONSE_DELAY, 0, TAG_ANY);
+               if (vp) {
+                       if (vp->vp_integer <= 10) {
+                               request->response_delay.tv_sec = vp->vp_integer;
+                       } else {
+                               request->response_delay.tv_sec = 10;
+                       }
+                       request->response_delay.tv_usec = 0;
+               } else {
+                       vp = fr_pair_find_by_num(request->reply->vps, PW_FREERADIUS_RESPONSE_DELAY_USEC, 0, TAG_ANY);
+                       if (vp) {
+                               if (vp->vp_integer <= 10 * USEC) {
+                                       request->response_delay.tv_sec = vp->vp_integer / USEC;
+                                       request->response_delay.tv_usec = vp->vp_integer % USEC;
+                               } else {
+                                       request->response_delay.tv_sec = 10;
+                                       request->response_delay.tv_usec = 0;
+                               }
+                       }
+               }
+
 #ifdef WITH_PROXY
                /*
                 *      If we timed out a proxy packet, don't delay
@@ -1416,22 +1444,26 @@ static void request_finish(REQUEST *request, int action)
        /*
         *      Send the reply.
         */
-       if (request->response_delay.tv_sec == 0) {
-               rad_assert(request->response_delay.tv_usec == 0);
+       if ((request->response_delay.tv_sec == 0) &&
+           (request->response_delay.tv_usec == 0)) {
 
                /*
                 *      Don't print a reply if there's none to send.
                 */
                if (request->reply->code != 0) {
+                       if (rad_debug_lvl && request->state &&
+                           (request->reply->code == PW_CODE_ACCESS_ACCEPT)) {
+                               if (!fr_pair_find_by_num(request->packet->vps, PW_STATE, 0, TAG_ANY)) {
+                                       RWDEBUG2("Unused attributes found in &session-state:");
+                               }
+                       }
+
                        debug_packet(request, request->reply, false);
                        request->listener->send(request->listener, request);
                }
 
        done:
                RDEBUG2("Finished request");
-               request->component = "<core>";
-               request->module = "<done>";
-
                request_cleanup_delay_init(request);
 
        } else {
@@ -1443,11 +1475,9 @@ static void request_finish(REQUEST *request, int action)
                RDEBUG2("Delaying response for %d.%06d seconds",
                        (int) request->response_delay.tv_sec, (int) request->response_delay.tv_usec);
                request->listener->encode(request->listener, request);
-               request->component = "<core>";
-               request->module = "<delay>";
                request->process = request_response_delay;
-               NO_CHILD_THREAD;
-               request->child_state = REQUEST_RESPONSE_DELAY;
+
+               FINAL_STATE(REQUEST_RESPONSE_DELAY);
        }
 }
 
@@ -1474,7 +1504,7 @@ static void request_running(REQUEST *request, int action)
        switch (action) {
        case FR_ACTION_TIMER:
                COA_SEPARATE;
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_DUP:
@@ -1489,9 +1519,7 @@ static void request_running(REQUEST *request, int action)
                                               child_state_names[request->child_state],
                                               child_state_names[REQUEST_DONE]);
 #endif
-
-                       NO_CHILD_THREAD;
-                       request->child_state = REQUEST_DONE;
+                       FINAL_STATE(REQUEST_DONE);
                        break;
                }
 
@@ -1790,9 +1818,6 @@ static REQUEST *request_setup(TALLOC_CTX *ctx, rad_listen_t *listener, RADIUS_PA
                               child_state_names[request->child_state],
                               child_state_names[REQUEST_RUNNING]);
 #endif
-#ifdef HAVE_PTHREAD_H
-       request->child_pid = NO_SUCH_CHILD_PID;
-#endif
        request->handle = fun;
        NO_CHILD_THREAD;
 
@@ -2406,6 +2431,28 @@ static int process_proxy_reply(REQUEST *request, RADIUS_PACKET *reply)
        return 1;
 }
 
+static void mark_home_server_alive(REQUEST *request, home_server_t *home)
+{
+       char buffer[128];
+
+       home->state = HOME_STATE_ALIVE;
+       home->response_timeouts = 0;
+       exec_trigger(request, home->cs, "home_server.alive", false);
+       home->currently_outstanding = 0;
+       home->num_sent_pings = 0;
+       home->num_received_pings = 0;
+       gettimeofday(&home->revive_time, NULL);
+
+       fr_event_delete(el, &home->ev);
+
+       RPROXY("Marking home server %s port %d alive",
+              inet_ntop(request->proxy->dst_ipaddr.af,
+                        &request->proxy->dst_ipaddr.ipaddr,
+                        buffer, sizeof(buffer)),
+              request->proxy->dst_port);
+}
+
+
 int request_proxy_reply(RADIUS_PACKET *packet)
 {
        RADIUS_PACKET **proxy_p;
@@ -2545,12 +2592,13 @@ int request_proxy_reply(RADIUS_PACKET *packet)
 #endif
 
        /*
-        *      We've received a reply.  If we hadn't been sending it
-        *      packets for a while, just mark it alive.
+        *      If we hadn't been sending the home server packets for
+        *      a while, just mark it alive.  Or, if it was zombie,
+        *      it's now responded, and is therefore alive.
         */
-       if (request->home_server->state == HOME_STATE_UNKNOWN) {
-               request->home_server->state = HOME_STATE_ALIVE;
-               request->home_server->response_timeouts = 0;
+       if ((request->home_server->state == HOME_STATE_UNKNOWN) ||
+           (request->home_server->state == HOME_STATE_ZOMBIE)) {
+               mark_home_server_alive(request, request->home_server);
        }
 
        /*
@@ -2637,7 +2685,7 @@ static void proxy_no_reply(REQUEST *request, int action)
                break;
 
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_PROXY_REPLY:
@@ -2686,7 +2734,7 @@ static void proxy_running(REQUEST *request, int action)
                break;
 
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_RUN:
@@ -3182,9 +3230,15 @@ static int request_proxy(REQUEST *request, int retransmit)
        /*
         *      Set the state function, then the state, no child, and
         *      send the packet.
+        *
+        *      The order here is different from other state changes
+        *      due to race conditions with replies from the home
+        *      server.
         */
        request->process = proxy_wait_for_reply;
        request->child_state = REQUEST_PROXIED;
+       request->component = "<REQUEST_PROXIED>";
+       request->module = "";
        NO_CHILD_THREAD;
 
        /*
@@ -3343,21 +3397,7 @@ static void request_ping(REQUEST *request, int action)
                 *      Mark it alive and delete any outstanding
                 *      pings.
                 */
-               home->state = HOME_STATE_ALIVE;
-               home->response_timeouts = 0;
-               exec_trigger(request, home->cs, "home_server.alive", false);
-               home->currently_outstanding = 0;
-               home->num_sent_pings = 0;
-               home->num_received_pings = 0;
-               gettimeofday(&home->revive_time, NULL);
-
-               fr_event_delete(el, &home->ev);
-
-               RPROXY("Marking home server %s port %d alive",
-                      inet_ntop(request->proxy->dst_ipaddr.af,
-                                &request->proxy->dst_ipaddr.ipaddr,
-                                buffer, sizeof(buffer)),
-                      request->proxy->dst_port);
+               mark_home_server_alive(request, home);
                break;
 
        default:
@@ -3581,7 +3621,7 @@ static void mark_home_server_zombie(home_server_t *home, struct timeval *now, st
         */
        start = now->tv_sec - ((home->zombie_period + 3) / 4);
        if (home->last_packet_recv >= start) {
-               DEBUG("Recieved reply from home server %d seconds ago.  Might not be zombie.",
+               DEBUG("Received reply from home server %d seconds ago.  Might not be zombie.",
                      (int) (now->tv_sec - home->last_packet_recv));
                return;
        }
@@ -4338,18 +4378,16 @@ static void coa_wait_for_reply(REQUEST *request, int action)
        ASSERT_MASTER;
        CHECK_FOR_STOP;
 
+       if (request->parent) coa_separate(request);
+
        switch (action) {
        case FR_ACTION_TIMER:
-               request_max_time(request);
-
-               if (request->parent) coa_separate(request);
+               if (request_max_time(request)) break;
 
                coa_retransmit(request);
                break;
 
        case FR_ACTION_PROXY_REPLY:
-               if (request->parent) coa_separate(request);
-
                request_queue_or_run(request, coa_running);
                break;
 
@@ -4415,7 +4453,7 @@ static void coa_no_reply(REQUEST *request, int action)
 
        switch (action) {
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_PROXY_REPLY: /* too late! */
@@ -4464,7 +4502,7 @@ static void coa_running(REQUEST *request, int action)
 
        switch (action) {
        case FR_ACTION_TIMER:
-               request_max_time(request);
+               (void) request_max_time(request);
                break;
 
        case FR_ACTION_RUN:
@@ -4691,11 +4729,14 @@ static int event_new_fd(rad_listen_t *this)
                rad_assert(sock != NULL);
                if (just_started) {
                        DEBUG("Listening on %s", buffer);
+               } else {
+                       INFO(" ... adding new socket %s", buffer);
+               }
 
 #ifdef WITH_PROXY
-               } else if (this->type == RAD_LISTEN_PROXY) {
+               if (!just_started && (this->type == RAD_LISTEN_PROXY)) {
                        home_server_t *home;
-
+                       
                        home = sock->home;
                        if (!home || !home->limit.max_connections) {
                                INFO(" ... adding new socket %s", buffer);
@@ -4705,8 +4746,6 @@ static int event_new_fd(rad_listen_t *this)
                        }
 
 #endif
-               } else {
-                       INFO(" ... adding new socket %s", buffer);
                }
 
                switch (this->type) {
@@ -5176,6 +5215,119 @@ static int packet_entry_cmp(void const *one, void const *two)
        return fr_packet_cmp(*a, *b);
 }
 
+#ifdef WITH_PROXY
+/*
+ *     They haven't defined a proxy listener.  Automatically
+ *     add one for them, with the correct address family.
+ */
+static void create_default_proxy_listener(int af)
+{
+       uint16_t        port = 0;
+       home_server_t   home;
+       listen_socket_t *sock;
+       rad_listen_t    *this;
+
+       memset(&home, 0, sizeof(home));
+
+       /*
+        *      Open a default UDP port
+        */
+       home.proto = IPPROTO_UDP;
+       port = 0;
+
+       /*
+        *      Set the address family.
+        */
+       home.src_ipaddr.af = af;
+       home.ipaddr.af = af;
+
+       /*
+        *      Get the correct listener.
+        */
+       this = proxy_new_listener(proxy_ctx, &home, port);
+       if (!this) {
+               fr_exit_now(1);
+       }
+
+       sock = this->data;
+       if (!fr_packet_list_socket_add(proxy_list, this->fd,
+                                      sock->proto,
+                                      &sock->other_ipaddr, sock->other_port,
+                                      this)) {
+               ERROR("Failed adding proxy socket");
+               fr_exit_now(1);
+       }
+
+       /*
+        *      Insert the FD into list of FDs to listen on.
+        */
+       radius_update_listener(this);
+}
+
+/*
+ *     See if we automatically need to open a proxy socket.
+ */
+static void check_proxy(rad_listen_t *head)
+{
+       bool            defined_proxy;
+       bool            has_v4, has_v6;
+       rad_listen_t    *this;
+
+       if (check_config) return;
+       if (!main_config.proxy_requests) return;
+       if (!head) return;
+       if (!home_servers_udp) return;
+
+       /*
+        *      We passed "-i" on the command line.  Use that address
+        *      family for the proxy socket.
+        */
+       if (main_config.myip.af != AF_UNSPEC) {
+               create_default_proxy_listener(main_config.myip.af);
+               return;
+       }
+
+       defined_proxy = has_v4 = has_v6 = false;
+
+       /*
+        *      Figure out if we need to open a proxy socket, and if
+        *      so, which one.
+        */
+       for (this = head; this != NULL; this = this->next) {
+               listen_socket_t *sock;
+
+               switch (this->type) {
+               case RAD_LISTEN_PROXY:
+                       defined_proxy = true;
+                       break;
+
+               case RAD_LISTEN_AUTH:
+#ifdef WITH_ACCT
+               case RAD_LISTEN_ACCT:
+#endif
+#ifdef WITH_COA
+               case RAD_LISTEN_COA:
+#endif
+                       sock = this->data;
+                       if (sock->my_ipaddr.af == AF_INET) has_v4 = true;
+                       if (sock->my_ipaddr.af == AF_INET6) has_v6 = true;
+                       break;
+                       
+               default:
+                       break;
+               }
+       }
+
+       /*
+        *      Assume they know what they're doing.
+        */
+       if (defined_proxy) return;
+
+       if (has_v4) create_default_proxy_listener(AF_INET);
+
+       if (has_v6) create_default_proxy_listener(AF_INET6);
+}
+#endif
 
 int radius_event_start(CONF_SECTION *cs, bool have_children)
 {
@@ -5302,6 +5454,10 @@ int radius_event_start(CONF_SECTION *cs, bool have_children)
 
        main_config.listen = head;
 
+#ifdef WITH_PROXY
+       check_proxy(head);
+#endif
+
        /*
         *      At this point, no one has any business *ever* going
         *      back to root uid.