updates from 2.2
[vlendec/samba-autobuild/.git] / source3 / pam_smbpass / pam_smb_auth.c
1 /* Unix NT password database implementation, version 0.7.5.
2  *
3  * This program is free software; you can redistribute it and/or modify it under
4  * the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 675
15  * Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18 /* indicate the following groups are defined */
19 #define PAM_SM_AUTH
20
21 #include "includes.h"
22 #include "debug.h"
23
24 #ifndef LINUX
25
26 /* This is only used in the Sun implementation. */
27 #include <security/pam_appl.h>
28
29 #endif  /* LINUX */
30
31 #include <security/pam_modules.h>
32
33 #include "general.h"
34
35 #include "support.h"
36
37 #define AUTH_RETURN                                             \
38 do {                                                            \
39         if(ret_data) {                                          \
40                 *ret_data = retval;                             \
41                 pam_set_data( pamh, "smb_setcred_return"        \
42                               , (void *) ret_data, NULL );      \
43         }                                                       \
44         return retval;                                          \
45 } while (0)
46
47 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
48                          const char *name, SAM_ACCOUNT *sampass, BOOL exist);
49
50 /*
51  * pam_sm_authenticate() authenticates users against the samba password file.
52  *
53  *      First, obtain the password from the user. Then use a
54  *      routine in 'support.c' to authenticate the user.
55  */
56
57 #define _SMB_AUTHTOK  "-SMB-PASS"
58
59 int pam_sm_authenticate(pam_handle_t *pamh, int flags,
60                         int argc, const char **argv)
61 {
62     unsigned int ctrl;
63     int retval, *ret_data = NULL;
64     SAM_ACCOUNT *sampass = NULL;
65     extern BOOL in_client;
66     const char *name;
67     BOOL found;
68
69     /* Points to memory managed by the PAM library. Do not free. */
70     const char *p = NULL;
71
72
73     /* Samba initialization. */
74     setup_logging("pam_smbpass",False);
75     charset_initialise();
76     codepage_initialise(lp_client_code_page());
77     in_client = True;
78
79     ctrl = set_ctrl(flags, argc, argv);
80
81     /* Get a few bytes so we can pass our return value to
82        pam_sm_setcred(). */
83     ret_data = malloc(sizeof(int));
84
85     /* get the username */
86     retval = pam_get_user( pamh, &name, "Username: " );
87     if ( retval != PAM_SUCCESS ) {
88         if (on( SMB_DEBUG, ctrl )) {
89             _log_err(LOG_DEBUG, "auth: could not identify user");
90         }
91         AUTH_RETURN;
92     }
93     if (on( SMB_DEBUG, ctrl )) {
94         _log_err( LOG_DEBUG, "username [%s] obtained", name );
95     }
96
97     if (!initialize_password_db(True)) {
98         _log_err( LOG_ALERT, "Cannot access samba password database" );
99         retval = PAM_AUTHINFO_UNAVAIL;
100         AUTH_RETURN;
101     }
102
103     pdb_init_sam(&sampass);
104     
105     found = pdb_getsampwnam( sampass, name );
106
107     if (on( SMB_MIGRATE, ctrl )) {
108         retval = _smb_add_user(pamh, ctrl, name, sampass, found);
109         pdb_free_sam(&sampass);
110         AUTH_RETURN;
111     }
112
113     if (!found) {
114         _log_err(LOG_ALERT, "Failed to find entry for user %s.", name);
115         retval = PAM_USER_UNKNOWN;
116         pdb_free_sam(&sampass);
117         sampass = NULL;
118         AUTH_RETURN;
119     }
120    
121     /* if this user does not have a password... */
122
123     if (_smb_blankpasswd( ctrl, sampass )) {
124         pdb_free_sam(&sampass);
125         retval = PAM_SUCCESS;
126         AUTH_RETURN;
127     }
128
129     /* get this user's authentication token */
130
131     retval = _smb_read_password(pamh, ctrl, NULL, "Password: ", NULL, _SMB_AUTHTOK, &p);
132     if (retval != PAM_SUCCESS ) {
133         _log_err(LOG_CRIT, "auth: no password provided for [%s]"
134                  , name);
135         pdb_free_sam(&sampass);
136         AUTH_RETURN;
137     }
138
139     /* verify the password of this user */
140
141     retval = _smb_verify_password( pamh, sampass, p, ctrl );
142     pdb_free_sam(&sampass);
143     p = NULL;
144     AUTH_RETURN;
145 }
146
147 /*
148  * This function is for setting samba credentials.  If anyone comes up
149  * with any credentials they think should be set, let me know.
150  */
151
152 int pam_sm_setcred(pam_handle_t *pamh, int flags,
153                    int argc, const char **argv)
154 {
155     int retval, *pretval = NULL;
156
157     retval = PAM_SUCCESS;
158
159     pam_get_data(pamh, "smb_setcred_return", (const void **) &pretval);
160     if(pretval) {
161         retval = *pretval;
162         SAFE_FREE(pretval);
163     }
164     pam_set_data(pamh, "smb_setcred_return", NULL, NULL);
165
166     return retval;
167 }
168
169
170 /* Helper function for adding a user to the db. */
171 static int _smb_add_user(pam_handle_t *pamh, unsigned int ctrl,
172                          const char *name, SAM_ACCOUNT *sampass, BOOL exist)
173 {
174     pstring err_str;
175     pstring msg_str;
176     const char *pass = NULL;
177     int retval;
178
179     err_str[0] = '\0';
180     msg_str[0] = '\0';
181
182     /* Get the authtok; if we don't have one, silently fail. */
183     retval = pam_get_item( pamh, PAM_AUTHTOK, (const void **) &pass );
184
185     if (retval != PAM_SUCCESS) {
186         _log_err( LOG_ALERT
187                   , "pam_get_item returned error to pam_sm_authenticate" );
188         return PAM_AUTHTOK_RECOVER_ERR;
189     } else if (pass == NULL) {
190         return PAM_AUTHTOK_RECOVER_ERR;
191     }
192
193     /* Add the user to the db if they aren't already there. */
194    if (!exist) {
195         retval = local_password_change( name, LOCAL_ADD_USER,
196                                          pass, err_str,
197                                          sizeof(err_str),
198                                          msg_str, sizeof(msg_str) );
199         if (!retval && *err_str)
200         {
201             err_str[PSTRING_LEN-1] = '\0';
202             make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
203         }
204         else if (*msg_str)
205         {
206             msg_str[PSTRING_LEN-1] = '\0';
207             make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
208         }
209         pass = NULL;
210
211         return PAM_IGNORE;
212    }
213    else {
214     /* Change the user's password IFF it's null. */
215     if ((pdb_get_lanman_passwd(sampass) == NULL) && (pdb_get_acct_ctrl(sampass) & ACB_PWNOTREQ))
216     {
217         retval = local_password_change( name, 0, pass, err_str, sizeof(err_str),
218                                          msg_str, sizeof(msg_str) );
219         if (!retval && *err_str)
220         {
221             err_str[PSTRING_LEN-1] = '\0';
222             make_remark( pamh, ctrl, PAM_ERROR_MSG, err_str );
223         }
224         else if (*msg_str)
225         {
226             msg_str[PSTRING_LEN-1] = '\0';
227             make_remark( pamh, ctrl, PAM_TEXT_INFO, msg_str );
228         }
229     }
230    }
231     
232     pass = NULL;
233
234     return PAM_IGNORE;
235 }
236
237
238 /* static module data */
239 #ifdef PAM_STATIC
240 struct pam_module _pam_smbpass_auth_modstruct = {
241      "pam_smbpass",
242      pam_sm_authenticate,
243      pam_sm_setcred,
244      NULL,
245      NULL,
246      NULL,
247      NULL
248 };
249 #endif
250