Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/ypbind Sort contents of file.



details:   https://anonhg.NetBSD.org/src/rev/ef5a0503b071
branches:  trunk
changeset: 765297:ef5a0503b071
user:      dholland <dholland%NetBSD.org@localhost>
date:      Tue May 24 06:56:16 2011 +0000

description:
Sort contents of file.

diffstat:

 usr.sbin/ypbind/ypbind.c |  935 ++++++++++++++++++++++++----------------------
 1 files changed, 479 insertions(+), 456 deletions(-)

diffs (truncated from 1054 to 300 lines):

diff -r f240998d5025 -r ef5a0503b071 usr.sbin/ypbind/ypbind.c
--- a/usr.sbin/ypbind/ypbind.c  Tue May 24 02:31:11 2011 +0000
+++ b/usr.sbin/ypbind/ypbind.c  Tue May 24 06:56:16 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ypbind.c,v 1.67 2011/05/23 02:54:53 dholland Exp $     */
+/*     $NetBSD: ypbind.c,v 1.68 2011/05/24 06:56:16 dholland Exp $     */
 
 /*
  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt%fsa.ca@localhost>
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 #ifndef LINT
-__RCSID("$NetBSD: ypbind.c,v 1.67 2011/05/23 02:54:53 dholland Exp $");
+__RCSID("$NetBSD: ypbind.c,v 1.68 2011/05/24 06:56:16 dholland Exp $");
 #endif
 
 #include <sys/types.h>
@@ -68,14 +68,21 @@
 
 #include "pathnames.h"
 
+#define YPSERVERSSUFF  ".ypservers"
+#define BINDINGDIR     (_PATH_VAR_YP "binding")
+
 #ifndef O_SHLOCK
 #define O_SHLOCK 0
 #endif
 
-#define BUFSIZE                1400
+int _yp_invalid_domain(const char *);          /* XXX libc internal */
 
-#define YPSERVERSSUFF  ".ypservers"
-#define BINDINGDIR     (_PATH_VAR_YP "binding")
+////////////////////////////////////////////////////////////
+// types and globals
+
+typedef enum {
+       YPBIND_DIRECT, YPBIND_BROADCAST, YPBIND_SETLOCAL, YPBIND_SETALL
+} ypbind_mode_t;
 
 struct _dom_binding {
        struct _dom_binding *dom_pnext;
@@ -91,15 +98,13 @@
        uint32_t dom_xid;
 };
 
+#define BUFSIZE                1400
+
 static char *domainname;
 
 static struct _dom_binding *ypbindlist;
 static int check;
 
-typedef enum {
-       YPBIND_DIRECT, YPBIND_BROADCAST, YPBIND_SETLOCAL, YPBIND_SETALL
-} ypbind_mode_t;
-
 ypbind_mode_t ypbindmode;
 
 /*
@@ -109,13 +114,6 @@
  */
 int been_ypset;
 
-#ifdef DEBUG
-#define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
-static int debug;
-#else
-#define DPRINTF(...)
-#endif
-
 static int insecure;
 static int rpcsock, pingsock;
 static struct rmtcallargs rmtca;
@@ -124,8 +122,8 @@
 static unsigned long rmtcr_port;
 static SVCXPRT *udptransp, *tcptransp;
 
-int    _yp_invalid_domain(const char *);               /* from libc */
-int    main(int, char *[]);
+////////////////////////////////////////////////////////////
+// forward decls of functions
 
 static void usage(void);
 static void yp_log(int, const char *, ...) __printflike(2, 3);
@@ -149,20 +147,16 @@
 static int direct(char *, int);
 static int direct_set(char *, int, struct _dom_binding *);
 
-static void
-usage(void)
-{
-       const char *opt = "";
+////////////////////////////////////////////////////////////
+// logging
+
 #ifdef DEBUG
-       opt = " [-d]";
+#define DPRINTF(...) (debug ? (void)printf(__VA_ARGS__) : (void)0)
+static int debug;
+#else
+#define DPRINTF(...)
 #endif
 
-       (void)fprintf(stderr,
-           "Usage: %s [-broadcast] [-insecure] [-ypset] [-ypsetme]%s\n",
-           getprogname(), opt);
-       exit(1);
-}
-
 static void
 yp_log(int pri, const char *fmt, ...)
 {
@@ -179,6 +173,32 @@
        va_end(ap);
 }
 
+////////////////////////////////////////////////////////////
+// struct _dom_binding
+
+static struct _dom_binding *
+xid2ypdb(uint32_t xid)
+{
+       struct _dom_binding *ypdb;
+
+       for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
+               if (ypdb->dom_xid == xid)
+                       break;
+       return (ypdb);
+}
+
+static uint32_t
+unique_xid(struct _dom_binding *ypdb)
+{
+       uint32_t tmp_xid;
+
+       tmp_xid = ((uint32_t)(unsigned long)ypdb) & 0xffffffff;
+       while (xid2ypdb(tmp_xid) != NULL)
+               tmp_xid++;
+
+       return tmp_xid;
+}
+
 static struct _dom_binding *
 makebinding(const char *dm)
 {
@@ -194,6 +214,9 @@
        return ypdb;
 }
 
+////////////////////////////////////////////////////////////
+// locks
+
 static int
 makelock(struct _dom_binding *ypdb)
 {
@@ -269,6 +292,97 @@
        return(0);
 }
 
+////////////////////////////////////////////////////////////
+// sunrpc twaddle
+
+/*
+ * LOOPBACK IS MORE IMPORTANT: PUT IN HACK
+ */
+void
+rpc_received(char *dom, struct sockaddr_in *raddrp, int force)
+{
+       struct _dom_binding *ypdb;
+       struct iovec iov[2];
+       struct ypbind_resp ybr;
+       int fd;
+
+       DPRINTF("returned from %s about %s\n",
+               inet_ntoa(raddrp->sin_addr), dom);
+
+       if (dom == NULL)
+               return;
+
+       if (_yp_invalid_domain(dom))
+               return; 
+
+               /* don't support insecure servers by default */
+       if (!insecure && ntohs(raddrp->sin_port) >= IPPORT_RESERVED)
+               return;
+
+       for (ypdb = ypbindlist; ypdb; ypdb = ypdb->dom_pnext)
+               if (!strcmp(ypdb->dom_domain, dom))
+                       break;
+
+       if (ypdb == NULL) {
+               if (force == 0)
+                       return;
+               ypdb = makebinding(dom);
+               ypdb->dom_lockfd = -1;
+               ypdb->dom_pnext = ypbindlist;
+               ypbindlist = ypdb;
+       }
+
+       /* soft update, alive */
+       if (ypdb->dom_alive == 1 && force == 0) {
+               if (!memcmp(&ypdb->dom_server_addr, raddrp,
+                           sizeof ypdb->dom_server_addr)) {
+                       ypdb->dom_alive = 1;
+                       /* recheck binding in 60 sec */
+                       ypdb->dom_checktime = time(NULL) + 60;
+               }
+               return;
+       }
+       
+       (void)memcpy(&ypdb->dom_server_addr, raddrp,
+           sizeof ypdb->dom_server_addr);
+       /* recheck binding in 60 seconds */
+       ypdb->dom_checktime = time(NULL) + 60;
+       ypdb->dom_vers = YPVERS;
+       ypdb->dom_alive = 1;
+
+       if (ypdb->dom_lockfd != -1)
+               (void)close(ypdb->dom_lockfd);
+
+       if ((fd = makelock(ypdb)) == -1)
+               return;
+
+       /*
+        * ok, if BINDINGDIR exists, and we can create the binding file,
+        * then write to it..
+        */
+       ypdb->dom_lockfd = fd;
+
+       iov[0].iov_base = &(udptransp->xp_port);
+       iov[0].iov_len = sizeof udptransp->xp_port;
+       iov[1].iov_base = &ybr;
+       iov[1].iov_len = sizeof ybr;
+
+       (void)memset(&ybr, 0, sizeof ybr);
+       ybr.ypbind_status = YPBIND_SUCC_VAL;
+       ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr =
+           raddrp->sin_addr;
+       ybr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_port =
+           raddrp->sin_port;
+
+       if ((size_t)writev(ypdb->dom_lockfd, iov, 2) !=
+           iov[0].iov_len + iov[1].iov_len) {
+               yp_log(LOG_WARNING, "writev: %m");
+               (void)close(ypdb->dom_lockfd);
+               removelock(ypdb);
+               ypdb->dom_lockfd = -1;
+       }
+}
+
 static void *
 /*ARGSUSED*/
 ypbindproc_null_2(SVCXPRT *transp, void *argp)
@@ -467,348 +581,8 @@
        return;
 }
 
-int
-main(int argc, char *argv[])
-{
-       struct timeval tv;
-       fd_set fdsr;
-       int width, lockfd;
-       int evil = 0, one;
-       char pathname[MAXPATHLEN];
-       struct stat st;
-
-       setprogname(argv[0]);
-       (void)yp_get_default_domain(&domainname);
-       if (domainname[0] == '\0')
-               errx(1, "Domainname not set. Aborting.");
-
-       /*
-        * Per traditional ypbind(8) semantics, if a ypservers
-        * file does not exist, we default to broadcast mode.
-        * If the file does exist, we default to direct mode.
-        * Note that we can still override direct mode by passing
-        * the -broadcast flag.
-        */
-       (void)snprintf(pathname, sizeof(pathname), "%s/%s%s", BINDINGDIR,
-           domainname, YPSERVERSSUFF);
-       if (stat(pathname, &st) < 0) {
-               DPRINTF("%s does not exist, defaulting to broadcast\n",
-                       pathname);
-               ypbindmode = YPBIND_BROADCAST;
-       } else
-               ypbindmode = YPBIND_DIRECT;
-
-       while (--argc) {
-               ++argv;
-               if (!strcmp("-insecure", *argv))
-                       insecure = 1;
-               else if (!strcmp("-ypset", *argv))
-                       ypbindmode = YPBIND_SETALL;
-               else if (!strcmp("-ypsetme", *argv))
-                       ypbindmode = YPBIND_SETLOCAL;
-               else if (!strcmp("-broadcast", *argv))
-                       ypbindmode = YPBIND_BROADCAST;



Home | Main Index | Thread Index | Old Index