/mandos/trunk

To get this branch, use:
bzr branch /loggerhead/mandos/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*  -*- coding: utf-8 -*- */
/*
 * Askpass-FIFO - Read a password from a FIFO and output it
 * 
 * Copyright © 2008-2019 Teddy Hogeborn
 * Copyright © 2008-2019 Björn Påhlsson
 * 
 * This file is part of Mandos.
 * 
 * Mandos is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Mandos is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contact the authors at <mandos@recompile.se>.
 */

#define _GNU_SOURCE		/* TEMP_FAILURE_RETRY() */
#include <sys/types.h>		/* uid_t, gid_t, ssize_t */
#include <sys/stat.h>		/* mkfifo(), S_IRUSR, S_IWUSR */
#include <iso646.h>		/* and */
#include <errno.h>		/* errno, EACCES, ENOTDIR, ELOOP,
				   ENAMETOOLONG, ENOSPC, EROFS,
				   ENOENT, EEXIST, EFAULT, EMFILE,
				   ENFILE, ENOMEM, EBADF, EINVAL, EIO,
				   EISDIR, EFBIG */
#include <error.h>		/* error() */
#include <stdio.h>		/* fprintf(), vfprintf(),
				   vasprintf() */
#include <stdlib.h>		/* EXIT_FAILURE, NULL, size_t, free(),
				   realloc(), EXIT_SUCCESS */
#include <fcntl.h>		/* open(), O_RDONLY */
#include <unistd.h>		/* read(), close(), write(),
				   STDOUT_FILENO */
#include <sysexits.h>		/* EX_OSERR, EX_OSFILE,
				   EX_UNAVAILABLE, EX_IOERR */
#include <string.h> 		/* strerror() */
#include <stdarg.h>		/* va_list, va_start(), ... */

uid_t uid = 65534;
gid_t gid = 65534;

/* Function to use when printing errors */
__attribute__((format (gnu_printf, 3, 4)))
void error_plus(int status, int errnum, const char *formatstring,
		...){
  va_list ap;
  char *text;
  int ret;
  
  va_start(ap, formatstring);
  ret = vasprintf(&text, formatstring, ap);
  if(ret == -1){
    fprintf(stderr, "Mandos plugin %s: ",
	    program_invocation_short_name);
    vfprintf(stderr, formatstring, ap);
    fprintf(stderr, ": ");
    fprintf(stderr, "%s\n", strerror(errnum));
    error(status, errno, "vasprintf while printing error");
    if(status){
      __builtin_unreachable();
    }
    return;
  }
  fprintf(stderr, "Mandos plugin ");
  error(status, errnum, "%s", text);
  if(status){
    __builtin_unreachable();
  }
  free(text);
}

int main(__attribute__((unused))int argc,
	 __attribute__((unused))char **argv){
  int ret = 0;
  ssize_t sret;
  
  uid = getuid();
  gid = getgid();
  
  /* Create FIFO */
  const char passfifo[] = "/lib/cryptsetup/passfifo";
  ret = mkfifo(passfifo, S_IRUSR | S_IWUSR);
  if(ret == -1){
    int e = errno;
    switch(e){
    case EACCES:
    case ENOTDIR:
    case ELOOP:
      error_plus(EX_OSFILE, errno, "mkfifo");
      __builtin_unreachable();
    case ENAMETOOLONG:
    case ENOSPC:
    case EROFS:
    default:
      error_plus(EX_OSERR, errno, "mkfifo");
      __builtin_unreachable();
    case ENOENT:
      /* no "/lib/cryptsetup"? */
      error_plus(EX_UNAVAILABLE, errno, "mkfifo");
      __builtin_unreachable();
    case EEXIST:
      break;			/* not an error */
    }
  }
  
  /* Open FIFO */
  int fifo_fd = open(passfifo, O_RDONLY);
  if(fifo_fd == -1){
    int e = errno;
    error_plus(0, errno, "open");
    switch(e){
    case EACCES:
    case ENOENT:
    case EFAULT:
      return EX_UNAVAILABLE;
    case ENAMETOOLONG:
    case EMFILE:
    case ENFILE:
    case ENOMEM:
    default:
      return EX_OSERR;
    case ENOTDIR:
    case ELOOP:
      return EX_OSFILE;
    }
  }
  
  /* Lower group privileges  */
  if(setgid(gid) == -1){
    error_plus(0, errno, "setgid");
  }
  
  /* Lower user privileges */
  if(setuid(uid) == -1){
    error_plus(0, errno, "setuid");
  }
  
  /* Read from FIFO */
  char *buf = NULL;
  size_t buf_len = 0;
  {
    size_t buf_allocated = 0;
    const size_t blocksize = 1024;
    do {
      if(buf_len + blocksize > buf_allocated){
	char *tmp = realloc(buf, buf_allocated + blocksize);
	if(tmp == NULL){
	  error_plus(0, errno, "realloc");
	  free(buf);
	  return EX_OSERR;
	}
	buf = tmp;
	buf_allocated += blocksize;
      }
      sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
      if(sret == -1){
	int e = errno;
	free(buf);
	errno = e;
	error_plus(0, errno, "read");
	switch(e){
	case EBADF:
	case EFAULT:
	case EINVAL:
	default:
	  return EX_OSERR;
	case EIO:
	  return EX_IOERR;
	case EISDIR:
	  return EX_UNAVAILABLE;
	}
      }
      buf_len += (size_t)sret;
    } while(sret != 0);
  }
  
  /* Close FIFO */
  close(fifo_fd);
  
  /* Print password to stdout */
  size_t written = 0;
  while(written < buf_len){
    sret = write(STDOUT_FILENO, buf + written, buf_len - written);
    if(sret == -1){
      int e = errno;
      free(buf);
      errno = e;
      error_plus(0, errno, "write");
      switch(e){
      case EBADF:
      case EFAULT:
      case EINVAL:
	return EX_OSFILE;
      case EFBIG:
      case EIO:
      case ENOSPC:
      default:
	return EX_IOERR;
      }
    }
    written += (size_t)sret;
  }
  free(buf);
  
  ret = close(STDOUT_FILENO);
  if(ret == -1){
    int e = errno;
    error_plus(0, errno, "close");
    switch(e){
    case EBADF:
      return EX_OSFILE;
    case EIO:
    default:
      return EX_IOERR;
    }
  }
  return EXIT_SUCCESS;
}