summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/minidentd/deps-exe/minidentd6
-rw-r--r--src/minidentd/mgetuid-default.c16
-rw-r--r--src/minidentd/mgetuid-linux.c184
-rw-r--r--src/minidentd/mgetuid.h12
-rw-r--r--src/minidentd/minidentd.c275
5 files changed, 0 insertions, 493 deletions
diff --git a/src/minidentd/deps-exe/minidentd b/src/minidentd/deps-exe/minidentd
deleted file mode 100755
index 572ca89..0000000
--- a/src/minidentd/deps-exe/minidentd
+++ /dev/null
@@ -1,6 +0,0 @@
-mgetuid.o
-${LIBNSSS}
--lskarnet
-${MAYBEPTHREAD_LIB}
-${SOCKET_LIB}
-${SYSCLOCK_LIB}
diff --git a/src/minidentd/mgetuid-default.c b/src/minidentd/mgetuid-default.c
deleted file mode 100644
index 5c9f1d2..0000000
--- a/src/minidentd/mgetuid-default.c
+++ /dev/null
@@ -1,16 +0,0 @@
-/* ISC license. */
-
-#include <sys/types.h>
-#include <stdint.h>
-#include <errno.h>
-#include <skalibs/ip46.h>
-#include "mgetuid.h"
-
-uid_t mgetuid (ip46_t const *localaddr, uint16_t localport, ip46_t const *remoteaddr, uint16_t remoteport)
-{
- (void)localaddr ;
- (void)localport ;
- (void)remoteaddr ;
- (void)remoteport ;
- return (errno = ENOSYS, -2) ;
-}
diff --git a/src/minidentd/mgetuid-linux.c b/src/minidentd/mgetuid-linux.c
deleted file mode 100644
index f374adf..0000000
--- a/src/minidentd/mgetuid-linux.c
+++ /dev/null
@@ -1,184 +0,0 @@
-/* ISC license. */
-
-#include <sys/types.h>
-#include <string.h>
-#include <stdint.h>
-#include <skalibs/uint64.h>
-#include <skalibs/types.h>
-#include <skalibs/fmtscan.h>
-#include <skalibs/buffer.h>
-#include <skalibs/stralloc.h>
-#include <skalibs/djbunix.h>
-#include <skalibs/ip46.h>
-#include <skalibs/skamisc.h>
-#include "mgetuid.h"
-
-#ifdef DEBUG
-#include <skalibs/strerr2.h>
-#define bug(a) do { strerr_warn4x("bug parsing ", a, "remaining: ", cur) ; return 0 ; } while (0)
-#else
-#define bug(a) return 0
-#endif
-
-#define LINESIZE 256
-
-static int skipspace (char **s)
-{
- while (**s && ((**s == ' ') || (**s == '\t')))
- (*s)++ ;
- return (int)**s ;
-}
-
-static void reverse_address (char *s, size_t n)
-{
- size_t i = n >> 1 ;
- while (i--)
- {
- char tmp = s[i] ;
- s[i] = s[n-1-i] ;
- s[n-1-i] = tmp ;
- }
-}
-
-static int parseline (char *s, size_t len, uid_t *u, char *la, uint16_t *lp, char *ra, uint16_t *rp, int is6)
-{
- char *cur = s ;
- size_t pos ;
- uint64_t uu ;
- uint32_t junk ;
- unsigned int iplen = is6 ? 16 : 4 ;
-
- if (!skipspace(&cur)) bug("initial whitespace") ;
- pos = uint32_scan(cur, &junk) ; /* sl */
- if (!pos || (cur-s+1+pos) > len) bug("sl") ;
- cur += pos ;
- if ((*cur++) != ':') bug("sl:") ;
- if (!skipspace(&cur)) bug("sl: SPACE") ;
-
- if ((cur - s + 1 + iplen) > len) bug("local_address") ;
- pos = ucharn_scan(cur, la, iplen) ; /* local_address */
- reverse_address(la, iplen) ;
- if (!pos) bug("local_address") ;
- cur += pos ;
- if ((*cur++) != ':') bug("local_address:") ;
-
- pos = uint16_xscan(cur, lp) ; /* :port */
- if (!pos || (cur-s+pos) > len) bug("local_port") ;
- cur += pos ;
- if (!skipspace(&cur)) bug("local_port SPACE") ;
-
- if ((cur - s + 1 + iplen) > len) bug("remote_address") ;
- pos = ucharn_scan(cur, ra, iplen) ; /* remote_address */
- reverse_address(ra, iplen) ;
- if (!pos) bug("remote_address") ;
- cur += pos ;
- if ((*cur++) != ':') bug("remote_address:") ;
-
- pos = uint16_xscan(cur, rp) ; /* :port */
- if (!pos || (cur-s+pos) > len) bug("remote_port") ;
- cur += pos ;
- if (!skipspace(&cur)) bug("remote_port SPACE");
-
- pos = uint32_xscan(cur, &junk) ; /* st */
- if (!pos || (cur-s+pos) > len) bug("st") ;
- cur += pos ;
- if (!skipspace(&cur)) bug("st SPACE") ;
- pos = uint32_xscan(cur, &junk) ; /* tx_queue */
- if (!pos || (cur-s+1+pos) > len) bug("tx_queue") ;
- cur += pos ;
- if ((*cur++) != ':') bug("tx_queue:") ;
- pos = uint32_xscan(cur, &junk) ; /* rx_queue */
- if (!pos || (cur-s+pos) > len) bug("rx_queue") ;
- cur += pos ;
- if (!skipspace(&cur)) bug("rx_queue SPACE") ;
- pos = uint32_xscan(cur, &junk) ; /* tr */
- if (!pos || (cur-s+1+pos) > len) bug("tr") ;
- cur += pos ;
- if ((*cur++) != ':') bug("tr:") ;
- pos = uint32_xscan(cur, &junk) ; /* tm->when */
- if (!pos || (cur-s+pos) > len) bug("tm->when") ;
- cur += pos ;
- if (!skipspace(&cur)) bug("tm->when SPACE") ;
- pos = uint32_xscan(cur, &junk) ; /* retrnsmt */
- if (!pos || (cur-s+pos) > len) bug("retrnsmt") ;
- cur += pos ;
-
- if (!skipspace(&cur)) bug("retrnsmt SPACE") ;
- pos = uint64_scan(cur, &uu) ; /* uid */
- if (!pos || (cur-s+1+pos) > len) bug("uid") ;
- *u = uu ;
- return 1 ;
-}
-
-#ifdef DEBUG
-
-static void debuglog (uint16_t a, uint16_t b, unsigned int c, char const *d, char const *e, int is6)
-{
- char sa[UINT16_FMT] ;
- char sb[UINT16_FMT] ;
- char sc[UINT_FMT] ;
- char sd[IP46_FMT] ;
- char se[IP46_FMT] ;
-
- sa[uint16_fmt(sa, a)] = 0 ;
- sb[uint16_fmt(sb, b)] = 0 ;
- sc[uint_fmt(sc, c)] = 0 ;
- sd[is6 ? ip6_fmt(sd, d) : ip4_fmt(sd, d)] = 0 ;
- se[is6 ? ip6_fmt(se, e) : ip4_fmt(se, e)] = 0 ;
-
- buffer_puts(buffer_2, sd) ;
- buffer_puts(buffer_2, ":") ;
- buffer_puts(buffer_2, sa) ;
- buffer_puts(buffer_2, " , ") ;
- buffer_puts(buffer_2, se) ;
- buffer_puts(buffer_2, ":") ;
- buffer_puts(buffer_2, sb) ;
- buffer_puts(buffer_2, " -> ") ;
- buffer_puts(buffer_2, sc) ;
- buffer_putsflush(buffer_2, "\n") ;
-}
-
-#endif
-
-uid_t mgetuid (ip46_t const *localaddr, uint16_t localport, ip46_t const *remoteaddr, uint16_t remoteport)
-{
- int r ;
- uid_t u = -2 ;
- stralloc line = STRALLOC_ZERO ;
- buffer b ;
- char y[BUFFER_INSIZE] ;
- int is6 = ip46_is6(localaddr) ;
- int fd = open_readb(is6 ? "/proc/net/tcp6" : "/proc/net/tcp") ;
- if (fd == -1) return -2 ;
- buffer_init(&b, &buffer_read, fd, y, BUFFER_INSIZE_SMALL) ;
- if (skagetln(&b, &line, '\n') < 1) goto err ;
-#ifdef DEBUG
- line.s[line.len-1] = 0 ;
- debuglog(localport, remoteport, 65535, localaddr->ip, remoteaddr->ip, is6) ;
-#endif
- for (;;)
- {
- char la[16] ;
- char ra[16] ;
- uid_t nu ;
- uint16_t lp, rp ;
- line.len = 0 ;
- r = skagetln(&b, &line, '\n') ;
- if (r <= 0) { u = -1 ; break ; }
- line.s[line.len-1] = 0 ;
- if (!parseline(line.s, line.len, &nu, la, &lp, ra, &rp, is6)) break ;
-#ifdef DEBUG
- debuglog(lp, rp, nu, la, ra, is6) ;
-#endif
- if ((lp == localport) && (rp == remoteport)
- && !memcmp(la, localaddr->ip, is6 ? 16 : 4)
- && !memcmp(ra, remoteaddr->ip, is6 ? 16 : 4))
- {
- u = nu ; break ;
- }
- }
- stralloc_free(&line) ;
- err:
- fd_close(fd) ;
- return u ;
-}
diff --git a/src/minidentd/mgetuid.h b/src/minidentd/mgetuid.h
deleted file mode 100644
index 4b882e4..0000000
--- a/src/minidentd/mgetuid.h
+++ /dev/null
@@ -1,12 +0,0 @@
-/* ISC license. */
-
-#ifndef MGETUID_H
-#define MGETUID_H
-
-#include <sys/types.h>
-#include <stdint.h>
-#include <skalibs/ip46.h>
-
-extern uid_t mgetuid (ip46_t const *, uint16_t, ip46_t const *, uint16_t) ;
-
-#endif
diff --git a/src/minidentd/minidentd.c b/src/minidentd/minidentd.c
deleted file mode 100644
index a5115b7..0000000
--- a/src/minidentd/minidentd.c
+++ /dev/null
@@ -1,275 +0,0 @@
-/* ISC license. */
-
-#include <string.h>
-#include <stdint.h>
-#include <unistd.h>
-#include <errno.h>
-#include <pwd.h>
-
-#include <skalibs/types.h>
-#include <skalibs/allreadwrite.h>
-#include <skalibs/bytestr.h>
-#include <skalibs/fmtscan.h>
-#include <skalibs/buffer.h>
-#include <skalibs/strerr2.h>
-#include <skalibs/stralloc.h>
-#include <skalibs/env.h>
-#include <skalibs/djbunix.h>
-#include <skalibs/sgetopt.h>
-#include <skalibs/tai.h>
-#include <skalibs/random.h>
-#include <skalibs/unix-timed.h>
-
-#include "mgetuid.h"
-
-#define USAGE "minidentd [ -v ] [ -n | -i | -r ] [ -y file ] [ -t timeout ]"
-#define dieusage() strerr_dieusage(100, USAGE)
-
-
-static int how = 0 ;
-static int flagverbose = 0 ;
-static char const *userfile = ".ident" ;
-
-static tain_t deadline ;
-static unsigned int nquery = 0 ;
-static char logfmt[UINT_FMT] ;
-
-#define godecimal(s) while (*(s) && !strchr("0123456789", *(s))) (s)++
-
-static int parseline (char const *s, uint16_t *localport, uint16_t *remoteport)
-{
- size_t pos ;
- godecimal(s) ;
- if (!*s) return 0 ;
- pos = uint16_scan(s, localport) ;
- if (!pos) return 0 ;
- s += pos ;
- if (!*s) return 0 ;
- s += str_chr(s, ',') ;
- if (*s) s++ ;
- godecimal(s) ;
- if (!*s) return 0 ;
- if (!uint16_scan(s, remoteport)) return 0 ;
- return 1 ;
-}
-
-static void formatlr (char *s, uint16_t lp, uint16_t rp)
-{
- s += uint16_fmt(s, lp) ;
- *s++ = ',' ;
- *s++ = ' ' ;
- s += uint16_fmt(s, rp) ;
- *s = 0 ;
-}
-
-static void reply (char const *s, char const *r, char const *info)
-{
- buffer_puts(buffer_1small, s) ;
- buffer_put(buffer_1small, " : ", 3) ;
- buffer_puts(buffer_1small, r) ;
- buffer_put(buffer_1small, " : ", 3) ;
- buffer_puts(buffer_1small, info) ;
- buffer_put(buffer_1small, "\r\n", 2) ;
- if (!buffer_timed_flush_g(buffer_1small, &deadline))
- strerr_diefu1sys(111, "write to stdout") ;
-}
-
-static void logquery (char const *s)
-{
- if (!flagverbose) return ;
- buffer_puts(buffer_2, PROG) ;
- buffer_puts(buffer_2, ": info : query ") ;
- logfmt[uint_fmt(logfmt, ++nquery)] = 0 ;
- buffer_puts(buffer_2, logfmt) ;
- buffer_put(buffer_2, ": ", 2) ;
- buffer_puts(buffer_2, s) ;
- buffer_putflush(buffer_2, "\n", 1) ;
-}
-
-static void logreply (char const *type, char const *reply1, char const *reply2)
-{
- if (!flagverbose) return ;
- buffer_puts(buffer_2, PROG) ;
- buffer_puts(buffer_2, ": info: reply type ") ;
- buffer_puts(buffer_2, type) ;
- buffer_put(buffer_2, ": ", 2) ;
- buffer_puts(buffer_2, logfmt) ;
- buffer_put(buffer_2, ": ", 2) ;
- buffer_puts(buffer_2, reply1) ;
- buffer_put(buffer_2, ": ", 2) ;
- buffer_puts(buffer_2, reply2) ;
- buffer_putflush(buffer_2, "\n", 1) ;
-}
-
-static int userident (char *s, char const *home)
-{
- int fd ;
- size_t r = 1 ;
- {
- size_t homelen = strlen(home) ;
- size_t userlen = strlen(userfile) ;
- char tmp[homelen + userlen + 2] ;
- memcpy(tmp, home, homelen) ;
- tmp[homelen] = '/' ;
- memcpy(tmp + homelen + 1, userfile, userlen + 1) ;
- fd = open_readb(tmp) ;
- }
- if (fd == -1) return (errno != ENOENT) ? -1 : 0 ;
- if (how == 1)
- {
- fd_close(fd) ;
- return 1 ;
- }
- r = allread(fd, s, 14) ;
- fd_close(fd) ;
- if (!r) return 1 ;
- s[r] = 0 ;
- s[byte_chr(s, r, '\n')] = 0 ;
- return 2 ;
-}
-
-
-static void doit (char const *s, ip46_t const *localaddr, ip46_t const *remoteaddr)
-{
- char lr[15] ;
- uint16_t localport, remoteport ;
- struct passwd *pw ;
- uid_t uid ;
- if (!parseline(s, &localport, &remoteport))
- {
- reply("0, 0", "ERROR", "INVALID-PORT") ;
- return ;
- }
- formatlr(lr, localport, remoteport) ;
- logquery(lr) ;
-
- uid = mgetuid(localaddr, localport, remoteaddr, remoteport) ;
- if (uid == -2)
- {
- strerr_warnwu1sys("get uid") ;
- reply(lr, "ERROR", "UNKNOWN-ERROR") ;
- return ;
- }
- else if (uid == -1)
- {
- reply(lr, "ERROR", "NO-USER") ;
- logreply("error", "ERROR", "NO-USER") ;
- return ;
- }
-
- if (how == 3)
- {
- char name[9] ;
- char fmt[4 + UINT_FMT] = "uid " ;
- fmt[4 + uint_fmt(fmt+4, uid)] = 0 ;
- random_name(name, 8) ;
- reply(lr, "UNIX", name) ;
- logreply("random", fmt, name) ;
- return ;
- }
-
- pw = getpwuid(uid) ;
- if (!pw)
- {
- char fmt[UINT_FMT] ;
- fmt[uint_fmt(fmt, uid)] = 0 ;
- strerr_warnw2x("unknown uid ", fmt) ;
- reply(lr, "ERROR", "UNKNOWN-ERROR") ;
- return ;
- }
-
- if (how)
- {
- char s[15] ;
- int r = userident(s, pw->pw_dir) ;
- if ((how == 1) || (r == 1))
- {
- reply(lr, "ERROR", "HIDDEN-USER") ;
- logreply("user", "ERROR", "HIDDEN-USER") ;
- return ;
- }
- else if (r == 2)
- {
- reply(lr, "USERID : UNIX", s) ;
- logreply("user", "UNIX", s) ;
- return ;
- }
- }
-
- reply(lr, "USERID : UNIX", pw->pw_name) ;
- logreply("user", "UNIX", pw->pw_name) ;
-}
-
-
-int main (int argc, char const *const *argv, char const *const *envp)
-{
- stralloc line = STRALLOC_ZERO ;
- tain_t tto ;
- ip46_t localaddr, remoteaddr ;
- PROG = "minidentd" ;
-
- {
- subgetopt_t l = SUBGETOPT_ZERO ;
- unsigned int t = 0 ;
- for (;;)
- {
- int opt = subgetopt_r(argc, argv, "vniry:t:", &l) ;
- if (opt == -1) break ;
- switch (opt)
- {
- case 'v' : flagverbose = 1 ; break ;
- case 'n' : how = 1 ; break ;
- case 'i' : how = 2 ; break ;
- case 'r' : how = 3 ; break ;
- case 'y' : userfile = l.arg ; break ;
- case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ;
- default : dieusage() ;
- }
- }
- if (t) tain_from_millisecs(&tto, t) ; else tto = tain_infinite_relative ;
- argc -= l.ind ; argv += l.ind ;
- }
-
- {
- char const *proto = env_get2(envp, "PROTO") ;
- if (!proto) strerr_dienotset(100, "PROTO") ;
- {
- char const *x ;
- size_t protolen = strlen(proto) ;
- char tmp[protolen + 9] ;
- memcpy(tmp, proto, protolen) ;
- memcpy(tmp + protolen, "LOCALIP", 8) ;
- x = env_get2(envp, tmp) ;
- if (!x) strerr_dienotset(100, tmp) ;
- if (!ip46_scan(x, &localaddr)) strerr_dieinvalid(100, tmp) ;
- memcpy(tmp + protolen, "REMOTEIP", 9) ;
- x = env_get2(envp, tmp) ;
- if (!x) strerr_dienotset(100, tmp) ;
- if (!ip46_scan(x, &remoteaddr)) strerr_dieinvalid(100, tmp) ;
- }
- }
-
- if (ip46_is6(&localaddr) != ip46_is6(&remoteaddr))
- strerr_dief1x(100, "local and remote address not of the same family") ;
- if (!random_init())
- strerr_diefu1sys(111, "init random generator") ;
-
- tain_now_set_stopwatch_g() ;
-
- for (;;)
- {
- int r ;
- line.len = 0 ;
- tain_add_g(&deadline, &tto) ;
- r = timed_getln_g(buffer_0small, &line, '\n', &deadline) ;
- if (r == -1)
- {
- if (errno == ETIMEDOUT || errno == ECONNRESET) return 1 ;
- else strerr_diefu1sys(111, "read from stdin") ;
- }
- if (!r) break ;
- line.s[line.len - 1] = 0 ;
- doit(line.s, &localaddr, &remoteaddr) ;
- }
- return 0 ;
-}