/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

  • Committer: teddy at bsnet
  • Date: 2010-09-27 18:57:12 UTC
  • Revision ID: teddy@fukt.bsnet.se-20100927185712-d4t34t3y002xr44x
* mandos-clients.conf.xml (EXPANSION/RUNTIME EXPANSION): List possible
                                                         keys.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
 
#define _GNU_SOURCE             /* getline(), asprintf() */
 
25
#define _GNU_SOURCE             /* getline() */
26
26
 
27
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,
31
 
                                   tcgetattr(), ECHO, readlink() */
 
31
                                   tcgetattr(), ECHO */
32
32
#include <signal.h>             /* sig_atomic_t, raise(), struct
33
33
                                   sigaction, sigemptyset(),
34
34
                                   sigaction(), sigaddset(), SIGINT,
35
35
                                   SIGQUIT, SIGHUP, SIGTERM,
36
36
                                   raise() */
37
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
 
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t, ssize_t */
 
38
#include <sys/types.h>          /* ssize_t */
39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
40
 
                                   getenv(), free() */
41
 
#include <dirent.h>             /* scandir(), alphasort() */
 
40
                                   getenv() */
42
41
#include <stdio.h>              /* fprintf(), stderr, getline(),
43
42
                                   stdin, feof(), fputc()
44
43
                                */
48
47
#include <error.h>              /* error() */
49
48
#include <iso646.h>             /* or, not */
50
49
#include <stdbool.h>            /* bool, false, true */
51
 
#include <inttypes.h>           /* strtoumax() */
52
 
#include <sys/stat.h>           /* struct stat, lstat() */
53
 
#include <string.h>             /* strlen, rindex, memcmp */
 
50
#include <string.h>             /* strlen, rindex */
54
51
#include <argp.h>               /* struct argp_option, struct
55
52
                                   argp_state, struct argp,
56
53
                                   argp_parse(), error_t,
65
62
const char *argp_program_version = "password-prompt " VERSION;
66
63
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
67
64
 
68
 
/* Needed for conflic resolution */
69
 
const char plymouthd_path[] = "/sbin/plymouth";
70
 
 
71
 
 
72
65
static void termination_handler(int signum){
73
66
  if(quit_now){
74
67
    return;
77
70
  signal_received = signum;
78
71
}
79
72
 
80
 
bool conflict_detection(void){
81
 
 
82
 
  /* plymouth conflicts with password-promt since both want to control the
83
 
     associated terminal. Password-prompt exit since plymouth perferms the same
84
 
     functionallity.
85
 
   */
86
 
  int is_plymouth(const struct dirent *proc_entry){
87
 
    int ret;
88
 
    {
89
 
      uintmax_t maxvalue;
90
 
      char *tmp;
91
 
      errno = 0;
92
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
93
 
      
94
 
      if(errno != 0 or *tmp != '\0'
95
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
96
 
        return 0;
97
 
      }
98
 
    }
99
 
    char exe_target[sizeof(plymouthd_path)];
100
 
    char *exe_link;
101
 
    ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
102
 
    if(ret == -1){
103
 
      error(0, errno, "asprintf");
104
 
      return 0;
105
 
    }
106
 
  
107
 
    struct stat exe_stat;
108
 
    ret = lstat(exe_link, &exe_stat);
109
 
    if(ret == -1){
110
 
      free(exe_link);
111
 
      if(errno != ENOENT){
112
 
        error(0, errno, "lstat");
113
 
      }
114
 
      return 0;
115
 
    }
116
 
  
117
 
    if(not S_ISLNK(exe_stat.st_mode)
118
 
       or exe_stat.st_uid != 0
119
 
       or exe_stat.st_gid != 0){
120
 
      free(exe_link);
121
 
      return 0;
122
 
    }
123
 
  
124
 
    ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
125
 
    free(exe_link);
126
 
    if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
127
 
       (memcmp(plymouthd_path, exe_target,
128
 
               sizeof(plymouthd_path)-1) != 0)){
129
 
      return 0;
130
 
    }
131
 
    return 1;
132
 
  }
133
 
 
134
 
  struct dirent **direntries;
135
 
  int ret;
136
 
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
137
 
  if (ret == -1){
138
 
    error(1, errno, "scandir");
139
 
  }
140
 
  if (ret < 0){
141
 
    return 1;
142
 
  } else {
143
 
    return 0;
144
 
  }
145
 
}
146
 
 
147
 
 
148
73
int main(int argc, char **argv){
149
74
  ssize_t sret;
150
75
  int ret;
226
151
  if(debug){
227
152
    fprintf(stderr, "Starting %s\n", argv[0]);
228
153
  }
229
 
 
230
 
  if (conflict_detection()){
231
 
    if(debug){
232
 
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
233
 
    }
234
 
    return EXIT_FAILURE;
235
 
  }
236
 
  
237
154
  if(debug){
238
155
    fprintf(stderr, "Storing current terminal attributes\n");
239
156
  }