]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/misc/tcpwrapper.c
tcpwrapper: Fix signage problems in the tcp_wrappers hash function
[nfs-utils.git] / support / misc / tcpwrapper.c
index af626adc5b5cdb430f20c92549a0c02a813823f4..6f65c1375c4b1cfa5e2b18654604f20af649ce9f 100644 (file)
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
+
 #ifdef HAVE_LIBWRAP
-#include <tcpwrapper.h>
 #include <unistd.h>
 #include <string.h>
 #include <rpc/rpc.h>
 #include <rpc/pmap_prot.h>
-#include <syslog.h>
 #include <netdb.h>
 #include <pwd.h>
 #include <sys/types.h>
@@ -49,6 +48,7 @@
 #include <sys/stat.h>
 #include <tcpd.h>
 
+#include "tcpwrapper.h"
 #include "xlog.h"
 
 #ifdef SYSV40
 #include <rpc/rpcent.h>
 #endif
 
-static void logit(int severity, struct sockaddr_in *addr,
-                 u_long procnum, u_long prognum, char *text);
 static int check_files(void);
 
-/*
- * These need to exist since they are externed 
- * public header files.
- */
-int     verboselog = 0;
-int     allow_severity = LOG_INFO;
-int     deny_severity = LOG_WARNING;
-
-#define log_bad_host(addr, proc, prog) \
-  logit(deny_severity, addr, proc, prog, "request from unauthorized host")
-
 #define ALLOW 1
 #define DENY 0
 
@@ -88,29 +75,35 @@ hash_head haccess_tbl[HASH_TABLE_SIZE];
 static haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long);
 static void haccess_add(struct sockaddr_in *addr, u_long, int);
 
-inline unsigned int strtoint(char *str)
+static unsigned long
+strtoint(const char *str)
 {
-       unsigned int n = 0;
-       int len = strlen(str);
-       int i;
+       unsigned long i, n = 0;
+       size_t len = strlen(str);
 
-       for (i=0; i < len; i++)
-               n+=((int)str[i])*i;
+       for (i = 0; i < len; i++)
+               n += (unsigned char)str[i] * i;
 
        return n;
 }
-static inline int hashint(unsigned int num)
+
+static unsigned int
+hashint(const unsigned long num)
+{
+       return (unsigned int)(num % HASH_TABLE_SIZE);
+}
+
+static unsigned int
+HASH(const char *addr, const unsigned long program)
 {
-       return num % HASH_TABLE_SIZE;
+       return hashint(strtoint(addr) + program);
 }
-#define HASH(_addr, _prog) \
-       hashint((strtoint((_addr))+(_prog)))
 
 void haccess_add(struct sockaddr_in *addr, u_long prog, int access)
 {
        hash_head *head;
-       haccess_t *hptr;
-       int hash;
+       haccess_t *hptr;
+       unsigned int hash;
 
        hptr = (haccess_t *)malloc(sizeof(haccess_t));
        if (hptr == NULL)
@@ -130,8 +123,8 @@ void haccess_add(struct sockaddr_in *addr, u_long prog, int access)
 haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long prog)
 {
        hash_head *head;
-       haccess_t *hptr;
-       int hash;
+       haccess_t *hptr;
+       unsigned int hash;
 
        hash = HASH(inet_ntoa(addr->sin_addr), prog);
        head = &(haccess_tbl[hash]);
@@ -143,6 +136,16 @@ haccess_t *haccess_lookup(struct sockaddr_in *addr, u_long prog)
        return NULL;
 }
 
+static void
+logit(const struct sockaddr_in *sin)
+{
+       char buf[INET_ADDRSTRLEN];
+
+       xlog_warn("connect from %s denied: request from unauthorized host",
+                       inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)));
+               
+}
+
 int
 good_client(daemon, addr)
 char *daemon;
@@ -186,14 +189,17 @@ static int check_files()
        return changed;
 }
 
-/* check_default - additional checks for NULL, DUMP, GETPORT and unknown */
-
+/**
+ * check_default - additional checks for NULL, DUMP, GETPORT and unknown
+ * @daemon: pointer to '\0'-terminated ASCII string containing name of the
+ *             daemon requesting the access check
+ * @addr: pointer to socket address containing address of caller
+ * @prog: RPC program number caller is attempting to access
+ *
+ * Returns TRUE if the caller is allowed access; otherwise FALSE is returned.
+ */
 int
-check_default(daemon, addr, proc, prog)
-char *daemon;
-struct sockaddr_in *addr;
-u_long  proc;
-u_long  prog;
+check_default(char *daemon, struct sockaddr_in *addr, u_long prog)
 {
        haccess_t *acc = NULL;
        int changed = check_files();
@@ -203,7 +209,7 @@ u_long  prog;
                return (acc->access);
 
        if (!(from_local((struct sockaddr *)addr) || good_client(daemon, addr))) {
-               log_bad_host(addr, proc, prog);
+               logit(addr);
                if (acc)
                        acc->access = FALSE;
                else 
@@ -219,12 +225,4 @@ u_long  prog;
     return (TRUE);
 }
 
-/* logit - report events of interest via the syslog daemon */
-
-static void logit(int severity, struct sockaddr_in *addr,
-                 u_long procnum, u_long prognum, char *text)
-{
-       syslog(severity, "connect from %s denied: %s",
-              inet_ntoa(addr->sin_addr), text);
-}
-#endif
+#endif /* HAVE_LIBWRAP */