/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
237.1.2 by Teddy Hogeborn
Further steps towards a D-Bus server interface, plus minor syntax
1
/*  -*- coding: utf-8 -*- */
2
/*
237.2.24 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Fix name in header.
3
 * Askpass-FIFO - Read a password from a FIFO and output it
237.1.2 by Teddy Hogeborn
Further steps towards a D-Bus server interface, plus minor syntax
4
 * 
237.7.14 by Teddy Hogeborn
Update copyright year to "2011" wherever appropriate.
5
 * Copyright © 2008-2011 Teddy Hogeborn
6
 * Copyright © 2008-2011 Björn Påhlsson
237.1.2 by Teddy Hogeborn
Further steps towards a D-Bus server interface, plus minor syntax
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
 * 
237.2.126 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
237.1.2 by Teddy Hogeborn
Further steps towards a D-Bus server interface, plus minor syntax
23
 */
24
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
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 */
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
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 */
24.1.155 by Björn Påhlsson
mandos server: Added debuglevel that adjust at what level information
34
#include <error.h>		/* error() */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
35
#include <stdio.h>		/* fprintf(), vfprintf(), vasprintf() */
237.2.118 by Teddy Hogeborn
* mandos: White-space fixes only.
36
#include <stdlib.h>		/* EXIT_FAILURE, NULL, size_t, free(),
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
37
				   realloc(), EXIT_SUCCESS */
38
#include <fcntl.h>		/* open(), O_RDONLY */
39
#include <unistd.h>		/* read(), close(), write(),
40
				   STDOUT_FILENO */
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
41
#include <sysexits.h>		/* EX_OSERR, EX_OSFILE,
42
				   EX_UNAVAILABLE, EX_IOERR */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
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
}
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
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";
237.2.132 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
75
  ret = mkfifo(passfifo, S_IRUSR | S_IWUSR);
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
76
  if(ret == -1){
77
    int e = errno;
78
    switch(e){
79
    case EACCES:
80
    case ENOTDIR:
81
    case ELOOP:
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
82
      error_plus(EX_OSFILE, errno, "mkfifo");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
83
    case ENAMETOOLONG:
84
    case ENOSPC:
85
    case EROFS:
86
    default:
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
87
      error_plus(EX_OSERR, errno, "mkfifo");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
88
    case ENOENT:
237.7.27 by Teddy Hogeborn
* plugins.d/askpass-fifo.c (main): Don't print message if FIFO exists.
89
      /* no "/lib/cryptsetup"? */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
90
      error_plus(EX_UNAVAILABLE, errno, "mkfifo");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
91
    case EEXIST:
92
      break;			/* not an error */
93
    }
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
94
  }
95
  
96
  /* Open FIFO */
237.2.132 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
97
  int fifo_fd = open(passfifo, O_RDONLY);
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
98
  if(fifo_fd == -1){
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
99
    int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
100
    error_plus(0, errno, "open");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
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
    }
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
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;
237.2.126 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
124
    do {
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
125
      if(buf_len + blocksize > buf_allocated){
126
	char *tmp = realloc(buf, buf_allocated + blocksize);
127
	if(tmp == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
128
	  error_plus(0, errno, "realloc");
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
129
	  free(buf);
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
130
	  return EX_OSERR;
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
131
	}
132
	buf = tmp;
133
	buf_allocated += blocksize;
134
      }
237.2.132 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
135
      sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len);
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
136
      if(sret == -1){
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
137
	int e = errno;
138
	free(buf);
139
	errno = e;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
140
	error_plus(0, errno, "read");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
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
	}
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
152
      }
153
      buf_len += (size_t)sret;
237.2.126 by Teddy Hogeborn
* plugin-runner.c: Minor stylistic changes.
154
    } while(sret != 0);
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
155
  }
156
  
157
  /* Close FIFO */
237.2.132 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
158
  close(fifo_fd);
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
159
  
160
  /* Print password to stdout */
161
  size_t written = 0;
162
  while(written < buf_len){
237.2.132 by Teddy Hogeborn
* init.d-mandos (Required-Start, Required-Stop): Bug fix: Added
163
    sret = write(STDOUT_FILENO, buf + written, buf_len - written);
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
164
    if(sret == -1){
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
165
      int e = errno;
166
      free(buf);
167
      errno = e;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
168
      error_plus(0, errno, "write");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
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
      }
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
180
    }
181
    written += (size_t)sret;
182
  }
183
  free(buf);
184
  
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
185
  ret = close(STDOUT_FILENO);
186
  if(ret == -1){
187
    int e = errno;
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
188
    error_plus(0, errno, "close");
237.2.153 by Teddy Hogeborn
* plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to
189
    switch(e){
190
    case EBADF:
191
      return EX_OSFILE;
192
    case EIO:
193
    default:
194
      return EX_IOERR;
195
    }
196
  }
214 by Teddy Hogeborn
* Makefile (PLUGINS): Added "plugins.d/askpass-fifo".
197
  return EXIT_SUCCESS;
198
}