Make sure we don't clobber the stack when response consists of the empty
[Samba/gebeck_regimport.git] / source3 / smbd / password.c
blobba57fecd51d8abdbc14e724899aade99b6bd0b4a
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 destroy_privilege(&vuser->privs);
91 SAFE_FREE(vuser);
92 num_validated_vuids--;
95 /****************************************************************************
96 Invalidate all vuid entries for this process.
97 ****************************************************************************/
99 void invalidate_all_vuids(void)
101 user_struct *usp, *next=NULL;
103 for (usp=validated_users;usp;usp=next) {
104 next = usp->next;
106 invalidate_vuid(usp->vuid);
111 * register that a valid login has been performed, establish 'session'.
112 * @param server_info The token returned from the authentication process.
113 * (now 'owned' by register_vuid)
115 * @param session_key The User session key for the login session (now also 'owned' by register_vuid)
117 * @param respose_blob The NT challenge-response, if available. (May be freed after this call)
119 * @param smb_name The untranslated name of the user
121 * @return Newly allocated vuid, biased by an offset. (This allows us to
122 * tell random client vuid's (normally zero) from valid vuids.)
126 int register_vuid(auth_serversupplied_info *server_info, DATA_BLOB session_key, DATA_BLOB response_blob, const char *smb_name)
128 user_struct *vuser = NULL;
130 /* Ensure no vuid gets registered in share level security. */
131 if(lp_security() == SEC_SHARE) {
132 data_blob_free(&session_key);
133 return UID_FIELD_INVALID;
136 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
137 if (num_validated_vuids >= 0xFFFF-VUID_OFFSET) {
138 data_blob_free(&session_key);
139 return UID_FIELD_INVALID;
142 if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
143 DEBUG(0,("Failed to malloc users struct!\n"));
144 data_blob_free(&session_key);
145 return UID_FIELD_INVALID;
148 ZERO_STRUCTP(vuser);
150 /* Allocate a free vuid. Yes this is a linear search... :-) */
151 while( get_valid_user_struct(next_vuid) != NULL ) {
152 next_vuid++;
153 /* Check for vuid wrap. */
154 if (next_vuid == UID_FIELD_INVALID)
155 next_vuid = VUID_OFFSET;
158 DEBUG(10,("register_vuid: allocated vuid = %u\n", (unsigned int)next_vuid ));
160 vuser->vuid = next_vuid;
162 /* the next functions should be done by a SID mapping system (SMS) as
163 * the new real sam db won't have reference to unix uids or gids
166 vuser->uid = server_info->uid;
167 vuser->gid = server_info->gid;
169 vuser->n_groups = server_info->n_groups;
170 if (vuser->n_groups) {
171 if (!(vuser->groups = memdup(server_info->groups, sizeof(gid_t) * vuser->n_groups))) {
172 DEBUG(0,("register_vuid: failed to memdup vuser->groups\n"));
173 data_blob_free(&session_key);
174 free(vuser);
175 free_server_info(&server_info);
176 return UID_FIELD_INVALID;
180 vuser->guest = server_info->guest;
181 fstrcpy(vuser->user.unix_name, server_info->unix_name);
183 /* This is a potentially untrusted username */
184 alpha_strcpy(vuser->user.smb_name, smb_name, ". _-$", sizeof(vuser->user.smb_name));
186 fstrcpy(vuser->user.domain, pdb_get_domain(server_info->sam_account));
187 fstrcpy(vuser->user.full_name, pdb_get_fullname(server_info->sam_account));
190 /* Keep the homedir handy */
191 const char *homedir = pdb_get_homedir(server_info->sam_account);
192 const char *logon_script = pdb_get_logon_script(server_info->sam_account);
194 if (!IS_SAM_DEFAULT(server_info->sam_account, PDB_UNIXHOMEDIR)) {
195 const char *unix_homedir = pdb_get_unix_homedir(server_info->sam_account);
196 if (unix_homedir) {
197 vuser->unix_homedir = smb_xstrdup(unix_homedir);
199 } else {
200 struct passwd *passwd = getpwnam_alloc(vuser->user.unix_name);
201 if (passwd) {
202 vuser->unix_homedir = smb_xstrdup(passwd->pw_dir);
203 passwd_free(&passwd);
207 if (homedir) {
208 vuser->homedir = smb_xstrdup(homedir);
210 if (logon_script) {
211 vuser->logon_script = smb_xstrdup(logon_script);
215 vuser->session_key = session_key;
217 DEBUG(10,("register_vuid: (%u,%u) %s %s %s guest=%d\n",
218 (unsigned int)vuser->uid,
219 (unsigned int)vuser->gid,
220 vuser->user.unix_name, vuser->user.smb_name, vuser->user.domain, vuser->guest ));
222 DEBUG(3, ("User name: %s\tReal name: %s\n",vuser->user.unix_name,vuser->user.full_name));
224 if (server_info->ptok) {
225 vuser->nt_user_token = dup_nt_token(server_info->ptok);
226 } else {
227 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
228 free_server_info(&server_info);
229 data_blob_free(&session_key);
230 SAFE_FREE(vuser->homedir);
231 SAFE_FREE(vuser->unix_homedir);
232 SAFE_FREE(vuser->logon_script);
234 SAFE_FREE(vuser);
235 return UID_FIELD_INVALID;
238 if (server_info->privs) {
239 init_privilege(&(vuser->privs));
240 dup_priv_set(vuser->privs, server_info->privs);
243 /* use this to keep tabs on all our info from the authentication */
244 vuser->server_info = server_info;
246 DEBUG(3,("UNIX uid %d is UNIX user %s, and will be vuid %u\n",(int)vuser->uid,vuser->user.unix_name, vuser->vuid));
248 next_vuid++;
249 num_validated_vuids++;
251 DLIST_ADD(validated_users, vuser);
253 if (!session_claim(vuser)) {
254 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
255 invalidate_vuid(vuser->vuid);
256 return -1;
259 /* Register a home dir service for this user iff
260 (a) This is not a guest connection,
261 (b) we have a home directory defined, and
262 (c) there s not an existing static share by that name */
264 if ( (!vuser->guest)
265 && vuser->unix_homedir
266 && *(vuser->unix_homedir)
267 && (lp_servicenumber(vuser->user.unix_name) == -1) )
269 DEBUG(3, ("Adding/updating homes service for user '%s' using home directory: '%s'\n",
270 vuser->user.unix_name, vuser->unix_homedir));
272 vuser->homes_snum = add_home_service(vuser->user.unix_name,
273 vuser->user.unix_name, vuser->unix_homedir);
274 } else {
275 vuser->homes_snum = -1;
278 if (srv_is_signing_negotiated() && !vuser->guest && !srv_signing_started()) {
279 /* Try and turn on server signing on the first non-guest sessionsetup. */
280 srv_set_signing(vuser->session_key, response_blob);
283 return vuser->vuid;
286 /****************************************************************************
287 Add a name to the session users list.
288 ****************************************************************************/
290 void add_session_user(const char *user)
292 fstring suser;
293 struct passwd *passwd;
295 if (!(passwd = Get_Pwnam(user)))
296 return;
298 fstrcpy(suser,passwd->pw_name);
300 if (suser && *suser && !in_list(suser,session_users,False)) {
301 if (strlen(suser) + strlen(session_users) + 2 >= sizeof(pstring)) {
302 DEBUG(1,("Too many session users??\n"));
303 } else {
304 pstrcat(session_users," ");
305 pstrcat(session_users,suser);
310 /****************************************************************************
311 Check if a username is valid.
312 ****************************************************************************/
314 BOOL user_ok(const char *user,int snum, gid_t *groups, size_t n_groups)
316 char **valid, **invalid;
317 BOOL ret;
319 valid = invalid = NULL;
320 ret = True;
322 if (lp_invalid_users(snum)) {
323 str_list_copy(&invalid, lp_invalid_users(snum));
324 if (invalid && str_list_substitute(invalid, "%S", lp_servicename(snum))) {
325 if ( invalid && str_list_sub_basic(invalid, current_user_info.smb_name) ) {
326 ret = !user_in_list(user, (const char **)invalid, groups, n_groups);
330 if (invalid)
331 str_list_free (&invalid);
333 if (ret && lp_valid_users(snum)) {
334 str_list_copy(&valid, lp_valid_users(snum));
335 if ( valid && str_list_substitute(valid, "%S", lp_servicename(snum)) ) {
336 if ( valid && str_list_sub_basic(valid, current_user_info.smb_name) ) {
337 ret = user_in_list(user, (const char **)valid, groups, n_groups);
341 if (valid)
342 str_list_free (&valid);
344 if (ret && lp_onlyuser(snum)) {
345 char **user_list = str_list_make (lp_username(snum), NULL);
346 if (user_list && str_list_substitute(user_list, "%S", lp_servicename(snum))) {
347 ret = user_in_list(user, (const char **)user_list, groups, n_groups);
349 if (user_list) str_list_free (&user_list);
352 return(ret);
355 /****************************************************************************
356 Validate a group username entry. Return the username or NULL.
357 ****************************************************************************/
359 static char *validate_group(char *group, DATA_BLOB password,int snum)
361 #ifdef HAVE_NETGROUP
363 char *host, *user, *domain;
364 setnetgrent(group);
365 while (getnetgrent(&host, &user, &domain)) {
366 if (user) {
367 if (user_ok(user, snum, NULL, 0) &&
368 password_ok(user,password)) {
369 endnetgrent();
370 return(user);
374 endnetgrent();
376 #endif
378 #ifdef HAVE_GETGRENT
380 struct group *gptr;
381 setgrent();
382 while ((gptr = (struct group *)getgrent())) {
383 if (strequal(gptr->gr_name,group))
384 break;
388 * As user_ok can recurse doing a getgrent(), we must
389 * copy the member list into a pstring on the stack before
390 * use. Bug pointed out by leon@eatworms.swmed.edu.
393 if (gptr) {
394 pstring member_list;
395 char *member;
396 size_t copied_len = 0;
397 int i;
399 *member_list = '\0';
400 member = member_list;
402 for(i = 0; gptr->gr_mem && gptr->gr_mem[i]; i++) {
403 size_t member_len = strlen(gptr->gr_mem[i]) + 1;
404 if( copied_len + member_len < sizeof(pstring)) {
406 DEBUG(10,("validate_group: = gr_mem = %s\n", gptr->gr_mem[i]));
408 safe_strcpy(member, gptr->gr_mem[i], sizeof(pstring) - copied_len - 1);
409 copied_len += member_len;
410 member += copied_len;
411 } else {
412 *member = '\0';
416 endgrent();
418 member = member_list;
419 while (*member) {
420 static fstring name;
421 fstrcpy(name,member);
422 if (user_ok(name,snum, NULL, 0) &&
423 password_ok(name,password)) {
424 endgrent();
425 return(&name[0]);
428 DEBUG(10,("validate_group = member = %s\n", member));
430 member += strlen(member) + 1;
432 } else {
433 endgrent();
434 return NULL;
437 #endif
438 return(NULL);
441 /****************************************************************************
442 Check for authority to login to a service with a given username/password.
443 Note this is *NOT* used when logging on using sessionsetup_and_X.
444 ****************************************************************************/
446 BOOL authorise_login(int snum, fstring user, DATA_BLOB password,
447 BOOL *guest)
449 BOOL ok = False;
451 #if DEBUG_PASSWORD
452 DEBUG(100,("authorise_login: checking authorisation on user=%s pass=%s\n",
453 user,password.data));
454 #endif
456 *guest = False;
458 /* there are several possibilities:
459 1) login as the given user with given password
460 2) login as a previously registered username with the given password
461 3) login as a session list username with the given password
462 4) login as a previously validated user/password pair
463 5) login as the "user =" user with given password
464 6) login as the "user =" user with no password (guest connection)
465 7) login as guest user with no password
467 if the service is guest_only then steps 1 to 5 are skipped
470 /* now check the list of session users */
471 if (!ok) {
472 char *auser;
473 char *user_list = strdup(session_users);
474 if (!user_list)
475 return(False);
477 for (auser=strtok(user_list,LIST_SEP); !ok && auser;
478 auser = strtok(NULL,LIST_SEP)) {
479 fstring user2;
480 fstrcpy(user2,auser);
481 if (!user_ok(user2,snum, NULL, 0))
482 continue;
484 if (password_ok(user2,password)) {
485 ok = True;
486 fstrcpy(user,user2);
487 DEBUG(3,("authorise_login: ACCEPTED: session list username (%s) \
488 and given password ok\n", user));
492 SAFE_FREE(user_list);
495 /* check the user= fields and the given password */
496 if (!ok && lp_username(snum)) {
497 char *auser;
498 pstring user_list;
499 pstrcpy(user_list,lp_username(snum));
501 pstring_sub(user_list,"%S",lp_servicename(snum));
503 for (auser=strtok(user_list,LIST_SEP); auser && !ok;
504 auser = strtok(NULL,LIST_SEP)) {
505 if (*auser == '@') {
506 auser = validate_group(auser+1,password,snum);
507 if (auser) {
508 ok = True;
509 fstrcpy(user,auser);
510 DEBUG(3,("authorise_login: ACCEPTED: group username \
511 and given password ok (%s)\n", user));
513 } else {
514 fstring user2;
515 fstrcpy(user2,auser);
516 if (user_ok(user2,snum, NULL, 0) && password_ok(user2,password)) {
517 ok = True;
518 fstrcpy(user,user2);
519 DEBUG(3,("authorise_login: ACCEPTED: user list username \
520 and given password ok (%s)\n", user));
526 /* check for a normal guest connection */
527 if (!ok && GUEST_OK(snum)) {
528 fstring guestname;
529 fstrcpy(guestname,lp_guestaccount());
530 if (Get_Pwnam(guestname)) {
531 fstrcpy(user,guestname);
532 ok = True;
533 DEBUG(3,("authorise_login: ACCEPTED: guest account and guest ok (%s)\n",
534 user));
535 } else {
536 DEBUG(0,("authorise_login: Invalid guest account %s??\n",guestname));
538 *guest = True;
541 if (ok && !user_ok(user, snum, NULL, 0)) {
542 DEBUG(0,("authorise_login: rejected invalid user %s\n",user));
543 ok = False;
546 return(ok);