Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/libexec/identd Some minor cleanup:
details: https://anonhg.NetBSD.org/src/rev/fb0810c1d63c
branches: trunk
changeset: 574756:fb0810c1d63c
user: peter <peter%NetBSD.org@localhost>
date: Fri Mar 11 15:49:52 2005 +0000
description:
Some minor cleanup:
* Improve some comments and (error) messages.
* Use EXIT_FAILURE and EXIT_SUCCESS.
* Add function `maybe_syslog' (only log when -l is enabled).
Reviewed by christos.
diffstat:
libexec/identd/identd.c | 280 +++++++++++++++++++++++------------------------
1 files changed, 138 insertions(+), 142 deletions(-)
diffs (truncated from 605 to 300 lines):
diff -r a9ce419ba122 -r fb0810c1d63c libexec/identd/identd.c
--- a/libexec/identd/identd.c Fri Mar 11 15:48:40 2005 +0000
+++ b/libexec/identd/identd.c Fri Mar 11 15:49:52 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: identd.c,v 1.24 2004/11/05 21:56:01 dsl Exp $ */
+/* $NetBSD: identd.c,v 1.25 2005/03/11 15:49:52 peter Exp $ */
/*
* identd.c - TCP/IP Ident protocol server.
@@ -31,17 +31,18 @@
#include <poll.h>
#include <pwd.h>
#include <signal.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
-__RCSID("$NetBSD: identd.c,v 1.24 2004/11/05 21:56:01 dsl Exp $");
+__RCSID("$NetBSD: identd.c,v 1.25 2005/03/11 15:49:52 peter Exp $");
-#define OPSYS_NAME "UNIX"
-#define IDENT_SERVICE "auth"
-#define TIMEOUT 30 /* seconds */
+#define OPSYS_NAME "UNIX"
+#define IDENT_SERVICE "auth"
+#define TIMEOUT 30 /* seconds */
static int idhandle(int, const char *, const char *, const char *,
const char *, int);
@@ -53,10 +54,11 @@
static int check_noident(const char *);
static int check_userident(const char *, char *, size_t);
static void random_string(char *, size_t);
-static void change_format(const char *, struct passwd *, char *, size_t);
+static int change_format(const char *, struct passwd *, char *, size_t);
static void timeout_handler(int);
static void waitchild(int);
static void fatal(const char *);
+static void maybe_syslog(int, const char *, ...);
static int bflag, eflag, fflag, Fflag, iflag, Iflag;
static int lflag, Lflag, nflag, Nflag, rflag;
@@ -65,9 +67,8 @@
main(int argc, char *argv[])
{
int IPv4or6, ch, *socks, timeout;
- char *address, *charset, *fmt;
+ char *address, *charset, *fmt, *p;
const char *osname, *portno;
- char *p;
char user[LOGIN_NAME_MAX];
struct group *grp;
struct passwd *pw;
@@ -78,19 +79,16 @@
osname = OPSYS_NAME;
portno = IDENT_SERVICE;
timeout = TIMEOUT;
- address = NULL;
- charset = NULL;
- fmt = NULL;
- socks = NULL;
- gid = uid = 0;
+ address = charset = fmt = NULL;
+ uid = gid = 0;
bflag = eflag = fflag = Fflag = iflag = Iflag = 0;
lflag = Lflag = nflag = Nflag = rflag = 0;
- /* Started from a tty? then run as daemon */
+ /* Started from a tty? then run as daemon. */
if (isatty(0))
bflag = 1;
- /* Parse arguments */
+ /* Parse command line arguments. */
while ((ch = getopt(argc, argv, "46a:bceF:f:g:IiL:lNno:p:rt:u:")) != -1)
switch (ch) {
case '4':
@@ -125,7 +123,8 @@
if ((grp = getgrnam(optarg)) != NULL)
gid = grp->gr_gid;
else
- errx(1, "No such group '%s'", optarg);
+ errx(EXIT_FAILURE,
+ "No such group '%s'", optarg);
}
break;
case 'I':
@@ -159,7 +158,8 @@
case 't':
timeout = (int)strtol(optarg, &p, 0);
if (*p != '\0')
- errx(1, "Invalid timeout value '%s'", optarg);
+ errx(EXIT_FAILURE,
+ "Invalid timeout value '%s'", optarg);
break;
case 'u':
uid = (uid_t)strtol(optarg, &p, 0);
@@ -168,46 +168,47 @@
uid = pw->pw_uid;
gid = pw->pw_gid;
} else
- errx(1, "No such user '%s'", optarg);
+ errx(EXIT_FAILURE,
+ "No such user '%s'", optarg);
}
break;
default:
- exit(1);
+ exit(EXIT_FAILURE);
}
if (lflag)
openlog("identd", LOG_PID, LOG_DAEMON);
- /* Setup sockets if -b flag */
- if (bflag) {
+ /* Setup sockets when running in the background. */
+ if (bflag)
socks = socketsetup(address, portno, IPv4or6);
- if (socks == NULL)
- return 1;
+
+ /* Switch to another uid/gid? */
+ if (gid && setgid(gid) == -1) {
+ maybe_syslog(LOG_ERR, "setgid: %m");
+ if (bflag)
+ warn("setgid");
+ exit(EXIT_FAILURE);
+ }
+ if (uid && setuid(uid) == -1) {
+ maybe_syslog(LOG_ERR, "setuid: %m");
+ if (bflag)
+ warn("setuid");
+ exit(EXIT_FAILURE);
}
- /* Switch to another uid/gid ? */
- if (gid && setgid(gid) == -1) {
- if (lflag)
- syslog(LOG_ERR, "setgid: %m");
- if (bflag)
- warn("setgid");
- exit(1);
- }
- if (uid && setuid(uid) == -1) {
- if (lflag)
- syslog(LOG_ERR, "setuid: %m");
- if (bflag)
- warn("setuid");
- exit(1);
- }
-
- /* Daemonize, setup pollfds and start mainloop if -b flag */
+ /*
+ * When running as daemon: daemonize, setup pollfds and go into
+ * the mainloop. Otherwise, just read the input from stdin and
+ * let inetd handle the sockets.
+ */
if (bflag) {
int fd, i, nfds, rv;
struct pollfd *rfds;
(void)signal(SIGCHLD, waitchild);
- (void)daemon(0, 0);
+ if (daemon(0, 0) < 0)
+ err(EXIT_FAILURE, "daemon");
rfds = malloc(*socks * sizeof(struct pollfd));
if (rfds == NULL)
@@ -218,33 +219,32 @@
rfds[i].events = POLLIN;
rfds[i].revents = 0;
}
- /* Mainloop for daemon */
+ /* Mainloop for daemon. */
for (;;) {
rv = poll(rfds, nfds, INFTIM);
- if (rv < 0 && errno == EINTR)
- continue;
- if (rv < 0)
+ if (rv < 0) {
+ if (errno == EINTR)
+ continue;
fatal("poll");
+ }
for (i = 0; i < nfds; i++) {
if (rfds[i].revents & POLLIN) {
fd = accept(rfds[i].fd, NULL, NULL);
if (fd < 0) {
- if (lflag)
- syslog(LOG_ERR,
- "accept: %m");
+ maybe_syslog(LOG_ERR,
+ "accept: %m");
continue;
}
switch (fork()) {
case -1: /* error */
- if (lflag)
- syslog(LOG_ERR,
- "fork: %m");
+ maybe_syslog(LOG_ERR,
+ "fork: %m");
(void)sleep(1);
break;
case 0: /* child */
(void)idhandle(fd, charset,
fmt, osname, user, timeout);
- _exit(0);
+ _exit(EXIT_SUCCESS);
default: /* parent */
(void)close(fd);
}
@@ -277,38 +277,35 @@
(void)signal(SIGALRM, timeout_handler);
(void)alarm(timeout);
- /* Get foreign internet address */
+ /* Get foreign internet address. */
len = sizeof(ss[0]);
if (getpeername(fd, (struct sockaddr *)&ss[0], &len) < 0)
fatal("getpeername");
- if (lflag)
- syslog(LOG_INFO, "Connection from %s", gethost(&ss[0]));
+ maybe_syslog(LOG_INFO, "Connection from %s", gethost(&ss[0]));
- /* Get local internet address */
+ /* Get local internet address. */
len = sizeof(ss[1]);
if (getsockname(fd, (struct sockaddr *)&ss[1], &len) < 0)
fatal("getsockname");
- /* Be sure to have the same address family's */
+ /* Be sure to have the same address families. */
if (ss[0].ss_family != ss[1].ss_family) {
- if (lflag)
- syslog(LOG_ERR, "Foreign/local AF are different!");
+ maybe_syslog(LOG_ERR, "Different foreign/local address family");
return 1;
}
- /* Receive data from the client */
+ /* Receive data from the client. */
if ((n = recv(fd, buf, sizeof(buf) - 1, 0)) < 0) {
fatal("recv");
} else if (n == 0) {
- if (lflag)
- syslog(LOG_NOTICE, "recv: EOF");
+ maybe_syslog(LOG_NOTICE, "recv: EOF");
iderror(fd, 0, 0, "UNKNOWN-ERROR");
return 1;
}
buf[n] = '\0';
- /* Get local and remote ports from the received data */
+ /* Get local and remote ports from the received data. */
p = buf;
while (*p != '\0' && isspace((unsigned char)*p))
p++;
@@ -320,23 +317,21 @@
/* Are the ports valid? */
if (lport < 1 || lport > 65535 || fport < 1 || fport > 65535) {
- if (lflag)
- syslog(LOG_NOTICE, "Invalid port(s): %d, %d from %s",
- lport, fport, gethost(&ss[0]));
+ maybe_syslog(LOG_NOTICE, "Invalid port(s): %d, %d from %s",
+ lport, fport, gethost(&ss[0]));
iderror(fd, 0, 0, eflag ? "UNKNOWN-ERROR" : "INVALID-PORT");
return 1;
}
- /* If there is a 'lie' user enabled, then handle it now and quit */
+ /* If there is a 'lie' user enabled, then handle it now and stop. */
if (Lflag) {
- if (lflag)
- syslog(LOG_NOTICE, "Lying with name %s to %s",
- idbuf, gethost(&ss[0]));
+ maybe_syslog(LOG_NOTICE, "Lying with name %s to %s",
+ idbuf, gethost(&ss[0]));
idparse(fd, lport, fport, charset, osname, idbuf);
return 0;
}
- /* Protocol dependent stuff */
+ /* Protocol dependent stuff. */
switch (ss[0].ss_family) {
case AF_INET:
((struct sockaddr_in *)&ss[0])->sin_port = htons(fport);
@@ -347,20 +342,17 @@
((struct sockaddr_in6 *)&ss[1])->sin6_port = htons(lport);
break;
default:
- if (lflag)
- syslog(LOG_ERR, "Unsupported protocol, proto no. %d",
- ss[0].ss_family);
Home |
Main Index |
Thread Index |
Old Index