missed one on BUG 1195; make sure to set the private * to NULL
[Samba/gebeck_regimport.git] / source3 / smbd / password.c
blob9449113ddc550d4060fe37c68b2ed915b37cf80c
1 /*
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.
21 #include "includes.h"
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)
42 user_struct *usp;
43 int count=0;
45 if (vuid == UID_FIELD_INVALID)
46 return NULL;
48 for (usp=validated_users;usp;usp=usp->next,count++) {
49 if (vuid == usp->vuid) {
50 if (count > 10) {
51 DLIST_PROMOTE(validated_users, usp);
53 return usp;
57 return NULL;
60 /****************************************************************************
61 Invalidate a uid.
62 ****************************************************************************/
64 void invalidate_vuid(uint16 vuid)
66 user_struct *vuser = get_valid_user_struct(vuid);
68 if (vuser == NULL)
69 return;
71 SAFE_FREE(vuser->homedir);
72 SAFE_FREE(vuser->unix_homedir);
73 SAFE_FREE(vuser->logon_script);
75 session_yield(vuser);
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);
90 SAFE_FREE(vuser);
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) {
103 next = 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;
147 ZERO_STRUCTP(vuser);
149 /* Allocate a free vuid. Yes this is a linear search... :-) */
150 while( get_valid_user_struct(next_vuid) != NULL ) {
151 next_vuid++;
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);
173 free(vuser);
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);
195 if (unix_homedir) {
196 vuser->unix_homedir = smb_xstrdup(unix_homedir);
198 } else {
199 struct passwd *passwd = getpwnam_alloc(vuser->user.unix_name);
200 if (passwd) {
201 vuser->unix_homedir = smb_xstrdup(passwd->pw_dir);
202 passwd_free(&passwd);
206 if (homedir) {
207 vuser->homedir = smb_xstrdup(homedir);
209 if (logon_script) {
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);
225 } else {
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);
233 SAFE_FREE(vuser);
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));
242 next_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);
250 return -1;
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 */
258 if ( (!vuser->guest)
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);
268 } else {
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);
277 /* fill in the current_user_info struct */
278 set_current_user_info( &vuser->user );
281 return vuser->vuid;
284 /****************************************************************************
285 Add a name to the session users list.
286 ****************************************************************************/
288 void add_session_user(const char *user)
290 fstring suser;
291 struct passwd *passwd;
293 if (!(passwd = Get_Pwnam(user)))
294 return;
296 fstrcpy(suser,passwd->pw_name);
298 if (suser && *suser && !in_list(suser,session_users,False)) {
299 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring)) {
300 DEBUG(1,("Too many session users??\n"));
301 } else {
302 pstrcat(session_users," ");
303 pstrcat(session_users,suser);
308 /****************************************************************************
309 Check if a username is valid.
310 ****************************************************************************/
312 BOOL user_ok(const char *user,int snum, gid_t *groups, size_t n_groups)
314 char **valid, **invalid;
315 BOOL ret;
317 valid = invalid = NULL;
318 ret = True;
320 if (lp_invalid_users(snum)) {
321 str_list_copy(&invalid, lp_invalid_users(snum));
322 if (invalid && str_list_substitute(invalid, "%S", lp_servicename(snum))) {
323 if ( invalid && str_list_sub_basic(invalid, current_user_info.smb_name) ) {
324 ret = !user_in_list(user, (const char **)invalid, groups, n_groups);
328 if (invalid)
329 str_list_free (&invalid);
331 if (ret && lp_valid_users(snum)) {
332 str_list_copy(&valid, lp_valid_users(snum));
333 if ( valid && str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
334 if ( valid && str_list_sub_basic(valid, current_user_info.smb_name) ) {
335 ret = user_in_list(user, (const char **)valid, groups, n_groups);
339 if (valid)
340 str_list_free (&valid);
342 if (ret && lp_onlyuser(snum)) {
343 char **user_list = str_list_make (lp_username(snum), NULL);
344 if (user_list && str_list_substitute(user_list, "%S", lp_servicename(snum))) {
345 ret = user_in_list(user, (const char **)user_list, groups, n_groups);
347 if (user_list) str_list_free (&user_list);
350 return(ret);
353 /****************************************************************************
354 Validate a group username entry. Return the username or NULL.
355 ****************************************************************************/
357 static char *validate_group(char *group, DATA_BLOB password,int snum)
359 #ifdef HAVE_NETGROUP
361 char *host, *user, *domain;
362 setnetgrent(group);
363 while (getnetgrent(&host, &user, &domain)) {
364 if (user) {
365 if (user_ok(user, snum, NULL, 0) &&
366 password_ok(user,password)) {
367 endnetgrent();
368 return(user);
372 endnetgrent();
374 #endif
376 #ifdef HAVE_GETGRENT
378 struct group *gptr;
379 setgrent();
380 while ((gptr = (struct group *)getgrent())) {
381 if (strequal(gptr->gr_name,group))
382 break;
386 * As user_ok can recurse doing a getgrent(), we must
387 * copy the member list into a pstring on the stack before
388 * use. Bug pointed out by leon@eatworms.swmed.edu.
391 if (gptr) {
392 pstring member_list;
393 char *member;
394 size_t copied_len = 0;
395 int i;
397 *member_list = '\0';
398 member = member_list;
400 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
401 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
402 if( copied_len + member_len < sizeof(pstring)) {
404 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
406 safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
407 copied_len += member_len;
408 member += copied_len;
409 } else {
410 *member = '\0';
414 endgrent();
416 member = member_list;
417 while (*member) {
418 static fstring name;
419 fstrcpy(name,member);
420 if (user_ok(name,snum, NULL, 0) &&
421 password_ok(name,password)) {
422 endgrent();
423 return(&name[0]);
426 DEBUG(10,("validate_group = member = %s\n", member));
428 member += strlen(member) + 1;
430 } else {
431 endgrent();
432 return NULL;
435 #endif
436 return(NULL);
439 /****************************************************************************
440 Check for authority to login to a service with a given username/password.
441 Note this is *NOT* used when logging on using sessionsetup_and_X.
442 ****************************************************************************/
444 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
445 BOOL *guest)
447 BOOL ok = False;
449 #if DEBUG_PASSWORD
450 DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
451 user,password.data));
452 #endif
454 *guest = False;
456 /* there are several possibilities:
457 1) login as the given user with given password
458 2) login as a previously registered username with the given password
459 3) login as a session list username with the given password
460 4) login as a previously validated user/password pair
461 5) login as the "user =" user with given password
462 6) login as the "user =" user with no password (guest connection)
463 7) login as guest user with no password
465 if the service is guest_only then steps 1 to 5 are skipped
468 /* now check the list of session users */
469 if (!ok) {
470 char *auser;
471 char *user_list = strdup(session_users);
472 if (!user_list)
473 return(False);
475 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
476 auser = strtok(NULL,LIST_SEP)) {
477 fstring user2;
478 fstrcpy(user2,auser);
479 if (!user_ok(user2,snum, NULL, 0))
480 continue;
482 if (password_ok(user2,password)) {
483 ok = True;
484 fstrcpy(user,user2);
485 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
486 and given password ok\n", user));
490 SAFE_FREE(user_list);
493 /* check the user= fields and the given password */
494 if (!ok && lp_username(snum)) {
495 char *auser;
496 pstring user_list;
497 pstrcpy(user_list,lp_username(snum));
499 pstring_sub(user_list,"%S",lp_servicename(snum));
501 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
502 auser = strtok(NULL,LIST_SEP)) {
503 if (*auser == '@') {
504 auser = validate_group(auser+1,password,snum);
505 if (auser) {
506 ok = True;
507 fstrcpy(user,auser);
508 DEBUG(3,("authorise_login: ACCEPTED: group username \
509 and given password ok (%s)\n", user));
511 } else {
512 fstring user2;
513 fstrcpy(user2,auser);
514 if (user_ok(user2,snum, NULL, 0) && password_ok(user2,password)) {
515 ok = True;
516 fstrcpy(user,user2);
517 DEBUG(3,("authorise_login: ACCEPTED: user list username \
518 and given password ok (%s)\n", user));
524 /* check for a normal guest connection */
525 if (!ok && GUEST_OK(snum)) {
526 fstring guestname;
527 fstrcpy(guestname,lp_guestaccount());
528 if (Get_Pwnam(guestname)) {
529 fstrcpy(user,guestname);
530 ok = True;
531 DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
532 user));
533 } else {
534 DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
536 *guest = True;
539 if (ok && !user_ok(user, snum, NULL, 0)) {
540 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
541 ok = False;
544 return(ok);