On Fri, 2021-10-29 at 04:52 -0600, Theo de Raadt wrote:
> Stuart Henderson <[email protected]> wrote:
> 
> > > Diff below calls uname(3) only a single time and if the variables
> > > aren't snmpd.conf I simply rebuild it every time.
> > 
> > I'm in two minds about this. While it's not something that will happen
> > very often, hostnames do get changed sometimes, and it would be nice to
> > have snmpd reflect the current name of the system rather than what it
> > was named when snmpd started.
> 
> I also disagree with this caching.  Someone should not need to restart
> the daemon if they change the hostname, that is crazy, so the daemon
> should get it continually (or at least poll once in a while to look for
> a change).
> 
> snmpd's job is to export current & correct information, not cache.

That would be the diff below. Keep in mind that there are two edge-cases
here, which will not use current information:
1) snmpd.conf(5)'s "system description" and "system name"
2) snmpd(8) allows these values to be set via a set-request. In this
   case the information doesn't change any kernel values, but does set a
   cache string.

Index: mib.c
===================================================================
RCS file: /cvs/src/usr.sbin/snmpd/mib.c,v
retrieving revision 1.102
diff -u -p -r1.102 mib.c
--- mib.c       1 Sep 2021 15:54:40 -0000       1.102
+++ mib.c       30 Oct 2021 16:06:59 -0000
@@ -126,22 +126,21 @@ mib_getsys(struct oid *oid, struct ber_o
 {
        struct ber_oid           sysoid = OID(MIB_SYSOID_DEFAULT);
        char                    *s = oid->o_data;
+       char                     buf[256];
        struct ber_oid          *so = oid->o_data;
-       struct utsname           u;
+       struct utsname   u;
        long long                ticks;
 
-       if (uname(&u) == -1)
-               return (-1);
 
        switch (oid->o_oid[OIDIDX_system]) {
        case 1:
                if (s == NULL) {
-                       if (asprintf(&s, "%s %s %s %s %s",
-                           u.sysname, u.nodename, u.release,
-                           u.version, u.machine) == -1)
+                       if (uname(&u) == -1)
                                return (-1);
-                       oid->o_data = s;
-                       oid->o_val = strlen(s);
+                       snprintf(buf, sizeof(buf), "%s %s %s %s %s",
+                           u.sysname, u.nodename, u.release,
+                           u.version, u.machine);
+                       s = buf;
                }
                *elm = ober_add_string(*elm, s);
                break;
@@ -157,20 +156,15 @@ mib_getsys(struct oid *oid, struct ber_o
                ober_set_header(*elm, BER_CLASS_APPLICATION, SNMP_T_TIMETICKS);
                break;
        case 4:
-               if (s == NULL) {
-                       if (asprintf(&s, "root@%s", u.nodename) == -1)
-                               return (-1);
-                       oid->o_data = s;
-                       oid->o_val = strlen(s);
-               }
+               if (s == NULL)
+                       s = "";
                *elm = ober_add_string(*elm, s);
                break;
        case 5:
                if (s == NULL) {
-                       if ((s = strdup(u.nodename)) == NULL)
-                               return (-1);
-                       oid->o_data = s;
-                       oid->o_val = strlen(s);
+                       if (gethostname(buf, sizeof(buf)) == -1)
+                               return -1;
+                       s = buf;
                }
                *elm = ober_add_string(*elm, s);
                break;
Index: snmpd.conf.5
===================================================================
RCS file: /cvs/src/usr.sbin/snmpd/snmpd.conf.5,v
retrieving revision 1.58
diff -u -p -r1.58 snmpd.conf.5
--- snmpd.conf.5        2 Sep 2021 05:41:02 -0000       1.58
+++ snmpd.conf.5        30 Oct 2021 16:06:59 -0000
@@ -244,9 +244,6 @@ This is the default value.
 .It Ic system contact Ar string
 Specify the name or description of the system contact, typically a
 name or an email address.
-The default value is
-.Ar root@hostname
-using the hostname of the local machine.
 .It Ic system description Ar string
 Specify a description of the local system.
 The default value is the operating system identification as printed by the


Reply via email to