Fix typo in license headers
[oweals/cde.git] / cde / lib / pam / pam_modules / sample / sample_authenticate.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: sample_authenticate.c /main/2 1996/05/09 04:29:50 drk $ */
24 /*
25  * Copyright (c) 1995, by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28
29 #ident  "@(#)sample_authenticate.c 1.14     96/01/15 SMI"
30
31 #include <security/pam_appl.h>
32 #include <security/pam_modules.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <pwd.h>
38 #include <syslog.h>
39 #include <libintl.h>
40
41 #include "sample_utils.h"
42
43 #define SLEEPTIME       4
44
45 /*
46  *
47  * Sample module for pam_sm_authenticate.
48  *
49  * options -
50  *
51  *      debug
52  *      use_first_pass
53  *      try_first_pass
54  *      first_pass_good  (first password is always good when used with use/try)
55  *      first_pass_bad   (first password is always bad when used with use/try)
56  *      pass=foobar      (set good password to "foobar". default good password
57  *                       is test)
58  *      always_fail      always return PAM_AUTH_ERR
59  *      always_succeed   always return PAM_SUCCESS
60  *      always_ignore
61  *
62  *
63  */
64
65 /*
66  * pam_sm_authenticate          - Authenticate user
67  */
68
69 int
70 pam_sm_authenticate(
71         pam_handle_t            *pamh,
72         int                     flags,
73         int                     argc,
74         const char              **argv)
75 {
76         char                    *user;
77         struct pam_conv         *pam_convp;
78         int                     err, result = PAM_AUTH_ERR;
79         struct pam_response     *ret_resp = (struct pam_response *)0;
80         char                    messages[PAM_MAX_NUM_MSG][PAM_MAX_MSG_SIZE];
81         int                     debug = 0;
82         int                     try_first_pass = 0;
83         int                     use_first_pass = 0;
84         int                     first_pass_good = 0;
85         int                     first_pass_bad = 0;
86         int                     i, num_msg;
87         char                    *firstpass, *password;
88         char                    the_password[64];
89
90         if (debug)
91                 syslog(LOG_DEBUG, "Sample Authentication\n");
92
93         strcpy(the_password, "test");
94
95         for (i = 0; i < argc; i++) {
96                 if (strcmp(argv[i], "debug") == 0)
97                         debug = 1;
98                 else if (strcmp(argv[i], "try_first_pass") == 0)
99                         try_first_pass = 1;
100                 else if (strcmp(argv[i], "first_pass_good") == 0)
101                         first_pass_good = 1;
102                 else if (strcmp(argv[i], "first_pass_bad") == 0)
103                         first_pass_bad = 1;
104                 else if (strcmp(argv[i], "use_first_pass") == 0)
105                         use_first_pass = 1;
106                 else if (strcmp(argv[i], "always_fail") == 0)
107                         return (PAM_AUTH_ERR);
108                 else if (strcmp(argv[i], "always_succeed") == 0)
109                         return (PAM_SUCCESS);
110                 else if (strcmp(argv[i], "always_ignore") == 0)
111                         return (PAM_IGNORE);
112                 else if (sscanf(argv[i], "pass=%s", the_password) == 1) {
113                         /* nothing */;
114                 }
115                 else
116                         syslog(LOG_DEBUG, "illegal scheme option %s", argv[i]);
117         }
118
119         err = pam_get_item(pamh, PAM_USER, (void**) &user);
120         if (err != PAM_SUCCESS)
121                 return (err);
122
123         err = pam_get_item(pamh, PAM_CONV, (void**) &pam_convp);
124         if (err != PAM_SUCCESS)
125                 return (err);
126
127         (void) pam_get_item(pamh, PAM_AUTHTOK, (void **) &firstpass);
128
129         if (firstpass && (use_first_pass || try_first_pass)) {
130
131                 if ((first_pass_good ||
132                         strcmp(firstpass, the_password) == 0) &&
133                                 !first_pass_bad) {
134                                         result = PAM_SUCCESS;
135                                         goto out;
136                 }
137                 if (use_first_pass) goto out;
138         }
139
140         /*
141          * Get the password from the user
142          */
143         if (firstpass) {
144                 (void) sprintf(messages[0], (const char *) PAM_MSG(pamh, 1,
145                         "TEST Password: "));
146         } else {
147                 (void) sprintf(messages[0], (const char *) PAM_MSG(pamh, 2,
148                         "Password: "));
149         }
150         num_msg = 1;
151         err = get_authtok(pam_convp->conv,
152                                 num_msg, messages, NULL, &ret_resp);
153
154         if (err != PAM_SUCCESS) {
155                 result = err;
156                 goto out;
157         }
158
159         password = ret_resp->resp;
160
161         if (password == NULL) {
162                 result = PAM_AUTH_ERR;
163                 goto out;
164         }
165
166         /* one last ditch attempt to "login" to TEST */
167
168         if (strcmp(password, the_password) == 0) {
169                 result = PAM_SUCCESS;
170                 if (firstpass == NULL) {
171                 /* this is the first password, stash it away */
172                 pam_set_item(pamh, PAM_AUTHTOK, password);
173                 }
174         }
175
176 out:
177         if (num_msg > 0) {
178                 if (ret_resp != 0) {
179                         if (ret_resp->resp != 0) {
180                                 /* avoid leaving password cleartext around */
181                                 memset(ret_resp->resp, 0,
182                                         strlen(ret_resp->resp));
183                         }
184                         free_resp(num_msg, ret_resp);
185                         ret_resp = 0;
186                 }
187         }
188
189         return (result);
190 }