summaryrefslogtreecommitdiff
path: root/lib/rpmscript.c
blob: 11a78f65ebb80d7d12f6a39fb9ca86fe317616be (plain)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "system.h"

#include <errno.h>
#include <unistd.h>

#define _RPMSQ_INTERNAL
#include <rpm/rpmsq.h>
#include <rpm/rpmts.h>
#include <rpm/rpmfileutil.h>
#include <rpm/rpmmacro.h>
#include <rpm/rpmio.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmsw.h>
#include <rpm/header.h>

#include "rpmio/rpmlua.h"
#include "lib/rpmscript.h"

#include "debug.h"

/**
 * Run internal Lua script.
 */
static rpmRC runLuaScript(rpmts ts, ARGV_const_t prefixes,
		   const char *sname, rpmlogLvl lvl,
		   ARGV_t * argvp, const char *script, int arg1, int arg2)
{
    rpmRC rc = RPMRC_FAIL;
#ifdef WITH_LUA
    int rootFd = -1;
    int xx;
    ARGV_t argv = argvp ? *argvp : NULL;
    rpmlua lua = NULL; /* Global state. */
    rpmluav var;

    rpmlog(RPMLOG_DEBUG, "%s: running <lua> scriptlet.\n", sname);
    if (!rpmtsChrootDone(ts)) {
	const char *rootDir = rpmtsRootDir(ts);
	xx = chdir("/");
	rootFd = open(".", O_RDONLY, 0);
	if (rootFd >= 0) {
	    if (rootDir != NULL && !rstreq(rootDir, "/") && *rootDir == '/')
		xx = chroot(rootDir);
	    xx = rpmtsSetChrootDone(ts, 1);
	}
    }

    /* Create arg variable */
    rpmluaPushTable(lua, "arg");
    var = rpmluavNew();
    rpmluavSetListMode(var, 1);
    if (argv) {
	char **p;
	for (p = argv; *p; p++) {
	    rpmluavSetValue(var, RPMLUAV_STRING, *p);
	    rpmluaSetVar(lua, var);
	}
    }
    if (arg1 >= 0) {
	rpmluavSetValueNum(var, arg1);
	rpmluaSetVar(lua, var);
    }
    if (arg2 >= 0) {
	rpmluavSetValueNum(var, arg2);
	rpmluaSetVar(lua, var);
    }
    var = rpmluavFree(var);
    rpmluaPop(lua);

    if (rpmluaRunScript(lua, script, sname) == 0) {
	rc = RPMRC_OK;
    }

    rpmluaDelVar(lua, "arg");

    if (rootFd >= 0) {
	const char *rootDir = rpmtsRootDir(ts);
	xx = fchdir(rootFd);
	xx = close(rootFd);
	if (rootDir != NULL && !rstreq(rootDir, "/") && *rootDir == '/')
	    xx = chroot(".");
	xx = rpmtsSetChrootDone(ts, 0);
    }
#else
    rpmlog(lvl, _("<lua> scriptlet support not built in\n"));
#endif

    return rc;
}

static const char * const SCRIPT_PATH = "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin";

static void doScriptExec(rpmts ts, ARGV_const_t argv, ARGV_const_t prefixes,
			FD_t scriptFd, FD_t out)
{
    const char * rootDir;
    int pipes[2];
    int flag;
    int fdno;
    int xx;
    int open_max;

    (void) signal(SIGPIPE, SIG_DFL);
    pipes[0] = pipes[1] = 0;
    /* make stdin inaccessible */
    xx = pipe(pipes);
    xx = close(pipes[1]);
    xx = dup2(pipes[0], STDIN_FILENO);
    xx = close(pipes[0]);

    /* XXX Force FD_CLOEXEC on all inherited fdno's. */
    open_max = sysconf(_SC_OPEN_MAX);
    if (open_max == -1) {
	open_max = 1024;
    }
    for (fdno = 3; fdno < open_max; fdno++) {
	flag = fcntl(fdno, F_GETFD);
	if (flag == -1 || (flag & FD_CLOEXEC))
	    continue;
	xx = fcntl(fdno, F_SETFD, FD_CLOEXEC);
	/* XXX W2DO? debug msg for inheirited fdno w/o FD_CLOEXEC */
    }

    if (scriptFd != NULL) {
	int sfdno = Fileno(scriptFd);
	int ofdno = Fileno(out);
	if (sfdno != STDERR_FILENO)
	    xx = dup2(sfdno, STDERR_FILENO);
	if (ofdno != STDOUT_FILENO)
	    xx = dup2(ofdno, STDOUT_FILENO);
	/* make sure we don't close stdin/stderr/stdout by mistake! */
	if (ofdno > STDERR_FILENO && ofdno != sfdno)
	    xx = Fclose (out);
	if (sfdno > STDERR_FILENO && ofdno != sfdno)
	    xx = Fclose (scriptFd);
    }

    {   char *ipath = rpmExpand("%{_install_script_path}", NULL);
	const char *path = SCRIPT_PATH;

	if (ipath && ipath[5] != '%')
	    path = ipath;

	xx = setenv("PATH", path, 1);
	ipath = _free(ipath);
    }

    for (ARGV_const_t pf = prefixes; pf && *pf; pf++) {
	char *name = NULL;
	int num = (pf - prefixes);

	rasprintf(&name, "RPM_INSTALL_PREFIX%d", num);
	setenv(name, *pf, 1);
	free(name);

	/* scripts might still be using the old style prefix */
	if (num == 0) {
	    setenv("RPM_INSTALL_PREFIX", *pf, 1);
	}
    }
	
    rootDir = rpmtsRootDir(ts);
    if (rootDir  != NULL) {	/* XXX can't happen */
	if (!rpmtsChrootDone(ts) &&
	    !(rootDir[0] == '/' && rootDir[1] == '\0'))
	{
	    xx = chroot(rootDir);
	}
	xx = chdir("/");

	/* XXX Don't mtrace into children. */
	unsetenv("MALLOC_CHECK_");

	/* Permit libselinux to do the scriptlet exec. */
	if (rpmtsSELinuxEnabled(ts) == 1) {	
	    xx = rpm_execcon(0, argv[0], argv, environ);
	}

	if (xx == 0) {
	    xx = execv(argv[0], argv);
	}
    }
    _exit(127); /* exit 127 for compatibility with bash(1) */
}

/**
 * Run an external script.
 */
static rpmRC runExtScript(rpmts ts, ARGV_const_t prefixes,
		   const char *sname, rpmlogLvl lvl,
		   ARGV_t * argvp, const char *script, int arg1, int arg2)
{
    FD_t scriptFd;
    FD_t out = NULL;
    char * fn = NULL;
    int xx;
    rpmRC rc = RPMRC_FAIL;
    struct rpmsqElem sq;

    memset(&sq, 0, sizeof(sq));
    sq.reaper = 1;

    rpmlog(RPMLOG_DEBUG, "%s: scriptlet start\n", sname);

    if (script) {
	const char * rootDir = rpmtsRootDir(ts);
	FD_t fd;

	fd = rpmMkTempFile((!rpmtsChrootDone(ts) ? rootDir : "/"), &fn);
	if (fd == NULL || Ferror(fd)) {
	    rpmlog(RPMLOG_ERR, _("Couldn't create temporary file for %s: %s\n"),
		   sname, strerror(errno));
	    goto exit;
	}

	if (rpmIsDebug() &&
	    (rstreq(*argvp[0], "/bin/sh") || rstreq(*argvp[0], "/bin/bash")))
	{
	    static const char set_x[] = "set -x\n";
	    xx = Fwrite(set_x, sizeof(set_x[0]), sizeof(set_x)-1, fd);
	}

	xx = Fwrite(script, sizeof(script[0]), strlen(script), fd);
	xx = Fclose(fd);

	{   const char * sn = fn;
	    if (!rpmtsChrootDone(ts) && rootDir != NULL &&
		!(rootDir[0] == '/' && rootDir[1] == '\0'))
	    {
		sn += strlen(rootDir)-1;
	    }
	    argvAdd(argvp, sn);
	}

	if (arg1 >= 0) {
	    argvAddNum(argvp, arg1);
	}
	if (arg2 >= 0) {
	    argvAddNum(argvp, arg2);
	}
    }

    scriptFd = rpmtsScriptFd(ts);
    if (scriptFd != NULL) {
	if (rpmIsVerbose()) {
	    out = fdDup(Fileno(scriptFd));
	} else {
	    out = Fopen("/dev/null", "w.fdio");
	    if (Ferror(out)) {
		out = fdDup(Fileno(scriptFd));
	    }
	}
    } else {
	out = fdDup(STDOUT_FILENO);
    }
    if (out == NULL) { 
	rpmlog(RPMLOG_ERR, _("Couldn't duplicate file descriptor: %s: %s\n"),
	       sname, strerror(errno));
	goto exit;
    }

    xx = rpmsqFork(&sq);
    if (sq.child == 0) {
	rpmlog(RPMLOG_DEBUG, "%s: execv(%s) pid %d\n",
	       sname, *argvp[0], (unsigned)getpid());
	doScriptExec(ts, *argvp, prefixes, scriptFd, out);
    }

    if (sq.child == (pid_t)-1) {
	rpmlog(RPMLOG_ERR, _("Couldn't fork %s: %s\n"), sname, strerror(errno));
	goto exit;
    }

    rpmsqWait(&sq);

    rpmlog(RPMLOG_DEBUG, "%s: waitpid(%d) rc %d status %x\n",
	   sname, (unsigned)sq.child, (unsigned)sq.reaped, sq.status);

    if (sq.reaped < 0) {
	rpmlog(lvl, _("%s scriptlet failed, waitpid(%d) rc %d: %s\n"),
		 sname, sq.child, sq.reaped, strerror(errno));
    } else if (!WIFEXITED(sq.status) || WEXITSTATUS(sq.status)) {
      	if (WIFSIGNALED(sq.status)) {
	    rpmlog(lvl, _("%s scriptlet failed, signal %d\n"),
                   sname, WTERMSIG(sq.status));
	} else {
	    rpmlog(lvl, _("%s scriptlet failed, exit status %d\n"),
		   sname, WEXITSTATUS(sq.status));
	}
    } else {
	/* if we get this far we're clear */
	rc = RPMRC_OK;
    }

exit:
    if (out)
	xx = Fclose(out);	/* XXX dup'd STDOUT_FILENO */

    if (script) {
	if (!rpmIsDebug())
	    xx = unlink(fn);
	fn = _free(fn);
    }
    return rc;
}

rpmRC rpmScriptRun(rpmScript script, int arg1, int arg2,
		   rpmts ts, ARGV_const_t prefixes, int warn_only)
{
    ARGV_t args = NULL;
    rpmlogLvl lvl = warn_only ? RPMLOG_WARNING : RPMLOG_ERR;
    rpmRC rc;

    if (script == NULL) return RPMRC_OK;

    /* construct a new argv as we can't modify the one from header */
    if (script->args) {
	argvAppend(&args, script->args);
    } else {
	argvAdd(&args, "/bin/sh");
    }

    rpmswEnter(rpmtsOp(ts, RPMTS_OP_SCRIPTLETS), 0);
    if (rstreq(args[0], "<lua>")) {
	rc = runLuaScript(ts, prefixes, script->descr, lvl, &args, script->body, arg1, arg2);
    } else {
	rc = runExtScript(ts, prefixes, script->descr, lvl, &args, script->body, arg1, arg2);
    }
    rpmswExit(rpmtsOp(ts, RPMTS_OP_SCRIPTLETS), 0);
    argvFree(args);

    return rc;
}

static rpmTag getProgTag(rpmTag scriptTag)
{
    switch (scriptTag) {
    case RPMTAG_PREIN:		return RPMTAG_PREINPROG;
    case RPMTAG_POSTIN: 	return RPMTAG_POSTINPROG;
    case RPMTAG_PREUN: 		return RPMTAG_PREUNPROG;
    case RPMTAG_POSTUN: 	return RPMTAG_POSTUNPROG;
    case RPMTAG_PRETRANS: 	return RPMTAG_PRETRANSPROG;
    case RPMTAG_POSTTRANS:	return RPMTAG_POSTTRANSPROG;
    case RPMTAG_VERIFYSCRIPT:	return RPMTAG_VERIFYSCRIPTPROG;
    default:			return RPMTAG_NOT_FOUND;
    }
}

static rpmTag getFlagTag(rpmTag scriptTag)
{
    switch (scriptTag) {
    case RPMTAG_PRETRANS:	return RPMTAG_PRETRANSFLAGS;
    case RPMTAG_POSTTRANS:	return RPMTAG_POSTTRANSFLAGS;
    case RPMTAG_PREUN:		return RPMTAG_PREUNFLAGS;
    case RPMTAG_POSTUN:		return RPMTAG_POSTUNFLAGS;
    case RPMTAG_PREIN:		return RPMTAG_PREINFLAGS;
    case RPMTAG_POSTIN:		return RPMTAG_POSTINFLAGS;
    case RPMTAG_VERIFYSCRIPT:	return RPMTAG_VERIFYSCRIPTFLAGS;
    case RPMTAG_TRIGGERSCRIPTS:	return RPMTAG_TRIGGERSCRIPTFLAGS;
    default:
       break;
    }
    return RPMTAG_NOT_FOUND;
}

static const char * tag2sln(rpmTag tag)
{
    switch ((rpm_tag_t) tag) {
    case RPMTAG_PRETRANS:       return "%pretrans";
    case RPMTAG_TRIGGERPREIN:   return "%triggerprein";
    case RPMTAG_PREIN:          return "%pre";
    case RPMTAG_POSTIN:         return "%post";
    case RPMTAG_TRIGGERIN:      return "%triggerin";
    case RPMTAG_TRIGGERUN:      return "%triggerun";
    case RPMTAG_PREUN:          return "%preun";
    case RPMTAG_POSTUN:         return "%postun";
    case RPMTAG_POSTTRANS:      return "%posttrans";
    case RPMTAG_TRIGGERPOSTUN:  return "%triggerpostun";
    case RPMTAG_VERIFYSCRIPT:   return "%verify";
    default: break;
    }
    return "%unknownscript";
}

rpmScript rpmScriptFromTag(Header h, rpmTag scriptTag)
{
    rpmScript script = NULL;
    rpmTag progTag = getProgTag(scriptTag);

    if (headerIsEntry(h, scriptTag) || headerIsEntry(h, progTag)) {
	struct rpmtd_s prog;
	char *nevra = headerGetAsString(h, RPMTAG_NEVRA);
	rpmscriptFlags flags = headerGetNumber(h, getFlagTag(scriptTag));

	script = xcalloc(1, sizeof(*script));
	script->tag = scriptTag;
	rasprintf(&script->descr, "%s(%s)", tag2sln(scriptTag), nevra);
	script->body = headerGetAsString(h, scriptTag);

	/* macros need to be expanded before possible queryformat */
	if (script->body && (flags & RPMSCRIPT_EXPAND)) {
	    char *body = rpmExpand(script->body, NULL);
	    free(script->body);
	    script->body = body;
	}
	if (script->body && (flags & RPMSCRIPT_QFORMAT)) {
	    /* XXX TODO: handle queryformat errors */
	    char *body = headerFormat(h, script->body, NULL);
	    free(script->body);
	    script->body = body;
	}

	if (headerGet(h, progTag, &prog, (HEADERGET_ALLOC|HEADERGET_ARGV))) {
	    script->args = prog.data;
	}
	free(nevra);
    }
    return script;
}

rpmScript rpmScriptFree(rpmScript script)
{
    if (script) {
	free(script->args);
	free(script->body);
	free(script->descr);
	free(script);
    }
    return NULL;
}