/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to plugins.d/askpass-fifo.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-24 10:49:09 UTC
  • mfrom: (24.1.63 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080824104909-loh761dpgglkvos1
* mandos (fingerprint): Bug fix: Check crtverify.value, not crtverify.

* mandos-keygen (password): Also print "host = ".

* plugins.d/password-request.c (pgp_packet_decrypt): Only print
                                                     detailed result
                                                     of decryption if
                                                     it failed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
2
 
/*
3
 
 * Askpass-FIFO - Read a password from a FIFO and output it
4
 
 * 
5
 
 * Copyright © 2008-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 Björn Påhlsson
7
 
 * 
8
 
 * This program is free software: you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation, either version 3 of the
11
 
 * License, or (at your option) any later version.
12
 
 * 
13
 
 * This program is distributed in the hope that it will be useful, but
14
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * General Public License for more details.
17
 
 * 
18
 
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
21
 
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
 
 */
24
 
 
25
 
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
26
 
#include <sys/types.h>          /* ssize_t */
27
 
#include <sys/stat.h>           /* mkfifo(), S_IRUSR, S_IWUSR */
28
 
#include <iso646.h>             /* and */
29
 
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
30
 
                                   ENAMETOOLONG, ENOSPC, EROFS,
31
 
                                   ENOENT, EEXIST, EFAULT, EMFILE,
32
 
                                   ENFILE, ENOMEM, EBADF, EINVAL, EIO,
33
 
                                   EISDIR, EFBIG */
34
 
#include <error.h>              /* error() */
35
 
#include <stdio.h>              /* fprintf(), vfprintf(), vasprintf() */
36
 
#include <stdlib.h>             /* EXIT_FAILURE, NULL, size_t, free(),
37
 
                                   realloc(), EXIT_SUCCESS */
38
 
#include <fcntl.h>              /* open(), O_RDONLY */
39
 
#include <unistd.h>             /* read(), close(), write(),
40
 
                                   STDOUT_FILENO */
41
 
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
42
 
                                   EX_UNAVAILABLE, EX_IOERR */
43
 
#include <string.h>             /* strerror() */
44
 
#include <stdarg.h>             /* va_list, va_start(), ... */
45
 
 
46
 
 
47
 
/* Function to use when printing errors */
48
 
void error_plus(int status, int errnum, const char *formatstring, ...){
49
 
  va_list ap;
50
 
  char *text;
51
 
  int ret;
52
 
  
53
 
  va_start(ap, formatstring);
54
 
  ret = vasprintf(&text, formatstring, ap);
55
 
  if (ret == -1){
56
 
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
57
 
    vfprintf(stderr, formatstring, ap);
58
 
    fprintf(stderr, ": ");
59
 
    fprintf(stderr, "%s\n", strerror(errnum));
60
 
    error(status, errno, "vasprintf while printing error");
61
 
    return;
62
 
  }
63
 
  fprintf(stderr, "Mandos plugin ");
64
 
  error(status, errnum, "%s", text);
65
 
  free(text);
66
 
}
67
 
 
68
 
int main(__attribute__((unused))int argc,
69
 
         __attribute__((unused))char **argv){
70
 
  int ret = 0;
71
 
  ssize_t sret;
72
 
  
73
 
  /* Create FIFO */
74
 
  const char passfifo[] = "/lib/cryptsetup/passfifo";
75
 
  ret = mkfifo(passfifo, S_IRUSR | S_IWUSR);
76
 
  if(ret == -1){
77
 
    int e = errno;
78
 
    switch(e){
79
 
    case EACCES:
80
 
    case ENOTDIR:
81
 
    case ELOOP:
82
 
      error_plus(EX_OSFILE, errno, "mkfifo");
83
 
    case ENAMETOOLONG:
84
 
    case ENOSPC:
85
 
    case EROFS:
86
 
    default:
87
 
      error_plus(EX_OSERR, errno, "mkfifo");
88
 
    case ENOENT:
89
 
      /* no "/lib/cryptsetup"? */
90
 
      error_plus(EX_UNAVAILABLE, errno, "mkfifo");
91
 
    case EEXIST:
92
 
      break;                    /* not an error */
93
 
    }
94
 
  }
95
 
  
96
 
  /* Open FIFO */
97
 
  int fifo_fd = open(passfifo, O_RDONLY);
98
 
  if(fifo_fd == -1){
99
 
    int e = errno;
100
 
    error_plus(0, errno, "open");
101
 
    switch(e){
102
 
    case EACCES:
103
 
    case ENOENT:
104
 
    case EFAULT:
105
 
      return EX_UNAVAILABLE;
106
 
    case ENAMETOOLONG:
107
 
    case EMFILE:
108
 
    case ENFILE:
109
 
    case ENOMEM:
110
 
    default:
111
 
      return EX_OSERR;
112
 
    case ENOTDIR:
113
 
    case ELOOP:
114
 
      return EX_OSFILE;
115
 
    }
116
 
  }
117
 
  
118
 
  /* Read from FIFO */
119
 
  char *buf = NULL;
120
 
  size_t buf_len = 0;
121
 
  {
122
 
    size_t buf_allocated = 0;
123
 
    const size_t blocksize = 1024;
124
 
    do {
125
 
      if(buf_len + blocksize > buf_allocated){
126
 
        char *tmp = realloc(buf, buf_allocated + blocksize);
127
 
        if(tmp == NULL){
128
 
          error_plus(0, errno, "realloc");
129
 
          free(buf);
130
 
          return EX_OSERR;
131
 
        }
132
 
        buf = tmp;
133
 
        buf_allocated += blocksize;
134
 
      }
135
 
      sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
136
 
      if(sret == -1){
137
 
        int e = errno;
138
 
        free(buf);
139
 
        errno = e;
140
 
        error_plus(0, errno, "read");
141
 
        switch(e){
142
 
        case EBADF:
143
 
        case EFAULT:
144
 
        case EINVAL:
145
 
        default:
146
 
          return EX_OSERR;
147
 
        case EIO:
148
 
          return EX_IOERR;
149
 
        case EISDIR:
150
 
          return EX_UNAVAILABLE;
151
 
        }
152
 
      }
153
 
      buf_len += (size_t)sret;
154
 
    } while(sret != 0);
155
 
  }
156
 
  
157
 
  /* Close FIFO */
158
 
  close(fifo_fd);
159
 
  
160
 
  /* Print password to stdout */
161
 
  size_t written = 0;
162
 
  while(written < buf_len){
163
 
    sret = write(STDOUT_FILENO, buf + written, buf_len - written);
164
 
    if(sret == -1){
165
 
      int e = errno;
166
 
      free(buf);
167
 
      errno = e;
168
 
      error_plus(0, errno, "write");
169
 
      switch(e){
170
 
      case EBADF:
171
 
      case EFAULT:
172
 
      case EINVAL:
173
 
        return EX_OSFILE;
174
 
      case EFBIG:
175
 
      case EIO:
176
 
      case ENOSPC:
177
 
      default:
178
 
        return EX_IOERR;
179
 
      }
180
 
    }
181
 
    written += (size_t)sret;
182
 
  }
183
 
  free(buf);
184
 
  
185
 
  ret = close(STDOUT_FILENO);
186
 
  if(ret == -1){
187
 
    int e = errno;
188
 
    error_plus(0, errno, "close");
189
 
    switch(e){
190
 
    case EBADF:
191
 
      return EX_OSFILE;
192
 
    case EIO:
193
 
    default:
194
 
      return EX_IOERR;
195
 
    }
196
 
  }
197
 
  return EXIT_SUCCESS;
198
 
}