Enable building WITHOUT_ACCOUNTING
[freeradius.git] / src / modules / rlm_radutmp / rlm_radutmp.c
index 861f0c3..65fe1c0 100644 (file)
@@ -115,7 +115,7 @@ static int radutmp_detach(void *instance)
 /*
  *     Zap all users on a NAS from the radutmp file.
  */
-static int radutmp_zap(rlm_radutmp_t *inst,
+static int radutmp_zap(UNUSED rlm_radutmp_t *inst,
                       const char *filename,
                       uint32_t nasaddr,
                       time_t t)
@@ -175,6 +175,7 @@ static NAS_PORT *nas_port_find(NAS_PORT *nas_port_list, uint32_t nasaddr, unsign
 }
 
 
+#ifdef WITH_ACCOUNTING
 /*
  *     Store logins in the RADIUS utmp file.
  */
@@ -183,14 +184,10 @@ static int radutmp_accounting(void *instance, REQUEST *request)
        struct radutmp  ut, u;
        VALUE_PAIR      *vp;
        int             status = -1;
-       uint32_t        nas_address = 0;
-       uint32_t        framed_address = 0;
        int             protocol = -1;
        time_t          t;
        int             fd;
-       int             just_an_update = 0;
        int             port_seen = 0;
-       int             nas_port_type = 0;
        int             off;
        rlm_radutmp_t   *inst = instance;
        char            buffer[256];
@@ -208,7 +205,7 @@ static int radutmp_accounting(void *instance, REQUEST *request)
        /*
         *      Which type is this.
         */
-       if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) == NULL) {
+       if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE, 0)) == NULL) {
                radlog(L_ERR, "rlm_radutmp: No Accounting-Status-Type record.");
                return RLM_MODULE_NOOP;
        }
@@ -231,10 +228,10 @@ static int radutmp_accounting(void *instance, REQUEST *request)
                int check1 = 0;
                int check2 = 0;
 
-               if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_TIME))
+               if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_TIME, 0))
                     == NULL || vp->vp_date == 0)
                        check1 = 1;
-               if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_ID))
+               if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_ID, 0))
                     != NULL && vp->length == 8 &&
                     memcmp(vp->vp_strvalue, "00000000", 8) == 0)
                        check2 = 1;
@@ -256,6 +253,7 @@ static int radutmp_accounting(void *instance, REQUEST *request)
        time(&t);
        memset(&ut, 0, sizeof(ut));
        ut.porttype = 'A';
+       ut.nas_address = htonl(INADDR_NONE);
 
        /*
         *      First, find the interesting attributes.
@@ -264,14 +262,12 @@ static int radutmp_accounting(void *instance, REQUEST *request)
                switch (vp->attribute) {
                        case PW_LOGIN_IP_HOST:
                        case PW_FRAMED_IP_ADDRESS:
-                               framed_address = vp->vp_ipaddr;
                                ut.framed_address = vp->vp_ipaddr;
                                break;
                        case PW_FRAMED_PROTOCOL:
                                protocol = vp->vp_integer;
                                break;
                        case PW_NAS_IP_ADDRESS:
-                               nas_address = vp->vp_ipaddr;
                                ut.nas_address = vp->vp_ipaddr;
                                break;
                        case PW_NAS_PORT:
@@ -302,7 +298,6 @@ static int radutmp_accounting(void *instance, REQUEST *request)
                        case PW_NAS_PORT_TYPE:
                                if (vp->vp_integer <= 4)
                                        ut.porttype = porttypes[vp->vp_integer];
-                               nas_port_type = vp->vp_integer;
                                break;
                        case PW_CALLING_STATION_ID:
                                if(inst->callerid_ok)
@@ -317,32 +312,20 @@ static int radutmp_accounting(void *instance, REQUEST *request)
         *      If we didn't find out the NAS address, use the
         *      originator's IP address.
         */
-       if (nas_address == 0) {
-               nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
-               ut.nas_address = nas_address;
-               nas = client_name_old(&request->packet->src_ipaddr); /* MUST be a valid client */
+       if (ut.nas_address == htonl(INADDR_NONE)) {
+               ut.nas_address = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
+               nas = request->client->shortname;
 
-       } else if (request->packet->src_ipaddr.ipaddr.ip4addr.s_addr == nas_address) {          /* might be a client, might not be. */
-               RADCLIENT *cl;
+       } else if (request->packet->src_ipaddr.ipaddr.ip4addr.s_addr == ut.nas_address) {               /* might be a client, might not be. */
+               nas = request->client->shortname;
 
-               /*
-                *      Hack like 'client_name()', but with sane
-                *      fall-back.
-                */
-               cl = client_find_old(&request->packet->src_ipaddr);
-               if (!cl) rad_assert(0 == 1); /* WTF? */
-               if (cl->shortname && cl->shortname[0]) {
-                       nas = cl->shortname;
-               } else {
-                       nas = cl->longname;
-               }
        } else {
                /*
                 *      The NAS isn't a client, it's behind
                 *      a proxy server.  In that case, just
                 *      get the IP address.
                 */
-               nas = ip_ntoa(ip_name, nas_address);
+               nas = ip_ntoa(ip_name, ut.nas_address);
        }
 
        /*
@@ -368,17 +351,19 @@ static int radutmp_accounting(void *instance, REQUEST *request)
         *      the NAS comes up, because of issues with receiving
         *      UDP packets out of order.
         */
-       if (status == PW_STATUS_ACCOUNTING_ON && nas_address) {
+       if (status == PW_STATUS_ACCOUNTING_ON &&
+           (ut.nas_address != htonl(INADDR_NONE))) {
                radlog(L_INFO, "rlm_radutmp: NAS %s restarted (Accounting-On packet seen)",
                       nas);
-               radutmp_zap(inst, filename, nas_address, ut.time);
+               radutmp_zap(inst, filename, ut.nas_address, ut.time);
                return RLM_MODULE_OK;
        }
 
-       if (status == PW_STATUS_ACCOUNTING_OFF && nas_address) {
+       if (status == PW_STATUS_ACCOUNTING_OFF &&
+           (ut.nas_address != htonl(INADDR_NONE))) {
                radlog(L_INFO, "rlm_radutmp: NAS %s rebooted (Accounting-Off packet seen)",
                       nas);
-               radutmp_zap(inst, filename, nas_address, ut.time);
+               radutmp_zap(inst, filename, ut.nas_address, ut.time);
                return RLM_MODULE_OK;
        }
 
@@ -507,8 +492,6 @@ static int radutmp_accounting(void *instance, REQUEST *request)
                         *      Keep the original login time.
                         */
                        ut.time = u.time;
-                       if (u.login[0] != 0)
-                               just_an_update = 1;
                }
 
                if (lseek(fd, -(off_t)sizeof(u), SEEK_CUR) < 0) {
@@ -557,14 +540,15 @@ static int radutmp_accounting(void *instance, REQUEST *request)
                } else if (r == 0) {
                        radlog(L_ERR, "rlm_radutmp: Logout for NAS %s port %u, but no Login record",
                               nas, ut.nas_port);
-                       r = -1;
                }
        }
        close(fd);      /* and implicitely release the locks */
 
        return RLM_MODULE_OK;
 }
+#endif
 
+#ifdef WITH_SESSION_MGMT
 /*
  *     See if a user is already logged in. Sets request->simul_count to the
  *     current session count for this user and sets request->simul_mpp to 2
@@ -649,9 +633,9 @@ static int radutmp_checksimul(void *instance, REQUEST *request)
        /*
         *      Setup some stuff, like for MPP detection.
         */
-       if ((vp = pairfind(request->packet->vps, PW_FRAMED_IP_ADDRESS)) != NULL)
+       if ((vp = pairfind(request->packet->vps, PW_FRAMED_IP_ADDRESS, 0)) != NULL)
                ipno = vp->vp_ipaddr;
-       if ((vp = pairfind(request->packet->vps, PW_CALLING_STATION_ID)) != NULL)
+       if ((vp = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0)) != NULL)
                call_num = vp->vp_strvalue;
 
        /*
@@ -744,20 +728,29 @@ static int radutmp_checksimul(void *instance, REQUEST *request)
 
        return RLM_MODULE_OK;
 }
+#endif
 
 /* globally exported name */
 module_t rlm_radutmp = {
        RLM_MODULE_INIT,
        "radutmp",
-       0,                            /* type: reserved */
+       RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
        radutmp_instantiate,          /* instantiation */
        radutmp_detach,               /* detach */
        {
                NULL,                 /* authentication */
                NULL,                 /* authorization */
                NULL,                 /* preaccounting */
+#ifdef WITH_ACCOUNTING
                radutmp_accounting,   /* accounting */
+#else
+               NULL,
+#endif
+#ifdef WITH_SESSION_MGMT
                radutmp_checksimul,     /* checksimul */
+#else
+               NULL,
+#endif
                NULL,                   /* pre-proxy */
                NULL,                   /* post-proxy */
                NULL                    /* post-auth */