summaryrefslogtreecommitdiffstats
path: root/driver/passwd.c
blob: 11ca134f6fa58b0f2a3e657fd4a1a6a8d0f2ccfd (plain) (blame)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* passwd.c --- verifying typed passwords with the OS.
 * xscreensaver, Copyright © 1993-2021 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>

#include <pwd.h>		/* for getpwuid() */

#ifdef HAVE_SYSLOG
# include <syslog.h>
#endif /* HAVE_SYSLOG */

#include <X11/Intrinsic.h>

#include "xscreensaver.h"
#include "auth.h"


#ifdef NO_LOCKING

Bool lock_init (void) { return 0; }
Bool lock_priv_init (void) { return 0; }
Bool xscreensaver_auth (void *closure,
                        Bool (*conv_fn) (void *closure,
                                         int nmsgs,
                                         const auth_message *msg,
                                         auth_response **resp),
                        void (*finished_fn) (void *closure, Bool status))
{
  return False;
}

#else  /* NO_LOCKING -- whole file */

extern const char *blurb(void);


/* blargh */
#undef  Bool
#undef  True
#undef  False
#define Bool  int
#define True  1
#define False 0

#undef countof
#define countof(x) (sizeof((x))/sizeof(*(x)))

struct auth_methods {
  const char *name;
  Bool (*init) (void);
  Bool (*priv_init) (void);
  Bool (*valid_p) (void *closure, const char *plaintext);
  Bool (*try_unlock) (void *closure,
                      Bool (*conv_fn) (void *closure,
                                       int nmsgs,
                                       const auth_message *msg,
                                       auth_response **resp));
  Bool initted_p;
  Bool priv_initted_p;
};



/* The authorization methods to try, in order of preference.
   The first that initializes successfully is used and others are ignored.
 */
struct auth_methods methods[] = {
# ifdef HAVE_PAM
  { "PAM",   0, pam_priv_init, 0, pam_try_unlock, 0, },
# endif
# ifdef HAVE_KERBEROS
  { "KRB",   kerberos_lock_init, 0, kerberos_passwd_valid_p, 0, },
# endif
# ifdef PASSWD_HELPER_PROGRAM
  { "EXT",   0, ext_priv_init, ext_passwd_valid_p, 0, },
# endif
  { "pwnam", pwent_lock_init, pwent_priv_init, pwent_passwd_valid_p, 0, }
};


Bool
lock_priv_init (void)
{
  int i;
  Bool any_ok = False;
  for (i = 0; i < countof(methods); i++)
    {
      if (!methods[i].priv_init)
        methods[i].priv_initted_p = True;
      else
        methods[i].priv_initted_p = methods[i].priv_init();

      if (methods[i].priv_initted_p)
        any_ok = True;
    }
  return any_ok;
}


Bool
lock_init (void)
{
  int i;
  Bool any_ok = False;
  for (i = 0; i < countof(methods); i++)
    {
      if (!methods[i].priv_initted_p)	/* Bail if lock_priv_init failed. */
        continue;

      if (!methods[i].init)
        methods[i].initted_p = True;
      else
        methods[i].initted_p = methods[i].init();

      if (methods[i].initted_p)
        any_ok = True;
      else if (verbose_p)
        fprintf (stderr, "%s: %s: passwords initialization failed\n",
                 blurb(), methods[i].name);
    }
  return any_ok;
}


/* For those auth methods that have a 'valid_p' function instead of a
   'try_unlock' function, this does a PAM-like conversation that first
   prompts for a password and then tests it with the 'valid_p' function.
 */
static Bool
try_valid_p (void *closure,
             const char *name,
             Bool (*valid_p) (void *closure, const char *typed_passwd),
             Bool (*conv_fn) (void *closure,
                              int nmsgs,
                              const auth_message *msg,
                              auth_response **resp))
{
  auth_message message;
  auth_response *response = NULL;
  Bool ok = False;

  memset (&message, 0, sizeof(message));

  if (verbose_p)
    fprintf (stderr, "%s: %s: non-PAM password auth\n", blurb(), name);

  /* Call the auth_conv function with "Password:", then feed the result
     into valid_p() */
  message.type = AUTH_MSGTYPE_PROMPT_NOECHO;
  message.msg = "Password:";

  ok = conv_fn (closure, 1, &message, &response);
  if (!response || !response->response)
    ok = False;

  if (ok)
    ok = valid_p (closure, response->response);

  if (response)
    {
      if (response->response)
        free (response->response);
      free (response);
    }

  return ok;
}


/* Write a password failure to the system log.
 */
static void
do_syslog (void)
{
# ifdef HAVE_SYSLOG
  struct passwd *pw = getpwuid (getuid ());
  char *d = getenv ("DISPLAY");
  char *u = (pw && pw->pw_name ? pw->pw_name : "???");
  int opt = 0;
  int fac = 0;
  int pri = LOG_NOTICE;

#  ifdef LOG_PID
  opt = LOG_PID;
#  endif

#  if defined(LOG_AUTHPRIV)
  fac = LOG_AUTHPRIV;
#  elif defined(LOG_AUTH)
  fac = LOG_AUTH;
#  else
  fac = LOG_DAEMON;
#  endif

  if (!d) d = "";

  openlog (progname, opt, fac);
  syslog (pri, "Failed login on display \"%s\" for \"%s\"", d, u);
  closelog ();

# endif /* HAVE_SYSLOG */
}


/* Runs through each authentication driver calling its try_unlock function.
 */
Bool
xscreensaver_auth (void *closure,
                   Bool (*conv_fn) (void *closure,
                                    int nmsgs,
                                    const auth_message *msg,
                                    auth_response **resp),
                   void (*finished_fn) (void *closure, Bool status))
{
  int i;
  Bool ok = False;

  for (i = 0; i < countof(methods); i++)
    {
      if (!methods[i].initted_p)
        continue;

      if (methods[i].try_unlock)
        ok = methods[i].try_unlock (closure, conv_fn);
      else if (methods[i].valid_p)
        ok = try_valid_p (closure, methods[i].name, methods[i].valid_p,
                          conv_fn);
      else
        abort();  /* method must have one or the other function */

      /* Only try the first method that initialized properly.  That means that
         if PAM initialized correctly, we will never try pwent or Kerberos.
         If we did, then typing an incorrect password at PAM would result in a
         second password prompt that would only go to pwent.  There's no
         sensible way to re-use the password typed the first time, if there
         even was one.  With fingerprint readers or OTP fobs, there might have
         been 0, 2, or more passwords entered. */
      break;
    }

  if (!ok)
    do_syslog ();

  if (finished_fn)
    finished_fn (closure, ok);

  return ok;
}

#endif /* NO_LOCKING -- whole file */