void critical_alert(char *instance, char *msg, ...)
{
- FILE *crit; /* FILE for critical log file */
- char buf[BUFSIZ]; /* Holds the formatted message */
+ FILE *crit;
+ char *buf;
va_list ap;
-
- va_start(ap, msg);
- vsprintf(buf, msg, ap);
- va_end(ap);
-
- /* Send zephyr notice */
- send_zgram(instance, buf);
+ long start;
/* Log message to critical file */
if ((crit = fopen(CRITERRLOG, "a")))
time_s = ctime(&t) + 4;
time_s[strlen(time_s) - 6] = '\0';
- fprintf(crit, "%s <%ld> %s\n", time_s, (long)getpid(), buf);
+ fprintf(crit, "%s <%ld>", time_s, (long)getpid());
+ start = ftell(crit);
+ va_start(ap, msg);
+ vfprintf(crit, msg, ap);
+ va_end(ap);
+ fprintf(crit, "\n");
+
+ buf = malloc(ftell(crit) - start);
fclose(crit);
+
+ if (buf)
+ {
+ va_start(ap, msg);
+ vsprintf(buf, msg, ap);
+ va_end(ap);
+
+ send_zgram(instance, buf);
+ com_err(whoami, 0, buf);
+
+ free(buf);
+ }
}
- com_err(whoami, 0, buf);
+ if (!crit || !buf)
+ {
+ send_zgram(instance, "Couldn't format critical syslog!");
+ com_err(whoami, 0, "Couldn't format critical syslog!");
+ }
}
-
/* Sends a zephyrgram of class "MOIRA", instance as a parameter. Ignores
* errors while sending message.
*/
void send_zgram(char *inst, char *msg)
{
+#ifdef SYSLOG
+ char *buf;
+#endif
+
#ifdef ZEPHYR
ZNotice_t znotice;
ZSendNotice(&znotice, ZNOAUTH);
#endif
#ifdef SYSLOG
- {
- char buf[512];
- sprintf(buf, "MOIRA: %s %s", inst, msg);
- syslog(LOG_ERR, buf);
- }
+ buf = malloc(9 + strlen(instance) + strlen(msg));
+ if (buf)
+ {
+ sprintf(buf, "MOIRA: %s %s", inst, msg);
+ syslog(LOG_ERR, buf);
+ free(buf);
+ }
#endif
}
char *canonicalize_hostname(char *host)
{
struct hostent *hp;
- int n_len;
+ int len;
int has_dot = 0;
- char tbuf[BUFSIZ];
- struct utsname name;
- char *cp;
+ char *tbuf, *cp;
- if (strlen(host) > 2 && host[0] == '"' && host[strlen(host) - 1] == '"')
+ len = strlen(host);
+ if (len > 2 && host[0] == '"' && host[len - 1] == '"')
{
- strcpy(tbuf, host + 1);
+ tbuf = malloc(len - 1);
+ if (!tbuf)
+ return NULL;
+ strncpy(tbuf, host + 1, len - 2);
+ tbuf[len - 1] = '\0';
free(host);
- tbuf[strlen(tbuf) - 1] = '\0';
- return strdup(tbuf);
+ return tbuf;
}
- if (strchr(host, '*') || strchr(host, '?') || strchr(host, '['))
+ if (strchr(host, '*') || strchr(host, '?'))
return host;
hp = gethostbyname(host);
if (hp)
{
- n_len = strlen(hp->h_name) + 1;
- host = realloc(host, n_len);
-
- strcpy(host, hp->h_name);
+ host = realloc(host, strlen(hp->h_name) + 1);
+ if (host)
+ strcpy(host, hp->h_name);
return host;
}
else
/* can't get name from nameserver; fix up the format a bit */
for (cp = host; *cp; cp++)
{
- int c;
- if (islower(c = *cp))
- *cp = toupper(c);
- has_dot |= (c == '.');
+ if (islower(*cp))
+ *cp = toupper(*cp);
+ has_dot |= (*cp == '.');
}
if (!has_dot)
{
if (domain == NULL)
{
+ struct utsname name;
+
uname(&name);
hp = gethostbyname(name.nodename);
- cp = strchr(hp->h_name, '.');
- if (cp)
- domain = strdup(++cp);
+ if (hp)
+ {
+ cp = strchr(hp->h_name, '.');
+ if (cp)
+ domain = strdup(++cp);
+ if (!domain)
+ domain = "";
+ }
else
domain = "";
}
+ tbuf = malloc(strlen(host) + strlen(domain) + 2);
+ if (!tbuf)
+ return NULL;
sprintf(tbuf, "%s.%s", host, domain);
free(host);
- host = strdup(tbuf);
+ host = tbuf;
}
return host;
}
}
}
+/* XXX no way to avoid possible buffer overrun here since we don't know
+ how long nm is and we're trying to make it one longer */
void LookForSt(char *nm) /* ST PIERRE, etc. */
{
char temp[256];
#include <moira.h>
#include "mr_private.h"
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
char **nargv = malloc(sizeof(char *) * (argc + 1));
int status = 0;
+ if (!nargv)
+ return ENOMEM;
nargv[0] = name;
memcpy(nargv + 1, argv, sizeof(char *) * argc);
status = mr_access_internal(argc + 1, nargv);
int mr_connect(char *server)
{
- char *p, **pp, sbuf[256];
+ char *p, **pp, *sbuf = NULL;
if (!mr_inited)
mr_init();
{
p = strchr(MOIRA_SERVER, ':');
p++;
+ sbuf = malloc(strlen(server) + strlen(p) + 2);
+ if (!sbuf)
+ return ENOMEM;
sprintf(sbuf, "%s:%s", server, p);
server = sbuf;
}
* stash hostname for later use
*/
- mr_server_host = strdup(server);
+ if (!sbuf)
+ sbuf = strdup(server);
+ mr_server_host = sbuf;
if ((p = strchr(mr_server_host, ':')))
*p = '\0';
mr_server_host = canonicalize_hostname(mr_server_host);
#include <moira.h>
#include "mr_private.h"
+#include <errno.h>
+#include <stdlib.h>
#include <string.h>
RCSID("$Header$");
mr_params param_st;
struct mr_params *params = NULL;
struct mr_params *reply = NULL;
- static char buffer[1024];
+ static char *buffer = NULL;
*motd = NULL;
CHECK_CONNECTED;
{
if (reply->mr_argc > 0)
{
- strncpy(buffer, reply->mr_argv[0], sizeof(buffer));
+ buffer = realloc(buffer, reply->mr_argl[0] + 1);
+ if (!buffer)
+ {
+ mr_disconnect();
+ return ENOMEM;
+ }
+ strcpy(buffer, reply->mr_argv[0]);
*motd = buffer;
}
mr_destroy_reply(reply);
#include <moira.h>
#include "mr_private.h"
+#include <errno.h>
#include <stdlib.h>
#include <string.h>
char **nargv = malloc(sizeof(char *) * (argc + 1));
int status = 0;
+ if (!nargv)
+ return ENOMEM;
nargv[0] = name;
memcpy(nargv + 1, argv, sizeof(char *) * argc);
status = mr_query_internal(argc + 1, nargv, callproc, callarg);