aboutsummaryrefslogtreecommitdiff
path: root/src/directive.c
blob: b52c18dcb483bfe1e8042cee2e334ad1fa2c6f35 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2008, 2010 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"

/* Directive file support */

/* Parse directives from TRP->blurb. Fill TRP->directive */
int
directive_parse (struct file_triplet *trp)
{
  size_t dcount, i, j;
  char *p;

  if (debug_level > 2)
    logmsg (LOG_DEBUG, _("%s: parsing directive blurb: %s"),
	    trp->file[file_directive].name, trp->blurb);

  dcount = 0;
  for (p = trp->blurb; *p; p++)
    if (*p == '\n')
      dcount++;

  trp->directive = xcalloc (dcount + 1, sizeof trp->directive[0]);
  p = trp->blurb;
  for (i = j = 0; i < dcount; i++)
    {
      trp->directive[j] = p;
      p = strchr (p, '\n');
      if (p)
	*p++ = 0;
      if (trim (trp->directive[j]) == 0) /* ignore empty lines */
	continue;
      if (strchr (trp->directive[j], ':') == NULL)
	{
	  logmsg (LOG_ERR, _("%s: invalid line: %s"),
		  trp->file[file_directive].name, trp->directive[j]);
	  free (trp->directive);
	  trp->directive = NULL;
	  return 1;
	}
      j++;
      if (!p)
	break;
    }
  trp->directive[j] = NULL;
  return 0;
}

/* If a directive KEY exists in the triplet TRP, return 0 and point PVAL
   (unless it is NULL) to its value. Othervise, return 1. */
int
directive_get_value (struct file_triplet *trp, const char *key,
		     const char **pval)
{
  int keylen = strlen (key);
  int i;

  for (i = 0; trp->directive[i]; i++)
    {
      char *str = trp->directive[i];
      int len = strlen (str);
      if (len > keylen && memcmp (str, key, keylen) == 0 && str[keylen] == ':')
	{
	  str += keylen + 1;
	  while (*str && isspace (*str))
	    str++;
	  if (pval)
	    *pval = str;
	  return 0;
	}
    }
  return 1;
}

/* Auxiliary function for sequential access to directories from TRP.
   Arguments:
     N          - Index of the current directive,
     TRP        - Triplet,
     PKEY, PVAL - Return addresses.

     The function points PKEY and PVAL to the keyword and value of the Nth
     directive, and returns N + 1.

     If N points past all the directive, the function returns 0. */
static int
_directive_seq_get (int n, struct file_triplet *trp,
		    const char **pkey, const char **pval)
{
  char *p;
  size_t len;

  if (trp->directive[n] == NULL)
    return 0;

  p = strchr (trp->directive[n], ':');
  len = p - trp->directive[n];
  if (len + 1 > trp->tmpsize)
    {
      trp->tmpsize = len + 1;
      trp->tmp = x2realloc (trp->tmp, &trp->tmpsize);
    }
  memcpy (trp->tmp, trp->directive[n], len);
  trp->tmp[len] = 0;
  *pkey = trp->tmp;
  for (p++; *p && isspace (*p); p++)
    ;
  if (pval)
    *pval = p;
  return ++n;
}

/* Get the first directive from TRP. Point *PKEY to its keyword and
   *PVAL to its value. Return 1 on success, 0 on failure. */
int
directive_first (struct file_triplet *trp,
		 const char **pkey, const char **pval)
{
  int n = 0;
  return _directive_seq_get (n, trp, pkey, pval);
}

/* Get the first directive from TRP. Point *PKEY to its keyword and
   *PVAL to its value. Return 1 on success, 0 on failure.
   Return non-0 on success, 0 on failure */
int
directive_next (struct file_triplet *trp, int n,
		const char **pkey, const char **pval)
{
  return _directive_seq_get (n, trp, pkey, pval);
}

/* Pack a directive string VAL into an unsigned number */
int
directive_pack_version (const char *val, unsigned *pversion)
{
  char *p;
  unsigned v;

  v = strtoul (val, &p, 10);
  if (*p != '.')
    return 1;
  p++;
  v *= 100;
  v += strtoul (p, &p, 10);
  if (*p && *p != '.')
    return 1;
  *pversion = v;
  return 0;
}

/* Return true if the directory file version of the triplet TRP
   is within the inclusive range FROM and TO (packed) */
int
directive_version_in_range_p (struct file_triplet *trp,
			      unsigned from, unsigned to)
{
  const char *val;
  unsigned version;

  if (directive_get_value (trp, "version", &val))
    {
      logmsg (LOG_ERR, _("%s: missing `version' directive"),
	      trp->file[file_directive].name);
      return 0;
    }

  if (directive_pack_version (val, &version))
    {
      logmsg (LOG_ERR, _("%s: unparsable version: %s"),
	      trp->file[file_directive].name, val);
      return 0;
    }

  if (from <= version && version <= to)
    return 1;

  logmsg (LOG_ERR, _("%s: version %s is not in the allowed range"),
	  trp->file[file_directive].name, val);
  return 0;
}

enum directive
  {
    unknown_dir,
    comment_dir,
    directory_dir,
    version_dir,
    filename_dir,
    rmsymlink_dir,
    archive_dir,
    symlink_dir
  };

struct directive_table
{
  const char *name;
  enum directive dir;
};

static struct directive_table directive_table[] = {
  { "comment", comment_dir },
  { "directory", directory_dir },
  { "version", version_dir },
  { "filename", filename_dir },
  { "symlink", symlink_dir },
  { "rmsymlink", rmsymlink_dir },
  { "archive", archive_dir },
  { NULL }
};

static enum directive
find_directive (const char *key)
{
  int i;

  for (i = 0; directive_table[i].name; i++)
    if (strcmp (directive_table[i].name, key) == 0)
      return directive_table[i].dir;
  return unknown_dir;
}

/* Return 0 if the directory file format of the triplet TRP is OK. */
int
verify_directive_format (struct file_triplet *trp)
{
  int n, dnum;
  const char *key;

  if (!directive_version_in_range_p (trp, MIN_DIRECTIVE_VERSION,
				     MAX_DIRECTIVE_VERSION))
    return 1;

  dnum = 0;
  for (n = directive_first (trp, &key, NULL); n;
       n = directive_next (trp, n, &key, NULL))
    {
      if (strcmp (key, "comment") == 0)
	continue;
      dnum++;
      switch (dnum)
	{
	case 1:
	  if (strcmp (key, "version"))
	    {
	      logmsg (LOG_ERR, _("%s:%d: expected `%s' but found `%s'"),
		      trp->file[file_directive].name, n, "version", key);
	      return 1;
	    }
	  break;

	case 2:
	  if (strcmp (key, "directory"))
	    {
	      logmsg (LOG_ERR, _("%s:%d: expected `%s' but found `%s'"),
		      trp->file[file_directive].name, n, "directory", key);
	      return 1;
	    }
	  break;

	default:
	  if (find_directive (key) == unknown_dir)
	    {
	      logmsg (LOG_ERR, _("%s:%d: unknown directive `%s'"),
		      trp->file[file_directive].name, n, key);
	      return 1;
	    }
	}
    }

  if (trp->file[file_dist].name && trp->file[file_signature].name)
    {
      const char *filename;
      if (directive_get_value (trp, "filename", &filename))
	{
	  logmsg (LOG_ERR, _("%s: missing `filename' directive"),
		  trp->file[file_directive].name);
	  return 1;
	}
      if (strcmp (filename, trp->file[file_dist].name))
	{
	  logmsg (LOG_ERR, _("%s: filename %s does not match actual name"),
		  trp->file[file_dist].name, filename);
	  return 1;
	}
    }
  return 0;
}

/* Process the directives from TRP, using given SPOOL */
int
process_directives (struct file_triplet *trp, const struct spool *spool)
{
  int rc, n;
  const char *key, *val;
  char *relative_dir;

  UPDATE_STATS (STAT_COMPLETE_TRIPLETS);
  timer_start ("triplet");
  report_init ();
  for (n = directive_first (trp, &key, &val); n;
       n = directive_next (trp, n, &key, &val))
    {
      enum directive d = find_directive (key);
      switch (d)
	{
	case unknown_dir:
	  /* should not happen */
	  abort ();

	case comment_dir:
	  logmsg (LOG_NOTICE, _("%s: COMMENT: %s"),
		  trp->file[file_directive].name, val);
	  break;

	case directory_dir:
	  relative_dir = safe_file_name_alloc (val);
	  if (!relative_dir || relative_dir[0] == '/')
	    {
	      logmsg (LOG_ERR, _("%s: invalid directory: %s"),
	              trp->file[file_directive].name, val);
              return 1;
            }
	  break;

	case filename_dir:
	  rc = verify_detached_signature (trp, spool);
	  if (rc == 0)
	    {
	      if (move_file (trp, spool, file_dist, relative_dir)
		  || move_file (trp, spool, file_signature, relative_dir))
		return 1;
	    }
	  else
	    {
	      logmsg (LOG_ERR, _("invalid detached signature for %s"),
	              trp->name);
	      return 1;
	    }
	  break;

	case version_dir:
	  /* Already processed */
	  break;

	case archive_dir:
	  if (archive_file (trp, spool, relative_dir, val))
	    return 1;
	  break;

	case symlink_dir:
	  {
	    int rc = 0;
	    struct wordsplit ws;

	    if (wordsplit (val, &ws, WRDSF_DEFFLAGS))
	      {
		logmsg (LOG_ERR, _("cannot parse symlink value `%s'"),
			val);
		return 1;
	      }

	    if (ws.ws_wordc != 2)
	      {
		rc = 1;
		logmsg (LOG_ERR,
			_("wrong number of arguments to %s directive: `%s'"),
			key, val);
	      }
	    else
	      rc = symlink_file (trp, spool, relative_dir,
				 ws.ws_wordv[0], ws.ws_wordv[1]);

	    wordsplit_free (&ws);
	    if (rc)
	      return 1;
	  }
	  break;

	case rmsymlink_dir:
	  if (rmsymlink_file (trp, spool, relative_dir, val))
	    return 1;
	}
    }

  if (!dry_run_mode && unlink (trp->file[file_directive].name))
    {
      logmsg (LOG_CRIT, _("%s: cannot unlink directive file: %s"),
	      trp->file[file_directive].name, strerror (errno));
    }

  free (relative_dir);
  UPDATE_STATS (STAT_TRIPLET_SUCCESS);
  report_finish ();
  timer_stop ("triplet");
  notify (spool->notification, trp, ev_success);
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.