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.
24 /****************************************************************************
25 check if a uid has been validated, and return an pointer to the user_struct
26 if it has. NULL if not. vuid is biased by an offset. This allows us to
27 tell random client vuid's (normally zero) from valid vuids.
28 ****************************************************************************/
29 struct user_struct *get_valid_user_struct(struct server_context *smb, uint16_t vuid)
34 if (vuid == UID_FIELD_INVALID)
37 for (usp=smb->users.validated_users;usp;usp=usp->next,count++) {
38 if (vuid == usp->vuid) {
40 DLIST_PROMOTE(smb->users.validated_users, usp);
49 /****************************************************************************
51 ****************************************************************************/
52 void invalidate_vuid(struct server_context *smb, uint16_t vuid)
54 user_struct *vuser = get_valid_user_struct(smb, vuid);
59 data_blob_free(&vuser->session_key);
63 free_server_info(&vuser->server_info);
65 DLIST_REMOVE(smb->users.validated_users, vuser);
67 /* clear the vuid from the 'cache' on each connection, and
68 from the vuid 'owner' of connections */
69 /* REWRITE: conn_clear_vuid_cache(smb, vuid); */
72 smb->users.num_validated_vuids--;
75 /****************************************************************************
76 invalidate all vuid entries for this process
77 ****************************************************************************/
78 void invalidate_all_vuids(struct server_context *smb)
80 user_struct *usp, *next=NULL;
82 for (usp=smb->users.validated_users;usp;usp=next) {
85 invalidate_vuid(smb, usp->vuid);
90 * register that a valid login has been performed, establish 'session'.
91 * @param server_info The token returned from the authentication process.
92 * (now 'owned' by register_vuid)
94 * @param session_key The User session key for the login session (now also 'owned' by register_vuid)
96 * @param smb_name The untranslated name of the user
98 * @return Newly allocated vuid, biased by an offset. (This allows us to
99 * tell random client vuid's (normally zero) from valid vuids.)
103 int register_vuid(struct server_context *smb,
104 struct auth_serversupplied_info *server_info,
105 DATA_BLOB *session_key,
106 const char *smb_name)
108 user_struct *vuser = NULL;
110 /* Ensure no vuid gets registered in share level security. */
111 if(lp_security() == SEC_SHARE)
112 return UID_FIELD_INVALID;
114 /* Limit allowed vuids to 16bits - VUID_OFFSET. */
115 if (smb->users.num_validated_vuids >= 0xFFFF-VUID_OFFSET)
116 return UID_FIELD_INVALID;
118 if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
119 DEBUG(0,("Failed to malloc users struct!\n"));
120 return UID_FIELD_INVALID;
125 /* Allocate a free vuid. Yes this is a linear search... :-) */
126 while (get_valid_user_struct(smb, smb->users.next_vuid) != NULL ) {
127 smb->users.next_vuid++;
128 /* Check for vuid wrap. */
129 if (smb->users.next_vuid == UID_FIELD_INVALID)
130 smb->users.next_vuid = VUID_OFFSET;
133 DEBUG(10,("register_vuid: allocated vuid = %u\n",
134 (unsigned int)smb->users.next_vuid));
136 vuser->vuid = smb->users.next_vuid;
138 vuser->session_key = *session_key;
140 if (!server_info->ptok) {
141 DEBUG(1, ("server_info does not contain a user_token - cannot continue\n"));
142 free_server_info(&server_info);
145 return UID_FIELD_INVALID;
148 /* use this to keep tabs on all our info from the authentication */
149 vuser->server_info = server_info;
151 smb->users.next_vuid++;
152 smb->users.num_validated_vuids++;
154 DLIST_ADD(smb->users.validated_users, vuser);
156 if (!session_claim(smb, vuser)) {
157 DEBUG(1,("Failed to claim session for vuid=%d\n", vuser->vuid));
158 invalidate_vuid(smb, vuser->vuid);