~ubuntu-branches/ubuntu/jaunty/gnome-screensaver/jaunty

« back to all changes in this revision

Viewing changes to src/passwd.c

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2005-10-10 00:18:18 UTC
  • Revision ID: james.westby@ubuntu.com-20051010001818-3mujs05r8rht7xi1
Tags: upstream-0.0.15
ImportĀ upstreamĀ versionĀ 0.0.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * passwd.c --- verifying typed passwords with the OS.
 
4
 *
 
5
 * xscreensaver, Copyright (c) 1993-2004 Jamie Zawinski <jwz@jwz.org>
 
6
 *
 
7
 * Permission to use, copy, modify, distribute, and sell this software and its
 
8
 * documentation for any purpose is hereby granted without fee, provided that
 
9
 * the above copyright notice appear in all copies and that both that
 
10
 * copyright notice and this permission notice appear in supporting
 
11
 * documentation.  No representations are made about the suitability of this
 
12
 * software for any purpose.  It is provided "as is" without express or 
 
13
 * implied warranty.
 
14
 */
 
15
 
 
16
#include "config.h"
 
17
 
 
18
#ifndef NO_LOCKING  /* whole file */
 
19
 
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#ifdef HAVE_UNISTD_H
 
23
# include <unistd.h>
 
24
#endif
 
25
 
 
26
#include <glib.h>
 
27
 
 
28
#include "passwd.h"
 
29
 
 
30
struct auth_methods {
 
31
        const char *name;
 
32
        gboolean (*initialize)      (int argc, char **argv, gboolean verbose);
 
33
        gboolean (*priv_initialize) (int argc, char **argv, gboolean verbose);
 
34
        gboolean (*validate)        (const char *typed_passwd, gboolean verbose);
 
35
        gboolean initialized;
 
36
        gboolean priv_initialized;
 
37
};
 
38
 
 
39
 
 
40
#ifdef HAVE_KERBEROS
 
41
extern gboolean kerberos_lock_init      (int argc, char **argv, gboolean verbose);
 
42
extern gboolean kerberos_passwd_valid_p (const char *typed_passwd, gboolean verbose);
 
43
#endif
 
44
#ifdef HAVE_PAM
 
45
# include "passwd-pam.h"
 
46
#endif
 
47
#ifdef PASSWD_HELPER_PROGRAM
 
48
extern gboolean ext_priv_init           (int argc, char **argv, gboolean verbose);
 
49
extern gboolean ext_passwd_valid        (const char *typed_passwd, gboolean verbose);
 
50
#endif
 
51
#include "passwd-pwent.h"
 
52
 
 
53
/* The authorization methods to try, in order.
 
54
   Note that the last one (the pwent version) is actually two auth methods,
 
55
   since that code tries shadow passwords, and then non-shadow passwords.
 
56
   (It's all in the same file since the APIs are randomly nearly-identical.)
 
57
*/
 
58
 
 
59
static struct auth_methods methods[] = {
 
60
# ifdef HAVE_KERBEROS
 
61
        { "Kerberos",         kerberos_lock_init, NULL, kerberos_passwd_valid, FALSE, FALSE },
 
62
# endif
 
63
# ifdef HAVE_PAM
 
64
        { "PAM",              NULL, pam_priv_init, pam_passwd_valid, FALSE, FALSE },
 
65
# endif
 
66
# ifdef PASSWD_HELPER_PROGRAM
 
67
        { "external",         NULL, ext_priv_init, ext_passwd_valid, FALSE, FALSE },
 
68
#endif
 
69
        { "normal",           pwent_lock_init, pwent_priv_init, pwent_passwd_valid, FALSE, FALSE }
 
70
};
 
71
 
 
72
 
 
73
gboolean
 
74
lock_priv_init (int      argc,
 
75
                char   **argv,
 
76
                gboolean verbose)
 
77
{
 
78
        int i;
 
79
        gboolean any_ok = FALSE;
 
80
 
 
81
        for (i = 0; i < G_N_ELEMENTS (methods); i++) {
 
82
                if (verbose)
 
83
                        g_message ("priv initializing %s passwords", methods [i].name);
 
84
 
 
85
                if (!methods [i].priv_initialize)
 
86
                        methods [i].priv_initialized = TRUE;
 
87
                else
 
88
                        methods [i].priv_initialized
 
89
                                = methods [i].priv_initialize (argc, argv, verbose);
 
90
 
 
91
                if (methods [i].priv_initialized)
 
92
                        any_ok = TRUE;
 
93
                else if (verbose)
 
94
                        g_warning ("priv initialization of %s passwords failed.", methods [i].name);
 
95
        }
 
96
 
 
97
        return any_ok;
 
98
}
 
99
 
 
100
 
 
101
gboolean
 
102
lock_init (int      argc,
 
103
           char   **argv,
 
104
           gboolean verbose)
 
105
{
 
106
        int i;
 
107
        gboolean any_ok = FALSE;
 
108
 
 
109
        for (i = 0; i < G_N_ELEMENTS (methods); i++) {
 
110
                if (!methods[i].priv_initialized)       /* Bail if lock_priv_init failed. */
 
111
                        continue;
 
112
 
 
113
                if (verbose)
 
114
                        g_message ("initializing %s passwords", methods [i].name);
 
115
 
 
116
                if (!methods[i].initialize)
 
117
                        methods[i].initialized = TRUE;
 
118
                else
 
119
                        methods[i].initialized = methods [i].initialize (argc, argv, verbose);
 
120
 
 
121
                if (methods[i].initialized)
 
122
                        any_ok = TRUE;
 
123
                else if (verbose)
 
124
                        g_warning ("initialization of %s passwords failed.", methods [i].name);
 
125
        }
 
126
 
 
127
        return any_ok;
 
128
}
 
129
 
 
130
 
 
131
gboolean
 
132
validate_password (const char *typed_passwd,
 
133
                   gboolean    verbose)
 
134
{
 
135
        int i, j;
 
136
        for (i = 0; i < G_N_ELEMENTS (methods); i++) {
 
137
                int ok_p = (methods [i].initialized &&
 
138
                            methods [i].validate (typed_passwd, verbose));
 
139
 
 
140
                if (ok_p) {
 
141
                        /* If we successfully authenticated by method N, but attempting
 
142
                           to authenticate by method N-1 failed, mention that (since if
 
143
                           an earlier authentication method fails and a later one succeeds,
 
144
                           something screwy is probably going on.)
 
145
                        */
 
146
                        if (verbose && i > 0) {
 
147
                                for (j = 0; j < i; j++)
 
148
                                        if (methods [j].initialized)
 
149
                                                g_warning ("authentication via %s passwords failed.",
 
150
                                                           methods [j].name);
 
151
                                g_message ("authentication via %s passwords succeeded.",
 
152
                                           methods [i].name);
 
153
                        }
 
154
 
 
155
                        return TRUE;            /* Successfully authenticated! */
 
156
                }
 
157
        }
 
158
 
 
159
        return FALSE;                   /* Authentication failure. */
 
160
}
 
161
 
 
162
#endif /* NO_LOCKING -- whole file */