2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* users from session setup */
24 static pstring session_users="";
26 /* this holds info on user ids that are already validated for this VC */
27 static user_struct *validated_users;
28 static int next_vuid = VUID_OFFSET;
29 static int num_validated_vuids;
31 extern userdom_struct current_user_info;
34 /****************************************************************************
35 Check if a uid has been validated, and return an pointer to the user_struct
36 if it has. NULL if not. vuid is biased by an offset. This allows us to
37 tell random client vuid's (normally zero) from valid vuids.
38 ****************************************************************************/
40 user_struct *get_valid_user_struct(uint16 vuid)
45 if (vuid == UID_FIELD_INVALID)
48 for (usp=validated_users;usp;usp=usp->next,count++) {
49 if (vuid == usp->vuid) {
51 DLIST_PROMOTE(validated_users, usp);
60 /****************************************************************************
62 ****************************************************************************/
64 void invalidate_vuid(uint16 vuid)
66 user_struct *vuser = get_valid_user_struct(vuid);
71 SAFE_FREE(vuser->homedir);
72 SAFE_FREE(vuser->unix_homedir);
73 SAFE_FREE(vuser->logon_script);
76 SAFE_FREE(vuser->session_keystr);
78 free_server_info(&vuser->server_info);
80 data_blob_free(&vuser->session_key);
82 DLIST_REMOVE(validated_users, vuser);
84 /* clear the vuid from the 'cache' on each connection, and
85 from the vuid 'owner' of connections */
86 conn_clear_vuid_cache(vuid);
88 SAFE_FREE(vuser->groups);
89 delete_nt_token(&vuser->nt_user_token);
91 num_validated_vuids--;
94 /****************************************************************************
95 Invalidate all vuid entries for this process.
96 ****************************************************************************/
98 void invalidate_all_vuids(void)
100 user_struct *usp, *next=NULL;
102 for (usp=validated_users;usp;usp=next) {
105 invalidate_vuid(usp->vuid);
110 * register that a valid login has been performed, establish 'session'.
111 * @param server_info The token returned from the authentication process.
112 * (now 'owned' by register_vuid)
114 * @param session_key The User session key for the login session (now also 'owned' by register_vuid)
116 * @param respose_blob The NT challenge-response, if available. (May be freed after this call)
118 * @param smb_name The untranslated name of the user
120 * @return Newly allocated vuid, biased by an offset. (This allows us to
121 * tell random client vuid's (normally zero) from valid vuids.)
125 int register_vuid(auth_serversupplied_info *server_info, DATA_BLOB session_key, DATA_BLOB response_blob, const char *smb_name)
127 user_struct *vuser = NULL;
129 /* Ensure no vuid gets registered in share level security. */
130 if(lp_security() == SEC_SHARE) {
131 data_blob_free(&session_key);
132 return UID_FIELD_INVALID;
135 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
136 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
137 data_blob_free(&session_key);
138 return UID_FIELD_INVALID;
141 if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
142 DEBUG(0,("Failed to malloc users struct!\n"));
143 data_blob_free(&session_key);
144 return UID_FIELD_INVALID;
149 /* Allocate a free vuid. Yes this is a linear search... :-) */
150 while( get_valid_user_struct(next_vuid) != NULL ) {
152 /* Check for vuid wrap. */
153 if (next_vuid == UID_FIELD_INVALID)
154 next_vuid = VUID_OFFSET;
157 DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid ));
159 vuser->vuid = next_vuid;
161 /* the next functions should be done by a SID mapping system (SMS) as
162 * the new real sam db won't have reference to unix uids or gids
165 vuser->uid = server_info->uid;
166 vuser->gid = server_info->gid;
168 vuser->n_groups = server_info->n_groups;
169 if (vuser->n_groups) {
170 if (!(vuser->groups = memdup(server_info->groups, sizeof(gid_t) * vuser->n_groups))) {
171 DEBUG(0,("register_vuid: failed to memdup vuser->groups\n"));
172 data_blob_free(&session_key);
174 free_server_info(&server_info);
175 return UID_FIELD_INVALID;
179 vuser->guest = server_info->guest;
180 fstrcpy(vuser->user.unix_name, server_info->unix_name);
182 /* This is a potentially untrusted username */
183 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$", sizeof(vuser->user.smb_name));
185 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
186 fstrcpy(vuser->user.full_name, pdb_get_fullname(server_info->sam_account));
189 /* Keep the homedir handy */
190 const char *homedir = pdb_get_homedir(server_info->sam_account);
191 const char *logon_script = pdb_get_logon_script(server_info->sam_account);
193 if (!IS_SAM_DEFAULT(server_info->sam_account, PDB_UNIXHOMEDIR)) {
194 const char *unix_homedir = pdb_get_unix_homedir(server_info->sam_account);
196 vuser->unix_homedir = smb_xstrdup(unix_homedir);
199 struct passwd *passwd = getpwnam_alloc(vuser->user.unix_name);
201 vuser->unix_homedir = smb_xstrdup(passwd->pw_dir);
202 passwd_free(&passwd);
207 vuser->homedir = smb_xstrdup(homedir);
210 vuser->logon_script = smb_xstrdup(logon_script);
214 vuser->session_key = session_key;
216 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
217 (unsigned int)vuser->uid,
218 (unsigned int)vuser->gid,
219 vuser->user.unix_name, vuser->user.smb_name, vuser->user.domain, vuser->guest ));
221 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,vuser->user.full_name));
223 if (server_info->ptok) {
224 vuser->nt_user_token = dup_nt_token(server_info->ptok);
226 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
227 free_server_info(&server_info);
228 data_blob_free(&session_key);
229 SAFE_FREE(vuser->homedir);
230 SAFE_FREE(vuser->unix_homedir);
231 SAFE_FREE(vuser->logon_script);
234 return UID_FIELD_INVALID;
237 /* use this to keep tabs on all our info from the authentication */
238 vuser->server_info = server_info;
240 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",(int)vuser->uid,vuser->user.unix_name, vuser->vuid));
243 num_validated_vuids++;
245 DLIST_ADD(validated_users, vuser);
247 if (!session_claim(vuser)) {
248 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
249 invalidate_vuid(vuser->vuid);
253 /* Register a home dir service for this user */
254 if ((!vuser->guest) && vuser->unix_homedir && *(vuser->unix_homedir)) {
255 DEBUG(3, ("Adding/updating homes service for user '%s' using home directory: '%s'\n",
256 vuser->user.unix_name, vuser->unix_homedir));
257 vuser->homes_snum = add_home_service(vuser->user.unix_name, vuser->user.unix_name, vuser->unix_homedir);
259 vuser->homes_snum = -1;
262 if (lp_server_signing() && !vuser->guest && !srv_is_signing_active()) {
263 /* Try and turn on server signing on the first non-guest sessionsetup. */
264 srv_set_signing(vuser->session_key, response_blob);
270 /****************************************************************************
271 Add a name to the session users list.
272 ****************************************************************************/
274 void add_session_user(const char *user)
277 struct passwd *passwd;
279 if (!(passwd = Get_Pwnam(user)))
282 fstrcpy(suser,passwd->pw_name);
284 if (suser && *suser && !in_list(suser,session_users,False)) {
285 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring)) {
286 DEBUG(1,("Too many session users??\n"));
288 pstrcat(session_users," ");
289 pstrcat(session_users,suser);
294 /****************************************************************************
295 Check if a username is valid.
296 ****************************************************************************/
298 BOOL user_ok(const char *user,int snum, gid_t *groups, size_t n_groups)
300 char **valid, **invalid;
303 valid = invalid = NULL;
306 if (lp_invalid_users(snum)) {
307 str_list_copy(&invalid, lp_invalid_users(snum));
308 if (invalid && str_list_substitute(invalid, "%S", lp_servicename(snum))) {
309 if ( invalid && str_list_sub_basic(invalid, current_user_info.smb_name) ) {
310 ret = !user_in_list(user, (const char **)invalid, groups, n_groups);
315 str_list_free (&invalid);
317 if (ret && lp_valid_users(snum)) {
318 str_list_copy(&valid, lp_valid_users(snum));
319 if ( valid && str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
320 if ( valid && str_list_sub_basic(valid, current_user_info.smb_name) ) {
321 ret = user_in_list(user, (const char **)valid, groups, n_groups);
326 str_list_free (&valid);
328 if (ret && lp_onlyuser(snum)) {
329 char **user_list = str_list_make (lp_username(snum), NULL);
330 if (user_list && str_list_substitute(user_list, "%S", lp_servicename(snum))) {
331 ret = user_in_list(user, (const char **)user_list, groups, n_groups);
333 if (user_list) str_list_free (&user_list);
339 /****************************************************************************
340 Validate a group username entry. Return the username or NULL.
341 ****************************************************************************/
343 static char *validate_group(char *group, DATA_BLOB password,int snum)
347 char *host, *user, *domain;
349 while (getnetgrent(&host, &user, &domain)) {
351 if (user_ok(user, snum, NULL, 0) &&
352 password_ok(user,password)) {
366 while ((gptr = (struct group *)getgrent())) {
367 if (strequal(gptr->gr_name,group))
372 * As user_ok can recurse doing a getgrent(), we must
373 * copy the member list into a pstring on the stack before
374 * use. Bug pointed out by leon@eatworms.swmed.edu.
380 size_t copied_len = 0;
384 member = member_list;
386 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
387 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
388 if( copied_len + member_len < sizeof(pstring)) {
390 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
392 safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
393 copied_len += member_len;
394 member += copied_len;
402 member = member_list;
405 fstrcpy(name,member);
406 if (user_ok(name,snum, NULL, 0) &&
407 password_ok(name,password)) {
412 DEBUG(10,("validate_group = member = %s\n", member));
414 member += strlen(member) + 1;
425 /****************************************************************************
426 Check for authority to login to a service with a given username/password.
427 Note this is *NOT* used when logging on using sessionsetup_and_X.
428 ****************************************************************************/
430 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
436 DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
437 user,password.data));
442 /* there are several possibilities:
443 1) login as the given user with given password
444 2) login as a previously registered username with the given password
445 3) login as a session list username with the given password
446 4) login as a previously validated user/password pair
447 5) login as the "user =" user with given password
448 6) login as the "user =" user with no password (guest connection)
449 7) login as guest user with no password
451 if the service is guest_only then steps 1 to 5 are skipped
454 /* now check the list of session users */
457 char *user_list = strdup(session_users);
461 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
462 auser = strtok(NULL,LIST_SEP)) {
464 fstrcpy(user2,auser);
465 if (!user_ok(user2,snum, NULL, 0))
468 if (password_ok(user2,password)) {
471 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
472 and given password ok\n", user));
476 SAFE_FREE(user_list);
479 /* check the user= fields and the given password */
480 if (!ok && lp_username(snum)) {
483 pstrcpy(user_list,lp_username(snum));
485 pstring_sub(user_list,"%S",lp_servicename(snum));
487 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
488 auser = strtok(NULL,LIST_SEP)) {
490 auser = validate_group(auser+1,password,snum);
494 DEBUG(3,("authorise_login: ACCEPTED: group username \
495 and given password ok (%s)\n", user));
499 fstrcpy(user2,auser);
500 if (user_ok(user2,snum, NULL, 0) && password_ok(user2,password)) {
503 DEBUG(3,("authorise_login: ACCEPTED: user list username \
504 and given password ok (%s)\n", user));
510 /* check for a normal guest connection */
511 if (!ok && GUEST_OK(snum)) {
513 fstrcpy(guestname,lp_guestaccount());
514 if (Get_Pwnam(guestname)) {
515 fstrcpy(user,guestname);
517 DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
520 DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
525 if (ok && !user_ok(user, snum, NULL, 0)) {
526 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));