1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*----------------------------------------------------------------------------*/
/* Xymon client update tool. */
/* */
/* This tool is used to fetch the current client version from the config-file */
/* saved in etc/clientversion.cfg. The client script compares this with the */
/* current version on the server, and if they do not match then this utility */
/* is run to fetch the new version from the server and unpack it via "tar". */
/* */
/* Copyright (C) 2006-2011 Henrik Storner <henrik@hswn.dk> */
/* */
/* This program is released under the GNU General Public License (GPL), */
/* version 2. See the file "COPYING" for details. */
/* */
/*----------------------------------------------------------------------------*/
static char rcsid[] = "$Id: clientupdate.c 7085 2012-07-16 11:08:37Z storner $";
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include "libxymon.h"
#define CLIENTVERSIONFILE "etc/clientversion.cfg"
#define INPROGRESSFILE "tmp/.inprogress.update"
void cleanup(char *inprogressfn, char *selffn)
{
/* Remove temporary- and lock-files */
unlink(inprogressfn);
if (selffn) unlink(selffn);
}
int main(int argc, char *argv[])
{
int argi;
char *versionfn, *inprogressfn;
FILE *versionfd, *tarpipefd;
char version[1024];
char *newversion = NULL;
char *newverreq;
char *updateparam = NULL;
int removeself = 0;
int talkstat = 0;
sendreturn_t *sres;
#ifdef BIG_SECURITY_HOLE
/* Immediately drop all root privs, we'll regain them later when needed */
drop_root();
#else
/* We WILL not run as suid-root. */
drop_root_and_removesuid(argv[0]);
#endif
versionfn = (char *)malloc(strlen(xgetenv("XYMONHOME")) + strlen(CLIENTVERSIONFILE) + 2);
sprintf(versionfn, "%s/%s", xgetenv("XYMONHOME"), CLIENTVERSIONFILE);
inprogressfn = (char *)malloc(strlen(xgetenv("XYMONHOME")) + strlen(INPROGRESSFILE) + 2);
sprintf(inprogressfn, "%s/%s", xgetenv("XYMONHOME"), INPROGRESSFILE);
versionfd = fopen(versionfn, "r");
if (versionfd) {
char *p;
if (fgets(version, sizeof(version), versionfd) == NULL) *version = '\0';
p = strchr(version, '\n'); if (p) *p = '\0';
fclose(versionfd);
}
else {
*version = '\0';
}
if (chdir(xgetenv("XYMONHOME")) != 0) {
errprintf("Cannot chdir to XYMONHOME\n");
return 1;
}
for (argi=1; (argi < argc); argi++) {
if (strcmp(argv[argi], "--level") == 0) {
/* For checking what version we're at */
printf("%s\n", version);
return 0;
}
else if (strcmp(argv[argi], "--reexec") == 0) {
/*
* First step of the update procedure.
*
* To avoid problems with unpacking a new clientupdate
* on top of the running one (some tar's will abort
* if they try this), copy ourself to a temp. file and
* re-exec it to carry out the update.
*/
char tmpfn[PATH_MAX];
char *srcfn;
FILE *tmpfd, *srcfd;
unsigned char buf[8192];
long n;
struct stat st;
int cperr;
if (!updateparam) {
errprintf("clientupdate --reexec called with no update version\n");
return 1;
}
if ( (stat(inprogressfn, &st) == 0) && ((getcurrenttime(NULL) - st.st_mtime) < 3600) ) {
errprintf("Found update in progress or failed update (started %ld minutes ago)\n",
(long) (getcurrenttime(NULL)-st.st_mtime)/60);
return 1;
}
unlink(inprogressfn);
tmpfd = fopen(inprogressfn, "w"); if (tmpfd) fclose(tmpfd);
/* Copy the executable */
srcfn = argv[0];
srcfd = fopen(srcfn, "r"); cperr = errno;
sprintf(tmpfn, "%s/.update.%s.%ld.tmp",
xgetenv("XYMONTMP"), xgetenv("MACHINEDOTS"), (long)getcurrenttime(NULL));
dbgprintf("Starting update by copying %s to %s\n", srcfn, tmpfn);
unlink(tmpfn); /* To avoid symlink attacks */
if (srcfd) { tmpfd = fopen(tmpfn, "w"); cperr = errno; }
if (!srcfd || !tmpfd) {
errprintf("Cannot copy executable: %s\n", strerror(cperr));
return 1;
}
while ((n = fread(buf, 1, sizeof(buf), srcfd)) > 0) fwrite(buf, 1, n, tmpfd);
fclose(srcfd); fclose(tmpfd);
/* Make sure the temp. binary has execute permissions set */
chmod(tmpfn, S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP);
/*
* Set the temp. executable suid-root, and exec() it.
* If get_root() fails (because clientupdate was installed
* without suid-root privs), just carry on and do what we
* can without root privs. (It basically just means that
* logfetch() and clientupdate() will continue to run without
* root privs).
*/
#ifdef BIG_SECURITY_HOLE
get_root();
chown(tmpfn, 0, getgid());
chmod(tmpfn, S_ISUID|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP);
drop_root();
#endif
/* Run the temp. executable */
dbgprintf("Running command '%s %s ..remove-self'\n", tmpfn, updateparam);
execl(tmpfn, tmpfn, updateparam, "--remove-self", (char *)NULL);
/* We should never go here */
errprintf("exec() failed to launch update: %s\n", strerror(errno));
return 1;
}
else if (strncmp(argv[argi], "--update=", 9) == 0) {
newversion = strdup(argv[argi]+9);
updateparam = argv[argi];
}
else if (strcmp(argv[argi], "--remove-self") == 0) {
removeself = 1;
}
else if (strcmp(argv[argi], "--debug") == 0) {
debug = 1;
}
else if (strcmp(argv[argi], "--suid-setup") == 0) {
/*
* Final step of the update procedure.
*
* Become root to setup suid-root privs on utils that need it.
* Note: If get_root() fails, we're left with normal user privileges. That is
* OK, because that is how the client was installed originally, then.
*/
#ifdef BIG_SECURITY_HOLE
get_root();
chown("bin/logfetch", 0, getgid());
chmod("bin/logfetch", S_ISUID|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP);
drop_root();
#endif
return 0;
}
}
if (!newversion) {
errprintf("No new version string!\n");
cleanup(inprogressfn, (removeself ? argv[0] : NULL));
return 1;
}
/* Update to version "newversion" */
dbgprintf("Opening pipe to 'tar'\n");
tarpipefd = popen("tar xf -", "w");
if (tarpipefd == NULL) {
errprintf("Cannot launch 'tar xf -': %s\n", strerror(errno));
cleanup(inprogressfn, (removeself ? argv[0] : NULL));
return 1;
}
sres = newsendreturnbuf(1, tarpipefd);
newverreq = (char *)malloc(100+strlen(newversion));
sprintf(newverreq, "download %s.tar", newversion);
dbgprintf("Sending command to Xymon: %s\n", newverreq);
if ((talkstat = sendmessage(newverreq, NULL, XYMON_TIMEOUT, sres)) != XYMONSEND_OK) {
errprintf("Cannot fetch new client tarfile: Status %d\n", talkstat);
cleanup(inprogressfn, (removeself ? argv[0] : NULL));
freesendreturnbuf(sres);
return 1;
}
else {
dbgprintf("Download command completed OK\n");
freesendreturnbuf(sres);
}
dbgprintf("Closing tar pipe\n");
if ((talkstat = pclose(tarpipefd)) != 0) {
errprintf("Upgrade failed, tar exited with status %d\n", talkstat);
cleanup(inprogressfn, (removeself ? argv[0] : NULL));
return 1;
}
else {
dbgprintf("tar pipe exited with status 0 (OK)\n");
}
/* Create the new version file */
dbgprintf("Creating new version file %s with version %s\n", versionfn, newversion);
unlink(versionfn);
versionfd = fopen(versionfn, "w");
if (versionfd) {
fprintf(versionfd, "%s", newversion);
fclose(versionfd);
}
else {
errprintf("Cannot create version file: %s\n", strerror(errno));
}
/* Make sure these have execute permissions */
dbgprintf("Setting execute permissions on xymonclient.sh and clientupdate tools\n");
chmod("bin/xymonclient.sh", S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP);
chmod("bin/clientupdate", S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP);
/*
* Become root to setup suid-root privs on the new clientupdate util.
* Note: If get_root() fails, we're left with normal user privileges. That is
* OK, because that is how the client was installed originally, then.
*/
#ifdef BIG_SECURITY_HOLE
get_root();
chown("bin/clientupdate", 0, getgid());
chmod("bin/clientupdate", S_ISUID|S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP);
drop_root();
#endif
dbgprintf("Cleaning up after update\n");
cleanup(inprogressfn, (removeself ? argv[0] : NULL));
/*
* Exec the new client-update utility to fix suid-root permissions on
* the new files.
*/
execl("bin/clientupdate", "bin/clientupdate", "--suid-setup", (char *)NULL);
/* We should never go here */
errprintf("exec() of clientupdate --suid-setup failed: %s\n", strerror(errno));
return 0;
}
|