summaryrefslogtreecommitdiff
path: root/build/parseReqs.c
blob: ea16a4ef40cf7e04e0a7144e50eb4a18b425f527 (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
/** \ingroup rpmbuild
 * \file build/parseReqs.c
 *  Parse dependency tag from spec file or from auto-dependency generator.
 */

#include "system.h"

#include <ctype.h>
#include <rpm/rpmtypes.h>
#include <rpm/rpmlog.h>
#include "build/rpmbuild_internal.h"
#include "build/rpmbuild_misc.h"
#include "debug.h"


#define	SKIPWHITE(_x)	{while (*(_x) && (risspace(*_x) || *(_x) == ',')) (_x)++;}
#define	SKIPNONWHITE(_x){while (*(_x) &&!(risspace(*_x) || *(_x) == ',')) (_x)++;}

static rpmRC checkSep(const char *s, char c, char **emsg)
{
    const char *sep = strchr(s, c);
    if (sep && strchr(sep + 1, c)) {
	rasprintf(emsg, "Invalid version (double separator '%c'): %s", c, s);
	return RPMRC_FAIL;
    }
    return RPMRC_OK;
}

static rpmRC checkEpoch(const char *s, char **emsg)
{
    const char *si, *sep = strchr(s, ':');

    if (!sep)
	return RPMRC_OK;

    for (si = s; si != sep; si++) {
	if (!risdigit(*si)) {
	    rasprintf(emsg, "Invalid version (epoch must be unsigned integer): %s", s);
	    return RPMRC_FAIL;
	}
    }
    return RPMRC_OK;
}

static rpmRC checkDep(rpmSpec spec, char *N, char *EVR, char **emsg)
{
    /* 
     * Tokens must begin with alphanumeric, _, or /, but we don't know
     * the spec's encoding so we only check what we can: plain ascii.
     */
    if (isascii(N[0]) && !(risalnum(N[0]) || N[0] == '_' || N[0] == '/')) {
        //rasprintf(emsg, _("Dependency tokens must begin with alpha-numeric, '_' or '/'"));
	rpmlog(RPMLOG_INFO,
                   _("line %d: Dependency tokens must begin with alpha-numeric, '_' or '/': %s\n"),
                   spec->lineNum, spec->line);
        //return RPMRC_FAIL;
    }
    if (EVR) {
        if (N[0] == '/') {
            rasprintf(emsg, _("Versioned file name not permitted"));
            return RPMRC_FAIL;
        }
      //  if (rpmCharCheck(spec, EVR, ".-_+:%{}~"))
       //     return RPMRC_FAIL;
	if (checkSep(EVR, '-', emsg) != RPMRC_OK ||
	    checkSep(EVR, ':', emsg) != RPMRC_OK ||
	    checkEpoch(EVR, emsg) != RPMRC_OK) {

	    if (rpmExpandNumeric("%{?_wrong_version_format_terminate_build}"))
		return RPMRC_FAIL;
	}
    }
    return RPMRC_OK;
}

struct parseRCPOTRichData {
    rpmSpec spec;
    StringBuf sb;
};

/* Callback for the rich dependency parser. We use this to do check for invalid
 * characters and to build a normailzed version of the dependency */
static rpmRC parseRCPOTRichCB(void *cbdata, rpmrichParseType type,
		const char *n, int nl, const char *e, int el, rpmsenseFlags sense,
		rpmrichOp op, char **emsg) {
    struct parseRCPOTRichData *data = cbdata;
    StringBuf sb = data->sb;
    rpmRC rc = RPMRC_OK;

    if (type == RPMRICH_PARSE_ENTER) {
	appendStringBuf(sb, "(");
    } else if (type == RPMRICH_PARSE_LEAVE) {
	appendStringBuf(sb, ")");
    } else if (type == RPMRICH_PARSE_SIMPLE) {
	char *N = xmalloc(nl + 1);
	char *EVR = NULL;
	rstrlcpy(N, n, nl + 1);
	appendStringBuf(sb, N);
	if (el) {
	    char rel[6], *rp = rel;
	    EVR = xmalloc(el + 1);
	    rstrlcpy(EVR, e, el + 1);
	    *rp++ = ' ';
	    if (sense & RPMSENSE_LESS)
		*rp++ = '<';
	    if (sense & RPMSENSE_GREATER)
		*rp++ = '>';
	    if (sense & RPMSENSE_EQUAL)
		*rp++ = '=';
	    *rp++ = ' ';
	    *rp = 0;
	    appendStringBuf(sb, rel);
	    appendStringBuf(sb, EVR);
	}
	rc = checkDep(data->spec, N, EVR, emsg);
	_free(N);
	_free(EVR);
    } else if (type == RPMRICH_PARSE_OP) {
	appendStringBuf(sb, " ");
	appendStringBuf(sb, rpmrichOpStr(op));
	appendStringBuf(sb, " ");
    }
    return rc;
}

rpmRC parseRCPOT(rpmSpec spec, Package pkg, const char *field, rpmTagVal tagN,
	       int index, rpmsenseFlags tagflags, addReqProvFunction cb, void *cbdata)
{
	const char *r, *re, *v, *ve;
	char *emsg = NULL;
	char * N = NULL, * EVR = NULL;
	rpmTagVal nametag = RPMTAG_NOT_FOUND;
	rpmsenseFlags Flags;
	rpmRC rc = RPMRC_FAIL; /* assume failure */
	int allow_richdeps = 0;

	if (!cbdata)
	cbdata = pkg;

	switch (tagN) {
	default:
	case RPMTAG_REQUIRENAME:
	tagflags |= RPMSENSE_ANY;
	/* fall through */
	case RPMTAG_RECOMMENDNAME:
	case RPMTAG_SUGGESTNAME:
	case RPMTAG_SUPPLEMENTNAME:
	case RPMTAG_ENHANCENAME:
	case RPMTAG_CONFLICTNAME:
	allow_richdeps = 1;
	/* fall through */
	case RPMTAG_PROVIDENAME:
	case RPMTAG_OBSOLETENAME:
	case RPMTAG_ORDERNAME:
	nametag = tagN;
	break;
	case RPMTAG_PREREQ:
	/* XXX map legacy PreReq into Requires(pre,preun) */
	nametag = RPMTAG_REQUIRENAME;
	tagflags |= (RPMSENSE_SCRIPT_PRE|RPMSENSE_SCRIPT_PREUN);
	allow_richdeps = 1;
	break;
	case RPMTAG_TRIGGERPREIN:
	nametag = RPMTAG_TRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERPREIN;
	break;
	case RPMTAG_TRIGGERIN:
	nametag = RPMTAG_TRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERIN;
	break;
	case RPMTAG_TRIGGERPOSTUN:
	nametag = RPMTAG_TRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERPOSTUN;
	break;
	case RPMTAG_TRIGGERUN:
	nametag = RPMTAG_TRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERUN;
	break;
	case RPMTAG_BUILDPREREQ:
	case RPMTAG_BUILDREQUIRES:
	nametag = RPMTAG_REQUIRENAME;
	tagflags |= RPMSENSE_ANY;
	allow_richdeps = 1;
	break;
	case RPMTAG_BUILDCONFLICTS:
	nametag = RPMTAG_CONFLICTNAME;
	allow_richdeps = 1;
	break;
	case RPMTAG_FILETRIGGERIN:
	nametag = RPMTAG_FILETRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERIN;
	break;
	case RPMTAG_FILETRIGGERUN:
	nametag = RPMTAG_FILETRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERUN;
	break;
	case RPMTAG_FILETRIGGERPOSTUN:
	nametag = RPMTAG_FILETRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERPOSTUN;
	break;
	case RPMTAG_TRANSFILETRIGGERIN:
	nametag = RPMTAG_TRANSFILETRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERIN;
	break;
	case RPMTAG_TRANSFILETRIGGERUN:
	nametag = RPMTAG_TRANSFILETRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERUN;
	break;
	case RPMTAG_TRANSFILETRIGGERPOSTUN:
	nametag = RPMTAG_TRANSFILETRIGGERNAME;
	tagflags |= RPMSENSE_TRIGGERPOSTUN;
	break;
	}

	for (r = field; *r != '\0'; r = re) {
	SKIPWHITE(r);
	if (*r == '\0')
		break;

	Flags = (tagflags & ~RPMSENSE_SENSEMASK);

	if (r[0] == '(') {
		struct parseRCPOTRichData data;
		if (!allow_richdeps) {
		rasprintf(&emsg, _("No rich dependencies allowed for this type"));
		goto exit;
		}
		data.spec = spec;
		data.sb = newStringBuf();
		if (rpmrichParseForTag(&r, &emsg, parseRCPOTRichCB, &data, nametag) != RPMRC_OK) {
		freeStringBuf(data.sb);
		goto exit;
		}
		if (cb && cb(cbdata, nametag, getStringBuf(data.sb), NULL, Flags, index) != RPMRC_OK) {
		rasprintf(&emsg, _("invalid dependency"));
		freeStringBuf(data.sb);
		goto exit;
		}
		freeStringBuf(data.sb);
		re = r;
		continue;
	}

	re = r;
	SKIPNONWHITE(re);
	N = xmalloc((re-r) + 1);
	rstrlcpy(N, r, (re-r) + 1);

	/* Parse EVR */
	EVR = NULL;
	v = re;
	SKIPWHITE(v);
	ve = v;
	SKIPNONWHITE(ve);

	re = v; /* ==> next token (if no EVR found) starts here */

	/* Check for possible logical operator */
	if (ve > v) {
		rpmsenseFlags sense = rpmParseDSFlags(v, ve - v);
		if (sense) {
		Flags |= sense;

		/* now parse EVR */
		v = ve;
		SKIPWHITE(v);
		ve = v;
		SKIPNONWHITE(ve);
		if (*v == '\0' || ve == v) {
			rasprintf(&emsg, _("Version required"));
			goto exit;
		}
		EVR = xmalloc((ve-v) + 1);
		rstrlcpy(EVR, v, (ve-v) + 1);
		re = ve;	/* ==> next token after EVR string starts here */
		}
	}

	/* check that dependency is well-formed */
	if (checkDep(spec, N, EVR, &emsg))
		goto exit;

	if (nametag == RPMTAG_OBSOLETENAME) {
		if (rpmCharCheck(spec, N, WHITELIST_NAME)) {
		rasprintf(&emsg, _("Only package names are allowed in "
				   "Obsoletes"));
		goto exit;
		}
		if (!EVR) {
		rasprintf(&emsg, _("It's not recommended to have "
				   "unversioned Obsoletes"));
		} else if (Flags & RPMSENSE_GREATER) {
		rasprintf(&emsg, _("It's not recommended to use "
				   "'>' in Obsoletes"));
		}
	}

	if (nametag == RPMTAG_FILETRIGGERNAME ||
		nametag == RPMTAG_TRANSFILETRIGGERNAME) {
		if (N[0] != '/') {
		rasprintf(&emsg, _("Only absolute paths are allowed in "
					"file triggers"));
		}
	}


	/* Deny more "normal" triggers fired by the same pakage. File triggers are ok */
	if (nametag == RPMTAG_TRIGGERNAME) {
		rpmds *pdsp = packageDependencies(pkg, nametag);
		rpmds newds = rpmdsSingle(nametag, N, EVR, Flags);
		rpmdsInit(*pdsp);
		while (rpmdsNext(*pdsp) >= 0) {
		if (rpmdsCompare(*pdsp, newds) && (rpmdsFlags(*pdsp) & tagflags )) {
			rasprintf(&emsg, _("Trigger fired by the same package "
			"is already defined in spec file"));
			break;
		}
		}
		rpmdsFree(newds);
		if (emsg)
		goto exit;
	}

	if (cb && cb(cbdata, nametag, N, EVR, Flags, index) != RPMRC_OK) {
		rasprintf(&emsg, _("invalid dependency"));
		goto exit;
	}

	N = _free(N);
	EVR = _free(EVR);

	}
	rc = RPMRC_OK;

exit:
	if (emsg) {
	int lvl = (rc == RPMRC_OK) ? RPMLOG_WARNING : RPMLOG_ERR;
	/* Automatic dependencies don't relate to spec lines */
	if (tagflags & (RPMSENSE_FIND_REQUIRES|RPMSENSE_FIND_PROVIDES)) {
		rpmlog(lvl, "%s: %s\n", emsg, r);
	} else {
		rpmlog(lvl, _("line %d: %s: %s\n"),
		   spec->lineNum, emsg, spec->line);
	}
	free(emsg);
	}
	_free(N);
	_free(EVR);

	return rc;
}