Re: [PATCH] smsc: set SMSC_MII_BUSY to read/write PHY regs

2018-07-04 Thread Kevin Lo
On Wed, Jun 27, 2018 at 04:42:22PM +0800, Kevin Lo wrote:
> 
> Hi,
> 
> The diff below sets SMSC_MII_BUSY bit, which is necessary for PHY to 
> acknowledge a read/write request; from FreeBSD r333096.
> While here, mask off the PHY address and register.
> 
> Tested on rpi3.
> 
> # dmesg |grep smsc
> smsc0 at uhub1 port 1 configuration 1 interface 0 "Standard Microsystems 
> SMSC9512/14" rev 2.00/2.00 addr 3
> smsc0: address b8:27:eb:b0:32:5d
> ukphy0 at smsc0 phy 1: Generic IEEE 802.3u media interface, rev. 3: OUI 
> 0x0001f0, model 0x000c
> 

Anyone?  It also matches Linux commit 80928805babfd97b6f1721dd942a55dd2e7813ea.

> 
> Index: sys/dev/usb/if_smsc.c
> ===
> RCS file: /cvs/src/sys/dev/usb/if_smsc.c,v
> retrieving revision 1.31
> diff -u -p -u -p -r1.31 if_smsc.c
> --- sys/dev/usb/if_smsc.c 29 Jul 2017 17:24:04 -  1.31
> +++ sys/dev/usb/if_smsc.c 27 Jun 2018 08:37:34 -
> @@ -283,7 +283,6 @@ int
>  smsc_miibus_readreg(struct device *dev, int phy, int reg)
>  {
>   struct smsc_softc *sc = (struct smsc_softc *)dev;
> - uint32_t addr;
>   uint32_t val = 0;
>  
>   smsc_lock_mii(sc);
> @@ -292,8 +291,8 @@ smsc_miibus_readreg(struct device *dev, 
>   goto done;
>   }
>  
> - addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
> - smsc_write_reg(sc, SMSC_MII_ADDR, addr);
> + smsc_write_reg(sc, SMSC_MII_ADDR, SMSC_MII_READ | SMSC_MII_BUSY |
> + SMSC_MII_PHY_ADDR(phy) | SMSC_MII_REG_ADDR(reg));
>  
>   if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
>   smsc_warn_printf(sc, "MII read timeout\n");
> @@ -309,7 +308,6 @@ void
>  smsc_miibus_writereg(struct device *dev, int phy, int reg, int val)
>  {
>   struct smsc_softc *sc = (struct smsc_softc *)dev;
> - uint32_t addr;
>  
>   if (sc->sc_phyno != phy)
>   return;
> @@ -323,8 +321,9 @@ smsc_miibus_writereg(struct device *dev,
>  
>   smsc_write_reg(sc, SMSC_MII_DATA, val);
>  
> - addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
> - smsc_write_reg(sc, SMSC_MII_ADDR, addr);
> + smsc_write_reg(sc, SMSC_MII_ADDR, SMSC_MII_WRITE | SMSC_MII_BUSY |
> + SMSC_MII_PHY_ADDR(phy) | SMSC_MII_REG_ADDR(reg));
> +
>   smsc_unlock_mii(sc);
>  
>   if (smsc_wait_for_bits(sc, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0)
> Index: sys/dev/usb/if_smscreg.h
> ===
> RCS file: /cvs/src/sys/dev/usb/if_smscreg.h,v
> retrieving revision 1.5
> diff -u -p -u -p -r1.5 if_smscreg.h
> --- sys/dev/usb/if_smscreg.h  18 Jun 2015 09:28:54 -  1.5
> +++ sys/dev/usb/if_smscreg.h  27 Jun 2018 08:37:35 -
> @@ -215,9 +215,17 @@
>  #define SMSC_INTR_GPIOS 0x07FFUL
>  
>  /* Phy MII interface register */
> +#define SMSC_MII_PHY_ADDR_MASK  0xF800
> +#define SMSC_MII_PHY_ADDR_SHIFT 11
> +#define SMSC_MII_REG_ADDR_MASK  0x07C0
> +#define SMSC_MII_REG_ADDR_SHIFT 6
>  #define SMSC_MII_WRITE  (0x1UL << 1)
>  #define SMSC_MII_READ   (0x0UL << 1)
>  #define SMSC_MII_BUSY   (0x1UL << 0)
> +#define SMSC_MII_REG_ADDR(x)\
> +(((x) << SMSC_MII_REG_ADDR_SHIFT ) & SMSC_MII_REG_ADDR_MASK)
> +#define SMSC_MII_PHY_ADDR(x) \
> +(((x) << SMSC_MII_PHY_ADDR_SHIFT) & SMSC_MII_PHY_ADDR_MASK)
>  
>  /* H/W checksum register */
>  #define SMSC_COE_CTRL_TX_EN (0x1UL << 16)  /* Tx H/W csum enable */
> 
> 



Re: avoid vfprintf NULL errors in ldape.c log_debug()

2018-07-04 Thread Theo de Raadt
Rob Pierce  wrote:

> Running the current ldapd regression tests result in the following (repeated)
> errors in my /var/log/messages:
> 
> ... ldapd: vfprintf %s NULL in "current bind dn = %s "
> 
> This is because regress/usr.sbin/ldapd/run-tests.pl is performing
> unnecessary unbinds in END { }.
> 
> Though the regression test should probably be fixed, the following diff
> ensures that log_debug is not called with a NULL argument.
> 
> Does this make sense?
> 
> Index: ldape.c
> ===
> RCS file: /cvs/src/usr.sbin/ldapd/ldape.c,v
> retrieving revision 1.27
> diff -u -p -r1.27 ldape.c
> --- ldape.c   15 May 2018 11:19:21 -  1.27
> +++ ldape.c   3 Jul 2018 23:32:27 -
> @@ -229,7 +229,8 @@ ldap_abandon(struct request *req)
>  int
>  ldap_unbind(struct request *req)
>  {
> - log_debug("current bind dn = %s", req->conn->binddn);
> + log_debug("current bind dn = %s",
> + req->conn->binddn == NULL ? "" : req->conn->binddn);
>   conn_disconnect(req->conn);
>   request_free(req);
>   return -1;  /* don't send any response */
> 

I'd suggest:

if (req->conn->binddn)
log_debug("current bind dn = %s", req->conn->binddn);



Re: raw ip mbuf leak

2018-07-04 Thread Sebastian Benoit
Alexander Bluhm(alexander.bl...@gmx.net) on 2018.07.04 22:49:01 +0200:
> Hi,
> 
> It was possible to leak the control mbuf in raw ip user request
> with sendmsg(2) and MSG_OOB.  Sync the code in udp, rip, and
> rip6_usrreq.  Add an inp NULL check in rip6_usrreq for consistency.
> 
> ok?

ok
 
> bluhm
> 
> Index: netinet/raw_ip.c
> ===
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/raw_ip.c,v
> retrieving revision 1.110
> diff -u -p -r1.110 raw_ip.c
> --- netinet/raw_ip.c  4 Jul 2018 02:08:13 -   1.110
> +++ netinet/raw_ip.c  4 Jul 2018 20:12:28 -
> @@ -365,7 +365,7 @@ int
>  rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
>  struct mbuf *control, struct proc *p)
>  {
> - struct inpcb *inp = sotoinpcb(so);
> + struct inpcb *inp;
>   int error = 0;
>  
>   if (req == PRU_CONTROL)
> @@ -374,6 +374,7 @@ rip_usrreq(struct socket *so, int req, s
>  
>   soassertlocked(so);
>  
> + inp = sotoinpcb(so);
>   if (inp == NULL) {
>   error = EINVAL;
>   goto release;
> @@ -504,6 +505,7 @@ rip_usrreq(struct socket *so, int req, s
>   panic("rip_usrreq");
>   }
>  release:
> + m_freem(control);
>   m_freem(m);
>   return (error);
>  }
> Index: netinet/udp_usrreq.c
> ===
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/udp_usrreq.c,v
> retrieving revision 1.249
> diff -u -p -r1.249 udp_usrreq.c
> --- netinet/udp_usrreq.c  8 Jun 2018 14:09:57 -   1.249
> +++ netinet/udp_usrreq.c  4 Jul 2018 20:09:46 -
> @@ -1203,7 +1203,6 @@ udp_usrreq(struct socket *so, int req, s
>   default:
>   panic("udp_usrreq");
>   }
> -
>  release:
>   m_freem(control);
>   m_freem(m);
> Index: netinet6/raw_ip6.c
> ===
> RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/raw_ip6.c,v
> retrieving revision 1.128
> diff -u -p -r1.128 raw_ip6.c
> --- netinet6/raw_ip6.c4 Jul 2018 02:08:13 -   1.128
> +++ netinet6/raw_ip6.c4 Jul 2018 20:40:05 -
> @@ -544,7 +544,7 @@ int
>  rip6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
>   struct mbuf *control, struct proc *p)
>  {
> - struct inpcb *in6p = sotoinpcb(so);
> + struct inpcb *in6p;
>   int error = 0;
>  
>   if (req == PRU_CONTROL)
> @@ -553,6 +553,12 @@ rip6_usrreq(struct socket *so, int req, 
>  
>   soassertlocked(so);
>  
> + in6p = sotoinpcb(so);
> + if (in6p == NULL) {
> + error = EINVAL;
> + goto release;
> + }
> +
>   switch (req) {
>   case PRU_DISCONNECT:
>   if ((so->so_state & SS_ISCONNECTED) == 0) {
> @@ -654,6 +660,7 @@ rip6_usrreq(struct socket *so, int req, 
>   dst.sin6_scope_id = addr6->sin6_scope_id;
>   }
>   error = rip6_output(m, so, sin6tosa(&dst), control);
> + control = NULL;
>   m = NULL;
>   break;
>   }
> @@ -687,6 +694,8 @@ rip6_usrreq(struct socket *so, int req, 
>   default:
>   panic("rip6_usrreq");
>   }
> +release:
> + m_freem(control);
>   m_freem(m);
>   return (error);
>  }
> 



Re: Add support for the VIA VX900 chipset in viapm(4)

2018-07-04 Thread Mark Kettenis
> Date: Wed, 4 Jul 2018 23:41:22 +0200
> From: Frederic Cambus 
> 
> Hi tech@,
> 
> Here is a diff to add support for the VIA VX900 chipset in viapm(4).
> 
> Comments? OK?

ok kettenis@

> Index: share/man/man4/viapm.4
> ===
> RCS file: /cvs/src/share/man/man4/viapm.4,v
> retrieving revision 1.11
> diff -u -p -r1.11 viapm.4
> --- share/man/man4/viapm.416 Jul 2013 16:05:49 -  1.11
> +++ share/man/man4/viapm.43 Jul 2018 22:11:37 -
> @@ -28,8 +28,8 @@ The
>  .Nm
>  driver provides support for the bus control and power management
>  component of the VIA VT82C596, VT82C596B, VT82C686A, VT8231, VT8233,
> -VT8233A, VT8235, VT8237, VT8237A, VT8237S, VT8251, CX700, VX800 and
> -VX855 South Bridges.
> +VT8233A, VT8235, VT8237, VT8237A, VT8237S, VT8251, CX700, VX800,
> +VX855 and VX900 South Bridges.
>  Only the SMBus host interface is supported and can be used with the
>  .Xr iic 4
>  framework.
> Index: sys/dev/pci/viapm.c
> ===
> RCS file: /cvs/src/sys/dev/pci/viapm.c,v
> retrieving revision 1.16
> diff -u -p -r1.16 viapm.c
> --- sys/dev/pci/viapm.c   5 Oct 2012 10:51:28 -   1.16
> +++ sys/dev/pci/viapm.c   3 Jul 2018 22:11:37 -
> @@ -53,7 +53,7 @@
>  /*
>   * Driver for the SMBus controller and power management timer
>   * in the VIA VT82C596[B], VT82C686A, VT8231, VT8233[A], VT8235, VT8237[A,S],
> - * VT8251, CX700, VX800 and VX855 South Bridges.
> + * VT8251, CX700, VX800, VX855 and VX900 South Bridges.
>   * Also for the hardware monitoring part of the VIA VT82C686A and VT8231.
>   */
>  
> @@ -246,7 +246,8 @@ const struct pci_matchid viapm_ids[] = {
>   { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8251_ISA },
>   { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_CX700_ISA },
>   { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX800_ISA },
> - { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX855_ISA }
> + { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX855_ISA },
> + { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX900_ISA }
>  };
>  
>  /*
> 
> 



priofilter and rtm message types

2018-07-04 Thread Sebastian Benoit
Remi noticed that ospfd does no longer see new interfaces.

The priority filter should only work on routing messages that
have a rtm_priority.

So these are out:

RTM_DELADDR
RTM_NEWADDR (struct ifa_msghdr)
RTM_IFINFO (struct if_msghdr)
RTM_IFANNOUNCE (struct if_announcemsghdr)
RTM_BFD (struct bfd_msghdr)

and all others (struct rt_msghdr) are ok and handled in the default case
further down.

Move the rop_priority check there.

ok?

diff --git sys/net/rtsock.c sys/net/rtsock.c
index afab5d72505..4d3442d0924 100644
--- sys/net/rtsock.c
+++ sys/net/rtsock.c
@@ -465,9 +465,6 @@ next:
if (rtm->rtm_type != RTM_DESYNC && rop->rop_msgfilter != 0 &&
!(rop->rop_msgfilter & (1 << rtm->rtm_type)))
goto next;
-   if (rop->rop_priority != 0 &&
-   rop->rop_priority < rtm->rtm_priority)
-   goto next;
switch (rtm->rtm_type) {
case RTM_IFANNOUNCE:
case RTM_DESYNC:
@@ -483,6 +480,9 @@ next:
goto next;
break;
default:
+   if (rop->rop_priority != 0 &&
+   rop->rop_priority < rtm->rtm_priority)
+   goto next;
/* check against rtable id */
if (rop->rop_rtableid != RTABLE_ANY &&
rop->rop_rtableid != rtm->rtm_tableid)



Add support for the VIA VX900 chipset in viapm(4)

2018-07-04 Thread Frederic Cambus
Hi tech@,

Here is a diff to add support for the VIA VX900 chipset in viapm(4).

Comments? OK?

Index: share/man/man4/viapm.4
===
RCS file: /cvs/src/share/man/man4/viapm.4,v
retrieving revision 1.11
diff -u -p -r1.11 viapm.4
--- share/man/man4/viapm.4  16 Jul 2013 16:05:49 -  1.11
+++ share/man/man4/viapm.4  3 Jul 2018 22:11:37 -
@@ -28,8 +28,8 @@ The
 .Nm
 driver provides support for the bus control and power management
 component of the VIA VT82C596, VT82C596B, VT82C686A, VT8231, VT8233,
-VT8233A, VT8235, VT8237, VT8237A, VT8237S, VT8251, CX700, VX800 and
-VX855 South Bridges.
+VT8233A, VT8235, VT8237, VT8237A, VT8237S, VT8251, CX700, VX800,
+VX855 and VX900 South Bridges.
 Only the SMBus host interface is supported and can be used with the
 .Xr iic 4
 framework.
Index: sys/dev/pci/viapm.c
===
RCS file: /cvs/src/sys/dev/pci/viapm.c,v
retrieving revision 1.16
diff -u -p -r1.16 viapm.c
--- sys/dev/pci/viapm.c 5 Oct 2012 10:51:28 -   1.16
+++ sys/dev/pci/viapm.c 3 Jul 2018 22:11:37 -
@@ -53,7 +53,7 @@
 /*
  * Driver for the SMBus controller and power management timer
  * in the VIA VT82C596[B], VT82C686A, VT8231, VT8233[A], VT8235, VT8237[A,S],
- * VT8251, CX700, VX800 and VX855 South Bridges.
+ * VT8251, CX700, VX800, VX855 and VX900 South Bridges.
  * Also for the hardware monitoring part of the VIA VT82C686A and VT8231.
  */
 
@@ -246,7 +246,8 @@ const struct pci_matchid viapm_ids[] = {
{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT8251_ISA },
{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_CX700_ISA },
{ PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX800_ISA },
-   { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX855_ISA }
+   { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX855_ISA },
+   { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX900_ISA }
 };
 
 /*



Re: Unplugging USB network stick during tcpdump crashes system

2018-07-04 Thread Alexander Bluhm
On Wed, Jul 04, 2018 at 08:18:53PM +, Moritz Buhl wrote:
> Removing my usb network interface during `tcpdump -i rum0` caused my
> kernel to crash. A fix is attached.

This error comes from the usbd_is_dying() check in the device ioctl.
Some drivers return EIO in this case.  I think ENXIO is better, so
let's add one special case here and make the drivers consistently
use ENXIO in a next step.

OK bluhm@

> Index: net/bpf.c
> ===
> RCS file: /cvs/src/sys/net/bpf.c,v
> retrieving revision 1.169
> diff -u -p -r1.169 bpf.c
> --- net/bpf.c 2 Mar 2018 16:57:41 -   1.169
> +++ net/bpf.c 4 Jul 2018 19:42:35 -
> @@ -332,7 +332,8 @@ bpf_detachd(struct bpf_d *d)
>   mtx_enter(&d->bd_mtx);
>   bpf_put(d);
>  
> - if (error && !(error == EINVAL || error == ENODEV))
> + if (error && !(error == EINVAL || error == ENODEV ||
> + error == ENXIO))
>   /*
>* Something is really wrong if we were able to put
>* the driver into promiscuous mode, but can't



raw ip mbuf leak

2018-07-04 Thread Alexander Bluhm
Hi,

It was possible to leak the control mbuf in raw ip user request
with sendmsg(2) and MSG_OOB.  Sync the code in udp, rip, and
rip6_usrreq.  Add an inp NULL check in rip6_usrreq for consistency.

ok?

bluhm

Index: netinet/raw_ip.c
===
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/raw_ip.c,v
retrieving revision 1.110
diff -u -p -r1.110 raw_ip.c
--- netinet/raw_ip.c4 Jul 2018 02:08:13 -   1.110
+++ netinet/raw_ip.c4 Jul 2018 20:12:28 -
@@ -365,7 +365,7 @@ int
 rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
 struct mbuf *control, struct proc *p)
 {
-   struct inpcb *inp = sotoinpcb(so);
+   struct inpcb *inp;
int error = 0;
 
if (req == PRU_CONTROL)
@@ -374,6 +374,7 @@ rip_usrreq(struct socket *so, int req, s
 
soassertlocked(so);
 
+   inp = sotoinpcb(so);
if (inp == NULL) {
error = EINVAL;
goto release;
@@ -504,6 +505,7 @@ rip_usrreq(struct socket *so, int req, s
panic("rip_usrreq");
}
 release:
+   m_freem(control);
m_freem(m);
return (error);
 }
Index: netinet/udp_usrreq.c
===
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/udp_usrreq.c,v
retrieving revision 1.249
diff -u -p -r1.249 udp_usrreq.c
--- netinet/udp_usrreq.c8 Jun 2018 14:09:57 -   1.249
+++ netinet/udp_usrreq.c4 Jul 2018 20:09:46 -
@@ -1203,7 +1203,6 @@ udp_usrreq(struct socket *so, int req, s
default:
panic("udp_usrreq");
}
-
 release:
m_freem(control);
m_freem(m);
Index: netinet6/raw_ip6.c
===
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/raw_ip6.c,v
retrieving revision 1.128
diff -u -p -r1.128 raw_ip6.c
--- netinet6/raw_ip6.c  4 Jul 2018 02:08:13 -   1.128
+++ netinet6/raw_ip6.c  4 Jul 2018 20:40:05 -
@@ -544,7 +544,7 @@ int
 rip6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
struct mbuf *control, struct proc *p)
 {
-   struct inpcb *in6p = sotoinpcb(so);
+   struct inpcb *in6p;
int error = 0;
 
if (req == PRU_CONTROL)
@@ -553,6 +553,12 @@ rip6_usrreq(struct socket *so, int req, 
 
soassertlocked(so);
 
+   in6p = sotoinpcb(so);
+   if (in6p == NULL) {
+   error = EINVAL;
+   goto release;
+   }
+
switch (req) {
case PRU_DISCONNECT:
if ((so->so_state & SS_ISCONNECTED) == 0) {
@@ -654,6 +660,7 @@ rip6_usrreq(struct socket *so, int req, 
dst.sin6_scope_id = addr6->sin6_scope_id;
}
error = rip6_output(m, so, sin6tosa(&dst), control);
+   control = NULL;
m = NULL;
break;
}
@@ -687,6 +694,8 @@ rip6_usrreq(struct socket *so, int req, 
default:
panic("rip6_usrreq");
}
+release:
+   m_freem(control);
m_freem(m);
return (error);
 }



Re: sparc64: enable trunk(4) in RAMDISK

2018-07-04 Thread Mark Kettenis
> Date: Wed, 4 Jul 2018 20:10:45 +0200
> From: Klemens Nanni 
> 
> I currently have to prefetch sets to disk and upgrade offline since my
> uplink is configured via LACP.
> 
> This diff enables trunk interfaces in the ramdisk: I have successfully
> built a bsd.rd with it and upgraded via network as usual on my T5240.
> 
> Feedback? OK?

ok kettenis@

> Index: RAMDISK
> ===
> RCS file: /cvs/src/sys/arch/sparc64/conf/RAMDISK,v
> retrieving revision 1.121
> diff -u -p -r1.121 RAMDISK
> --- RAMDISK   4 Jul 2016 21:51:40 -   1.121
> +++ RAMDISK   4 Jul 2018 18:00:27 -
> @@ -295,4 +295,5 @@ pseudo-device rd 1
>  pseudo-deviceloop 1
>  pseudo-devicebpfilter 1
>  pseudo-devicevlan
> +pseudo-devicetrunk
>  pseudo-devicebio 1
> 
> 



Unplugging USB network stick during tcpdump crashes system

2018-07-04 Thread Moritz Buhl
Hello,

Removing my usb network interface during `tcpdump -i rum0` caused my
kernel to crash. A fix is attached.

Thanks,
Moritz Buhl

Index: net/bpf.c
===
RCS file: /cvs/src/sys/net/bpf.c,v
retrieving revision 1.169
diff -u -p -r1.169 bpf.c
--- net/bpf.c   2 Mar 2018 16:57:41 -   1.169
+++ net/bpf.c   4 Jul 2018 19:42:35 -
@@ -332,7 +332,8 @@ bpf_detachd(struct bpf_d *d)
mtx_enter(&d->bd_mtx);
bpf_put(d);
 
-   if (error && !(error == EINVAL || error == ENODEV))
+   if (error && !(error == EINVAL || error == ENODEV ||
+   error == ENXIO))
/*
 * Something is really wrong if we were able to put
 * the driver into promiscuous mode, but can't



sparc64: enable trunk(4) in RAMDISK

2018-07-04 Thread Klemens Nanni
I currently have to prefetch sets to disk and upgrade offline since my
uplink is configured via LACP.

This diff enables trunk interfaces in the ramdisk: I have successfully
built a bsd.rd with it and upgraded via network as usual on my T5240.

Feedback? OK?

Index: RAMDISK
===
RCS file: /cvs/src/sys/arch/sparc64/conf/RAMDISK,v
retrieving revision 1.121
diff -u -p -r1.121 RAMDISK
--- RAMDISK 4 Jul 2016 21:51:40 -   1.121
+++ RAMDISK 4 Jul 2018 18:00:27 -
@@ -295,4 +295,5 @@ pseudo-device   rd 1
 pseudo-device  loop 1
 pseudo-device  bpfilter 1
 pseudo-device  vlan
+pseudo-device  trunk
 pseudo-device  bio 1



[PATCH] fakertc: A Real time clock simulator

2018-07-04 Thread Aaron Lancaster
I wrote this patch/script to deal with a very irritating case of
circular dependency failure I recently encountered on an APU2 system.

On systems with non-existent or dead battery real time clocks, with DNS
over TLS being used, if your clock gets reset to a much earlier date
due to power off, DNS over TLS will cease to function due to certs not
being valid yet, thus DNS will fail entirely, meaning that pool.ntp.org
will fail to be looked up, and ntpd(8) can not fix the date. Thus
you arrive at a situation where DNS is broken because time is broken
because DNS is broken that is not fixable without manual intervention.

Similar situations can arise with DNSSEC, ntpd.conf(4) constraint
settings, as well as stuff like 802.1X authentication (although I have not
run into that specific case).

My solution is to sync the current system time to a file every 30
minutes, as well as on shutdown, and restore from it on boot if the
boot system time fails with some basic sanity checks. It is not a
perfect solution but there can't be a perfect solution as far as I
can see for this specific class of brokenness, and demanding users
bolt on an RTC to an embedded system so networking isn't fundamentally
broken seems like pretty extreme overkill. In any case this solution
is better than taking the system back to January 1st 1970.

It seems pretty universally useful, as RTC batteries die all the time,
and it rendering your network connectivity totally broken except for
manual intervention seems pretty awful for something like a remote
system, so I have not provided any knobs for turning it on and off.

It would make sense to emit the current date into the
/var/db/fakertc.time file from the installer, as well as do a clock
sanity check from /etc/daily but I wasn't certain how to modify these
files in the correct manner so I left them untouched.

Looking for feedback, thanks for your time
Index: libexec/fakertc/fakertc
===
RCS file: libexec/fakertc/fakertc
diff -N libexec/fakertc/fakertc
--- /dev/null	1 Jan 1970 00:00:00 -
+++ libexec/fakertc/fakertc	4 Jul 2018 16:49:28 -
@@ -0,0 +1,109 @@
+#!/bin/ksh
+#
+# $OpenBSD: fakertc.sh,v 1.0 2018/07/03 22:14:32 aal Exp $
+#
+# Copyright (c) 2018 Aaron Lancaster 
+#
+# Permission to use, copy, modify, and distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+set +o nounset 
+
+#subroutines
+
+# check to see if timefile is newer than current system clock, if it is 
+# not it is likely indicative of a broken or nonexistent RTC, which will 
+# play all sorts of havok with braindead crap like TLS cert expiry, 
+# which may hinder network connectivity all together, thus make ntpd 
+# unable to update the time, this creating a circular dependancy that
+# becomes unresovable without manual intervention
+
+check_timefile() {
+	if [[ -s $FILE ]]; then 
+		if [[ $(date -u +%s) -le $(cat $FILE) ]]; then
+			return 3
+		fi
+	else
+		echo "fakertc: $FILE missing!"
+		exit 2
+	fi
+}		
+
+# Restore time to the value stored in the timefile if we pass some basic
+# sanity checks, its not as good as a real RTC, but its better than 
+# time traveling back to January 1st 1970.
+
+restore_time() {
+	check_timefile
+	if [[ $? -eq 3 ]]; then
+		date $(date -ur $(cat $FILE) "+%C%y%m%d%H%M.%S")
+		echo "fakertc: Broken RTC, restoring last known time"
+	fi
+}
+
+# Apply basic sanity checks to see if the current time and the time 
+# in the fakertc file (if it exists) is both newer than /bsd and newer  
+# than the creation of this file 
+sane_time() {
+	if [[ $FAILSAFE_TIME -ge $(date -u +%s) ]] || \
+	   [[ $KERNEL_TIME -ge $(date -u +%s) ]]; then
+
+		echo -n "fakertc: Your clock is extremely out of date, "
+echo "you may lack an RTC, and not" 
+echo -n "fakertc: have your NTPD_FLAGS containing -s in " 
+echo "/etc/rc.conf.local "
+		logger -p daemon.notice -t fakertc \
+			"Your clock is extremely out of date, check RTC"
+		exit 1
+	fi
+}	
+
+# save RTC time if if passes basic sanity checks for not being far in  
+# the past
+
+save_time() {
+	sane_time
+	date -u +%s > $FILE
+}
+
+export PATH=/bin:/sbin:/usr/bin:/usr/sbin
+
+FILE=/var/db/fakertc.time
+
+#No matter what it is impossible for time to be earlier
+#then when this file was created, or the mtime of the current

Re: [PATCH] column(1): -r to right justify

2018-07-04 Thread Sebastian Benoit
Job Snijders(j...@openbsd.org) on 2018.07.04 14:09:56 +0200:
> Dear all,
> 
> Following some back and forth on how disklabel output should be
> formatted, I proposed to Kenneth to extend the column(1) utility. All that
> was missing is the ability to right justify. I've longed for this
> feature for a while: I often use 'column -t' to prettify data coming
> from an awk pipeline.

funny, i use an awk script for that ;)

code reads ok.

as to -r:

Linux:
there is -R mentioned in http://man7.org/linux/man-pages/man1/column.1.html

-R, --table-right columns
Right align text in the specified columns.

altough i dont know since when thats supported, because i could notfind that
on any linux system i have access to and the util-linux change log does not
mention it.

They use -R because their -r is

   -r, --tree column
  Specify column to use tree-like output. Note that the circular
  dependencies and another anomalies in child and parent
  relation are silently ignored.

FreeBSD doesn't have a right align in column.

So i think, if a new feature and flag is acceptable, it should be -R

/Benno

> Example:
> 
> job@vurt ~$ netstat -r | column -t -r | tail -5 
> ff01::%iwm0/32  fe80::4708:d2be:9a Um 03  - 4 
> iwm0  
>  ff01::%lo0/32   localhost Um 01  32768 4 
>  lo0  
>  ff02::/16   localhost   UGRS 01  32768 8 
>  lo0  
> ff02::%iwm0/32  fe80::4708:d2be:9a Um 03  - 4 
> iwm0  
>  ff02::%lo0/32   localhost Um 01  32768 4 
>  lo0  
> 
> 
> Patch courtesy of Kenneth R Westerback. OK?
> 
> Index: column.1
> ===
> RCS file: /cvs/src/usr.bin/column/column.1,v
> retrieving revision 1.18
> diff -u -p -r1.18 column.1
> --- column.1  24 Oct 2016 13:53:05 -  1.18
> +++ column.1  4 Jul 2018 10:27:54 -
> @@ -40,6 +40,7 @@
>  .Nm column
>  .Op Fl tx
>  .Op Fl c Ar columns
> +.Op Fl r Op Ar list
>  .Op Fl s Ar sep
>  .Op Ar
>  .Sh DESCRIPTION
> @@ -66,6 +67,16 @@ The options are as follows:
>  Output is formatted for a display
>  .Ar columns
>  wide.
> +.It Fl r Op Ar list
> +Table mode will right justify the column contents for the
> +specified columns.
> +.Ar list
> +is a list of comma separated column numbers or column ranges.
> +Column numbers start at 1.
> +The list must not contain whitespace.
> +If no
> +.Ar list
> +is provided then all columns will be right justified.
>  .It Fl s Ar sep
>  Specify a set of characters to delimit columns for the
>  .Fl t
> Index: column.c
> ===
> RCS file: /cvs/src/usr.bin/column/column.c,v
> retrieving revision 1.26
> diff -u -p -r1.26 column.c
> --- column.c  22 Jun 2018 12:27:00 -  1.26
> +++ column.c  4 Jul 2018 10:28:00 -
> @@ -47,7 +47,8 @@
>  void  c_columnate(void);
>  void *ereallocarray(void *, size_t, size_t);
>  void  input(FILE *);
> -void  maketbl(void);
> +int   rightjustify(const char *, const int);
> +void  maketbl(const int, const char *);
>  void  print(void);
>  void  r_columnate(void);
>  __dead void usage(void);
> @@ -69,8 +70,8 @@ main(int argc, char *argv[])
>  {
>   struct winsize win;
>   FILE *fp;
> - int ch, tflag, xflag;
> - char *p;
> + int ch, rflag, tflag, xflag;
> + char *p, *rcols;
>   const char *errstr;
>  
>   setlocale(LC_CTYPE, "");
> @@ -87,14 +88,19 @@ main(int argc, char *argv[])
>   if (pledge("stdio rpath", NULL) == -1)
>   err(1, "pledge");
>  
> - tflag = xflag = 0;
> - while ((ch = getopt(argc, argv, "c:s:tx")) != -1) {
> + rcols = NULL;
> + rflag = 0; tflag = xflag = 0;
> + while ((ch = getopt(argc, argv, "c:r::s:tx")) != -1) {
>   switch(ch) {
>   case 'c':
>   termwidth = strtonum(optarg, 1, INT_MAX, &errstr);
>   if (errstr != NULL)
>   errx(1, "%s: %s", errstr, optarg);
>   break;
> + case 'r':
> + rflag = 1;
> + rcols = optarg;
> + break;
>   case 's':
>   if ((separator = reallocarray(NULL, strlen(optarg) + 1,
>   sizeof(*separator))) == NULL)
> @@ -139,7 +145,7 @@ main(int argc, char *argv[])
>   return eval;
>  
>   if (tflag)
> - maketbl();
> + maketbl(rflag, rcols);
>   else if (*maxwidths >= termwidth)
>   print();
>   else if (xflag)
> @@ -207,18 +213,69 @@ print(void)
>   puts(table[row]->content);
>  }
>  
> +int
> +rightjustify(const char *rcols, const int col)
> +{
> + const char *errstr;
> + char c, *num, *temp;
> + long long ch, rangestart;
> + unsigned int i;
> +
>

usbdevs & hub ports status

2018-07-04 Thread Martin Pieuchot
Diff below adds support for printing USB ports status.  It includes an
ABI change as we currently do not export port status/change to userland.

I'd really like to export the current cached value to userland via the
USB_DEVICEINFO ioctl(2) to reduce the numbers of I/O.

Note that this diff also fixes some link-status defines in usb.h

Since it includes an ABI break I cranked devel/libusb1.

  $ usbdevs -dvv 
  Controller /dev/usb0:
  addr 01: 8086: Intel, xHCI root hub
   super speed, self powered, config 1, rev 1.00
   driver: uhub0
   port 01: .02a0 power Rx.detect
   port 02: .02a0 power Rx.detect
   port 03: .02a0 power Rx.detect
   port 04: .0503 connect enabled recovery
   port 05: .02a0 power Rx.detect
...
  addr 02: 04f2:b45d Chicony Electronics Co.,Ltd., Integrated Camera
   high speed, power 500 mA, config 1, rev 0.29, iSerialNumber 0x0001
   driver: uvideo0

ok?

Index: src/usr.sbin/usbdevs/usbdevs.c
===
RCS file: /cvs/src/usr.sbin/usbdevs/usbdevs.c,v
retrieving revision 1.27
diff -u -p -r1.27 usbdevs.c
--- src/usr.sbin/usbdevs/usbdevs.c  3 Jul 2018 13:21:31 -   1.27
+++ src/usr.sbin/usbdevs/usbdevs.c  4 Jul 2018 14:46:08 -
@@ -54,7 +54,7 @@ int verbose = 0;
 int showdevs = 0;
 
 void usage(void);
-void usbdev(int f, uint8_t, int rec);
+void usbdev(int f, uint8_t);
 void usbdump(int f);
 void dumpone(char *name, int f, int addr);
 int main(int, char **);
@@ -69,13 +69,13 @@ usage(void)
 }
 
 char done[USB_MAX_DEVICES];
-int indent;
 
 void
-usbdev(int f, uint8_t addr, int rec)
+usbdev(int f, uint8_t addr)
 {
struct usb_device_info di;
-   int e, p, i, s, nports;
+   int e, i, port, nports;
+   uint16_t change, status;
 
di.udi_addr = addr;
e = ioctl(f, USB_DEVICEINFO, &di);
@@ -89,6 +89,7 @@ usbdev(int f, uint8_t addr, int rec)
done[addr] = 1;
printf("%04x:%04x %s, %s", di.udi_vendorNo, di.udi_productNo,
di.udi_vendor, di.udi_product);
+
if (verbose) {
printf("\n\t ");
switch (di.udi_speed) {
@@ -122,48 +123,96 @@ usbdev(int f, uint8_t addr, int rec)
printf(", iSerialNumber %s", di.udi_serial);
}
printf("\n");
+
if (showdevs) {
for (i = 0; i < USB_MAX_DEVNAMES; i++)
if (di.udi_devnames[i][0])
-   printf("%*s  %s\n", indent, "",
-   di.udi_devnames[i]);
+   printf("\t driver: %s\n", di.udi_devnames[i]);
}
-   if (!rec)
-   return;
 
-   nports = MINIMUM(di.udi_nports, nitems(di.udi_ports));
if (verbose > 1) {
-   for (p = 0; p < nports; p++) {
-   s = di.udi_ports[p];
-   printf("\t port %02u:", p+1);
-   if (s < USB_MAX_DEVICES)
-   printf(" addr %02u\n", s);
-   else {
-   printf(" %s\n",
-   s == USB_PORT_ENABLED ? "enabled" :
-   s == USB_PORT_SUSPENDED ? "suspended" :
-   s == USB_PORT_POWERED ? "powered" :
-   s == USB_PORT_DISABLED ? "disabled" :
-   "???");
+   nports = MINIMUM(di.udi_nports, nitems(di.udi_ports));
+   for (port = 0; port < nports; port++) {
+   status = di.udi_ports[port] & 0x;
+   change = di.udi_ports[port] >> 16;
+   printf("\t port %02u: %04x.%04x", port+1, change,
+   status);
+
+   if (status & UPS_CURRENT_CONNECT_STATUS)
+   printf(" connect");
+
+   if (status & UPS_PORT_ENABLED)
+   printf(" enabled");
+
+   if (status & UPS_SUSPEND)
+   printf(" supsend");
+
+   if (status & UPS_OVERCURRENT_INDICATOR)
+   printf(" overcurrent");
+
+   if (di.udi_speed < USB_SPEED_SUPER) {
+   if (status & UPS_PORT_L1)
+   printf(" l1");
+
+   if (status & UPS_PORT_POWER)
+   printf(" power");
+   } else {
+   if (status & UPS_PORT_POWER_SS)
+   printf(" power");
+
+   switch (UPS_PORT_LS_GET(status)) {
+   case UPS_PORT_LS_U0:
+   printf(" U0");
+   

Re: errors in usage.c - libusbhid

2018-07-04 Thread David Bern
On Tue, 3 Jul 2018 07:55:47 +0200
Martin Pieuchot  wrote:
> > 
> > If wanted I can send the code I used to test this.  
> 
> It would be great if you could turn that into a regression test to put
> in /usr/src/regress/lib/libusbhid/nameofyourtest :)

Sure. 
Should say that my Bourne shell & bsd.regress.mk knowledge/experience 
is limited. Anyone feuer frei if any errors or unnecessary lines in the 
files are found.

The regression test.
Added a group called usage and has three test cases. 
I call them; hex, dec & static.

hex is disabled, as this(%x) is unhandled at the moment but included 
for the future.
dec tests usages in page that has %u or %d formats.
static tests lines that has statically defined names.

Index: libusbhid/Makefile
===
RCS file: libusbhid/Makefile
diff -N libusbhid/Makefile
--- /dev/null   1 Jan 1970 00:00:00 -
+++ libusbhid/Makefile  4 Jul 2018 14:10:18 -
@@ -0,0 +1,7 @@
+#  $OpenBSD$   
+
+SUBDIR+= usage
+
+install:
+
+.include 

Index: libusbhid/usage/Makefile
===
RCS file: libusbhid/usage/Makefile
diff -N libusbhid/usage/Makefile
--- /dev/null   1 Jan 1970 00:00:00 -
+++ libusbhid/usage/Makefile4 Jul 2018 14:32:49 -
@@ -0,0 +1,25 @@
+#  $OpenBSD$
+
+LDADD= -lusbhid
+DPADD= ${LIBUSBHID}
+WARNINGS= Yes
+CFLAGS+= -Werror
+
+
+PROG= parsetest
+REGRESS_TARGETS+= run-regress-${PROG}-hex
+REGRESS_TARGETS+= run-regress-${PROG}-dec
+REGRESS_TARGETS+= run-regress-${PROG}-static
+
+.include 
+
+# hextest is commented in runparse.sh
+run-regress-${PROG}-hex: ${PROG}
+   ./runparse.sh hex "12 100012"
+
+run-regress-${PROG}-dec: ${PROG}
+   ./runparse.sh dec "90002 A0002 810002"
+
+run-regress-${PROG}-static: ${PROG}
+   ./runparse.sh static "B0002 D0002 820012"
+

Index: libusbhid/usage/parsetest.c
===
RCS file: libusbhid/usage/parsetest.c
diff -N libusbhid/usage/parsetest.c
--- /dev/null   1 Jan 1970 00:00:00 -
+++ libusbhid/usage/parsetest.c 4 Jul 2018 14:33:06 -
@@ -0,0 +1,50 @@
+/* $OpenBSD$   */
+/*
+ * Copyright (c) 2018 David Bern 
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+int
+main(int argc, char *argv[])
+{
+   char *table = NULL;
+   char usage[1024];
+   int testval;
+   const char *errstr;
+
+   if (hid_start(table) == -1)
+   errx(1, "\nUnable to load table");
+
+   /* No args given, just test if able to load table */
+   if (argc < 2)
+   return 0;
+
+   testval = strtonum(argv[1], 0x0, 0x, &errstr);
+   if (errstr != NULL)
+   errx(1, "\nInvalid argument");
+
+   snprintf(usage, sizeof(usage), "%s:%s",
+   hid_usage_page(HID_PAGE(testval)),
+   hid_usage_in_page(testval));
+
+   return (hid_parse_usage_in_page(usage) != testval);
+}

Index: libusbhid/usage/runparse.sh
===
RCS file: libusbhid/usage/runparse.sh
diff -N libusbhid/usage/runparse.sh
--- /dev/null   1 Jan 1970 00:00:00 -
+++ libusbhid/usage/runparse.sh 4 Jul 2018 14:33:19 -
@@ -0,0 +1,32 @@
+#!/bin/sh
+#  $OpenBSD$   
+
+run() {
+for i in $* ; do
+   ./parsetest $(echo "ibase=16; $i" | bc)
+   if [ $? -ne 0 ]
+   then
+   printf "\nFailed on 0x%s\n" "$i"
+   return 1
+   fi
+   printf " $i"
+done
+printf "\n"
+return 0
+}
+
+case $1 in
+hex)
+   printf "Unable to handle %%x format names - DISABLED\n"
+   # run $2
+   exit 0 ;;
+dec)
+   printf "Testing %%d & %%u format names"
+   run $2
+   exit $? ;;
+static)
+   printf "Testing staticly named usage names"
+   run $2
+   exit $? ;;
+esac
+



Re: bgpd softreconf in optimisation

2018-07-04 Thread Peter Hessler
yes please!

OK

On 2018 Jul 03 (Tue) at 22:37:29 +0200 (+0200), Claudio Jeker wrote:
:There is no need to run against both input filters.
:path_update() and prefix_remove() are both smart enough to handle all
:cases (similar to a regular update).  Should make reloads a bit faster.
:
:More precise
:- prefix_remove() of a non existing prefix is a NOP
:- path_update() does itself a path_compare() call and will not issue an
:  UPDATE if nothing changed.
:
:OK?
:-- 
::wq Claudio
:
:Index: rde.c
:===
:RCS file: /cvs/src/usr.sbin/bgpd/rde.c,v
:retrieving revision 1.383
:diff -u -p -r1.383 rde.c
:--- rde.c  28 Jun 2018 09:54:48 -  1.383
:+++ rde.c  3 Jul 2018 19:09:13 -
:@@ -3047,8 +3047,8 @@ rde_softreconfig_in(struct rib_entry *re
:   struct prefix   *p, *np;
:   struct pt_entry *pt;
:   struct rde_peer *peer;
:-  struct rde_aspath   *asp, *oasp, *nasp;
:-  enum filter_actions  oa, na;
:+  struct rde_aspath   *asp, *fasp;
:+  enum filter_actions  action;
:   struct bgpd_addr addr;
: 
:   pt = re->prefix;
:@@ -3062,39 +3062,20 @@ rde_softreconfig_in(struct rib_entry *re
:   asp = prefix_aspath(p);
:   peer = asp->peer;
: 
:-  /* check if prefix changed */
:-  if (rib->state == RECONF_RELOAD) {
:-  oa = rde_filter(rib->in_rules_tmp, peer, &oasp, p);
:-  oasp = oasp != NULL ? oasp : asp;
:-  } else {
:-  /* make sure we update everything for RECONF_REINIT */
:-  oa = ACTION_DENY;
:-  oasp = asp;
:-  }
:-  na = rde_filter(rib->in_rules, peer, &nasp, p);
:-  nasp = nasp != NULL ? nasp : asp;
:+  action = rde_filter(rib->in_rules, peer, &fasp, p);
:+  fasp = fasp != NULL ? fasp : asp;
: 
:-  /* go through all 4 possible combinations */
:-  /* if (oa == ACTION_DENY && na == ACTION_DENY) */
:-  /* nothing todo */
:-  if (oa == ACTION_DENY && na == ACTION_ALLOW) {
:+  if (action == ACTION_ALLOW) {
:   /* update Local-RIB */
:-  path_update(&rib->rib, peer, nasp, &addr,
:+  path_update(&rib->rib, peer, fasp, &addr,
:   pt->prefixlen, 0);
:-  } else if (oa == ACTION_ALLOW && na == ACTION_DENY) {
:+  } else if (action == ACTION_DENY) {
:   /* remove from Local-RIB */
:   prefix_remove(&rib->rib, peer, &addr, pt->prefixlen, 0);
:-  } else if (oa == ACTION_ALLOW && na == ACTION_ALLOW) {
:-  if (path_compare(nasp, oasp) != 0)
:-  /* send update */
:-  path_update(&rib->rib, peer, nasp, &addr,
:-  pt->prefixlen, 0);
:   }
: 
:-  if (oasp != asp)
:-  path_put(oasp);
:-  if (nasp != asp)
:-  path_put(nasp);
:+  if (fasp != asp)
:+  path_put(fasp);
:   }
: }
: 
:

-- 
Old programmers never die.  They just branch to a new address.



[PATCH] column(1): -r to right justify

2018-07-04 Thread Job Snijders
Dear all,

Following some back and forth on how disklabel output should be
formatted, I proposed to Kenneth to extend the column(1) utility. All that
was missing is the ability to right justify. I've longed for this
feature for a while: I often use 'column -t' to prettify data coming
from an awk pipeline.

Example:

job@vurt ~$ netstat -r | column -t -r | tail -5 
ff01::%iwm0/32  fe80::4708:d2be:9a Um 03  - 4 
iwm0  
 ff01::%lo0/32   localhost Um 01  32768 4  
lo0  
 ff02::/16   localhost   UGRS 01  32768 8  
lo0  
ff02::%iwm0/32  fe80::4708:d2be:9a Um 03  - 4 
iwm0  
 ff02::%lo0/32   localhost Um 01  32768 4  
lo0  


Patch courtesy of Kenneth R Westerback. OK?

Index: column.1
===
RCS file: /cvs/src/usr.bin/column/column.1,v
retrieving revision 1.18
diff -u -p -r1.18 column.1
--- column.124 Oct 2016 13:53:05 -  1.18
+++ column.14 Jul 2018 10:27:54 -
@@ -40,6 +40,7 @@
 .Nm column
 .Op Fl tx
 .Op Fl c Ar columns
+.Op Fl r Op Ar list
 .Op Fl s Ar sep
 .Op Ar
 .Sh DESCRIPTION
@@ -66,6 +67,16 @@ The options are as follows:
 Output is formatted for a display
 .Ar columns
 wide.
+.It Fl r Op Ar list
+Table mode will right justify the column contents for the
+specified columns.
+.Ar list
+is a list of comma separated column numbers or column ranges.
+Column numbers start at 1.
+The list must not contain whitespace.
+If no
+.Ar list
+is provided then all columns will be right justified.
 .It Fl s Ar sep
 Specify a set of characters to delimit columns for the
 .Fl t
Index: column.c
===
RCS file: /cvs/src/usr.bin/column/column.c,v
retrieving revision 1.26
diff -u -p -r1.26 column.c
--- column.c22 Jun 2018 12:27:00 -  1.26
+++ column.c4 Jul 2018 10:28:00 -
@@ -47,7 +47,8 @@
 void  c_columnate(void);
 void *ereallocarray(void *, size_t, size_t);
 void  input(FILE *);
-void  maketbl(void);
+int   rightjustify(const char *, const int);
+void  maketbl(const int, const char *);
 void  print(void);
 void  r_columnate(void);
 __dead void usage(void);
@@ -69,8 +70,8 @@ main(int argc, char *argv[])
 {
struct winsize win;
FILE *fp;
-   int ch, tflag, xflag;
-   char *p;
+   int ch, rflag, tflag, xflag;
+   char *p, *rcols;
const char *errstr;
 
setlocale(LC_CTYPE, "");
@@ -87,14 +88,19 @@ main(int argc, char *argv[])
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
 
-   tflag = xflag = 0;
-   while ((ch = getopt(argc, argv, "c:s:tx")) != -1) {
+   rcols = NULL;
+   rflag = 0; tflag = xflag = 0;
+   while ((ch = getopt(argc, argv, "c:r::s:tx")) != -1) {
switch(ch) {
case 'c':
termwidth = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr != NULL)
errx(1, "%s: %s", errstr, optarg);
break;
+   case 'r':
+   rflag = 1;
+   rcols = optarg;
+   break;
case 's':
if ((separator = reallocarray(NULL, strlen(optarg) + 1,
sizeof(*separator))) == NULL)
@@ -139,7 +145,7 @@ main(int argc, char *argv[])
return eval;
 
if (tflag)
-   maketbl();
+   maketbl(rflag, rcols);
else if (*maxwidths >= termwidth)
print();
else if (xflag)
@@ -207,18 +213,69 @@ print(void)
puts(table[row]->content);
 }
 
+int
+rightjustify(const char *rcols, const int col)
+{
+   const char *errstr;
+   char c, *num, *temp;
+   long long ch, rangestart;
+   unsigned int i;
+
+   if (rcols == NULL)
+   return 1;
+
+   temp = strdup(rcols);
+   num = temp;
+   rangestart = -1;
+
+   c = 0;
+   for (i = 0; i <= strlen(rcols); i++) {
+   ch = temp[i];
+   if (ch == ',' || ch == '-')
+   temp[i] = '\0';
+   if (temp[i] != '\0')
+   continue;
+
+   c = strtonum(num, 1, INT_MAX, &errstr);
+   if (errstr != NULL)
+   break;
+   c--;/* Users are 1-based. Reality is 0-based. */
+
+   if (c == col)
+   goto found;
+   if (ch == '-') {
+   rangestart = c;
+   } else if ((ch == ',' || ch == '\0') && rangestart != -1) {
+   if (rangestart <= col && c >= col)
+   goto found;
+   rangestart = -1;
+   }
+   num = temp + i + 1;
+   }
+
+ 

Re: avoid vfprintf NULL errors in ldape.c log_debug()

2018-07-04 Thread Sebastian Benoit
ok

Rob Pierce(r...@2keys.ca) on 2018.07.03 19:38:51 -0400:
> Running the current ldapd regression tests result in the following (repeated)
> errors in my /var/log/messages:
> 
> ... ldapd: vfprintf %s NULL in "current bind dn = %s "
> 
> This is because regress/usr.sbin/ldapd/run-tests.pl is performing
> unnecessary unbinds in END { }.
> 
> Though the regression test should probably be fixed, the following diff
> ensures that log_debug is not called with a NULL argument.
> 
> Does this make sense?
> 
> Index: ldape.c
> ===
> RCS file: /cvs/src/usr.sbin/ldapd/ldape.c,v
> retrieving revision 1.27
> diff -u -p -r1.27 ldape.c
> --- ldape.c   15 May 2018 11:19:21 -  1.27
> +++ ldape.c   3 Jul 2018 23:32:27 -
> @@ -229,7 +229,8 @@ ldap_abandon(struct request *req)
>  int
>  ldap_unbind(struct request *req)
>  {
> - log_debug("current bind dn = %s", req->conn->binddn);
> + log_debug("current bind dn = %s",
> + req->conn->binddn == NULL ? "" : req->conn->binddn);
>   conn_disconnect(req->conn);
>   request_free(req);
>   return -1;  /* don't send any response */
> 



Re: realpath(3) on a dangling symlink

2018-07-04 Thread Martijn van Duren
On 07/03/18 23:17, Stuart Henderson wrote:
> On 2018/06/28 08:52, Martijn van Duren wrote:
>> - If realpath fails the content of resolved is undefined, so why waste
>> cycles setting it to ".". If anyone thinks we should return something
>> sensible I reckon we should make it an empty string.
> 
> Not sure if it's important or not, but if someone ignores the failure
> and uses the result anyway (for example perhaps appending "/something"
> to the string), "." seems less likely to cause problems than an empty
> string would.

With my earlier diff it would return the same garbage the user would
have put in, I only mentioned an empty string as a (shorter)
alternative, since we wouldn't have to call strlcpy.

A similar issue is that we don't return a sensible return value in all
the other error cases. resolved is filled up up to and including the
first component that can't be found. If that were to be used with
dirname (e.g. to save a new config in the same dir as the original) it
could end up a directory higher, potentially overwriting something
important.

To see what some of our neighbours do I did a quick test with the
following programs on a stock alpine and debian (only other OSes I
had available):
$ cat test.c 
#include 
#include 
#include 
#include 
#include 

#include 

int
main(int argc, char *argv[])
{
char *path;

path = calloc(PATH_MAX, 1);
path[0] = 'q';
if (mkdir("/tmp/tmp", 0777) == -1)
err(1, "mkdir");
if (chdir("/tmp/tmp") == -1)
err(1, "chdir");
if (rmdir("/tmp/tmp") == -1)
err(1, "rmdir");
if (realpath("test", path) != NULL)
err(1, "realpath");

printf("%s\n", path);
}
alpine$ cc test.c && ./a.out
q
debian$ cc test.c && ./a.out

openbsd$ cc test.c && ./a.out
.
$ cat test1.c 
#include 
#include 
#include 

int
main(int argc, char *argv[])
{
char *path;

path = calloc(PATH_MAX, 1);
path[0] = 'q';

printf("%p\n", realpath("/etc/unexistend/whatever.conf", path));
printf("%s\n", path);
}
alpine$ cc test1.c && ./a.out
0
q
debian$ cc test1.c && ./a.out
(nil)
/etc/unexistend
openbsd$ cc test1.c && ./a.out
0x0
/etc/unexistend

So alpine (musl-libc) doesn't touch resolved until it has fully resolved
the path and debian (glibc) clears resolved from the start and fills it
up as it resolves.

Since the behaviour seems to be pretty much all over the place I don't
think it matters much what we do; ergo, just leave resolved as is and
don't change it at all on error. But if we do think people could shoot
themselves in the foot with this we should fix this for a half-filled
resolved as well.

Diff below adds such a possible solution, where risk of misuse is
minimal. It creates the maximum amount of intermediate directories with
the least amount of probability that they exist and leaves no room
at the end (PATH_MAX) to create a file. The only two downside I see are:
1) if some application does hit this *and* decides to and is allowed to
create each and every directory it would be less fun for the admin to
remember how to do: "rm -rf /$(printf '\777')".
2) Execing an application calls realpath multiple times, potentially
failing a couple of times, so the exec-times increase slightly.
> 
> One area where this might possibly be triggered is if the user doesn't
> have permissions on the current directory (sometimes I've had problems
> in this area where rc scripts leave cwd as /root but it's mode 700 and
> a uid change has rendered it unreadable by that uid).
> 
> If you want to look at users in ports to see what sort of checks they
> do, here's a starter ..
> 
> $ grep -lwR realpath ~sthen/ports-nm/ | cut -d/ -f6- | sort
> ...

Index: ./stdlib/realpath.c
===
RCS file: /cvs/src/lib/libc/stdlib/realpath.c,v
retrieving revision 1.22
diff -u -p -r1.22 realpath.c
--- ./stdlib/realpath.c 24 Dec 2017 01:50:50 -  1.22
+++ ./stdlib/realpath.c 4 Jul 2018 06:08:32 -
@@ -49,7 +49,7 @@ realpath(const char *path, char *resolve
char *q;
size_t left_len, resolved_len, next_token_len;
unsigned symlinks;
-   int serrno, mem_allocated;
+   int serrno, mem_allocated, i;
ssize_t slen;
char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
 
@@ -82,13 +82,8 @@ realpath(const char *path, char *resolve
resolved_len = 1;
left_len = strlcpy(left, path + 1, sizeof(left));
} else {
-   if (getcwd(resolved, PATH_MAX) == NULL) {
-   if (mem_allocated)
-   free(resolved);
-   else
-   strlcpy(resolved, ".", PATH_MAX);
-   return (NULL);
-   }
+   if (getcwd(resolved, PATH_MAX) == NULL)
+   goto err;
resolved_len = strlen(reso