/mandos/trunk

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

« back to all changes in this revision

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

merge
new approve/deny functionallity in mandos-monitor

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Askpass-FIFO - Read a password from a FIFO and output it
4
4
 * 
5
 
 * Copyright © 2008-2014 Teddy Hogeborn
6
 
 * Copyright © 2008-2014 Björn Påhlsson
 
5
 * Copyright © 2008,2009 Teddy Hogeborn
 
6
 * Copyright © 2008,2009 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
26
 
#include <sys/types.h>          /* uid_t, gid_t, ssize_t */
 
26
#include <sys/types.h>          /* ssize_t */
27
27
#include <sys/stat.h>           /* mkfifo(), S_IRUSR, S_IWUSR */
28
28
#include <iso646.h>             /* and */
29
29
#include <errno.h>              /* errno, EACCES, ENOTDIR, ELOOP,
31
31
                                   ENOENT, EEXIST, EFAULT, EMFILE,
32
32
                                   ENFILE, ENOMEM, EBADF, EINVAL, EIO,
33
33
                                   EISDIR, EFBIG */
34
 
#include <error.h>              /* error() */
35
 
#include <stdio.h>              /* fprintf(), vfprintf(),
36
 
                                   vasprintf() */
 
34
#include <stdio.h>              /* perror() */
37
35
#include <stdlib.h>             /* EXIT_FAILURE, NULL, size_t, free(),
38
36
                                   realloc(), EXIT_SUCCESS */
39
37
#include <fcntl.h>              /* open(), O_RDONLY */
41
39
                                   STDOUT_FILENO */
42
40
#include <sysexits.h>           /* EX_OSERR, EX_OSFILE,
43
41
                                   EX_UNAVAILABLE, EX_IOERR */
44
 
#include <string.h>             /* strerror() */
45
 
#include <stdarg.h>             /* va_list, va_start(), ... */
46
 
 
47
 
uid_t uid = 65534;
48
 
gid_t gid = 65534;
49
 
 
50
 
/* Function to use when printing errors */
51
 
__attribute__((format (gnu_printf, 3, 4)))
52
 
void error_plus(int status, int errnum, const char *formatstring,
53
 
                ...){
54
 
  va_list ap;
55
 
  char *text;
56
 
  int ret;
57
 
  
58
 
  va_start(ap, formatstring);
59
 
  ret = vasprintf(&text, formatstring, ap);
60
 
  if(ret == -1){
61
 
    fprintf(stderr, "Mandos plugin %s: ",
62
 
            program_invocation_short_name);
63
 
    vfprintf(stderr, formatstring, ap);
64
 
    fprintf(stderr, ": ");
65
 
    fprintf(stderr, "%s\n", strerror(errnum));
66
 
    error(status, errno, "vasprintf while printing error");
67
 
    return;
68
 
  }
69
 
  fprintf(stderr, "Mandos plugin ");
70
 
  error(status, errnum, "%s", text);
71
 
  free(text);
72
 
}
 
42
 
73
43
 
74
44
int main(__attribute__((unused))int argc,
75
45
         __attribute__((unused))char **argv){
76
46
  int ret = 0;
77
47
  ssize_t sret;
78
48
  
79
 
  uid = getuid();
80
 
  gid = getgid();
81
 
  
82
49
  /* Create FIFO */
83
50
  const char passfifo[] = "/lib/cryptsetup/passfifo";
84
51
  ret = mkfifo(passfifo, S_IRUSR | S_IWUSR);
85
52
  if(ret == -1){
86
53
    int e = errno;
 
54
    perror("mkfifo");
87
55
    switch(e){
88
56
    case EACCES:
89
57
    case ENOTDIR:
90
58
    case ELOOP:
91
 
      error_plus(EX_OSFILE, errno, "mkfifo");
 
59
      return EX_OSFILE;
92
60
    case ENAMETOOLONG:
93
61
    case ENOSPC:
94
62
    case EROFS:
95
63
    default:
96
 
      error_plus(EX_OSERR, errno, "mkfifo");
 
64
      return EX_OSERR;
97
65
    case ENOENT:
98
 
      /* no "/lib/cryptsetup"? */
99
 
      error_plus(EX_UNAVAILABLE, errno, "mkfifo");
 
66
      return EX_UNAVAILABLE;    /* no "/lib/cryptsetup"? */
100
67
    case EEXIST:
101
68
      break;                    /* not an error */
102
69
    }
106
73
  int fifo_fd = open(passfifo, O_RDONLY);
107
74
  if(fifo_fd == -1){
108
75
    int e = errno;
109
 
    error_plus(0, errno, "open");
 
76
    perror("open");
110
77
    switch(e){
111
78
    case EACCES:
112
79
    case ENOENT:
124
91
    }
125
92
  }
126
93
  
127
 
  /* Lower group privileges  */
128
 
  if(setgid(gid) == -1){
129
 
    error_plus(0, errno, "setgid");
130
 
  }
131
 
  
132
 
  /* Lower user privileges */
133
 
  if(setuid(uid) == -1){
134
 
    error_plus(0, errno, "setuid");
135
 
  }
136
 
  
137
94
  /* Read from FIFO */
138
95
  char *buf = NULL;
139
96
  size_t buf_len = 0;
144
101
      if(buf_len + blocksize > buf_allocated){
145
102
        char *tmp = realloc(buf, buf_allocated + blocksize);
146
103
        if(tmp == NULL){
147
 
          error_plus(0, errno, "realloc");
 
104
          perror("realloc");
148
105
          free(buf);
149
106
          return EX_OSERR;
150
107
        }
156
113
        int e = errno;
157
114
        free(buf);
158
115
        errno = e;
159
 
        error_plus(0, errno, "read");
 
116
        perror("read");
160
117
        switch(e){
161
118
        case EBADF:
162
119
        case EFAULT:
184
141
      int e = errno;
185
142
      free(buf);
186
143
      errno = e;
187
 
      error_plus(0, errno, "write");
 
144
      perror("write");
188
145
      switch(e){
189
146
      case EBADF:
190
147
      case EFAULT:
204
161
  ret = close(STDOUT_FILENO);
205
162
  if(ret == -1){
206
163
    int e = errno;
207
 
    error_plus(0, errno, "close");
 
164
    perror("close");
208
165
    switch(e){
209
166
    case EBADF:
210
167
      return EX_OSFILE;