Signed / unsigned fixes and function prototypes
[freeradius.git] / src / main / radwho.c
index 7aeba60..fec34c9 100644 (file)
@@ -1,3 +1,4 @@
+/*@-skipposixheaders@*/
 /*
  * radwho.c    Show who is logged in on the terminal servers.
  *             Can also be installed as fingerd on the UNIX
  *
  *   You should have received a copy of the GNU General Public License
  *   along with this program; if not, write to the Free Software
- *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  *
- * Copyright 2000  The FreeRADIUS server project
+ * Copyright 2000,2006  The FreeRADIUS server project
  * Copyright 2000  Alan DeKok <aland@ox.org>
  */
 
-static const char rcsid[] =
-"$Id$";
+#include <freeradius-devel/ident.h>
+RCSID("$Id$")
 
-#include "autoconf.h"
-#include "libradius.h"
+#include <freeradius-devel/radiusd.h>
+#include <freeradius-devel/sysutmp.h>
+#include <freeradius-devel/radutmp.h>
 
-#include <stdlib.h>
-#include <string.h>
+#ifdef HAVE_PWD_H
 #include <pwd.h>
-#include <sys/stat.h>
-#include <sys/utsname.h>
-#include <ctype.h>
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
 #endif
 
-#include "sysutmp.h"
-#include "radutmp.h"
-#include "radiusd.h"
-#include "conffile.h"
+#include <sys/stat.h>
+
+#include <ctype.h>
 
 /*
  *     FIXME: put in header file.
@@ -54,14 +49,14 @@ static const char rcsid[] =
  *     Header above output and format.
  */
 static const char *hdr1 =
-"Login      Name              What  TTY  When      From      Location";
-static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3d %-9.9s %-9.9s %-.19s%s";
-static const char *rfmt1r = "%s,%s,%s,%s%d,%s,%s,%s%s";
+"Login      Name              What  TTY  When      From            Location";
+static const char *rfmt1 = "%-10.10s %-17.17s %-5.5s %s%-3u %-9.9s %-15.15s %-.19s%s";
+static const char *rfmt1r = "%s,%s,%s,%s%u,%s,%s,%s%s";
 
 static const char *hdr2 =
-"Login      Port    What      When          From       Location";
-static const char *rfmt2 = "%-10.10s %s%-5d  %-6.6s %-13.13s %-10.10s %-.28s%s";
-static const char *rfmt2r = "%s,%s%d,%s,%s,%s,%s%s";
+"Login      Port    What      When          From            Location";
+static const char *rfmt2 = "%-10.10s %s%-5u  %-6.6s %-13.13s %-15.15s %-.28s%s";
+static const char *rfmt2r = "%s,%s%u,%s,%s,%s,%s%s";
 
 static const char *eol = "\n";
 static int showname = -1;
@@ -72,7 +67,7 @@ const char *progname = "radwho";
 const char *radlog_dir = NULL;
 const char *radutmp_file = NULL;
 
-const char *radius_dir = NULL;
+char *radius_dir = NULL;
 const char *radacct_dir = NULL;
 const char *radlib_dir = NULL;
 uint32_t myip = INADDR_ANY;
@@ -82,12 +77,20 @@ int log_stripped_names;
  *     Global, for log.c to use.
  */
 struct main_config_t mainconfig;
+char *request_log_file = NULL;
+char *debug_log_file = NULL;
+int radius_xlat(char *out, UNUSED int outlen, UNUSED const char *fmt,
+               UNUSED REQUEST *request, UNUSED RADIUS_ESCAPE_STRING func)
+{
+       *out = 0;
+       return 0;
+}
 
 struct radutmp_config_t {
   char *radutmp_fn;
 } radutmpconfig;
 
-static CONF_PARSER module_config[] = {
+static const CONF_PARSER module_config[] = {
   { "filename", PW_TYPE_STRING_PTR, 0, &radutmpconfig.radutmp_fn,  RADUTMP },
   { NULL, -1, 0, NULL, NULL }
 };
@@ -103,7 +106,7 @@ static FILE *safe_popen(const char *cmd, const char *mode)
        /*
         *      Change all suspect characters into a space.
         */
-       strncpy(buf, cmd, sizeof(buf));
+       strlcpy(buf, cmd, sizeof(buf));
        buf[sizeof(buf) - 1] = 0;
        for (p = buf; *p; p++) {
                if (isalnum((int) *p))
@@ -117,7 +120,7 @@ static FILE *safe_popen(const char *cmd, const char *mode)
 
 /*
  *     Print a file from FINGER_DIR. If the file is executable,
- *     execute it instead. Return 0 if succesfull.
+ *     execute it instead. Return 0 if successful.
  */
 static int ffile(const char *arg)
 {
@@ -183,6 +186,7 @@ static void sys_finger(const char *l)
  */
 static char *fullname(char *username)
 {
+#ifdef HAVE_PWD_Hx
        struct passwd *pwd;
        char *s;
 
@@ -190,6 +194,8 @@ static char *fullname(char *username)
                if ((s = strchr(pwd->pw_gecos, ',')) != NULL) *s = 0;
                return pwd->pw_gecos;
        }
+#endif
+
        return username;
 }
 
@@ -224,62 +230,41 @@ static char *dotime(time_t t)
        char *s = ctime(&t);
 
        if (showname) {
-               strncpy(s + 4, s + 11, 5);
+               strlcpy(s + 4, s + 11, 6);
                s[9] = 0;
        } else {
-               strncpy(s + 4, s + 8, 8);
+               strlcpy(s + 4, s + 8, 9);
                s[12] = 0;
        }
 
        return s;
 }
 
-/*
- *     Shorten tty name.
- */
-static const char *ttyshort(char *tty)
-{
-       static char tmp[16];
-
-       if (tty[0] == '/') tty += 5;
-
-       if (strncmp(tty, "tty", 3) == 0) {
-               if (tty[3] >= '0' && tty[3] <= '9')
-                       snprintf(tmp, sizeof(tmp), "v%.14s", tty + 3);
-               else
-                       snprintf(tmp, sizeof(tmp), "%.15s", tty + 3);
-               return tmp;
-       }
-       if (strncmp(tty, "vc", 2) == 0) {
-               snprintf(tmp, sizeof(tmp), "v.14%s", tty + 2);
-               return tmp;
-       }
-       if (strncmp(tty, "cu", 2) == 0) {
-               return tmp + 2;
-       }
-       return "??";
-}
-
 
 /*
  *     Print address of NAS.
  */
 static const char *hostname(char *buf, size_t buflen, uint32_t ipaddr)
 {
+       /*
+        *      WTF is this code for?
+        */
        if (ipaddr == 0 || ipaddr == (uint32_t)-1 || ipaddr == (uint32_t)-2)
                return "";
-       return ip_hostname(buf, buflen, ipaddr);
+
+       return inet_ntop(AF_INET, &ipaddr, buf, buflen);
+
 }
 
 
 /*
  *     Print usage message and exit.
  */
-static void usage(int status)
+static void NEVER_RETURNS usage(int status)
 {
        FILE *output = status?stderr:stdout;
 
-       fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsS] [-N nas] [-P nas_port] [-u user] [-U user]\n");
+       fprintf(output, "Usage: radwho [-d raddb] [-cfihnprRsSZ] [-N nas] [-P nas_port] [-u user] [-U user]\n");
        fprintf(output, "       -c: show caller ID, if available\n");
        fprintf(output, "       -d: set the raddb directory (default is %s)\n",
                RADIUS_DIR);
@@ -289,12 +274,14 @@ static void usage(int status)
        fprintf(output, "       -N <nas-ip-address>: Show entries matching the given NAS IP address\n");
        fprintf(output, "       -p: show port type\n");
        fprintf(output, "       -P <port>: Show entries matching the given nas port\n");
-       fprintf(output, "       -r: output as raw data\n");
-       fprintf(output, "       -R: output as RADIUS attributes and values\n");
+       fprintf(output, "       -r: Print output as raw comma-delimited data\n");
+       fprintf(output, "       -R: Print output as RADIUS attributes and values\n");
+       fprintf(output, "           Includes ALL information from the radutmp record.\n");
        fprintf(output, "       -s: show full name\n");
        fprintf(output, "       -S: hide shell users from radius\n");
        fprintf(output, "       -u <user>: Show entries matching the given user\n");
        fprintf(output, "       -U <user>: like -u, but case-sensitive\n");
+       fprintf(output, "       -Z: Include accounting stop information in radius output.  Requires -R.\n");
        exit(status);
 }
 
@@ -307,7 +294,6 @@ int main(int argc, char **argv)
        CONF_SECTION *maincs, *cs;
        FILE *fp;
        struct radutmp rt;
-       struct utmp ut;
        char inbuf[128];
        char othername[256];
        char nasname[1024];
@@ -319,7 +305,8 @@ int main(int argc, char **argv)
        int radiusoutput = 0;   /* Radius attributes */
        char *p, *q;
        const char *portind;
-       int c, portno;
+       int c;
+       unsigned int portno;
        char buffer[2048];
        const char *user = NULL;
        int user_cmp = 0;
@@ -348,8 +335,7 @@ int main(int argc, char **argv)
                        showname = 0;
                        break;
                case 'N':
-                       nas_ip_address = ip_addr(optarg);
-                       if (nas_ip_address == INADDR_NONE) {
+                       if (inet_pton(AF_INET, optarg, &nas_ip_address) < 0) {
                                usage(1);
                        }
                        break;
@@ -398,21 +384,21 @@ int main(int argc, char **argv)
        if (zap && !radiusoutput) zap = 0;
 
        /*
-        *      Don't zap EVERYONE.     
-        */
-       if (zap && (nas_ip_address == INADDR_NONE)) usage(1);
-
-       /*
         *      zap EVERYONE, but only on this nas
         */
        if (zap && !user && (~nas_port == 0)) {
+               /*
+                *      We need to know which NAS to zap users in.
+                */
+               if (nas_ip_address == INADDR_NONE) usage(1);
+
                printf("Acct-Status-Type = Accounting-Off\n");
                printf("NAS-IP-Address = %s\n",
-                      ip_hostname(buffer, sizeof(buffer), nas_ip_address));
+                      hostname(buffer, sizeof(buffer), nas_ip_address));
                printf("Acct-Delay-Time = 0\n");
                exit(0);        /* don't bother printing anything else */
        }
-       
+
        /*
         *      Initialize mainconfig
         */
@@ -421,7 +407,7 @@ int main(int argc, char **argv)
 
         /* Read radiusd.conf */
        snprintf(buffer, sizeof(buffer), "%.200s/radiusd.conf", radius_dir);
-       maincs = conf_read(NULL, 0, buffer, NULL);
+       maincs = cf_file_read(buffer);
        if (!maincs) {
                fprintf(stderr, "%s: Error reading radiusd.conf.\n", argv[0]);
                exit(1);
@@ -525,10 +511,10 @@ int main(int argc, char **argv)
                if (nas_ip_address != INADDR_NONE) {
                        if (rt.nas_address != nas_ip_address) continue;
                }
-               
+
                memcpy(session_id, rt.session_id, sizeof(rt.session_id));
                session_id[sizeof(rt.session_id)] = 0;
-               
+
                if (!rawoutput && rt.nas_port > (showname ? 999 : 99999)) {
                        portind = ">";
                        portno = (showname ? 999 : 99999);
@@ -537,28 +523,36 @@ int main(int argc, char **argv)
                        portno = rt.nas_port;
                }
 
-#define CPY(foo) memcpy(buffer, foo, sizeof(foo));buffer[sizeof(foo)] = 0
                /*
                 *      Print output as RADIUS attributes
                 */
                if (radiusoutput) {
-                       CPY(rt.login);
+                       memcpy(nasname, rt.login, sizeof(rt.login));
+                       nasname[sizeof(rt.login)] = '\0';
+
+                       fr_print_string(nasname, 0, buffer,
+                                        sizeof(buffer));
                        printf("User-Name = \"%s\"\n", buffer);
-                       printf("Acct-Session-Id = \"%s\"\n", session_id);
+
+                       fr_print_string(session_id, 0, buffer,
+                                        sizeof(buffer));
+                       printf("Acct-Session-Id = \"%s\"\n", buffer);
 
                        if (zap) printf("Acct-Status-Type = Stop\n");
 
                        printf("NAS-IP-Address = %s\n",
-                              ip_hostname(buffer, sizeof(buffer),
-                                          rt.nas_address));
-                       printf("NAS-Port = %d\n", rt.nas_port);
+                              hostname(buffer, sizeof(buffer),
+                                       rt.nas_address));
+                       printf("NAS-Port = %u\n", rt.nas_port);
 
                        switch (rt.proto) {
                                case 'S':
-                                       printf("Service-Type = SLIP\n");
+                                       printf("Service-Type = Framed-User\n");
+                                       printf("Framed-Protocol = SLIP\n");
                                        break;
                                case 'P':
-                                       printf("Service-Type = PPP\n");
+                                       printf("Service-Type = Framed-User\n");
+                                       printf("Framed-Protocol = PPP\n");
                                        break;
                                default:
                                        printf("Service-type = Login-User\n");
@@ -566,10 +560,10 @@ int main(int argc, char **argv)
                        }
                        if (rt.framed_address != INADDR_NONE) {
                                printf("Framed-IP-Address = %s\n",
-                                      ip_hostname(buffer, sizeof(buffer),
-                                                  rt.framed_address));
+                                      hostname(buffer, sizeof(buffer),
+                                               rt.framed_address));
                        }
-                       
+
                        /*
                         *      Some sanity checks on the time
                         */
@@ -579,6 +573,16 @@ int main(int argc, char **argv)
                                       now - rt.time);
                        }
 
+                       if (rt.caller_id[0] != '\0') {
+                               memcpy(nasname, rt.caller_id,
+                                      sizeof(rt.caller_id));
+                               nasname[sizeof(rt.caller_id)] = '\0';
+
+                               fr_print_string(nasname, 0, buffer,
+                                                sizeof(buffer));
+                               printf("Calling-Station-Id = \"%s\"\n", buffer);
+                       }
+
                        printf("\n"); /* separate entries with a blank line */
                        continue;
                }
@@ -594,7 +598,7 @@ int main(int argc, char **argv)
                               proto(rt.proto, rt.porttype),
                               portind, portno,
                               dotime(rt.time),
-                              ip_hostname(nasname, sizeof(nasname), rt.nas_address),
+                              hostname(nasname, sizeof(nasname), rt.nas_address),
                               hostname(othername, sizeof(othername), rt.framed_address), eol);
                } else {
                        printf((rawoutput == 0? rfmt2: rfmt2r),
@@ -602,7 +606,7 @@ int main(int argc, char **argv)
                               portind, portno,
                               proto(rt.proto, rt.porttype),
                               dotime(rt.time),
-                              ip_hostname(nasname, sizeof(nasname), rt.nas_address),
+                              hostname(nasname, sizeof(nasname), rt.nas_address),
                               hostname(othername, sizeof(othername), rt.framed_address),
                               eol);
                }