Enable building #WITHOUT_PROXY
[freeradius.git] / src / modules / rlm_detail / rlm_detail.c
index 69c1041..6446359 100644 (file)
@@ -26,6 +26,7 @@ RCSID("$Id$")
 #include       <freeradius-devel/radiusd.h>
 #include       <freeradius-devel/modules.h>
 #include       <freeradius-devel/rad_assert.h>
+#include       <freeradius-devel/detail.h>
 
 #include       <sys/stat.h>
 #include       <ctype.h>
@@ -33,22 +34,6 @@ RCSID("$Id$")
 
 #define        DIRLEN  8192
 
-static const char *packet_codes[] = {
-  "",
-  "Access-Request",
-  "Access-Accept",
-  "Access-Reject",
-  "Accounting-Request",
-  "Accounting-Response",
-  "Accounting-Status",
-  "Password-Request",
-  "Password-Accept",
-  "Password-Reject",
-  "Accounting-Message",
-  "Access-Challenge"
-};
-
-
 struct detail_instance {
        /* detail file */
        char *detailfile;
@@ -203,6 +188,8 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
 
        struct detail_instance *inst = instance;
 
+       rad_assert(request != NULL);
+
        /*
         *      Nothing to log: don't do anything.
         */
@@ -219,7 +206,7 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
         *      variables.
         */
        radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
-       DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
+       RDEBUG2("%s expands to %s", inst->detailfile, buffer);
 
        /*
         *      Grab the last directory delimiter.
@@ -257,7 +244,7 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
                 *      deleted a directory that the server was using.
                 */
                if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
-                       radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
+                       radlog_request(L_ERR, 0, request, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
                        return RLM_MODULE_FAIL;
                }
 
@@ -273,7 +260,7 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
                 */
                if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
                                  inst->detailperm)) < 0) {
-                       radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
+                       radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
                               buffer, strerror(errno));
                        return RLM_MODULE_FAIL;
                }
@@ -299,20 +286,20 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
                         *      the lock (race condition)
                         */
                        if (fstat(outfd, &st) != 0) {
-                               radlog(L_ERR, "rlm_detail: Couldn't stat file %s: %s",
+                               radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't stat file %s: %s",
                                       buffer, strerror(errno));
                                close(outfd);
                                return RLM_MODULE_FAIL;
                        }
                        if (st.st_nlink == 0) {
-                               DEBUG("rlm_detail: File %s removed by another program, retrying",
+                               RDEBUG2("File %s removed by another program, retrying",
                                      buffer);
                                close(outfd);
                                lock_count = 0;
                                continue;
                        }
 
-                       DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
+                       RDEBUG2("Acquired filelock, tried %d time(s)",
                              lock_count + 1);
                        locked = 1;
                }
@@ -320,7 +307,7 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
 
        if (inst->locking && !locked) {
                close(outfd);
-               radlog(L_ERR, "rlm_detail: Failed to acquire filelock for %s, giving up",
+               radlog_request(L_ERR, 0, request, "rlm_detail: Failed to acquire filelock for %s, giving up",
                       buffer);
                return RLM_MODULE_FAIL;
        }
@@ -330,12 +317,12 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
         *      after this operation.
         */
        if ((outfp = fdopen(outfd, "a")) == NULL) {
-               radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
+               radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
                       buffer, strerror(errno));
                if (inst->locking) {
                        lseek(outfd, 0L, SEEK_SET);
                        rad_unlockfd(outfd, 0);
-                       DEBUG("rlm_detail: Released filelock");
+                       RDEBUG2("Released filelock");
                }
                close(outfd);   /* automatically releases the lock */
 
@@ -358,9 +345,9 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
                 *      Numbers, if not.
                 */
                if ((packet->code > 0) &&
-                   (packet->code <= PW_ACCESS_CHALLENGE)) {
+                   (packet->code < FR_MAX_PACKET_CODE)) {
                        fprintf(outfp, "\tPacket-Type = %s\n",
-                               packet_codes[packet->code]);
+                               fr_packet_codes[packet->code]);
                } else {
                        fprintf(outfp, "\tPacket-Type = %d\n", packet->code);
                }
@@ -445,6 +432,7 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
         *      Add non-protocol attibutes.
         */
        if (compat) {
+#ifdef WITH_PROXY
                if (request->proxy) {
                        char proxy_buffer[128];
 
@@ -453,9 +441,10 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
                                  proxy_buffer, sizeof(proxy_buffer));
                        fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
                                        proxy_buffer);
-                               DEBUG("rlm_detail: Freeradius-Proxied-To = %s",
+                               RDEBUG("Freeradius-Proxied-To = %s",
                                      proxy_buffer);
                }
+#endif
 
                fprintf(outfp, "\tTimestamp = %ld\n",
                        (unsigned long) request->timestamp);
@@ -473,7 +462,7 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
                fflush(outfp);
                lseek(outfd, 0L, SEEK_SET);
                rad_unlockfd(outfd, 0);
-               DEBUG("rlm_detail: Released filelock");
+               RDEBUG2("Released filelock");
        }
 
        fclose(outfp);
@@ -489,8 +478,10 @@ static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
  */
 static int detail_accounting(void *instance, REQUEST *request)
 {
-       if (request->listener->type == RAD_LISTEN_DETAIL) {
-               DEBUG2("  rlm_detail: Suppressing writes to detail file as the request was just read from a detail file.");
+       if (request->listener->type == RAD_LISTEN_DETAIL &&
+           strcmp(((struct detail_instance *)instance)->detailfile,
+                  ((listen_detail_t *)request->listener->data)->filename) == 0) {
+               RDEBUG("Suppressing writes to detail file as the request was just read from a detail file.");
                return RLM_MODULE_NOOP;
        }
 
@@ -513,10 +504,28 @@ static int detail_postauth(void *instance, REQUEST *request)
        return do_detail(instance,request,request->reply, FALSE);
 }
 
+#ifdef WITH_COA
+/*
+ *     Incoming CoA - write the detail files.
+ */
+static int detail_recv_coa(void *instance, REQUEST *request)
+{
+       return do_detail(instance,request,request->packet, FALSE);
+}
+
+/*
+ *     Outgoing CoA - write the detail files.
+ */
+static int detail_send_coa(void *instance, REQUEST *request)
+{
+       return do_detail(instance,request,request->reply, FALSE);
+}
+#endif
 
 /*
  *     Outgoing Access-Request to home server - write the detail files.
  */
+#ifdef WITH_PROXY
 static int detail_pre_proxy(void *instance, REQUEST *request)
 {
        if (request->proxy &&
@@ -546,11 +555,18 @@ static int detail_post_proxy(void *instance, REQUEST *request)
         *      it's doing normal accounting.
         */
        if (!request->proxy_reply) {
-               return detail_accounting(instance, request);
+               int rcode;
+
+               rcode = detail_accounting(instance, request);
+               if (rcode == RLM_MODULE_OK) {
+                       request->reply->code = PW_ACCOUNTING_RESPONSE;
+               }
+               return rcode;
        }
 
        return RLM_MODULE_NOOP;
 }
+#endif
 
 
 /* globally exported name */
@@ -566,9 +582,17 @@ module_t rlm_detail = {
                NULL,                   /* preaccounting */
                detail_accounting,      /* accounting */
                NULL,                   /* checksimul */
+#ifdef WITH_PROXY
                detail_pre_proxy,       /* pre-proxy */
                detail_post_proxy,      /* post-proxy */
+#else
+               NULL, NULL,
+#endif
                detail_postauth         /* post-auth */
+#ifdef WITH_COA
+               , detail_recv_coa,
+               detail_send_coa
+#endif
        },
 };