1 /* loadswdb.c - Load the swdb file from versions.gnupg.org
2 * Copyright (C) 2016 g10 Code GmbH
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
27 #include "../common/ccparray.h"
28 #include "../common/exectool.h"
30 #include "ks-engine.h"
33 /* Get the time from the current swdb file and store it at R_FILEDATE
34 * and R_VERIFIED. If the file does not exist 0 is stored at there.
35 * The function returns 0 on sucess or an error code. */
37 time_of_saved_swdb (const char *fname, time_t *r_filedate, time_t *r_verified)
42 size_t length_of_line = 0;
47 time_t filedate = (time_t)(-1);
48 time_t verified = (time_t)(-1);
53 fp = es_fopen (fname, "r");
54 err = fp? 0 : gpg_error_from_syserror ();
57 if (gpg_err_code (err) == GPG_ERR_ENOENT)
58 err = 0; /* No file - assume time is the year of Unix. */
62 /* Note that the parser uses the first occurance of a matching
63 * values and ignores possible duplicated values. */
64 maxlen = 2048; /* Set limit. */
65 while ((len = es_read_line (fp, &line, &length_of_line, &maxlen)) > 0)
69 err = gpg_error (GPG_ERR_LINE_TOO_LONG);
72 /* Strip newline and carriage return, if present. */
73 while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
76 if (split_fields (line, fields, DIM (fields)) < DIM(fields))
77 continue; /* Skip empty lines and names w/o a value. */
78 if (*fields[0] == '#')
79 continue; /* Skip comments. */
81 /* Record the meta data. */
82 if (filedate == (time_t)(-1) && !strcmp (fields[0], ".filedate"))
84 if (string2isotime (isot, fields[1]))
85 filedate = isotime2epoch (isot);
87 else if (verified == (time_t)(-1) && !strcmp (fields[0], ".verified"))
89 if (string2isotime (isot, fields[1]))
90 verified = isotime2epoch (isot);
93 if (len < 0 || es_ferror (fp))
95 err = gpg_error_from_syserror ();
98 if (filedate == (time_t)(-1) || verified == (time_t)(-1))
100 err = gpg_error (GPG_ERR_INV_TIME);
104 *r_filedate = filedate;
105 *r_verified = verified;
109 log_error (_("error reading '%s': %s\n"), fname, gpg_strerror (err));
117 /* Read a file from URL and return it as an estream memory buffer at
120 fetch_file (ctrl_t ctrl, const char *url, estream_t *r_fp)
124 estream_t httpfp = NULL;
125 size_t nread, nwritten;
128 if ((err = ks_http_fetch (ctrl, url, &httpfp)))
131 /* We now read the data from the web server into a memory buffer.
132 * To avoid excessive memory use in case of a ill behaving server we
133 * put a 64 k size limit on the buffer. As of today the actual size
134 * of the swdb.lst file is 3k. */
135 fp = es_fopenmem (64*1024, "rw");
138 err = gpg_error_from_syserror ();
139 log_error ("error allocating memory buffer: %s\n", gpg_strerror (err));
145 if (es_read (httpfp, buffer, sizeof buffer, &nread))
147 err = gpg_error_from_syserror ();
148 log_error ("error reading '%s': %s\n",
149 es_fname_get (httpfp), gpg_strerror (err));
155 if (es_write (fp, buffer, nread, &nwritten))
157 err = gpg_error_from_syserror ();
158 log_error ("error writing '%s': %s\n",
159 es_fname_get (fp), gpg_strerror (err));
162 else if (nread != nwritten)
164 err = gpg_error (GPG_ERR_EIO);
165 log_error ("error writing '%s': %s\n",
166 es_fname_get (fp), "short write");
182 /* Communication object for verify_status_cb. */
183 struct verify_status_parm_s
190 verify_status_cb (void *opaque, const char *keyword, char *args)
192 struct verify_status_parm_s *parm = opaque;
194 /* We care only about the first valid signature. */
195 if (!strcmp (keyword, "VALIDSIG") && !parm->anyvalid)
200 if (split_fields (args, fields, DIM (fields)) >= 3)
201 parm->sigtime = parse_timestamp (fields[2], NULL);
207 /* Load the swdb file into the current home directory. Do this onlky
208 * when needed unless FORCE is set which will always get a new
211 dirmngr_load_swdb (ctrl_t ctrl, int force)
214 char *fname = NULL; /* The swdb.lst file. */
215 char *tmp_fname = NULL; /* The temporary swdb.lst file. */
216 char *keyfile_fname = NULL;
217 estream_t swdb = NULL;
218 estream_t swdb_sig = NULL;
220 const char **argv = NULL;
221 struct verify_status_parm_s verify_status_parm = { (time_t)(-1), 0 };
222 estream_t outfp = NULL;
223 time_t now = gnupg_get_time ();
224 time_t filedate = 0; /* ".filedate" from our swdb. */
225 time_t verified = 0; /* ".verified" from our swdb. */
226 gnupg_isotime_t isotime;
229 fname = make_filename_try (gnupg_homedir (), "swdb.lst", NULL);
232 err = gpg_error_from_syserror ();
236 /* Check whether there is a need to get an update. */
239 static int not_older_than;
240 static time_t lastcheck;
244 /* To balance access to the server we use a random time from
245 * 5 to 7 days for update checks. */
246 not_older_than = 5 * 86400;
247 not_older_than += (get_uint_nonce () % (2*86400));
250 if (now - lastcheck < 3600)
252 /* We checked our swdb file in the last hour - don't check
253 * again to avoid unnecessary disk access. */
259 err = time_of_saved_swdb (fname, &filedate, &verified);
260 if (gpg_err_code (err) == GPG_ERR_INV_TIME)
261 err = 0; /* Force reading. */
265 goto leave; /* Current or newer. */
266 if (now - filedate < not_older_than)
267 goto leave; /* Our copy is pretty new (not older than 7 days). */
268 if (verified > now && now - verified < 3*3600)
269 goto leave; /* We downloaded and verified in the last 3 hours. */
272 /* Create the filename of the file with the keys. */
273 keyfile_fname = make_filename_try (gnupg_datadir (), "distsigkey.gpg", NULL);
276 err = gpg_error_from_syserror ();
280 /* Fetch the swdb from the web. */
281 err = fetch_file (ctrl, "https://versions.gnupg.org/swdb.lst", &swdb);
284 err = fetch_file (ctrl, "https://versions.gnupg.org/swdb.lst.sig", &swdb_sig);
289 ccparray_init (&ccp, 0);
290 ccparray_put (&ccp, "--enable-special-filenames");
291 ccparray_put (&ccp, "--status-fd=2");
292 ccparray_put (&ccp, "--keyring");
293 ccparray_put (&ccp, keyfile_fname);
294 ccparray_put (&ccp, "--");
295 ccparray_put (&ccp, "-&@INEXTRA@");
296 ccparray_put (&ccp, "-");
297 ccparray_put (&ccp, NULL);
298 argv = ccparray_get (&ccp, NULL);
301 err = gpg_error_from_syserror ();
305 err = gnupg_exec_tool_stream (gnupg_module_name (GNUPG_MODULE_NAME_GPGV),
306 argv, swdb, swdb_sig, NULL,
307 verify_status_cb, &verify_status_parm);
308 if (!err && verify_status_parm.sigtime == (time_t)(-1))
309 err = gpg_error (verify_status_parm.anyvalid? GPG_ERR_BAD_SIGNATURE
310 /**/ : GPG_ERR_INV_TIME );
314 /* If our swdb is not older than the downloaded one. We don't
315 * bother to update. */
316 if (!force && filedate >= verify_status_parm.sigtime)
319 /* Create a file name for a temporary file in the home directory.
320 * We will later rename that file to the real name. */
324 #ifdef HAVE_W32_SYSTEM
325 tmpstr = es_bsprintf ("tmp-%u-swdb", (unsigned int)getpid ());
327 tmpstr = es_bsprintf (".#%u.swdb", (unsigned int)getpid ());
331 err = gpg_error_from_syserror ();
334 tmp_fname = make_filename_try (gnupg_homedir (), tmpstr, NULL);
338 err = gpg_error_from_syserror ();
343 outfp = es_fopen (tmp_fname, "w");
346 err = gpg_error_from_syserror ();
347 log_error (_("error creating '%s': %s\n"), tmp_fname, gpg_strerror (err));
351 epoch2isotime (isotime, verify_status_parm.sigtime);
352 es_fprintf (outfp, ".filedate %s\n", isotime);
353 epoch2isotime (isotime, now);
354 es_fprintf (outfp, ".verified %s\n", isotime);
356 if (es_fseek (swdb, 0, SEEK_SET))
358 err = gpg_error_from_syserror ();
362 err = copy_stream (swdb, outfp);
365 /* Well, it might also be a reading error, but that is pretty
366 * unlikely for a memory stream. */
367 log_error (_("error writing '%s': %s\n"), tmp_fname, gpg_strerror (err));
371 if (es_fclose (outfp))
373 err = gpg_error_from_syserror ();
374 log_error (_("error writing '%s': %s\n"), tmp_fname, gpg_strerror (err));
379 err = gnupg_rename_file (tmp_fname, fname, NULL);
389 gnupg_remove (tmp_fname); /* This is a temporary file. */
391 es_fclose (swdb_sig);
393 xfree (keyfile_fname);