aboutsummaryrefslogtreecommitdiff
path: root/src/gpg.c
blob: a722b4ec92b30ca09a5b5aee3dee64d10f5749bc (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
/* wydawca - FTP release synchronisation daemon
   Copyright (C) 2007 Sergey Poznyakoff

   This program 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.

   This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include "save-cwd.h"
#include <gpgme.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

#define fail_if_err(expr)						      \
  do									      \
    {									      \
      int a = expr;							      \
      if (a)								      \
	{ 								      \
	  logmsg (LOG_ERR, "%s: GPGME error: %s", #expr, gpgme_strerror (a)); \
	  return 1;							      \
	} 								      \
    }                                                                         \
  while (0)

static char *homedir;

static int rmdir_r (const char *name);

static int
recursive_rmdir (const char *name)
{
  int rc;
  DIR *dir;
  struct dirent *ent;

  if (chdir (name))
    {
      logmsg (LOG_ERR, "cannot change to directory %s: %s",
	      name, strerror (errno));
      return 1;
    }

  dir = opendir (".");
  if (!dir)
    {
      logmsg (LOG_ERR, "cannot open directory %s: %s",
	      name, strerror (errno));
      return 1;
    }

  for (rc = 0; rc == 0 && (ent = readdir (dir));)
    {
      struct stat st;

      if (strcmp (ent->d_name, ".") == 0
	  || strcmp (ent->d_name, "..") == 0)
	continue;

      if (stat (ent->d_name, &st) && errno != ENOENT)
	{
	  logmsg (LOG_ERR, "cannot stat file `%s': %s",
		  name, strerror (errno));
	  rc = 1;
	}
      else if (S_ISDIR (st.st_mode))
	rc = rmdir_r (ent->d_name);
      else if ((rc = unlink (ent->d_name)) != 0 && errno != ENOENT)
	logmsg (LOG_ERR, "cannot unlink %s: %s",
		ent->d_name, strerror (errno));
    }
  closedir (dir);
  return rc;
}

static int
rmdir_r (const char *name)
{
  int rc;
  struct saved_cwd cwd;

  if (save_cwd (&cwd))
    {
      logmsg (LOG_ERR, "cannot save current directory: %s",
	      strerror (errno));
      return 1;
    }
  rc = recursive_rmdir (name);
  if (restore_cwd (&cwd))
    {
      logmsg (LOG_ERR, "cannot restore current directory: %s",
	      strerror (errno));
      rc = 1;
    }

  if (rc == 0 && rmdir (name))
    {
      logmsg (LOG_ERR, "cannot remove directory %s: %s",
	      name, strerror (errno));
      return 1;
    }
  
  return rc;
}

static void
remove_homedir ()
{
  if (debug_level > 1)
    logmsg (LOG_DEBUG, "Removing GNUPG home directory: %s", homedir);
  if (rmdir_r (homedir))
    logmsg (LOG_CRIT, "Failed to remove GPG directory %s", homedir);
}

int
wydawca_gpg_homedir ()
{
  if (homedir)
    return;
  
  homedir = xstrdup ("/tmp/wydawca-XXXXXX");
  if (!mkdtemp (homedir))
    {
      logmsg (LOG_CRIT, "cannot create GPG home directory (%s): %s",
	      homedir, strerror (errno));
      return 1;
    }
  atexit (remove_homedir);
  if (debug_level > 1)
    logmsg (LOG_DEBUG, "GNUPG home directory: %s", homedir);
  setenv ("GNUPGHOME", homedir, 1);
}

/* FIXME: dpair currently unused */
int
verify_directive_signature (struct file_register *reg,
			    struct directory_pair *dpair, const char *pubkey)
{
  gpgme_ctx_t ctx;
  gpgme_data_t key_data, directive_data, plain;
  off_t size;
  gpgme_error_t ec;
  int rc;
    
  wydawca_gpg_homedir ();
  fail_if_err (gpgme_new (&ctx));
  fail_if_err (gpgme_data_new_from_mem (&key_data, pubkey, strlen (pubkey),
					0));
  fail_if_err (gpgme_op_import (ctx, key_data));

  fail_if_err (gpgme_data_new_from_file (&directive_data,
					 reg->file[file_directive].name, 1));
  gpgme_data_new (&plain);
  ec = gpgme_op_verify (ctx, directive_data, NULL, plain);
  if (ec == GPG_ERR_NO_ERROR)
    {
      size_t dcount, i;
      char *p;
      
      rc = 0;

      size = gpgme_data_seek (plain, 0, SEEK_END);
      gpgme_data_seek (plain, 0, SEEK_SET);
      reg->blurb = xmalloc (size + 1);
      gpgme_data_read (plain, reg->blurb, size);
      reg->blurb[size] = 0;
      gpgme_data_release (plain);
      
      dcount = 1;
      for (p = reg->blurb; *p; p++)
	if (*p == '\n')
	  dcount++;
      
      reg->directive = xcalloc (dcount, sizeof reg->directive[0]);
      p = reg->blurb;
      for (i = 0; i < dcount; i++)
	{
	  reg->directive[i] = p;
	  p = strchr (p, '\n');
	  if (!p)
	    break;
	  *p++ = 0;
	}
      reg->directive[i] = NULL;
    }
  else
    {
      rc = 1;
      logmsg (LOG_ERR, "%s: directive verification failed: %s",
	      reg->name, gpgme_strerror (ec));
      /* FIXME: Send mail to the project maintainer */
    }

  gpgme_data_release (directive_data);
  gpgme_data_release (key_data);

  return rc;
}

FILE *
start_prog (int argc, char **argv, pid_t *ppid)
{
  int p[2];
  FILE *fp;
  pid_t pid;
  int i;

  pipe (p);
  switch (pid = fork ())
    {
    case 0:
      /* Child process */

      if (p[1] != 1)
	dup2 (p[1], 1);
      if (p[1] != 1)
	dup2 (p[1], 2);
      close (p[0]);

      /* Close unneded descripitors */
      for (i = getmaxfd (); i > 2; i--)
	close (i);

      execv (argv[0], argv);
      logmsg (LOG_CRIT, "cannot run %s: %s", argv[0], strerror (errno));
      exit (1);
      
    case -1:
      logmsg (LOG_CRIT, "cannot run `%s': fork failed: %s",
	      argv[0], strerror (errno));
      return NULL;

    default:
      /* Master process */
      close (p[1]);
      fp = fdopen (p[0], "r");
      if (!fp)
	logmsg (LOG_ERR, "cannot fdopen: %s", strerror (errno));
      *ppid = pid;
    }
  return fp;
}

static void
log_output (int prio, const char *prog, FILE *fp)
{
  size_t size = 0;
  char *buf = NULL;
	  
  logmsg (prio, "%s output follows:", prog);
  while (getline (&buf, &size, fp) > 0)
    logmsg (prio, "%s", buf);
  logmsg (prio, "end of %s output", prog);
  free (buf);
}

int
verify_detached_signature (struct file_register *reg,
			   struct directory_pair *dpair)
{
  gpgme_engine_info_t info;
  const char *gpg_prog;
  char *argv[5];
  FILE *fp;
  pid_t pid, npid;
  int status;
  int i;
  int rc;
  
  fail_if_err (gpgme_get_engine_info (&info));
  while (info && info->protocol != GPGME_PROTOCOL_OpenPGP)
    info = info->next;
  if (!info)
    {
      logmsg (LOG_CRIT,
	      "cannot find path to gpg binary (attempting to verify "
	      "the detached signature for %s", reg->name);
      return 1;
    }
  
  wydawca_gpg_homedir ();
  argv[0] = info->file_name;
  argv[1] = "--verify";
  argv[2] = reg->file[file_signature].name;
  argv[3] = reg->file[file_dist].name;
  argv[4] = NULL;

  fp = start_prog (5, argv, &pid);
  if (!fp)
    {
      logmsg (LOG_CRIT,
	      "cannot verify detached signature for %s", reg->name);
      return 1;
    }
	    
  for (i = 0; i < 5 && (npid = waitpid (pid, &status, WNOHANG)) == 0; i++)
    sleep (1);

  switch (npid)
    {
    case -1:
      logmsg (LOG_CRIT,
	      "cannot verify detached signature for %s: "
	      "waitpid failed: %s",  reg->name, strerror (errno));
      return 1;

    case 0:
      logmsg (LOG_CRIT,
	      "cannot verify detached signature for %s: "
	      "process %s did not respond within 5 seconds: %s",
	      reg->name, argv[0], strerror (errno));
      kill (pid, SIGKILL);
      return 1;
      
    default:
      break;
    }

  if (WIFEXITED (status))
    {
      rc = WEXITSTATUS (status);
      if (rc)
	{
	  size_t size = 0;
	  char *buf = NULL;
	  
	  logmsg (LOG_ERR, "bad detached signature for %s", reg->name);
	  log_output (LOG_ERR, argv[0], fp);
	  /* FIXME: email? */
	}
      else if (debug_level > 1)
	log_output (LOG_DEBUG, argv[0], fp);
    }
  else
    {
      rc = 1;
      if (WIFSIGNALED (status))
	logmsg (LOG_ERR,
		"cannot verify detached signature for %s: "
		"%s terminated on signal %d",
		reg->name, argv[0], WTERMSIG (status));
      else if (WIFSTOPPED (status))
	logmsg (LOG_ERR,
		"cannot verify detached signature for %s: "
		"%s stopped on signal %d",
		reg->name, argv[0], WTERMSIG (status));
      else
	logmsg (LOG_ERR,
		"cannot verify detached signature for %s: "
		"%s terminated with unrecognized status",
		reg->name, argv[0]);
    }
  fclose (fp);
  
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.