Changeset View
Standalone View
usr.bin/logger/logger.c
Show All 38 Lines | |||||
#ifndef lint | #ifndef lint | ||||
static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; | static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93"; | ||||
#endif /* not lint */ | #endif /* not lint */ | ||||
#endif | #endif | ||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/capsicum.h> | |||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/socket.h> | #include <sys/socket.h> | ||||
#include <netinet/in.h> | #include <netinet/in.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <netdb.h> | #include <netdb.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <time.h> | #include <time.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <libcasper.h> | |||||
#include <casper/cap_syslog.h> | |||||
#define SYSLOG_NAMES | #define SYSLOG_NAMES | ||||
#include <syslog.h> | #include <syslog.h> | ||||
#define sstosa(ss) ((struct sockaddr *)(void *)ss) | #define sstosa(ss) ((struct sockaddr *)(void *)ss) | ||||
struct socks { | struct socks { | ||||
int sk_sock; | int sk_sock; | ||||
int sk_addrlen; | int sk_addrlen; | ||||
struct sockaddr_storage sk_addr; | struct sockaddr_storage sk_addr; | ||||
}; | }; | ||||
static int decode(char *, const CODE *); | static int decode(char *, const CODE *); | ||||
static int pencode(char *); | static int pencode(char *); | ||||
static ssize_t socksetup(const char *, const char *, const char *, | static ssize_t socksetup(const char *, const char *, const char *, | ||||
struct socks **); | struct socks **); | ||||
static void logmessage(int, const char *, const char *, const char *, | static void logmessage(int, const char *, const char *, const char *, | ||||
struct socks *, ssize_t, const char *); | struct socks *, ssize_t, const char *); | ||||
static void usage(void); | static void usage(void); | ||||
static cap_channel_t *capcas, *capsyslog; | |||||
oshogbo: There is no need for capcas to be global. | |||||
Done Inline ActionsFixed. tig_freebsdfoundation.org: Fixed. | |||||
#ifdef INET6 | #ifdef INET6 | ||||
static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ | static int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */ | ||||
#else | #else | ||||
static int family = PF_INET; /* protocol family (IPv4 only) */ | static int family = PF_INET; /* protocol family (IPv4 only) */ | ||||
#endif | #endif | ||||
static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ | static int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */ | ||||
/* | /* | ||||
* logger -- read and log utility | * logger -- read and log utility | ||||
* | * | ||||
* Reads from an input and arranges to write the result on the system | * Reads from an input and arranges to write the result on the system | ||||
* log. | * log. | ||||
*/ | */ | ||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
struct socks *socks; | struct socks *socks; | ||||
ssize_t nsock; | ssize_t nsock; | ||||
time_t now; | time_t now; | ||||
int ch, logflags, pri; | int ch, logflags, pri; | ||||
char *tag, *host, buf[1024], *timestamp, tbuf[26], | char *tag, *host, buf[1024], *timestamp, tbuf[26], | ||||
*hostname, hbuf[MAXHOSTNAMELEN]; | *hostname, hbuf[MAXHOSTNAMELEN], *pristr; | ||||
const char *svcname, *src; | const char *svcname, *src; | ||||
tag = NULL; | tag = NULL; | ||||
host = NULL; | host = NULL; | ||||
hostname = NULL; | hostname = NULL; | ||||
svcname = "syslog"; | svcname = "syslog"; | ||||
src = NULL; | src = NULL; | ||||
socks = NULL; | socks = NULL; | ||||
Show All 26 Lines | case 'h': /* hostname to deliver to */ | ||||
break; | break; | ||||
case 'i': /* log process id also */ | case 'i': /* log process id also */ | ||||
logflags |= LOG_PID; | logflags |= LOG_PID; | ||||
break; | break; | ||||
case 'P': /* service name or port number */ | case 'P': /* service name or port number */ | ||||
svcname = optarg; | svcname = optarg; | ||||
break; | break; | ||||
case 'p': /* priority */ | case 'p': /* priority */ | ||||
pri = pencode(optarg); | pristr = optarg; | ||||
break; | break; | ||||
case 's': /* log to standard error */ | case 's': /* log to standard error */ | ||||
logflags |= LOG_PERROR; | logflags |= LOG_PERROR; | ||||
break; | break; | ||||
case 'S': /* source address */ | case 'S': /* source address */ | ||||
src = optarg; | src = optarg; | ||||
break; | break; | ||||
case 't': /* tag */ | case 't': /* tag */ | ||||
Show All 11 Lines | if (host) { | ||||
if (nsock <= 0) | if (nsock <= 0) | ||||
errx(1, "socket"); | errx(1, "socket"); | ||||
} else { | } else { | ||||
if (src) | if (src) | ||||
errx(1, "-h option is missing."); | errx(1, "-h option is missing."); | ||||
nsock = 0; | nsock = 0; | ||||
} | } | ||||
capcas = cap_init(); | |||||
if (capcas == NULL) | |||||
err(1, "Unable to contact Casper"); | |||||
if (cap_enter() < 0) | |||||
Not Done Inline Actionsyou don't want to use caph_enter ? oshogbo: you don't want to use caph_enter ? | |||||
Done Inline ActionsIs there a difference? tig_freebsdfoundation.org: Is there a difference? | |||||
Not Done Inline ActionsYes. The difference is that cap_enter syscall may not be build in the system and then you have to check for errno != ENOSYS, otherwise tool will not work on the systems without CAPSICUM. caph_* hides it from you. oshogbo: Yes. The difference is that cap_enter syscall may not be build in the system and then you have… | |||||
err(1, "Unable to enter capability mode"); | |||||
Not Done Inline ActionsStyle error. oshogbo: Style error. | |||||
Done Inline ActionsFixed. tig_freebsdfoundation.org: Fixed. | |||||
capsyslog = cap_service_open(capcas, "system.syslog"); | |||||
Not Done Inline ActionsNow you don't have to check for errno. oshogbo: Now you don't have to check for errno. | |||||
if (capsyslog == NULL) | |||||
err(1, "Unable to open system.syslog service"); | |||||
cap_close(capcas); | |||||
if ((char)ch == 'p') | |||||
pri = pencode(pristr); | |||||
Not Done Inline Actionsoptarg may not point at the argument any longer - for example a command line of -p foo -S bar will have optarg pointing to bar at this point. emaste: optarg may not point at the argument any longer - for example a command line of `-p foo -S bar`… | |||||
Not Done Inline ActionsOops - ch will also not still be 'p'. emaste: Oops - `ch` will also not still be 'p'.
Probably initialize `pristr` to `NULL` and make this… | |||||
Done Inline Actionsoops. Fixed tig_freebsdfoundation.org: oops. Fixed | |||||
if (tag == NULL) | if (tag == NULL) | ||||
tag = getlogin(); | tag = getlogin(); | ||||
/* setup for logging */ | /* setup for logging */ | ||||
if (host == NULL) | if (host == NULL) | ||||
openlog(tag, logflags, 0); | cap_openlog(capsyslog, tag, logflags, 0); | ||||
(void) fclose(stdout); | |||||
(void )time(&now); | (void )time(&now); | ||||
Not Done Inline ActionsYou have to move this or read the tzdata. oshogbo: You have to move this or read the tzdata.
See caph_cache_tzdata. | |||||
Done Inline ActionsAdded caph_cache_tzdata() tig_freebsdfoundation.org: Added caph_cache_tzdata() | |||||
(void )ctime_r(&now, tbuf); | (void )ctime_r(&now, tbuf); | ||||
tbuf[19] = '\0'; | tbuf[19] = '\0'; | ||||
timestamp = tbuf + 4; | timestamp = tbuf + 4; | ||||
if (hostname == NULL) { | if (hostname == NULL) { | ||||
hostname = hbuf; | hostname = hbuf; | ||||
(void )gethostname(hbuf, MAXHOSTNAMELEN); | (void )gethostname(hbuf, MAXHOSTNAMELEN); | ||||
*strchrnul(hostname, '.') = '\0'; | *strchrnul(hostname, '.') = '\0'; | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | socksetup(const char *src, const char *dst, const char *svcname, | ||||
memset(&ss_src[0], 0, sizeof(ss_src)); | memset(&ss_src[0], 0, sizeof(ss_src)); | ||||
if (src) { | if (src) { | ||||
char *p, *p0, *hs, *hbuf, *sbuf; | char *p, *p0, *hs, *hbuf, *sbuf; | ||||
hbuf = sbuf = NULL; | hbuf = sbuf = NULL; | ||||
p0 = p = strdup(src); | p0 = p = strdup(src); | ||||
if (p0 == NULL) | if (p0 == NULL) | ||||
err(1, "strdup failed"); | err(1, "strdup failed"); | ||||
Not Done Inline ActionsThose err means you probably want NLS. oshogbo: Those err means you probably want NLS.
See caph_cache_catpages. | |||||
Done Inline ActionsAdded caph_cache_catpages tig_freebsdfoundation.org: Added caph_cache_catpages | |||||
hs = p0; /* point to search ":" */ | hs = p0; /* point to search ":" */ | ||||
#ifdef INET6 | #ifdef INET6 | ||||
/* -S option supports IPv6 addr in "[2001:db8::1]:service". */ | /* -S option supports IPv6 addr in "[2001:db8::1]:service". */ | ||||
if (*p0 == '[') { | if (*p0 == '[') { | ||||
p = strchr(p0, ']'); | p = strchr(p0, ']'); | ||||
if (p == NULL) | if (p == NULL) | ||||
errx(1, "\"]\" not found in src addr"); | errx(1, "\"]\" not found in src addr"); | ||||
*p = '\0'; | *p = '\0'; | ||||
▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | |||||
static void | static void | ||||
logmessage(int pri, const char *timestamp, const char *hostname, | logmessage(int pri, const char *timestamp, const char *hostname, | ||||
const char *tag, struct socks *sk, ssize_t nsock, const char *buf) | const char *tag, struct socks *sk, ssize_t nsock, const char *buf) | ||||
{ | { | ||||
char *line; | char *line; | ||||
int len, i, lsent; | int len, i, lsent; | ||||
if (nsock == 0) { | if (nsock == 0) { | ||||
syslog(pri, "%s", buf); | cap_syslog(capsyslog, pri, "%s", buf); | ||||
return; | return; | ||||
} | } | ||||
if ((len = asprintf(&line, "<%d>%s %s %s: %s", pri, timestamp, | if ((len = asprintf(&line, "<%d>%s %s %s: %s", pri, timestamp, | ||||
hostname, tag, buf)) == -1) | hostname, tag, buf)) == -1) | ||||
errx(1, "asprintf"); | errx(1, "asprintf"); | ||||
lsent = -1; | lsent = -1; | ||||
for (i = 0; i < nsock; i++) { | for (i = 0; i < nsock; i++) { | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |
There is no need for capcas to be global.