~ubuntu-branches/ubuntu/jaunty/gnupg2/jaunty-security

« back to all changes in this revision

Viewing changes to common/miscellaneous.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* miscellaneous.c - Stuff not fitting elsewhere
2
 
 *      Copyright (C) 2003 Free Software Foundation, Inc.
 
2
 *      Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of GnuPG.
5
5
 *
6
6
 * GnuPG is free software; you can redistribute it and/or modify
7
7
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * the Free Software Foundation; either version 3 of the License, or
9
9
 * (at your option) any later version.
10
10
 *
11
11
 * GnuPG is distributed in the hope that it will be useful,
14
14
 * GNU General Public License for more details.
15
15
 *
16
16
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
17
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#include <config.h>
22
21
#include <stdlib.h>
23
22
#include <errno.h>
24
23
 
 
24
#define JNLIB_NEED_LOG_LOGV
25
25
#include "util.h"
26
26
#include "iobuf.h"
 
27
#include "i18n.h"
 
28
 
 
29
 
 
30
/* Used by libgcrypt for logging.  */
 
31
static void
 
32
my_gcry_logger (void *dummy, int level, const char *fmt, va_list arg_ptr)
 
33
{
 
34
  /* Map the log levels.  */
 
35
  switch (level)
 
36
    {
 
37
    case GCRY_LOG_CONT: level = JNLIB_LOG_CONT; break;
 
38
    case GCRY_LOG_INFO: level = JNLIB_LOG_INFO; break;
 
39
    case GCRY_LOG_WARN: level = JNLIB_LOG_WARN; break;
 
40
    case GCRY_LOG_ERROR:level = JNLIB_LOG_ERROR; break;
 
41
    case GCRY_LOG_FATAL:level = JNLIB_LOG_FATAL; break;
 
42
    case GCRY_LOG_BUG:  level = JNLIB_LOG_BUG; break;
 
43
    case GCRY_LOG_DEBUG:level = JNLIB_LOG_DEBUG; break;
 
44
    default:            level = JNLIB_LOG_ERROR; break;  
 
45
    }
 
46
  log_logv (level, fmt, arg_ptr);
 
47
}
 
48
 
 
49
 
 
50
/* This function is called by libgcrypt on a fatal error.  */
 
51
static void
 
52
my_gcry_fatalerror_handler (void *opaque, int rc, const char *text)
 
53
{
 
54
  log_fatal ("libgcrypt problem: %s\n", text ? text : gpg_strerror (rc));
 
55
  abort ();
 
56
}
 
57
 
 
58
 
 
59
/* This function is called by libgcrypt if it ran out of core and
 
60
   there is no way to return that error to the caller.  We do our own
 
61
   function here to make use of our logging functions. */
 
62
static int
 
63
my_gcry_outofcore_handler (void *opaque, size_t req_n, unsigned int flags)
 
64
{
 
65
  static int been_here;  /* Used to protect against recursive calls. */
 
66
 
 
67
  if (!been_here)
 
68
    {
 
69
      been_here = 1;
 
70
      if ( (flags & 1) )
 
71
        log_fatal (_("out of core in secure memory "
 
72
                     "while allocating %lu bytes"), (unsigned long)req_n);
 
73
      else
 
74
        log_fatal (_("out of core while allocating %lu bytes"),
 
75
                   (unsigned long)req_n);
 
76
    }
 
77
  return 0; /* Let libgcrypt call its own fatal error handler.
 
78
               Actually this will turn out to be
 
79
               my_gcry_fatalerror_handler. */
 
80
}
 
81
 
 
82
 
 
83
/* Setup libgcrypt to use our own logging functions.  Should be used
 
84
   early at startup. */
 
85
void
 
86
setup_libgcrypt_logging (void)
 
87
{
 
88
  gcry_set_log_handler (my_gcry_logger, NULL);
 
89
  gcry_set_fatalerror_handler (my_gcry_fatalerror_handler, NULL);
 
90
  gcry_set_outofcore_handler (my_gcry_outofcore_handler, NULL);
 
91
}
 
92
 
27
93
 
28
94
 
29
95
/* Decide whether the filename is stdout or a real filename and return
47
113
    return s;
48
114
}
49
115
 
 
116
/* fixme: Globally replace it by print_sanitized_buffer. */
50
117
void
51
118
print_string( FILE *fp, const byte *p, size_t n, int delim )
52
119
{
65
132
    print_utf8_string2 (fp, p, n, 0);
66
133
}
67
134
 
 
135
/* Write LENGTH bytes of BUFFER to FP as a hex encoded string.
 
136
   RESERVED must be 0. */
 
137
void
 
138
print_hexstring (FILE *fp, const void *buffer, size_t length, int reserved)
 
139
{
 
140
#define tohex(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'A'))
 
141
  const unsigned char *s;
 
142
 
 
143
  for (s = buffer; length; s++, length--)
 
144
    {
 
145
      putc ( tohex ((*s>>4)&15), fp);
 
146
      putc ( tohex (*s&15), fp);
 
147
    }
 
148
#undef tohex
 
149
}
 
150
 
68
151
char *
69
152
make_printable_string (const void *p, size_t n, int delim )
70
153
{
72
155
}
73
156
 
74
157
 
 
158
 
75
159
/*
76
160
 * Check if the file is compressed.
77
161
 */
81
165
    iobuf_t a;
82
166
    byte buf[4];
83
167
    int i, rc = 0;
 
168
    int overflow;
84
169
 
85
170
    struct magic_compress_s {
86
171
        size_t len;
91
176
        { 4, { 0x50, 0x4b, 0x03, 0x04 } }, /* (pk)zip */
92
177
    };
93
178
    
94
 
    if ( !s || (*s == '-' && !s[1]) || !ret_rc )
 
179
    if ( iobuf_is_pipe_filename (s) || !ret_rc )
95
180
        return 0; /* We can't check stdin or no file was given */
96
181
 
97
182
    a = iobuf_open( s );
98
183
    if ( a == NULL ) {
99
 
        *ret_rc = gpg_error_from_errno (errno);
 
184
        *ret_rc = gpg_error_from_syserror ();
100
185
        return 0;
101
186
    }
102
187
 
103
 
    if ( iobuf_get_filelength( a ) < 4 ) {
 
188
    if ( iobuf_get_filelength( a, &overflow ) < 4 && !overflow) {
104
189
        *ret_rc = 0;
105
190
        goto leave;
106
191
    }
124
209
}
125
210
 
126
211
 
 
212
/* Try match against each substring of multistr, delimited by | */
 
213
int
 
214
match_multistr (const char *multistr,const char *match)
 
215
{
 
216
  do
 
217
    {
 
218
      size_t seglen = strcspn (multistr,"|");
 
219
      if (!seglen)
 
220
        break;
 
221
      /* Using the localized strncasecmp! */
 
222
      if (strncasecmp(multistr,match,seglen)==0)
 
223
        return 1;
 
224
      multistr += seglen;
 
225
      if (*multistr == '|')
 
226
        multistr++;
 
227
    }
 
228
  while (*multistr);
 
229
 
 
230
  return 0;
 
231
}
 
232
 
127
233