aboutsummaryrefslogtreecommitdiff
path: root/src/verify.c
blob: 01c61ea583bf00b5e46efd6d41109f45027f3ae2 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2008, 2009, 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"
#include "sql.h"

/* Return the length of the string without trailing whitespace */
size_t
trim_length (const char *str)
{
  size_t len;

  for (len = strlen (str); len > 0 && isspace (str[len-1]); len--)
    ;
  return len;
}

/* Chop off any trailing whitespace. Return the length of the resulting string.
 */
size_t
trim (char *str)
{
  size_t len = trim_length (str);
  str[len] = 0;
  return len;
}

#define MSG_BEGIN_MARKER_STR "-----BEGIN PGP SIGNED MESSAGE-----\n"
#define MSG_BEGIN_MARKER_LEN (sizeof (MSG_BEGIN_MARKER_STR) - 1)

#define SIG_BEGIN_MARKER_STR "-----BEGIN PGP SIGNATURE-----\n"
#define SIG_BEGIN_MARKER_LEN (sizeof (SIG_BEGIN_MARKER_STR) - 1)

static int
extract_plaintext (char *blurb)
{
  char *start, *p;
  
  if (memcmp (blurb, MSG_BEGIN_MARKER_STR, MSG_BEGIN_MARKER_LEN))
    return 1;

  p = blurb + MSG_BEGIN_MARKER_LEN;
  while (*p)
    {
      if (*p == '\n')
	{
	  p++;
	  break;
	}

      p = strchr (p, '\n');
      if (!p)
	return 1;
      p++;
    }

  if (!*p)
    return 1;

  start = p;
  while (*p)
    {
      if (strncmp (p, SIG_BEGIN_MARKER_STR, SIG_BEGIN_MARKER_LEN) == 0)
	{
	  *p++ = 0;
	  memmove (blurb, start, p - start);
	  return 0;
	}
      p = strchr (p, '\n');
      if (!p)
	return 1;
      p++;
    }
  return 1;
}

int
fill_project_name (struct file_triplet *trp)
{
  char *blurb;
  size_t size;
  FILE *fp;
  char *project, *p;
  const char *directory; 
  int rc;
  
  size = trp->file[file_directive].sb.st_size;
  if (size <= MSG_BEGIN_MARKER_LEN)
    {
      logmsg (LOG_ERR, _("too small directive file %s"),
	      trp->file[file_directive].name);
      return 1;
    }
      
  fp = fopen (trp->file[file_directive].name, "r");
  if (!fp)
    {
      logmsg (LOG_ERR, _("cannot open file %s: %s"),
	      trp->file[file_directive].name, strerror (errno));
      return 1;
    }

  blurb = xmalloc (size + 1);

  rc = fread (blurb, size, 1, fp);
  fclose (fp);
  
  if (rc != 1)
    {
      logmsg (LOG_ERR, _("error reading file %s: %s"),
	      trp->file[file_directive].name, strerror (errno));
      free (blurb);
      return 1;
    }
  
  blurb[size] = 0;
  if (extract_plaintext (blurb))
    {
      logmsg (LOG_ERR, _("%s: unrecognized format"),
	      trp->file[file_directive].name);
      free (blurb);
      return 1;
    }

  trp->blurb = blurb;

  if (directive_parse (trp))
    {
      free (blurb);
      trp->blurb = NULL;
      return 1;
    }
  
  if (directive_get_value (trp, "directory", &directory))
    {
      logmsg (LOG_ERR, _("%s: missing `directory' directive"),
	      trp->file[file_directive].name);
      return 1;
    }
  p = strchr (directory, '/');
  if (p)
    {
      size_t len = p - directory;
      if (len == 0)
	{
	  logmsg (LOG_ERR, _("%s: empty `directory' directive"),
		  trp->file[file_directive].name);
	  return 1;
	}
      project = xmalloc (len + 1);
      memcpy (project, directory, len);
      project[len] = 0;
    }
  else
    project = xstrdup (directory);
  trp->project = xstrdup (project);
  return 0;
}

struct uploader_info *
new_uploader_info (struct uploader_info *src)
{
  struct uploader_info *p = xmalloc (sizeof (*p));
  p->next = NULL;
  p->name = src->name;
  p->realname = src->realname;
  p->gpg_key = src->gpg_key;
  p->email = src->email;
  p->fpr = NULL;
  return p;
}

struct uploader_info *
uploader_find_frp (struct uploader_info *list, const char *fpr)
{
  for (; list; list = list->next)
    if (list->fpr && strcmp (list->fpr, fpr) == 0)
      break;
  return list;
}

int
verify_directive_file (struct file_triplet *trp)
{
  char *command;
  int rc;
  void *md;
  size_t nrows, ncols, i;
  struct uploader_info *head, *tail;
  const struct spool *spool;
  struct dictionary *dict;

  ASGN_SPOOL (spool, trp, return 1);
  dict = spool->dictionary[project_uploader_dict];
  
  if (!trp->file[file_directive].name)
    return 1;

  if (fill_project_name (trp))
    return 1;
  
  md = dictionary_open (dict);
  if (!md)
    return 1;

  command = triplet_expand_dictionary_query (dict, md, trp);

  rc = dictionary_lookup (dict, md, command);
  free (command);
  if (rc)
    {
      logmsg (LOG_ERR, _("cannot get uploaders for %s"), trp->name);
      dictionary_close (dict, md);
      return 1;
    }

  nrows = dictionary_num_rows (dict);
  if (nrows == 0)
    {
      logmsg (LOG_ERR, _("found no uploaders for %s"), trp->name);
      dictionary_close (dict, md);
      return 1;
    }

  ncols = dictionary_num_cols (dict);
  if (ncols < 4)
    {
      logmsg (LOG_ERR,
	      _("project-uploader dictionary error: too few columns (%lu)"),
	      (unsigned long) ncols);
      dictionary_close (dict, md);
      return 1;
    }

  head = tail = NULL;
  for (i = 0; i < nrows; i++)
    {
      const char *p;
      struct uploader_info info, *ptr;
      
      memset (&info, 0, sizeof (info));
      p = dictionary_result (dict, md, i, 0);
      if (p)
	info.name = xstrdup (p);
      p = dictionary_result (dict, md, i, 1);
      if (p)
	info.realname = xstrdup (p);
      p = dictionary_result (dict, md, i, 2);
      if (p)
	info.email = xstrdup (p);
      p = dictionary_result (dict, md, i, 3);
      if (p)
	info.gpg_key = xstrdup (p);
      
      if (debug_level > 3)
	{
	  logmsg (LOG_DEBUG, _("name: %s"), SP (info.name));
	  logmsg (LOG_DEBUG, _("realname: %s"), SP (info.realname));
	  logmsg (LOG_DEBUG, _("gpg-key: %s"), SP (info.gpg_key));
	  logmsg (LOG_DEBUG, _("email: %s"), SP (info.email));
	}
      
      if (!info.name || !info.realname || !info.gpg_key || !info.email)
	{
	  logmsg (LOG_ERR,
		  _("project-uploader dictionary error: malformed row %lu"),
		  (unsigned long) i);
	  free (info.name);
	  free (info.realname);
	  free (info.gpg_key);
	  free (info.email);
	  continue;
	}

      ptr = new_uploader_info (&info);
      if (tail)
	tail->next = ptr;
      else
	head = ptr;
      tail = ptr;
    }
	  
  dictionary_close (dict, md);

  if (!head)
    {
      logmsg (LOG_ERR, _("no valid uploaders found for %s"), trp->name);
      return 1;
    }

  trp->uploader_list = head;
  trp->uploader = NULL;

  if (verify_directive_signature (trp))
    {
      /*FIXME: Update stats */
      logmsg (LOG_ERR, _("invalid signature for %s"),
	      trp->name ? trp->name : "[unknown]");
      return 1;
    }
  else if (debug_level)
    logmsg (LOG_DEBUG, _("%s: directive file signature OK"), trp->name);

  if (debug_level > 1)
    {
      int i;
      for (i = 0; trp->directive[i]; i++)
	logmsg (LOG_DEBUG, "directive[%d] = %s", i, trp->directive[i]);
    }

  if (verify_directive_format (trp))
    return 1;

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.