/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/password-prompt.c

* debian/control (Standards-Version): Upgraded to "3.9.2".
  (DM-Upload-Allowed): New; set to "yes".

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* getline(), asprintf() */
26
26
 
27
 
#include <termios.h>            /* struct termios, tcsetattr(),
 
27
#include <termios.h>            /* struct termios, tcsetattr(),
28
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
29
29
#include <unistd.h>             /* struct termios, tcsetattr(),
30
30
                                   STDIN_FILENO, TCSAFLUSH,
41
41
                                   getenv(), free() */
42
42
#include <dirent.h>             /* scandir(), alphasort() */
43
43
#include <stdio.h>              /* fprintf(), stderr, getline(),
44
 
                                   stdin, feof(), fputc()
45
 
                                */
 
44
                                   stdin, feof(), fputc(), vfprintf(),
 
45
                                   vasprintf() */
46
46
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
47
47
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
48
48
                                */
50
50
#include <iso646.h>             /* or, not */
51
51
#include <stdbool.h>            /* bool, false, true */
52
52
#include <inttypes.h>           /* strtoumax() */
53
 
#include <sys/stat.h>           /* struct stat, lstat(), open() */
54
 
#include <string.h>             /* strlen, rindex, memcmp */
 
53
#include <sys/stat.h>           /* struct stat, lstat(), open() */
 
54
#include <string.h>             /* strlen, rindex, memcmp, strerror()
 
55
                                 */
55
56
#include <argp.h>               /* struct argp_option, struct
56
57
                                   argp_state, struct argp,
57
58
                                   argp_parse(), error_t,
60
61
#include <sysexits.h>           /* EX_SOFTWARE, EX_OSERR,
61
62
                                   EX_UNAVAILABLE, EX_IOERR, EX_OK */
62
63
#include <fcntl.h>              /* open() */
 
64
#include <stdarg.h>             /* va_list, va_start(), ... */
63
65
 
64
66
volatile sig_atomic_t quit_now = 0;
65
67
int signal_received;
66
68
bool debug = false;
67
69
const char *argp_program_version = "password-prompt " VERSION;
68
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
69
 
 
70
 
/* Needed for conflic resolution */
71
 
const char plymouthd_name[] = "plymouthd";
72
 
 
 
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
71
 
 
72
/* Needed for conflict resolution */
 
73
const char plymouth_name[] = "plymouthd";
 
74
 
 
75
/* Function to use when printing errors */
 
76
void error_plus(int status, int errnum, const char *formatstring,
 
77
                ...){
 
78
  va_list ap;
 
79
  char *text;
 
80
  int ret;
 
81
  
 
82
  va_start(ap, formatstring);
 
83
  ret = vasprintf(&text, formatstring, ap);
 
84
  if (ret == -1){
 
85
    fprintf(stderr, "Mandos plugin %s: ",
 
86
            program_invocation_short_name);
 
87
    vfprintf(stderr, formatstring, ap);
 
88
    fprintf(stderr, ": ");
 
89
    fprintf(stderr, "%s\n", strerror(errnum));
 
90
    error(status, errno, "vasprintf while printing error");
 
91
    return;
 
92
  }
 
93
  fprintf(stderr, "Mandos plugin ");
 
94
  error(status, errnum, "%s", text);
 
95
  free(text);
 
96
}
73
97
 
74
98
static void termination_handler(int signum){
75
99
  if(quit_now){
101
125
    }
102
126
    
103
127
    char *cmdline_filename;
104
 
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline", proc_entry->d_name);
 
128
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
 
129
                   proc_entry->d_name);
105
130
    if(ret == -1){
106
131
      error(0, errno, "asprintf");
107
132
      return 0;
108
133
    }
109
134
    
110
 
    /* Open /proc/<pid>/cmdline  */
 
135
    /* Open /proc/<pid>/cmdline */
111
136
    cl_fd = open(cmdline_filename, O_RDONLY);
112
137
    free(cmdline_filename);
113
138
    if(cl_fd == -1){
114
 
      error(0, errno, "open");
 
139
      if(errno != ENOENT){
 
140
        error(0, errno, "open");
 
141
      }
115
142
      return 0;
116
143
    }
117
144
    
165
192
      cmdline_base = cmdline;
166
193
    }
167
194
    
168
 
    if(strcmp(cmdline_base, plymouthd_name) != 0){
 
195
    if(strcmp(cmdline_base, plymouth_name) != 0){
 
196
      if(debug){
 
197
        fprintf(stderr, "\"%s\" is not \"%s\"\n", cmdline_base,
 
198
                plymouth_name);
 
199
      }
169
200
      free(cmdline);
170
201
      return 0;
171
202
    }
 
203
    if(debug){
 
204
      fprintf(stderr, "\"%s\" equals \"%s\"\n", cmdline_base,
 
205
              plymouth_name);
 
206
    }
172
207
    free(cmdline);
173
208
    return 1;
174
209
  }
175
 
 
176
 
  struct dirent **direntries;
 
210
  
 
211
  struct dirent **direntries = NULL;
177
212
  int ret;
178
213
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
179
214
  if (ret == -1){
180
215
    error(1, errno, "scandir");
181
216
  }
 
217
  free(direntries);
182
218
  return ret > 0;
183
219
}
184
220
 
267
303
 
268
304
  if (conflict_detection()){
269
305
    if(debug){
270
 
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
 
306
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
271
307
    }
272
308
    return EXIT_FAILURE;
273
309
  }
475
511
        break;
476
512
      }
477
513
    }
478
 
    /* if(sret == 0), then the only sensible thing to do is to retry to
479
 
       read from stdin */
 
514
    /* if(sret == 0), then the only sensible thing to do is to retry
 
515
       to read from stdin */
480
516
    fputc('\n', stderr);
481
517
    if(debug and not quit_now){
482
518
      /* If quit_now is nonzero, we were interrupted by a signal, and