Source-Changes-HG archive

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

[src/trunk]: src/sbin/setkey make it possible to process files.



details:   https://anonhg.NetBSD.org/src/rev/83e946bfca41
branches:  trunk
changeset: 551553:83e946bfca41
user:      itojun <itojun%NetBSD.org@localhost>
date:      Mon Sep 08 06:18:49 2003 +0000

description:
make it possible to process files.

diffstat:

 sbin/setkey/setkey.8 |  10 ++++-
 sbin/setkey/setkey.c |  89 ++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 84 insertions(+), 15 deletions(-)

diffs (214 lines):

diff -r 87cb5ebe483a -r 83e946bfca41 sbin/setkey/setkey.8
--- a/sbin/setkey/setkey.8      Mon Sep 08 05:34:35 2003 +0000
+++ b/sbin/setkey/setkey.8      Mon Sep 08 06:18:49 2003 +0000
@@ -1,5 +1,5 @@
-.\"    $NetBSD: setkey.8,v 1.23 2003/09/07 22:21:01 itojun Exp $
-.\"    $KAME: setkey.8,v 1.87 2003/09/07 22:16:01 itojun Exp $
+.\"    $NetBSD: setkey.8,v 1.24 2003/09/08 06:18:49 itojun Exp $
+.\"    $KAME: setkey.8,v 1.90 2003/09/08 06:10:43 itojun Exp $
 .\"
 .\" Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
 .\" All rights reserved.
@@ -39,6 +39,9 @@
 .Sh SYNOPSIS
 .Nm setkey
 .Op Fl v
+.Ar file...
+.Nm setkey
+.Op Fl v
 .Fl c
 .Nm setkey
 .Op Fl v
@@ -72,6 +75,9 @@
 .Fl f Ar filename
 .Pc .
 .Bl -tag -width Ds
+.It (no flag)
+Dump the SAD entries or SPD entries contained in the specified
+.Ar file .
 .It Fl D
 Dump the SAD entries.
 If with
diff -r 87cb5ebe483a -r 83e946bfca41 sbin/setkey/setkey.c
--- a/sbin/setkey/setkey.c      Mon Sep 08 05:34:35 2003 +0000
+++ b/sbin/setkey/setkey.c      Mon Sep 08 06:18:49 2003 +0000
@@ -1,5 +1,5 @@
-/*     $NetBSD: setkey.c,v 1.7 2003/07/01 07:20:14 itojun Exp $        */
-/*     $KAME: setkey.c,v 1.28 2003/06/27 07:15:45 itojun Exp $ */
+/*     $NetBSD: setkey.c,v 1.8 2003/09/08 06:18:49 itojun Exp $        */
+/*     $KAME: setkey.c,v 1.29 2003/09/08 06:08:52 itojun Exp $ */
 
 /*
  * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
@@ -50,6 +50,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <netdb.h>
+#include <fcntl.h>
 
 #include "libpfkey.h"
 
@@ -60,6 +61,7 @@
 void promisc __P((void));
 int sendkeymsg __P((char *, size_t));
 int postproc __P((struct sadb_msg *, int));
+int fileproc __P((const char *));
 const char *numstr __P((int));
 void shortdump_hdr __P((void));
 void shortdump __P((struct sadb_msg *));
@@ -100,21 +102,21 @@
 }
 
 int
-main(ac, av)
-       int ac;
-       char **av;
+main(argc, argv)
+       int argc;
+       char **argv;
 {
        FILE *fp = stdin;
        int c;
 
-       if (ac == 1) {
+       if (argc == 1) {
                usage();
                /* NOTREACHED */
        }
 
        thiszone = gmt2local(0);
 
-       while ((c = getopt(ac, av, "acdf:hlvxDFP")) != -1) {
+       while ((c = getopt(argc, argv, "acdf:hlvxDFP")) != -1) {
                switch (c) {
                case 'c':
                        f_mode = MODE_SCRIPT;
@@ -123,7 +125,7 @@
                case 'f':
                        f_mode = MODE_SCRIPT;
                        if ((fp = fopen(optarg, "r")) == NULL) {
-                               err(-1, "fopen");
+                               err(1, "fopen");
                                /*NOTREACHED*/
                        }
                        break;
@@ -158,6 +160,18 @@
                }
        }
 
+       argc -= optind;
+       argv += optind;
+
+       if (argc > 0) {
+               while (argc--)
+                       if (fileproc(*argv++) < 0) {
+                               err(1, "%s", argv[-1]);
+                               /*NOTREACHED*/
+                       }
+               exit(0);
+       }
+
        so = pfkey_open();
        if (so < 0) {
                perror("pfkey_open");
@@ -173,7 +187,7 @@
                break;
        case MODE_SCRIPT:
                if (get_supported() < 0) {
-                       errx(-1, "%s", ipsec_strerror());
+                       errx(1, "%s", ipsec_strerror());
                        /*NOTREACHED*/
                }
                if (parse(&fp))
@@ -200,7 +214,7 @@
        if (pfkey_recv_register(so) < 0)
                return -1;
 
-       return 0;
+       return (0);
 }
 
 void
@@ -360,7 +374,7 @@
        }
 
 end:
-       return(0);
+       return (0);
 }
 
 int
@@ -398,7 +412,7 @@
                        errmsg = strerror(msg->sadb_msg_errno);
                }
                printf("%s%s.\n", inf, errmsg);
-               return(-1);
+               return (-1);
        }
 
        switch (msg->sadb_msg_type) {
@@ -430,6 +444,10 @@
                }
                break;
 
+       case SADB_X_SPDGET:
+               pfkey_spdump(msg);
+               break;
+
        case SADB_X_SPDDUMP:
                pfkey_spdump(msg);
                if (msg->sadb_msg_seq == 0) break;
@@ -442,7 +460,52 @@
                break;
        }
 
-       return(0);
+       return (0);
+}
+
+int
+fileproc(filename)
+       const char *filename;
+{
+       int fd;
+       ssize_t len, l;
+       u_char *p, *ep;
+       struct sadb_msg *msg;
+       u_char rbuf[1024 * 32]; /* XXX: Enough ? Should I do MSG_PEEK ? */
+
+       fd = open(filename, O_RDONLY);
+       if (fd < 0)
+               return -1;
+
+       l = 0;
+       while (1) {
+               len = read(fd, rbuf + l, sizeof(rbuf) - l);
+               if (len < 0) {
+                       close(fd);
+                       return -1;
+               } else if (len == 0)
+                       break;
+               l += len;
+       }
+
+       if (l < sizeof(struct sadb_msg)) {
+               close(fd);
+               errno = EINVAL;
+               return -1;
+       }
+       close(fd);
+
+       p = rbuf;
+       ep = rbuf + l;
+
+       while (p < ep) {
+               msg = (struct sadb_msg *)p;
+               len = PFKEY_UNUNIT64(msg->sadb_msg_len);
+               postproc(msg, len);
+               p += len;
+       }
+
+       return (0);
 }
 
 /*------------------------------------------------------------*/



Home | Main Index | Thread Index | Old Index