~ubuntu-branches/ubuntu/natty/gnupg2/natty

« back to all changes in this revision

Viewing changes to kbx/keybox-update.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Dorland
  • Date: 2009-03-08 22:46:47 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090308224647-gq17gatcl71lrc2k
Tags: 2.0.11-1
* New upstream release. (Closes: #496663)
* debian/control: Make the description a little more distinctive than
  gnupg v1's. Thanks Jari Aalto. (Closes: #496323)

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
  /* Here is another Windoze bug?:
76
76
   * you cant rename("pubring.kbx.tmp", "pubring.kbx");
77
77
   * but        rename("pubring.kbx.tmp", "pubring.aaa");
78
 
   * works.  So we replace .kbx by .bak or .tmp
 
78
   * works.  So we replace ".kbx" by ".kb_" or ".k__".  Note that we
 
79
   * can't use ".bak" and ".tmp", because these suffixes are used by
 
80
   * gpg and would lead to a sharing violation or data corruption.
79
81
   */
80
82
  if (strlen (template) > 4
81
83
      && !strcmp (template+strlen(template)-4, EXTSEP_S "kbx") )
82
84
    {
83
85
      bakfname = xtrymalloc (strlen (template) + 1);
84
86
      if (!bakfname)
85
 
        return gpg_error (gpg_err_code_from_errno (errno));
 
87
        return gpg_error_from_syserror ();
86
88
      strcpy (bakfname, template);
87
 
      strcpy (bakfname+strlen(template)-4, EXTSEP_S "bak");
 
89
      strcpy (bakfname+strlen(template)-4, EXTSEP_S "kb_");
88
90
      
89
91
      tmpfname = xtrymalloc (strlen (template) + 1);
90
92
      if (!tmpfname)
91
93
        {
92
 
          gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
 
94
          gpg_error_t tmperr = gpg_error_from_syserror ();
93
95
          xfree (bakfname);
94
96
          return tmperr;
95
97
        }
96
98
      strcpy (tmpfname,template);
97
 
      strcpy (tmpfname + strlen (template)-4, EXTSEP_S "tmp");
 
99
      strcpy (tmpfname + strlen (template)-4, EXTSEP_S "k__");
98
100
    }
99
101
  else 
100
 
    { /* File does not end with kbx; hmmm. */
 
102
    { /* File does not end with kbx, thus we hope we are working on a
 
103
         modern file system and appending a suffix works. */
101
104
      bakfname = xtrymalloc ( strlen (template) + 5);
102
105
      if (!bakfname)
103
 
        return gpg_error (gpg_err_code_from_errno (errno));
104
 
      strcpy (stpcpy (bakfname, template), EXTSEP_S "bak");
 
106
        return gpg_error_from_syserror ();
 
107
      strcpy (stpcpy (bakfname, template), EXTSEP_S "kb_");
105
108
      
106
109
      tmpfname = xtrymalloc ( strlen (template) + 5);
107
110
      if (!tmpfname)
108
111
        {
109
 
          gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
 
112
          gpg_error_t tmperr = gpg_error_from_syserror ();
110
113
          xfree (bakfname);
111
114
          return tmperr;
112
115
        }
113
 
      strcpy (stpcpy (tmpfname, template), EXTSEP_S "tmp");
 
116
      strcpy (stpcpy (tmpfname, template), EXTSEP_S "k__");
114
117
    }
115
118
# else /* Posix file names */
116
119
  bakfname = xtrymalloc (strlen (template) + 2);
117
120
  if (!bakfname)
118
 
    return gpg_error (gpg_err_code_from_errno (errno));
 
121
    return gpg_error_from_syserror ();
119
122
  strcpy (stpcpy (bakfname,template),"~");
120
123
  
121
124
  tmpfname = xtrymalloc ( strlen (template) + 5);
122
125
  if (!tmpfname)
123
126
    {
124
 
      gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
 
127
      gpg_error_t tmperr = gpg_error_from_syserror ();
125
128
      xfree (bakfname);
126
129
      return tmperr;
127
130
    }
131
134
  *r_fp = fopen (tmpfname, "wb");
132
135
  if (!*r_fp)
133
136
    {
134
 
      gpg_error_t tmperr = gpg_error (gpg_err_code_from_errno (errno));
 
137
      gpg_error_t tmperr = gpg_error_from_syserror ();
135
138
      xfree (tmpfname);
136
139
      xfree (bakfname);
137
140
      return tmperr;
138
141
    }
139
 
  
 
142
 
140
143
  *r_bakfname = bakfname;
141
144
  *r_tmpfname = tmpfname;
142
145
  return 0;
167
170
/*    iobuf_ioctl (NULL, 2, 0, (char*)bakfname ); */
168
171
/*    iobuf_ioctl (NULL, 2, 0, (char*)fname ); */
169
172
 
170
 
  /* first make a backup file except for secret keyboxs */
 
173
  /* First make a backup file except for secret keyboxes. */
171
174
  if (!secret)
172
175
    { 
173
176
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
175
178
#endif
176
179
      if (rename (fname, bakfname) )
177
180
        {
178
 
          return gpg_error (gpg_err_code_from_errno (errno));
 
181
          return gpg_error_from_syserror ();
179
182
        }
180
183
    }
181
184
  
182
 
  /* then rename the file */
 
185
  /* Then rename the file. */
183
186
#if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
184
187
  remove (fname);
185
188
#endif
186
189
  if (rename (tmpfname, fname) )
187
190
    {
188
 
      rc = gpg_error (gpg_err_code_from_errno (errno));
 
191
      rc = gpg_error_from_syserror ();
189
192
      if (secret)
190
193
        {
191
194
/*            log_info ("WARNING: 2 files with confidential" */
209
212
*/
210
213
static int
211
214
blob_filecopy (int mode, const char *fname, KEYBOXBLOB blob, 
212
 
               int secret, off_t start_offset, unsigned int n_packets )
 
215
               int secret, off_t start_offset)
213
216
{
214
217
  FILE *fp, *newfp;
215
218
  int rc=0;
221
224
  /* Open the source file. Because we do a rename, we have to check the 
222
225
     permissions of the file */
223
226
  if (access (fname, W_OK))
224
 
    return gpg_error (gpg_err_code_from_errno (errno));
 
227
    return gpg_error_from_syserror ();
225
228
 
226
229
  fp = fopen (fname, "rb");
227
230
  if (mode == 1 && !fp && errno == ENOENT)
230
233
         Create a new keybox file. */
231
234
      newfp = fopen (fname, "wb");
232
235
      if (!newfp )
233
 
        return gpg_error (gpg_err_code_from_errno (errno));
 
236
        return gpg_error_from_syserror ();
234
237
 
235
238
      rc = _keybox_write_header_blob (newfp);
236
239
      if (rc)
241
244
        return rc;
242
245
 
243
246
      if ( fclose (newfp) )
244
 
        return gpg_error (gpg_err_code_from_errno (errno));
 
247
        return gpg_error_from_syserror ();
245
248
 
246
249
/*        if (chmod( fname, S_IRUSR | S_IWUSR )) */
247
250
/*          { */
253
256
 
254
257
  if (!fp)
255
258
    {
256
 
      rc = gpg_error (gpg_err_code_from_errno (errno));
 
259
      rc = gpg_error_from_syserror ();
257
260
      goto leave;
258
261
    }
259
262
 
273
276
        {
274
277
          if (fwrite (buffer, nread, 1, newfp) != 1)
275
278
            {
276
 
              rc = gpg_error (gpg_err_code_from_errno (errno));
 
279
              rc = gpg_error_from_syserror ();
277
280
              goto leave;
278
281
            }
279
282
        }
280
283
      if (ferror (fp))
281
284
        {
282
 
          rc = gpg_error (gpg_err_code_from_errno (errno));
 
285
          rc = gpg_error_from_syserror ();
283
286
          goto leave;
284
287
        }
285
288
    }
302
305
          
303
306
          if (fwrite (buffer, nread, 1, newfp) != 1)
304
307
            {
305
 
              rc = gpg_error (gpg_err_code_from_errno (errno));
 
308
              rc = gpg_error_from_syserror ();
306
309
              goto leave;
307
310
            }
308
311
        }
309
312
      if (ferror (fp))
310
313
        {
311
 
          rc = gpg_error (gpg_err_code_from_errno (errno));
 
314
          rc = gpg_error_from_syserror ();
312
315
          goto leave;
313
316
        }
314
317
      
333
336
        {
334
337
          if (fwrite (buffer, nread, 1, newfp) != 1)
335
338
            {
336
 
              rc = gpg_error (gpg_err_code_from_errno (errno));
 
339
              rc = gpg_error_from_syserror ();
337
340
              goto leave;
338
341
            }
339
342
        }
340
343
      if (ferror (fp))
341
344
        {
342
 
          rc = gpg_error (gpg_err_code_from_errno (errno));
 
345
          rc = gpg_error_from_syserror ();
343
346
          goto leave;
344
347
        }
345
348
    }
347
350
  /* Close both files. */
348
351
  if (fclose(fp))
349
352
    {
350
 
      rc = gpg_error (gpg_err_code_from_errno (errno));
 
353
      rc = gpg_error_from_syserror ();
351
354
      fclose (newfp);
352
355
      goto leave;
353
356
    }
354
357
  if (fclose(newfp))
355
358
    {
356
 
      rc = gpg_error (gpg_err_code_from_errno (errno));
 
359
      rc = gpg_error_from_syserror ();
357
360
      goto leave;
358
361
    }
359
362
 
386
389
 
387
390
  /* Close this one otherwise we will mess up the position for a next
388
391
     search.  Fixme: it would be better to adjust the position after
389
 
     the write opertions.  */
390
 
  if (hd->fp)
391
 
    {
392
 
      fclose (hd->fp);
393
 
      hd->fp = NULL;
394
 
    }
 
392
     the write operation.  */
 
393
  _keybox_close_file (hd);
395
394
 
396
395
  rc = _keybox_create_x509_blob (&blob, cert, sha1_digest, hd->ephemeral);
397
396
  if (!rc)
398
397
    {
399
 
      rc = blob_filecopy (1, fname, blob, hd->secret, 0, 0 );
 
398
      rc = blob_filecopy (1, fname, blob, hd->secret, 0);
400
399
      _keybox_release_blob (blob);
401
400
      /*    if (!rc && !hd->secret && kb_offtbl) */
402
401
      /*      { */
410
409
keybox_update_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
411
410
                    unsigned char *sha1_digest)
412
411
{
 
412
  (void)hd;
 
413
  (void)cert;
 
414
  (void)sha1_digest;
413
415
  return -1;
414
416
}
415
417
 
430
432
  const unsigned char *buffer;
431
433
  size_t length;
432
434
 
 
435
  (void)idx;  /* Not yet used.  */
 
436
 
433
437
  if (!hd)
434
438
    return gpg_error (GPG_ERR_INV_VALUE);
435
439
  if (!hd->found.blob)
453
457
  
454
458
  off += flag_pos;
455
459
 
456
 
  if (hd->fp)
457
 
    {
458
 
      fclose (hd->fp);
459
 
      hd->fp = NULL;
460
 
    }
 
460
  _keybox_close_file (hd);
461
461
  fp = fopen (hd->kb->fname, "r+b");
462
462
  if (!fp)
463
 
    return gpg_error (gpg_err_code_from_errno (errno));
 
463
    return gpg_error_from_syserror ();
464
464
 
465
465
  ec = 0;
466
466
  if (fseeko (fp, off, SEEK_SET))
467
 
    ec = gpg_error (gpg_err_code_from_errno (errno));
 
467
    ec = gpg_error_from_syserror ();
468
468
  else
469
469
    {
470
470
      unsigned char tmp[4];
480
480
        case 2:
481
481
        case 4:
482
482
          if (fwrite (tmp+4-flag_size, flag_size, 1, fp) != 1)
483
 
            ec = gpg_err_code_from_errno (errno);
 
483
            ec = gpg_err_code_from_syserror ();
484
484
          break;
485
485
        default:
486
486
          ec = GPG_ERR_BUG;
491
491
  if (fclose (fp))
492
492
    {
493
493
      if (!ec)
494
 
        ec = gpg_err_code_from_errno (errno);
 
494
        ec = gpg_err_code_from_syserror ();
495
495
    }
496
496
 
497
497
  return gpg_error (ec);
522
522
    return gpg_error (GPG_ERR_GENERAL);
523
523
  off += 4;
524
524
 
525
 
  if (hd->fp)
526
 
    {
527
 
      fclose (hd->fp);
528
 
      hd->fp = NULL;
529
 
    }
530
 
  
 
525
  _keybox_close_file (hd);
531
526
  fp = fopen (hd->kb->fname, "r+b");
532
527
  if (!fp)
533
 
    return gpg_error (gpg_err_code_from_errno (errno));
 
528
    return gpg_error_from_syserror ();
534
529
 
535
530
  if (fseeko (fp, off, SEEK_SET))
536
 
    rc = gpg_error (gpg_err_code_from_errno (errno));
 
531
    rc = gpg_error_from_syserror ();
537
532
  else if (putc (0, fp) == EOF)
538
 
    rc = gpg_error (gpg_err_code_from_errno (errno));
 
533
    rc = gpg_error_from_syserror ();
539
534
  else
540
535
    rc = 0;
541
536
 
542
537
  if (fclose (fp))
543
538
    {
544
539
      if (!rc)
545
 
        rc = gpg_error (gpg_err_code_from_errno (errno));
 
540
        rc = gpg_error_from_syserror ();
546
541
    }
547
542
 
548
543
  return rc;
575
570
  if (!fname)
576
571
    return gpg_error (GPG_ERR_INV_HANDLE); 
577
572
 
578
 
  if (hd->fp)
579
 
    {
580
 
      fclose (hd->fp);
581
 
      hd->fp = NULL;
582
 
    }
 
573
  _keybox_close_file (hd);
583
574
 
584
575
  /* Open the source file. Because we do a rename, we have to check the 
585
576
     permissions of the file */
586
577
  if (access (fname, W_OK))
587
 
    return gpg_error (gpg_err_code_from_errno (errno));
 
578
    return gpg_error_from_syserror ();
588
579
 
589
580
  fp = fopen (fname, "rb");
590
581
  if (!fp && errno == ENOENT)
591
582
    return 0; /* Ready. File has been deleted right after the access above. */
592
583
  if (!fp)
593
584
    {
594
 
      rc = gpg_error (gpg_err_code_from_errno (errno));
 
585
      rc = gpg_error_from_syserror ();
595
586
      return rc;
596
587
    }
597
588
 
712
703
 
713
704
  /* Close both files. */
714
705
  if (fclose(fp) && !rc)
715
 
    rc = gpg_error (gpg_err_code_from_errno (errno));
 
706
    rc = gpg_error_from_syserror ();
716
707
  if (fclose(newfp) && !rc)
717
 
    rc = gpg_error (gpg_err_code_from_errno (errno));
 
708
    rc = gpg_error_from_syserror ();
718
709
 
719
710
  /* Rename or remove the temporary file. */
720
711
  if (rc || !any_changes)