2005-10-26 Paul Jakma <paul.jakma@sun.com>

* command.c: Use MTYPE_HOST, MTYPE_STRVEC. Some other fixups,
	  including fixing some likely leaks in config_write_file.
	* vty.c: memory macro usage fixes.
	  (vty_read_config) fix leak where relative config file is
	  specified.
This commit is contained in:
paul 2005-10-26 05:49:54 +00:00
parent 0241684ea7
commit 05865c90ab
3 changed files with 65 additions and 65 deletions

View file

@ -7,6 +7,11 @@
* memtypes.h: update auto-built file. * memtypes.h: update auto-built file.
* if_rmap.c: Use MTYPE_IF_RMAP_NAME. * if_rmap.c: Use MTYPE_IF_RMAP_NAME.
* pqueue.c: Use the two MTYPE_PQUEUE mtypes for allocations. * pqueue.c: Use the two MTYPE_PQUEUE mtypes for allocations.
* command.c: Use MTYPE_HOST, MTYPE_STRVEC. Some other fixups,
including fixing some likely leaks in config_write_file.
* vty.c: memory macro usage fixes.
(vty_read_config) fix leak where relative config file is
specified.
2005-10-20 Andrew J. Schorr <ajschorr@alumni.princeton.edu> 2005-10-20 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

View file

@ -1,5 +1,5 @@
/* /*
$Id: command.c,v 1.50 2005/09/05 11:54:13 paul Exp $ $Id: command.c,v 1.51 2005/10/26 05:49:54 paul Exp $
Command interpreter routine for virtual terminal [aka TeletYpe] Command interpreter routine for virtual terminal [aka TeletYpe]
Copyright (C) 1997, 98, 99 Kunihiro Ishiguro Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
@ -1890,7 +1890,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
{ {
char *lcdstr; char *lcdstr;
lcdstr = XMALLOC (MTYPE_TMP, lcd + 1); lcdstr = XMALLOC (MTYPE_STRVEC, lcd + 1);
memcpy (lcdstr, matchvec->index[0], lcd); memcpy (lcdstr, matchvec->index[0], lcd);
lcdstr[lcd] = '\0'; lcdstr[lcd] = '\0';
@ -1900,7 +1900,7 @@ cmd_complete_command_real (vector vline, struct vty *vty, int *status)
for (i = 0; i < vector_active (matchvec); i++) for (i = 0; i < vector_active (matchvec); i++)
{ {
if (vector_slot (matchvec, i)) if (vector_slot (matchvec, i))
XFREE (MTYPE_TMP, vector_slot (matchvec, i)); XFREE (MTYPE_STRVEC, vector_slot (matchvec, i));
} }
vector_free (matchvec); vector_free (matchvec);
@ -2524,6 +2524,7 @@ DEFUN (config_write_file,
char *config_file; char *config_file;
char *config_file_tmp = NULL; char *config_file_tmp = NULL;
char *config_file_sav = NULL; char *config_file_sav = NULL;
int ret = CMD_WARNING;
struct vty *file_vty; struct vty *file_vty;
/* Check and see if we are operating under vtysh configuration */ /* Check and see if we are operating under vtysh configuration */
@ -2537,12 +2538,13 @@ DEFUN (config_write_file,
/* Get filename. */ /* Get filename. */
config_file = host.config; config_file = host.config;
config_file_sav = malloc (strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1); config_file_sav =
XMALLOC (MTYPE_TMP, strlen (config_file) + strlen (CONF_BACKUP_EXT) + 1);
strcpy (config_file_sav, config_file); strcpy (config_file_sav, config_file);
strcat (config_file_sav, CONF_BACKUP_EXT); strcat (config_file_sav, CONF_BACKUP_EXT);
config_file_tmp = malloc (strlen (config_file) + 8); config_file_tmp = XMALLOC (MTYPE_TMP, strlen (config_file) + 8);
sprintf (config_file_tmp, "%s.XXXXXX", config_file); sprintf (config_file_tmp, "%s.XXXXXX", config_file);
/* Open file to configuration write. */ /* Open file to configuration write. */
@ -2551,9 +2553,7 @@ DEFUN (config_write_file,
{ {
vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp, vty_out (vty, "Can't open configuration file %s.%s", config_file_tmp,
VTY_NEWLINE); VTY_NEWLINE);
free (config_file_tmp); goto finished;
free (config_file_sav);
return CMD_WARNING;
} }
/* Make vty for configuration file. */ /* Make vty for configuration file. */
@ -2579,55 +2579,45 @@ DEFUN (config_write_file,
{ {
vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav, vty_out (vty, "Can't unlink backup configuration file %s.%s", config_file_sav,
VTY_NEWLINE); VTY_NEWLINE);
free (config_file_sav); goto finished;
free (config_file_tmp);
unlink (config_file_tmp);
return CMD_WARNING;
} }
if (link (config_file, config_file_sav) != 0) if (link (config_file, config_file_sav) != 0)
{ {
vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav, vty_out (vty, "Can't backup old configuration file %s.%s", config_file_sav,
VTY_NEWLINE); VTY_NEWLINE);
free (config_file_sav); goto finished;
free (config_file_tmp);
unlink (config_file_tmp);
return CMD_WARNING;
} }
sync (); sync ();
if (unlink (config_file) != 0) if (unlink (config_file) != 0)
{ {
vty_out (vty, "Can't unlink configuration file %s.%s", config_file, vty_out (vty, "Can't unlink configuration file %s.%s", config_file,
VTY_NEWLINE); VTY_NEWLINE);
free (config_file_sav); goto finished;
free (config_file_tmp);
unlink (config_file_tmp);
return CMD_WARNING;
} }
if (link (config_file_tmp, config_file) != 0) if (link (config_file_tmp, config_file) != 0)
{ {
vty_out (vty, "Can't save configuration file %s.%s", config_file, vty_out (vty, "Can't save configuration file %s.%s", config_file,
VTY_NEWLINE); VTY_NEWLINE);
free (config_file_sav); goto finished;
free (config_file_tmp);
unlink (config_file_tmp);
return CMD_WARNING;
} }
unlink (config_file_tmp);
sync (); sync ();
free (config_file_sav);
free (config_file_tmp);
if (chmod (config_file, CONFIGFILE_MASK) != 0) if (chmod (config_file, CONFIGFILE_MASK) != 0)
{ {
vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s", vty_out (vty, "Can't chmod configuration file %s: %s (%d).%s",
config_file, safe_strerror(errno), errno, VTY_NEWLINE); config_file, safe_strerror(errno), errno, VTY_NEWLINE);
return CMD_WARNING; goto finished;
} }
vty_out (vty, "Configuration saved to %s%s", config_file, vty_out (vty, "Configuration saved to %s%s", config_file,
VTY_NEWLINE); VTY_NEWLINE);
return CMD_SUCCESS; ret = CMD_SUCCESS;
finished:
unlink (config_file_tmp);
XFREE (MTYPE_TMP, config_file_tmp);
XFREE (MTYPE_TMP, config_file_sav);
return ret;
} }
ALIAS (config_write_file, ALIAS (config_write_file,
@ -2739,9 +2729,9 @@ DEFUN (config_hostname,
} }
if (host.name) if (host.name)
XFREE (0, host.name); XFREE (MTYPE_HOST, host.name);
host.name = strdup (argv[0]); host.name = XSTRDUP (MTYPE_HOST, argv[0]);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -2753,7 +2743,7 @@ DEFUN (config_no_hostname,
"Host name of this router\n") "Host name of this router\n")
{ {
if (host.name) if (host.name)
XFREE (0, host.name); XFREE (MTYPE_HOST, host.name);
host.name = NULL; host.name = NULL;
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -2778,11 +2768,11 @@ DEFUN (config_password, password_cmd,
if (*argv[0] == '8') if (*argv[0] == '8')
{ {
if (host.password) if (host.password)
XFREE (0, host.password); XFREE (MTYPE_HOST, host.password);
host.password = NULL; host.password = NULL;
if (host.password_encrypt) if (host.password_encrypt)
XFREE (0, host.password_encrypt); XFREE (MTYPE_HOST, host.password_encrypt);
host.password_encrypt = XSTRDUP (0, strdup (argv[1])); host.password_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
else else
@ -2800,17 +2790,17 @@ DEFUN (config_password, password_cmd,
} }
if (host.password) if (host.password)
XFREE (0, host.password); XFREE (MTYPE_HOST, host.password);
host.password = NULL; host.password = NULL;
if (host.encrypt) if (host.encrypt)
{ {
if (host.password_encrypt) if (host.password_encrypt)
XFREE (0, host.password_encrypt); XFREE (MTYPE_HOST, host.password_encrypt);
host.password_encrypt = XSTRDUP (0, zencrypt (argv[0])); host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
} }
else else
host.password = XSTRDUP (0, argv[0]); host.password = XSTRDUP (MTYPE_HOST, argv[0]);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -2842,12 +2832,12 @@ DEFUN (config_enable_password, enable_password_cmd,
if (*argv[0] == '8') if (*argv[0] == '8')
{ {
if (host.enable) if (host.enable)
XFREE (0, host.enable); XFREE (MTYPE_HOST, host.enable);
host.enable = NULL; host.enable = NULL;
if (host.enable_encrypt) if (host.enable_encrypt)
XFREE (0, host.enable_encrypt); XFREE (MTYPE_HOST, host.enable_encrypt);
host.enable_encrypt = XSTRDUP (0, argv[1]); host.enable_encrypt = XSTRDUP (MTYPE_HOST, argv[1]);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -2866,18 +2856,18 @@ DEFUN (config_enable_password, enable_password_cmd,
} }
if (host.enable) if (host.enable)
XFREE (0, host.enable); XFREE (MTYPE_HOST, host.enable);
host.enable = NULL; host.enable = NULL;
/* Plain password input. */ /* Plain password input. */
if (host.encrypt) if (host.encrypt)
{ {
if (host.enable_encrypt) if (host.enable_encrypt)
XFREE (0, host.enable_encrypt); XFREE (MTYPE_HOST, host.enable_encrypt);
host.enable_encrypt = XSTRDUP (0, zencrypt (argv[0])); host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (argv[0]));
} }
else else
host.enable = XSTRDUP (0, argv[0]); host.enable = XSTRDUP (MTYPE_HOST, argv[0]);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -2897,11 +2887,11 @@ DEFUN (no_config_enable_password, no_enable_password_cmd,
"Assign the privileged level password\n") "Assign the privileged level password\n")
{ {
if (host.enable) if (host.enable)
XFREE (0, host.enable); XFREE (MTYPE_HOST, host.enable);
host.enable = NULL; host.enable = NULL;
if (host.enable_encrypt) if (host.enable_encrypt)
XFREE (0, host.enable_encrypt); XFREE (MTYPE_HOST, host.enable_encrypt);
host.enable_encrypt = NULL; host.enable_encrypt = NULL;
return CMD_SUCCESS; return CMD_SUCCESS;
@ -2921,14 +2911,14 @@ DEFUN (service_password_encrypt,
if (host.password) if (host.password)
{ {
if (host.password_encrypt) if (host.password_encrypt)
XFREE (0, host.password_encrypt); XFREE (MTYPE_HOST, host.password_encrypt);
host.password_encrypt = XSTRDUP (0, zencrypt (host.password)); host.password_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.password));
} }
if (host.enable) if (host.enable)
{ {
if (host.enable_encrypt) if (host.enable_encrypt)
XFREE (0, host.enable_encrypt); XFREE (MTYPE_HOST, host.enable_encrypt);
host.enable_encrypt = XSTRDUP (0, zencrypt (host.enable)); host.enable_encrypt = XSTRDUP (MTYPE_HOST, zencrypt (host.enable));
} }
return CMD_SUCCESS; return CMD_SUCCESS;
@ -2947,11 +2937,11 @@ DEFUN (no_service_password_encrypt,
host.encrypt = 0; host.encrypt = 0;
if (host.password_encrypt) if (host.password_encrypt)
XFREE (0, host.password_encrypt); XFREE (MTYPE_HOST, host.password_encrypt);
host.password_encrypt = NULL; host.password_encrypt = NULL;
if (host.enable_encrypt) if (host.enable_encrypt)
XFREE (0, host.enable_encrypt); XFREE (MTYPE_HOST, host.enable_encrypt);
host.enable_encrypt = NULL; host.enable_encrypt = NULL;
return CMD_SUCCESS; return CMD_SUCCESS;
@ -3220,9 +3210,9 @@ set_log_file(struct vty *vty, const char *fname, int loglevel)
} }
if (host.logfile) if (host.logfile)
XFREE (MTYPE_TMP, host.logfile); XFREE (MTYPE_HOST, host.logfile);
host.logfile = XSTRDUP (MTYPE_TMP, fname); host.logfile = XSTRDUP (MTYPE_HOST, fname);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -3263,7 +3253,7 @@ DEFUN (no_config_log_file,
zlog_reset_file (NULL); zlog_reset_file (NULL);
if (host.logfile) if (host.logfile)
XFREE (MTYPE_TMP, host.logfile); XFREE (MTYPE_HOST, host.logfile);
host.logfile = NULL; host.logfile = NULL;
@ -3432,8 +3422,8 @@ DEFUN (banner_motd_file,
"Filename\n") "Filename\n")
{ {
if (host.motdfile) if (host.motdfile)
XFREE (MTYPE_TMP, host.motdfile); XFREE (MTYPE_HOST, host.motdfile);
host.motdfile = XSTRDUP (MTYPE_TMP, argv[0]); host.motdfile = XSTRDUP (MTYPE_HOST, argv[0]);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -3458,7 +3448,7 @@ DEFUN (no_banner_motd,
{ {
host.motd = NULL; host.motd = NULL;
if (host.motdfile) if (host.motdfile)
XFREE (MTYPE_TMP, host.motdfile); XFREE (MTYPE_HOST, host.motdfile);
host.motdfile = NULL; host.motdfile = NULL;
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -3467,7 +3457,7 @@ DEFUN (no_banner_motd,
void void
host_config_set (char *filename) host_config_set (char *filename)
{ {
host.config = strdup (filename); host.config = XSTRDUP (MTYPE_HOST, filename);
} }
void void

View file

@ -2167,7 +2167,7 @@ vty_close (struct vty *vty)
close (vty->fd); close (vty->fd);
if (vty->address) if (vty->address)
XFREE (0, vty->address); XFREE (MTYPE_TMP, vty->address);
if (vty->buf) if (vty->buf)
XFREE (MTYPE_VTY, vty->buf); XFREE (MTYPE_VTY, vty->buf);
@ -2306,6 +2306,7 @@ vty_read_config (char *config_file,
char cwd[MAXPATHLEN]; char cwd[MAXPATHLEN];
FILE *confp = NULL; FILE *confp = NULL;
char *fullpath; char *fullpath;
char *tmp = NULL;
/* If -f flag specified. */ /* If -f flag specified. */
if (config_file != NULL) if (config_file != NULL)
@ -2313,9 +2314,10 @@ vty_read_config (char *config_file,
if (! IS_DIRECTORY_SEP (config_file[0])) if (! IS_DIRECTORY_SEP (config_file[0]))
{ {
getcwd (cwd, MAXPATHLEN); getcwd (cwd, MAXPATHLEN);
fullpath = XMALLOC (MTYPE_TMP, tmp = XMALLOC (MTYPE_TMP,
strlen (cwd) + strlen (config_file) + 2); strlen (cwd) + strlen (config_file) + 2);
sprintf (fullpath, "%s/%s", cwd, config_file); sprintf (tmp, "%s/%s", cwd, config_file);
fullpath = tmp;
} }
else else
fullpath = config_file; fullpath = config_file;
@ -2394,6 +2396,9 @@ vty_read_config (char *config_file,
fclose (confp); fclose (confp);
host_config_set (fullpath); host_config_set (fullpath);
if (tmp)
XFREE (MTYPE_TMP, fullpath);
} }
/* Small utility function which output log to the VTY. */ /* Small utility function which output log to the VTY. */