+ o Fixed hostnamestr() for hosts that can't find themselves using
+ gethostbyname() (Benoit Dolez)
o Add 'modnonsubposts' tunable that when set will moderate all posts
from non subscribers
o Fixed requeue for lists with noarchive enabled
#include <libgen.h>
#include <time.h>
#include <ctype.h>
+#include <errno.h>
#include "strgen.h"
#include "wrappers.h"
char *hostnamestr()
{
- struct hostent *hostlookup;
- char hostname[1024];
+ struct hostent *hostlookup;
+ char *hostname = NULL;
+ size_t len = 512;
+
+ for (;;) {
+ len *= 2;
+ free(hostname);
+
+ hostname = malloc(len);
+ hostname[len-1] = '\0';
+
+ /* gethostname() is allowed to:
+ * a) return -1 and undefined in hostname
+ * b) return 0 and an unterminated string in hostname
+ * c) return 0 and a NUL-terminated string in hostname
+ *
+ * We keep expanding the buffer until the hostname is
+ * NUL-terminated (and pray that it is not truncated)
+ * or an error occurs.
+ */
+ if (gethostname(hostname, len - 1)) {
+ if (errno == ENAMETOOLONG) {
+ continue;
+ }
+ myfree(hostname);
+ return mystrdup("localhost");
+ }
+
+ if (hostname[len-1] == '\0') {
+ break;
+ }
+ }
- /* TODO use dynamic allocation */
- gethostname(hostname, sizeof(hostname) - 1);
- /* gethostname() is allowed to return an unterminated string */
- hostname[sizeof(hostname)-1] = '\0';
- hostlookup = gethostbyname(hostname);
+ if (strchr(hostname, '.')) {
+ /* hostname is FQDN */
+ return hostname;
+ }
+
+ if ((hostlookup = gethostbyname(hostname))) {
+ myfree(hostname);
+ return mystrdup(hostlookup->h_name);
+ }
- return mystrdup(hostlookup->h_name);
+ return hostname;
}
char *mydirname(const char *path)