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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
#include "system.h"
const char *__progname;
#include <rpm/rpmbuild.h>
#include "build/buildio.h"
#include <rpm/header.h>
#include "lib/rpmlead.h"
#include <err.h> /* XXX !HAVE_ERR_H: get from misc */
#include "debug.h"
typedef enum injmode_e { INJ_UNKNOWN, INJ_ADD, INJ_DELETE, INJ_MODIFY } injmode_t;
injmode_t injmode = INJ_UNKNOWN;
typedef struct cmd_s {
injmode_t injmode;
char * tag;
int32_t tagval;
int done;
int oldcnt;
int nvals;
char ** vals;
} cmd_t;
#define MAXCMDS 40
cmd_t *cmds[MAXCMDS];
int ncmds = 0;
static const char * pr_injmode(injmode_t injmode)
{
switch(injmode) {
case INJ_ADD: return("add");
case INJ_DELETE: return("delete");
case INJ_MODIFY: return("modify");
case INJ_UNKNOWN: return("unknown");
default: return("???");
}
}
enum cvtaction {CA_OLD, CA_NEW, CA_OMIT, CA_ERR};
static enum cvtaction convertAMD(enum cvtaction ca, int32_t type,
void ** nvalsp, int32_t *ncountp, cmd_t *newc)
{
int i;
if (newc == NULL)
return ca;
if (!(nvalsp && ncountp))
return CA_ERR;
*nvalsp = NULL;
*ncountp = 0;
switch (ca) {
case CA_OLD:
case CA_OMIT:
case CA_ERR:
default:
break;
case CA_NEW:
switch (type) {
case RPM_INT32_TYPE:
{ int32_t *intp = xmalloc(newc->nvals * sizeof(*intp));
for (i = 0; i < newc->nvals; i++) {
long ival;
char *end;
end = NULL;
ival = strtol(newc->vals[i], &end, 0);
if (end && *end)
break;
if ((((unsigned long)ival) >> (8*sizeof(*intp))) != 0)
break;
intp[i] = ival;
}
if (i < newc->nvals) {
ca = CA_ERR;
free(intp);
break;
}
*nvalsp = intp;
*ncountp = newc->nvals;
} break;
case RPM_BIN_TYPE: /* icons & signatures */
case RPM_STRING_TYPE:
if (newc->nvals != 1) {
newc->done = 0;
ca = CA_ERR;
break;
}
*nvalsp = xstrdup(newc->vals[0]);
*ncountp = newc->nvals;
break;
case RPM_STRING_ARRAY_TYPE:
{ const char **av = xmalloc((newc->nvals+1) * sizeof(char *));
for (i = 0; i < newc->nvals; i++) {
av[i] = newc->vals[i];
}
av[newc->nvals] = NULL;
*nvalsp = av;
*ncountp = newc->nvals;
} break;
case RPM_NULL_TYPE:
case RPM_CHAR_TYPE:
case RPM_INT8_TYPE: /* arch & os */
case RPM_INT16_TYPE: /* file modes & rdevs */
case RPM_I18NSTRING_TYPE:
default: /* this conversion cannot be performed (yet) */
newc->done = 0;
ca = CA_ERR;
break;
}
break;
}
return ca;
}
static enum cvtaction convertExistingAMD(int32_t tag, int32_t type,
hPTR_t valsp, int32_t *countp, void ** nvalsp, int32_t *ncountp,
cmd_t *cmds[], int ncmds)
{
cmd_t *newc = NULL;
enum cvtaction ca = CA_OLD;
int i;
if (!((tag >= RPMTAG_NAME && tag < RPMTAG_FIRSTFREE_TAG)
|| tag >= RPMTAG_EXTERNAL_TAG))
return ca;
for (i = 0; i < ncmds; i++) {
cmd_t *c;
c = cmds[i];
if (tag != c->tagval)
continue;
if (c->done)
continue;
switch (c->injmode) {
case INJ_ADD:
if (ca != CA_OMIT) {/* old tag was deleted, now adding again */
c->done = -1;
continue;
}
ca = CA_NEW;
newc = c;
c->done = 1;
break;
case INJ_MODIFY: /* XXX for now, this is delete, then add */
if (ca == CA_OMIT) {/* old tag was deleted, can't modify */
c->done = -1;
continue;
}
ca = CA_NEW;
newc = c;
c->done = 1;
break;
case INJ_DELETE:
if (ca == CA_OMIT) {/* old tag was deleted, now deleting again */
c->done = -1;
continue;
}
ca = CA_OMIT;
newc = c;
c->done = 1;
break;
case INJ_UNKNOWN:
default:
c->done = -1;
break;
}
}
if (newc) {
ca = convertAMD(ca, type, nvalsp, ncountp, newc);
switch (ca) {
case CA_OMIT:
case CA_NEW:
newc->oldcnt = *countp;
break;
case CA_OLD:
case CA_ERR:
break;
}
}
return ca;
}
static
Header headerCopyWithConvert(Header h, cmd_t *cmds[], int ncmds)
{
int32_t tag, type, count;
hPTR_t vals;
HeaderIterator headerIter;
Header res = headerNew();
headerIter = headerInitIterator(h);
while (headerNextIterator(headerIter, &tag, &type, &vals, &count)) {
enum cvtaction ca;
void *nvals;
int32_t ncount;
nvals = NULL;
ncount = 0;
ca = convertExistingAMD(tag, type, &vals, &count, &nvals, &ncount, cmds, ncmds);
switch (ca) {
case CA_ERR:
case CA_OLD: /* copy old tag and values to header */
default:
/* Don't copy the old changelog, we'll do that later. */
switch (tag) {
case RPMTAG_CHANGELOGTIME:
case RPMTAG_CHANGELOGNAME:
case RPMTAG_CHANGELOGTEXT:
break;
default:
headerAddEntry(res, tag, type, vals, count);
break;
}
break;
case CA_NEW: /* copy new tag and values to header */
headerAddEntry(res, tag, type, nvals, ncount);
break;
case CA_OMIT: /* delete old tag and values from header */
break;
}
if (type == RPM_STRING_ARRAY_TYPE || type == RPM_I18NSTRING_TYPE)
_free(vals);
if (nvals)
_free(nvals);
}
headerFreeIterator(headerIter);
return res;
}
static char * genChangelog(cmd_t *cmds[], int ncmds)
{
#define MYBUFSIZ (2*BUFSIZ)
char *b, *buf = xmalloc(MYBUFSIZ);
int i;
b = buf;
for (i = 0; i < ncmds; i++) {
cmd_t *c;
if ((c = cmds[i]) == NULL)
continue;
b += sprintf(b, "- %s tag %s(%d)",
pr_injmode(c->injmode), c->tag, c->tagval);
if (c->oldcnt || c->nvals) {
*b++ = '\t';
*b++ = '(';
if (c->oldcnt)
b += sprintf(b, "oldcnt %d", c->oldcnt);
if (c->oldcnt && c->nvals) {
*b++ = ',';
*b++ = ' ';
}
if (c->nvals)
b += sprintf(b, "nvals %d", c->nvals);
*b++ = ')';
}
*b++ = '\n';
}
*b = '\0';
return buf;
}
static int
headerInject(Header *hdrp, cmd_t *cmds[], int ncmds)
{
Header h;
int ec = 0;
int i;
if (!(hdrp && cmds && ncmds > 0))
return -1;
h = headerCopyWithConvert(*hdrp, cmds, ncmds);
for (i = 0; i < ncmds; i++) {
cmd_t *c;
int rc;
if ((c = cmds[i]) == NULL)
continue;
rc = headerIsEntry(h, c->tagval);
if (!rc && !c->done && c->injmode != INJ_DELETE) {
int32_t type, ncount;
void *nvals;
enum cvtaction ca;
type = (c->nvals > 0) ? RPM_STRING_ARRAY_TYPE : RPM_STRING_TYPE;
ca = convertAMD(CA_NEW, type, &nvals, &ncount, c);
if (ca == CA_NEW)
headerAddEntry(h, c->tagval, type, nvals, ncount);
rc = headerIsEntry(h, c->tagval);
}
switch(c->injmode) {
case INJ_ADD:
if (!(rc && c->done > 0)) {
warnx(_("failed to add tag %s"), rpmTagGetName(c->tagval));
ec = 1;
}
break;
case INJ_DELETE:
if (!(!rc && c->done > 0)) {
warnx(_("failed to delete tag %s"), rpmTagGetName(c->tagval));
ec = 1;
}
break;
case INJ_MODIFY:
if (!(rc && c->done > 0)) {
warnx(_("failed to modify tag %s"), rpmTagGetName(c->tagval));
ec = 1;
}
break;
case INJ_UNKNOWN:
default:
ec = 1;
break;
}
/* XXX possibly need strict mode to exit immediately here */
}
if (ec == 0 && *hdrp) {
static char name[512] = "";
static const char *text = NULL;
static int cltags[] = {
RPMTAG_CHANGELOGTIME,
RPMTAG_CHANGELOGNAME,
RPMTAG_CHANGELOGTEXT,
0
};
if (name[0] == '\0')
sprintf(name, "rpminject <%s@%s>", getUname(getuid()), buildHost());
if (text == NULL)
text = genChangelog(cmds, ncmds);
addChangelogEntry(h, *getBuildTime(), name, text);
headerCopyTags(*hdrp, h, cltags);
headerSort(h);
*hdrp = headerFree(*hdrp);
*hdrp = h;
} else {
h = headerFree(h);
}
return ec;
}
/* ========================================================================= */
static int
rewriteRPM(const char *fni, const char *fno, cmd_t *cmds[], int ncmds)
{
Header sigs;
rpmSpec spec;
struct cpioSourceArchive_s csabuf, *csa = &csabuf;
int rc;
csa->cpioArchiveSize = 0;
csa->cpioFdIn = fdNew("init (rewriteRPM)");
csa->cpioList = NULL;
/* Read rpm and (partially) recreate spec/pkg control structures */
if ((rc = readRPM(fni, &spec, &sigs, csa)) != 0)
return rc;
/* Inject new strings into header tags */
if ((rc = headerInject(&spec->packages->header, cmds, ncmds)) != 0)
goto exit;
/* Rewrite the rpm */
if (headerIsSource(spec->packages->header)) {
rc = writeRPM(&spec->packages->header, NULL, fno,
csa, spec->passPhrase, &(spec->cookie));
} else {
rc = writeRPM(&spec->packages->header, NULL, fno,
csa, spec->passPhrase, NULL);
}
exit:
Fclose(csa->cpioFdIn);
return rc;
}
/* ========================================================================= */
static int
do_inject(cmd_t *cmds[], int ncmds, const char *argv[])
{
const char *arg;
int ec = 0;
if (argv == NULL || *argv == NULL) {
/* XXX generate lead/header to stdout */
return 0;
}
while ((arg = *argv++) != NULL) {
char *fni = xmalloc(strlen(arg) + sizeof("-SAVE"));
const char *fno = arg;
strcpy(fni, arg);
strcat(fni, "-SAVE");
unlink(fni);
if (link(fno, fni)) {
warn(_("can't link temp input file %s"), fni);
ec++;
continue;
}
if (rewriteRPM(fni, fno, cmds, ncmds)) {
unlink(fno);
if (rename(fni, fno))
warn(_("can't rename %s to %s"), fni, fno);
ec++;
}
if (fni) free(fni);
}
return ec;
}
static struct poptOption optionsTable[] = {
{ "add", 'a', 0, 0, 'a', NULL, NULL },
{ "del", 'd', 0, 0, 'd', NULL, NULL },
{ "injtags", 'i', 0, 0, 'i', NULL, NULL },
{ "modify", 'm', 0, 0, 'm', NULL, NULL },
{ "tag", 't', POPT_ARG_STRING, 0, 't', NULL, NULL },
{ "value", 'v', POPT_ARG_STRING, 0, 'v', NULL, NULL },
{ NULL, 0, 0, 0, 0, NULL, NULL }
};
int
main(int argc, char *argv[])
{
poptContext optCon;
const char * optArg;
cmd_t *c = NULL;
int arg;
int ec = 0;
injmode_t lastmode = INJ_UNKNOWN;
#if HAVE_MCHECK_H && HAVE_MTRACE
mtrace(); /* Trace malloc only if MALLOC_TRACE=mtrace-output-file. */
#endif
setprogname(argv[0]); /* Retrofit glibc __progname */
#if defined(ENABLE_NLS)
(void)setlocale(LC_ALL, "" );
(void)bindtextdomain(PACKAGE, LOCALEDIR);
(void)textdomain(PACKAGE);
#endif
optCon = poptGetContext("rpminject", argc, (const char **) argv, optionsTable, 0);
#if RPM_USES_POPTREADDEFAULTCONFIG
poptReadDefaultConfig(optCon, 1);
#endif
while ((arg = poptGetNextOpt(optCon)) > 0) {
optArg = poptGetOptArg(optCon);
switch (arg) {
case 'a':
injmode = INJ_ADD;
break;
case 'd':
injmode = INJ_DELETE;
break;
case 'm':
injmode = INJ_MODIFY;
break;
case 't':
if (ncmds == 0 || c == NULL)
errx(EXIT_FAILURE, _("missing inject mode before \"--tag %s\""), optArg);
if (c->tag) {
if (c->injmode != INJ_DELETE &&
(c->nvals <= 0 || c->vals == NULL))
errx(EXIT_FAILURE, _("add/modify inject mode with \"--tag %s\" needs a value"), c->tag);
cmds[ncmds] = c = xcalloc(1, sizeof(cmd_t));
cmds[ncmds]->injmode = cmds[ncmds-1]->injmode;
ncmds++;
}
c->tagval = rpmTagGetValue(optArg);
if (!((c->tagval >= RPMTAG_NAME && c->tagval < RPMTAG_FIRSTFREE_TAG)
|| c->tagval >= RPMTAG_EXTERNAL_TAG))
errx(EXIT_FAILURE, _("unknown rpm tag \"--tag %s\""), optArg);
c->tag = xstrdup(optArg);
break;
case 'v':
if (ncmds == 0 || c == NULL)
errx(EXIT_FAILURE, _("missing inject mode before \"--value %s\""), optArg);
if (c->tag == NULL)
errx(EXIT_FAILURE, _("missing tag name before \"--value %s\""), optArg);
if (c->nvals == 0 || c->vals == NULL) {
c->vals = xcalloc(2, sizeof(char *));
} else {
c->vals = xrealloc(c->vals,
(c->nvals+2)*sizeof(char *));
}
c->vals[c->nvals++] = xstrdup(optArg);
c->vals[c->nvals] = NULL;
break;
case 'i':
rpmDisplayQueryTags(stdout);
exit(EXIT_SUCCESS);
break;
default:
errx(EXIT_FAILURE, _("unknown popt return (%d)"), arg);
break;
}
if (injmode != lastmode) {
cmds[ncmds] = c = xcalloc(1, sizeof(cmd_t));
cmds[ncmds]->injmode = lastmode = injmode;
ncmds++;
}
}
/* XXX I don't want to read rpmrc */
addMacro(NULL, "_tmppath", NULL, "/tmp", RMIL_DEFAULT);
ec = do_inject(cmds, ncmds, poptGetArgs(optCon));
optCon = poptFreeContext(optCon);
return ec;
}
|