r889: convert samba4 to use [u]int16_t instead of [u]int16
[ira/wip.git] / source / smb_server / password.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Tridgell 1992-1998
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22
23
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)
30 {
31         user_struct *usp;
32         int count=0;
33
34         if (vuid == UID_FIELD_INVALID)
35                 return NULL;
36
37         for (usp=smb->users.validated_users;usp;usp=usp->next,count++) {
38                 if (vuid == usp->vuid) {
39                         if (count > 10) {
40                                 DLIST_PROMOTE(smb->users.validated_users, usp);
41                         }
42                         return usp;
43                 }
44         }
45
46         return NULL;
47 }
48
49 /****************************************************************************
50 invalidate a uid
51 ****************************************************************************/
52 void invalidate_vuid(struct server_context *smb, uint16_t vuid)
53 {
54         user_struct *vuser = get_valid_user_struct(smb, vuid);
55
56         if (vuser == NULL)
57                 return;
58         
59         data_blob_free(&vuser->session_key);
60
61         session_yield(vuser);
62
63         free_server_info(&vuser->server_info);
64
65         DLIST_REMOVE(smb->users.validated_users, vuser);
66
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); */
70
71         SAFE_FREE(vuser);
72         smb->users.num_validated_vuids--;
73 }
74
75 /****************************************************************************
76 invalidate all vuid entries for this process
77 ****************************************************************************/
78 void invalidate_all_vuids(struct server_context *smb)
79 {
80         user_struct *usp, *next=NULL;
81
82         for (usp=smb->users.validated_users;usp;usp=next) {
83                 next = usp->next;
84                 
85                 invalidate_vuid(smb, usp->vuid);
86         }
87 }
88
89 /**
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)
93  *
94  *  @param session_key The User session key for the login session (now also 'owned' by register_vuid)
95  *
96  *  @param smb_name The untranslated name of the user
97  *
98  *  @return Newly allocated vuid, biased by an offset. (This allows us to
99  *   tell random client vuid's (normally zero) from valid vuids.)
100  *
101  */
102
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)
107 {
108         user_struct *vuser = NULL;
109
110         /* Ensure no vuid gets registered in share level security. */
111         if(lp_security() == SEC_SHARE)
112                 return UID_FIELD_INVALID;
113
114         /* Limit allowed vuids to 16bits - VUID_OFFSET. */
115         if (smb->users.num_validated_vuids >= 0xFFFF-VUID_OFFSET)
116                 return UID_FIELD_INVALID;
117
118         if((vuser = (user_struct *)malloc( sizeof(user_struct) )) == NULL) {
119                 DEBUG(0,("Failed to malloc users struct!\n"));
120                 return UID_FIELD_INVALID;
121         }
122
123         ZERO_STRUCTP(vuser);
124
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;
131         }
132
133         DEBUG(10,("register_vuid: allocated vuid = %u\n", 
134                   (unsigned int)smb->users.next_vuid));
135
136         vuser->vuid = smb->users.next_vuid;
137
138         vuser->session_key = *session_key;
139
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);
143
144                 SAFE_FREE(vuser);
145                 return UID_FIELD_INVALID;
146         }
147
148         /* use this to keep tabs on all our info from the authentication */
149         vuser->server_info = server_info;
150
151         smb->users.next_vuid++;
152         smb->users.num_validated_vuids++;
153
154         DLIST_ADD(smb->users.validated_users, vuser);
155
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);
159                 return -1;
160         }
161
162         return vuser->vuid;
163 }