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 iff
254 (a) This is not a guest connection,
255 (b) we have a home directory defined, and
256 (c) there s not an existing static share by that name */
259 && vuser
->unix_homedir
260 && *(vuser
->unix_homedir
)
261 && (lp_servicenumber(vuser
->user
.unix_name
) == -1) )
263 DEBUG(3, ("Adding/updating homes service for user '%s' using home directory: '%s'\n",
264 vuser
->user
.unix_name
, vuser
->unix_homedir
));
266 vuser
->homes_snum
= add_home_service(vuser
->user
.unix_name
,
267 vuser
->user
.unix_name
, vuser
->unix_homedir
);
269 vuser
->homes_snum
= -1;
272 if (lp_server_signing() && !vuser
->guest
&& !srv_is_signing_active()) {
273 /* Try and turn on server signing on the first non-guest sessionsetup. */
274 srv_set_signing(vuser
->session_key
, response_blob
);
280 /****************************************************************************
281 Add a name to the session users list.
282 ****************************************************************************/
284 void add_session_user(const char *user
)
287 struct passwd
*passwd
;
289 if (!(passwd
= Get_Pwnam(user
)))
292 fstrcpy(suser
,passwd
->pw_name
);
294 if (suser
&& *suser
&& !in_list(suser
,session_users
,False
)) {
295 if (strlen(suser
) + strlen(session_users
) + 2 >= sizeof(pstring
)) {
296 DEBUG(1,("Too many session users??\n"));
298 pstrcat(session_users
," ");
299 pstrcat(session_users
,suser
);
304 /****************************************************************************
305 Check if a username is valid.
306 ****************************************************************************/
308 BOOL
user_ok(const char *user
,int snum
, gid_t
*groups
, size_t n_groups
)
310 char **valid
, **invalid
;
313 valid
= invalid
= NULL
;
316 if (lp_invalid_users(snum
)) {
317 str_list_copy(&invalid
, lp_invalid_users(snum
));
318 if (invalid
&& str_list_substitute(invalid
, "%S", lp_servicename(snum
))) {
319 if ( invalid
&& str_list_sub_basic(invalid
, current_user_info
.smb_name
) ) {
320 ret
= !user_in_list(user
, (const char **)invalid
, groups
, n_groups
);
325 str_list_free (&invalid
);
327 if (ret
&& lp_valid_users(snum
)) {
328 str_list_copy(&valid
, lp_valid_users(snum
));
329 if ( valid
&& str_list_substitute(valid
, "%S", lp_servicename(snum
)) ) {
330 if ( valid
&& str_list_sub_basic(valid
, current_user_info
.smb_name
) ) {
331 ret
= user_in_list(user
, (const char **)valid
, groups
, n_groups
);
336 str_list_free (&valid
);
338 if (ret
&& lp_onlyuser(snum
)) {
339 char **user_list
= str_list_make (lp_username(snum
), NULL
);
340 if (user_list
&& str_list_substitute(user_list
, "%S", lp_servicename(snum
))) {
341 ret
= user_in_list(user
, (const char **)user_list
, groups
, n_groups
);
343 if (user_list
) str_list_free (&user_list
);
349 /****************************************************************************
350 Validate a group username entry. Return the username or NULL.
351 ****************************************************************************/
353 static char *validate_group(char *group
, DATA_BLOB password
,int snum
)
357 char *host
, *user
, *domain
;
359 while (getnetgrent(&host
, &user
, &domain
)) {
361 if (user_ok(user
, snum
, NULL
, 0) &&
362 password_ok(user
,password
)) {
376 while ((gptr
= (struct group
*)getgrent())) {
377 if (strequal(gptr
->gr_name
,group
))
382 * As user_ok can recurse doing a getgrent(), we must
383 * copy the member list into a pstring on the stack before
384 * use. Bug pointed out by leon@eatworms.swmed.edu.
390 size_t copied_len
= 0;
394 member
= member_list
;
396 for(i
= 0; gptr
->gr_mem
&& gptr
->gr_mem
[i
]; i
++) {
397 size_t member_len
= strlen(gptr
->gr_mem
[i
]) + 1;
398 if( copied_len
+ member_len
< sizeof(pstring
)) {
400 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr
->gr_mem
[i
]));
402 safe_strcpy(member
, gptr
->gr_mem
[i
], sizeof(pstring
) - copied_len
- 1);
403 copied_len
+= member_len
;
404 member
+= copied_len
;
412 member
= member_list
;
415 fstrcpy(name
,member
);
416 if (user_ok(name
,snum
, NULL
, 0) &&
417 password_ok(name
,password
)) {
422 DEBUG(10,("validate_group = member = %s\n", member
));
424 member
+= strlen(member
) + 1;
435 /****************************************************************************
436 Check for authority to login to a service with a given username/password.
437 Note this is *NOT* used when logging on using sessionsetup_and_X.
438 ****************************************************************************/
440 BOOL
authorise_login(int snum
, fstring user
, DATA_BLOB password
,
446 DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
447 user
,password
.data
));
452 /* there are several possibilities:
453 1) login as the given user with given password
454 2) login as a previously registered username with the given password
455 3) login as a session list username with the given password
456 4) login as a previously validated user/password pair
457 5) login as the "user =" user with given password
458 6) login as the "user =" user with no password (guest connection)
459 7) login as guest user with no password
461 if the service is guest_only then steps 1 to 5 are skipped
464 /* now check the list of session users */
467 char *user_list
= strdup(session_users
);
471 for (auser
=strtok(user_list
,LIST_SEP
); !ok
&& auser
;
472 auser
= strtok(NULL
,LIST_SEP
)) {
474 fstrcpy(user2
,auser
);
475 if (!user_ok(user2
,snum
, NULL
, 0))
478 if (password_ok(user2
,password
)) {
481 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
482 and given password ok\n", user
));
486 SAFE_FREE(user_list
);
489 /* check the user= fields and the given password */
490 if (!ok
&& lp_username(snum
)) {
493 pstrcpy(user_list
,lp_username(snum
));
495 pstring_sub(user_list
,"%S",lp_servicename(snum
));
497 for (auser
=strtok(user_list
,LIST_SEP
); auser
&& !ok
;
498 auser
= strtok(NULL
,LIST_SEP
)) {
500 auser
= validate_group(auser
+1,password
,snum
);
504 DEBUG(3,("authorise_login: ACCEPTED: group username \
505 and given password ok (%s)\n", user
));
509 fstrcpy(user2
,auser
);
510 if (user_ok(user2
,snum
, NULL
, 0) && password_ok(user2
,password
)) {
513 DEBUG(3,("authorise_login: ACCEPTED: user list username \
514 and given password ok (%s)\n", user
));
520 /* check for a normal guest connection */
521 if (!ok
&& GUEST_OK(snum
)) {
523 fstrcpy(guestname
,lp_guestaccount());
524 if (Get_Pwnam(guestname
)) {
525 fstrcpy(user
,guestname
);
527 DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
530 DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname
));
535 if (ok
&& !user_ok(user
, snum
, NULL
, 0)) {
536 DEBUG(0,("authorise_login: rejected invalid user %s\n",user
));