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 /****************************************************************************
32 check if a uid has been validated, and return an pointer to the user_struct
33 if it has. NULL if not. vuid is biased by an offset. This allows us to
34 tell random client vuid's (normally zero) from valid vuids.
35 ****************************************************************************/
36 user_struct
*get_valid_user_struct(uint16 vuid
)
41 if (vuid
== UID_FIELD_INVALID
)
44 for (usp
=validated_users
;usp
;usp
=usp
->next
,count
++) {
45 if (vuid
== usp
->vuid
) {
47 DLIST_PROMOTE(validated_users
, usp
);
56 /****************************************************************************
58 ****************************************************************************/
59 void invalidate_vuid(uint16 vuid
)
61 user_struct
*vuser
= get_valid_user_struct(vuid
);
66 SAFE_FREE(vuser
->homedir
);
70 DLIST_REMOVE(validated_users
, vuser
);
72 /* clear the vuid from the 'cache' on each connection, and
73 from the vuid 'owner' of connections */
74 conn_clear_vuid_cache(vuid
);
76 SAFE_FREE(vuser
->groups
);
77 delete_nt_token(&vuser
->nt_user_token
);
79 num_validated_vuids
--;
82 /****************************************************************************
83 invalidate all vuid entries for this process
84 ****************************************************************************/
85 void invalidate_all_vuids(void)
87 user_struct
*usp
, *next
=NULL
;
89 for (usp
=validated_users
;usp
;usp
=next
) {
92 invalidate_vuid(usp
->vuid
);
96 /****************************************************************************
97 register a uid/name pair as being valid and that a valid password
98 has been given. vuid is biased by an offset. This allows us to
99 tell random client vuid's (normally zero) from valid vuids.
100 ****************************************************************************/
102 int register_vuid(auth_serversupplied_info
*server_info
, const char *smb_name
)
104 user_struct
*vuser
= NULL
;
106 /* Ensure no vuid gets registered in share level security. */
107 if(lp_security() == SEC_SHARE
)
108 return UID_FIELD_INVALID
;
110 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
111 if (num_validated_vuids
>= 0xFFFF-VUID_OFFSET
)
112 return UID_FIELD_INVALID
;
114 if((vuser
= (user_struct
*)malloc( sizeof(user_struct
) )) == NULL
) {
115 DEBUG(0,("Failed to malloc users struct!\n"));
116 return UID_FIELD_INVALID
;
121 /* Allocate a free vuid. Yes this is a linear search... :-) */
122 while( get_valid_user_struct(next_vuid
) != NULL
) {
124 /* Check for vuid wrap. */
125 if (next_vuid
== UID_FIELD_INVALID
)
126 next_vuid
= VUID_OFFSET
;
129 DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid
));
131 vuser
->vuid
= next_vuid
;
133 /* the next functions should be done by a SID mapping system (SMS) as
134 * the new real sam db won't have reference to unix uids or gids
136 if (!IS_SAM_UNIX_USER(server_info
->sam_account
)) {
137 DEBUG(0,("Attempted session setup with invalid user. No uid/gid in SAM_ACCOUNT\n"));
139 return UID_FIELD_INVALID
;
142 vuser
->uid
= pdb_get_uid(server_info
->sam_account
);
143 vuser
->gid
= pdb_get_gid(server_info
->sam_account
);
145 vuser
->n_groups
= server_info
->n_groups
;
146 if (vuser
->n_groups
) {
147 if (!(vuser
->groups
= memdup(server_info
->groups
, sizeof(gid_t
) * vuser
->n_groups
))) {
148 DEBUG(0,("register_vuid: failed to memdup vuser->groups\n"));
150 return UID_FIELD_INVALID
;
154 vuser
->guest
= server_info
->guest
;
155 fstrcpy(vuser
->user
.unix_name
, pdb_get_username(server_info
->sam_account
));
156 fstrcpy(vuser
->user
.smb_name
, smb_name
);
157 fstrcpy(vuser
->user
.domain
, pdb_get_domain(server_info
->sam_account
));
158 fstrcpy(vuser
->user
.full_name
, pdb_get_fullname(server_info
->sam_account
));
161 /* Keep the homedir handy */
162 const char *homedir
= pdb_get_homedir(server_info
->sam_account
);
163 const char *unix_homedir
= pdb_get_unix_homedir(server_info
->sam_account
); /* should be optained by SMS */
164 const char *logon_script
= pdb_get_logon_script(server_info
->sam_account
);
166 vuser
->homedir
= smb_xstrdup(homedir
);
170 vuser
->unix_homedir
= smb_xstrdup(unix_homedir
);
174 vuser
->logon_script
= smb_xstrdup(logon_script
);
178 memcpy(vuser
->session_key
, server_info
->session_key
, sizeof(vuser
->session_key
));
180 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
181 (unsigned int)vuser
->uid
,
182 (unsigned int)vuser
->gid
,
183 vuser
->user
.unix_name
, vuser
->user
.smb_name
, vuser
->user
.domain
, vuser
->guest
));
185 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser
->user
.unix_name
,vuser
->user
.full_name
));
187 if (server_info
->ptok
) {
188 vuser
->nt_user_token
= dup_nt_token(server_info
->ptok
);
190 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
192 return UID_FIELD_INVALID
;
195 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",(int)vuser
->uid
,vuser
->user
.unix_name
, vuser
->vuid
));
198 num_validated_vuids
++;
200 DLIST_ADD(validated_users
, vuser
);
202 if (!session_claim(vuser
)) {
203 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser
->vuid
));
204 invalidate_vuid(vuser
->vuid
);
208 /* Register a home dir service for this user */
209 if ((!vuser
->guest
) && vuser
->unix_homedir
&& *(vuser
->unix_homedir
)) {
210 DEBUG(3, ("Adding/updating homes service for user '%s' using home direcotry: '%s'\n",
211 vuser
->user
.unix_name
, vuser
->unix_homedir
));
212 vuser
->homes_snum
= add_home_service(vuser
->user
.unix_name
, vuser
->user
.unix_name
, vuser
->unix_homedir
);
214 vuser
->homes_snum
= -1;
221 /****************************************************************************
222 add a name to the session users list
223 ****************************************************************************/
224 void add_session_user(const char *user
)
227 struct passwd
*passwd
;
229 if (!(passwd
= Get_Pwnam(user
))) return;
231 StrnCpy(suser
,passwd
->pw_name
,sizeof(suser
)-1);
233 if (suser
&& *suser
&& !in_list(suser
,session_users
,False
))
235 if (strlen(suser
) + strlen(session_users
) + 2 >= sizeof(pstring
))
236 DEBUG(1,("Too many session users??\n"));
239 pstrcat(session_users
," ");
240 pstrcat(session_users
,suser
);
246 /****************************************************************************
247 check if a username is valid
248 ****************************************************************************/
249 BOOL
user_ok(const char *user
,int snum
)
251 char **valid
, **invalid
;
254 valid
= invalid
= NULL
;
257 if (lp_invalid_users(snum
)) {
258 str_list_copy(&invalid
, lp_invalid_users(snum
));
259 if (invalid
&& str_list_substitute(invalid
, "%S", lp_servicename(snum
))) {
260 ret
= !user_in_list(user
, (const char **)invalid
);
264 str_list_free (&invalid
);
266 if (ret
&& lp_valid_users(snum
)) {
267 str_list_copy(&valid
, lp_valid_users(snum
));
268 if (valid
&& str_list_substitute(valid
, "%S", lp_servicename(snum
))) {
269 ret
= user_in_list(user
, (const char **)valid
);
273 str_list_free (&valid
);
275 if (ret
&& lp_onlyuser(snum
)) {
276 char **user_list
= str_list_make (lp_username(snum
), NULL
);
277 if (user_list
&& str_list_substitute(user_list
, "%S", lp_servicename(snum
))) {
278 ret
= user_in_list(user
, (const char **)user_list
);
280 if (user_list
) str_list_free (&user_list
);
286 /****************************************************************************
287 validate a group username entry. Return the username or NULL
288 ****************************************************************************/
289 static char *validate_group(char *group
, DATA_BLOB password
,int snum
)
293 char *host
, *user
, *domain
;
295 while (getnetgrent(&host
, &user
, &domain
)) {
297 if (user_ok(user
, snum
) &&
298 password_ok(user
,password
)) {
312 while ((gptr
= (struct group
*)getgrent())) {
313 if (strequal(gptr
->gr_name
,group
))
318 * As user_ok can recurse doing a getgrent(), we must
319 * copy the member list into a pstring on the stack before
320 * use. Bug pointed out by leon@eatworms.swmed.edu.
326 size_t copied_len
= 0;
330 member
= member_list
;
332 for(i
= 0; gptr
->gr_mem
&& gptr
->gr_mem
[i
]; i
++) {
333 size_t member_len
= strlen(gptr
->gr_mem
[i
]) + 1;
334 if( copied_len
+ member_len
< sizeof(pstring
)) {
336 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr
->gr_mem
[i
]));
338 safe_strcpy(member
, gptr
->gr_mem
[i
], sizeof(pstring
) - copied_len
- 1);
339 copied_len
+= member_len
;
340 member
+= copied_len
;
348 member
= member_list
;
351 fstrcpy(name
,member
);
352 if (user_ok(name
,snum
) &&
353 password_ok(name
,password
)) {
358 DEBUG(10,("validate_group = member = %s\n", member
));
360 member
+= strlen(member
) + 1;
371 /****************************************************************************
372 Check for authority to login to a service with a given username/password.
373 Note this is *NOT* used when logging on using sessionsetup_and_X.
374 ****************************************************************************/
376 BOOL
authorise_login(int snum
, fstring user
, DATA_BLOB password
,
382 DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
383 user
,password
.data
));
388 /* there are several possibilities:
389 1) login as the given user with given password
390 2) login as a previously registered username with the given password
391 3) login as a session list username with the given password
392 4) login as a previously validated user/password pair
393 5) login as the "user =" user with given password
394 6) login as the "user =" user with no password (guest connection)
395 7) login as guest user with no password
397 if the service is guest_only then steps 1 to 5 are skipped
400 /* now check the list of session users */
403 char *user_list
= strdup(session_users
);
407 for (auser
=strtok(user_list
,LIST_SEP
); !ok
&& auser
;
408 auser
= strtok(NULL
,LIST_SEP
)) {
410 fstrcpy(user2
,auser
);
411 if (!user_ok(user2
,snum
))
414 if (password_ok(user2
,password
)) {
417 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
418 and given password ok\n", user
));
422 SAFE_FREE(user_list
);
425 /* check the user= fields and the given password */
426 if (!ok
&& lp_username(snum
)) {
429 StrnCpy(user_list
,lp_username(snum
),sizeof(pstring
));
431 pstring_sub(user_list
,"%S",lp_servicename(snum
));
433 for (auser
=strtok(user_list
,LIST_SEP
); auser
&& !ok
;
434 auser
= strtok(NULL
,LIST_SEP
)) {
436 auser
= validate_group(auser
+1,password
,snum
);
440 DEBUG(3,("authorise_login: ACCEPTED: group username \
441 and given password ok (%s)\n", user
));
445 fstrcpy(user2
,auser
);
446 if (user_ok(user2
,snum
) && password_ok(user2
,password
)) {
449 DEBUG(3,("authorise_login: ACCEPTED: user list username \
450 and given password ok (%s)\n", user
));
456 /* check for a normal guest connection */
457 if (!ok
&& GUEST_OK(snum
)) {
459 StrnCpy(guestname
,lp_guestaccount(),sizeof(guestname
)-1);
460 if (Get_Pwnam(guestname
)) {
461 fstrcpy(user
,guestname
);
463 DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
466 DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname
));
471 if (ok
&& !user_ok(user
,snum
)) {
472 DEBUG(0,("authorise_login: rejected invalid user %s\n",user
));