bzr branch
http://bzr.recompile.se/loggerhead/mandos/trunk
| 237.1.2
by Teddy Hogeborn Further steps towards a D-Bus server interface, plus minor syntax | 1 | /*  -*- coding: utf-8 -*- */
 | 
| 2 | /*
 | |
| 261
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 |  * 
 | 
| 246
by Teddy Hogeborn * README: Update copyright year; add "2009". | 5 |  * Copyright © 2008,2009 Teddy Hogeborn
 | 
| 6 |  * Copyright © 2008,2009 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 |  * 
 | |
| 363
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 */ | |
| 390
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() */ | 
| 355
by Teddy Hogeborn * mandos: White-space fixes only. | 35 | #include <stdlib.h> /* EXIT_FAILURE, NULL, size_t, free(), | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 36 | realloc(), EXIT_SUCCESS */ | 
| 37 | #include <fcntl.h> /* open(), O_RDONLY */ | |
| 38 | #include <unistd.h> /* read(), close(), write(), | |
| 39 | STDOUT_FILENO */ | |
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 40 | #include <sysexits.h> /* EX_OSERR, EX_OSFILE, | 
| 41 | EX_UNAVAILABLE, EX_IOERR */ | |
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 42 | |
| 43 | ||
| 44 | int main(__attribute__((unused))int argc, | |
| 45 | __attribute__((unused))char **argv){ | |
| 46 | int ret = 0; | |
| 47 | ssize_t sret; | |
| 48 |   
 | |
| 49 | /* Create FIFO */ | |
| 50 | const char passfifo[] = "/lib/cryptsetup/passfifo"; | |
| 369
by Teddy Hogeborn * init.d-mandos (Required-Start, Required-Stop): Bug fix: Added | 51 | ret = mkfifo(passfifo, S_IRUSR | S_IWUSR); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 52 | if(ret == -1){ | 
| 53 | int e = errno; | |
| 24.1.155
by Björn Påhlsson mandos server: Added debuglevel that adjust at what level information | 54 | error(0, errno, "mkfifo"); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 55 | switch(e){ | 
| 56 | case EACCES: | |
| 57 | case ENOTDIR: | |
| 58 | case ELOOP: | |
| 59 | return EX_OSFILE; | |
| 60 | case ENAMETOOLONG: | |
| 61 | case ENOSPC: | |
| 62 | case EROFS: | |
| 63 | default: | |
| 64 | return EX_OSERR; | |
| 65 | case ENOENT: | |
| 66 | return EX_UNAVAILABLE; /* no "/lib/cryptsetup"? */ | |
| 67 | case EEXIST: | |
| 68 | break; /* not an error */ | |
| 69 | } | |
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 70 | } | 
| 71 |   
 | |
| 72 | /* Open FIFO */ | |
| 369
by Teddy Hogeborn * init.d-mandos (Required-Start, Required-Stop): Bug fix: Added | 73 | int fifo_fd = open(passfifo, O_RDONLY); | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 74 | if(fifo_fd == -1){ | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 75 | int e = errno; | 
| 24.1.155
by Björn Påhlsson mandos server: Added debuglevel that adjust at what level information | 76 | error(0, errno, "open"); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 77 | switch(e){ | 
| 78 | case EACCES: | |
| 79 | case ENOENT: | |
| 80 | case EFAULT: | |
| 81 | return EX_UNAVAILABLE; | |
| 82 | case ENAMETOOLONG: | |
| 83 | case EMFILE: | |
| 84 | case ENFILE: | |
| 85 | case ENOMEM: | |
| 86 | default: | |
| 87 | return EX_OSERR; | |
| 88 | case ENOTDIR: | |
| 89 | case ELOOP: | |
| 90 | return EX_OSFILE; | |
| 91 | } | |
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 92 | } | 
| 93 |   
 | |
| 94 | /* Read from FIFO */ | |
| 95 | char *buf = NULL; | |
| 96 | size_t buf_len = 0; | |
| 97 | { | |
| 98 | size_t buf_allocated = 0; | |
| 99 | const size_t blocksize = 1024; | |
| 363
by Teddy Hogeborn * plugin-runner.c: Minor stylistic changes. | 100 | do { | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 101 | if(buf_len + blocksize > buf_allocated){ | 
| 102 | char *tmp = realloc(buf, buf_allocated + blocksize); | |
| 103 | if(tmp == NULL){ | |
| 24.1.155
by Björn Påhlsson mandos server: Added debuglevel that adjust at what level information | 104 | error(0, errno, "realloc"); | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 105 | free(buf); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 106 | return EX_OSERR; | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 107 | } | 
| 108 | buf = tmp; | |
| 109 | buf_allocated += blocksize; | |
| 110 | } | |
| 369
by Teddy Hogeborn * init.d-mandos (Required-Start, Required-Stop): Bug fix: Added | 111 | sret = read(fifo_fd, buf + buf_len, buf_allocated - buf_len); | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 112 | if(sret == -1){ | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 113 | int e = errno; | 
| 114 | free(buf); | |
| 115 | errno = e; | |
| 24.1.155
by Björn Påhlsson mandos server: Added debuglevel that adjust at what level information | 116 | error(0, errno, "read"); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 117 | switch(e){ | 
| 118 | case EBADF: | |
| 119 | case EFAULT: | |
| 120 | case EINVAL: | |
| 121 | default: | |
| 122 | return EX_OSERR; | |
| 123 | case EIO: | |
| 124 | return EX_IOERR; | |
| 125 | case EISDIR: | |
| 126 | return EX_UNAVAILABLE; | |
| 127 | } | |
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 128 | } | 
| 129 | buf_len += (size_t)sret; | |
| 363
by Teddy Hogeborn * plugin-runner.c: Minor stylistic changes. | 130 | } while(sret != 0); | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 131 | } | 
| 132 |   
 | |
| 133 | /* Close FIFO */ | |
| 369
by Teddy Hogeborn * init.d-mandos (Required-Start, Required-Stop): Bug fix: Added | 134 | close(fifo_fd); | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 135 |   
 | 
| 136 | /* Print password to stdout */ | |
| 137 | size_t written = 0; | |
| 138 | while(written < buf_len){ | |
| 369
by Teddy Hogeborn * init.d-mandos (Required-Start, Required-Stop): Bug fix: Added | 139 | sret = write(STDOUT_FILENO, buf + written, buf_len - written); | 
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 140 | if(sret == -1){ | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 141 | int e = errno; | 
| 142 | free(buf); | |
| 143 | errno = e; | |
| 24.1.155
by Björn Påhlsson mandos server: Added debuglevel that adjust at what level information | 144 | error(0, errno, "write"); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 145 | switch(e){ | 
| 146 | case EBADF: | |
| 147 | case EFAULT: | |
| 148 | case EINVAL: | |
| 149 | return EX_OSFILE; | |
| 150 | case EFBIG: | |
| 151 | case EIO: | |
| 152 | case ENOSPC: | |
| 153 | default: | |
| 154 | return EX_IOERR; | |
| 155 | } | |
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 156 | } | 
| 157 | written += (size_t)sret; | |
| 158 | } | |
| 159 | free(buf); | |
| 160 |   
 | |
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 161 | ret = close(STDOUT_FILENO); | 
| 162 | if(ret == -1){ | |
| 163 | int e = errno; | |
| 24.1.155
by Björn Påhlsson mandos server: Added debuglevel that adjust at what level information | 164 | error(0, errno, "close"); | 
| 390
by Teddy Hogeborn * plugins.d/askpass-fifo.c: Do close(STDOUT_FILENO) before exiting to | 165 | switch(e){ | 
| 166 | case EBADF: | |
| 167 | return EX_OSFILE; | |
| 168 | case EIO: | |
| 169 | default: | |
| 170 | return EX_IOERR; | |
| 171 | } | |
| 172 | } | |
| 214
by Teddy Hogeborn * Makefile (PLUGINS): Added "plugins.d/askpass-fifo". | 173 | return EXIT_SUCCESS; | 
| 174 | }
 |