wscript: drop checks for setnetgrent/endnetgrent/getnetgrent
[gd/samba-autobuild/.git] / source3 / smbd / password.c
1 /*
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2007.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/passwd.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../librpc/gen_ndr/netlogon.h"
26 #include "auth.h"
27 #include "../libcli/security/security.h"
28
29 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
30                                 SERVER_ALLOCATED_REQUIRED_NO,
31                                 SERVER_ALLOCATED_REQUIRED_ANY};
32
33 static struct user_struct *get_valid_user_struct_internal(
34                         struct smbd_server_connection *sconn,
35                         uint64_t vuid,
36                         enum server_allocated_state server_allocated)
37 {
38         struct user_struct *usp;
39         int count=0;
40
41         if (vuid == UID_FIELD_INVALID)
42                 return NULL;
43
44         usp=sconn->users;
45         for (;usp;usp=usp->next,count++) {
46                 if (vuid == usp->vuid) {
47                         switch (server_allocated) {
48                                 case SERVER_ALLOCATED_REQUIRED_YES:
49                                         if (usp->session_info == NULL) {
50                                                 continue;
51                                         }
52                                         break;
53                                 case SERVER_ALLOCATED_REQUIRED_NO:
54                                         if (usp->session_info != NULL) {
55                                                 continue;
56                                         }
57                                 case SERVER_ALLOCATED_REQUIRED_ANY:
58                                         break;
59                         }
60                         if (count > 10) {
61                                 DLIST_PROMOTE(sconn->users, usp);
62                         }
63                         return usp;
64                 }
65         }
66
67         return NULL;
68 }
69
70 /****************************************************************************
71  Check if a uid has been validated, and return an pointer to the user_struct
72  if it has. NULL if not. vuid is biased by an offset. This allows us to
73  tell random client vuid's (normally zero) from valid vuids.
74 ****************************************************************************/
75
76 struct user_struct *get_valid_user_struct(struct smbd_server_connection *sconn,
77                                           uint64_t vuid)
78 {
79         return get_valid_user_struct_internal(sconn, vuid,
80                         SERVER_ALLOCATED_REQUIRED_YES);
81 }
82
83 /****************************************************************************
84  Invalidate a uid.
85 ****************************************************************************/
86
87 void invalidate_vuid(struct smbd_server_connection *sconn, uint64_t vuid)
88 {
89         struct user_struct *vuser = NULL;
90
91         vuser = get_valid_user_struct_internal(sconn, vuid,
92                         SERVER_ALLOCATED_REQUIRED_ANY);
93         if (vuser == NULL) {
94                 return;
95         }
96
97         session_yield(vuser->session);
98
99         DLIST_REMOVE(sconn->users, vuser);
100         SMB_ASSERT(sconn->num_users > 0);
101         sconn->num_users--;
102
103         /* clear the vuid from the 'cache' on each connection, and
104            from the vuid 'owner' of connections */
105         conn_clear_vuid_caches(sconn, vuid);
106
107         TALLOC_FREE(vuser);
108 }
109
110 int register_homes_share(const char *username)
111 {
112         int result;
113         struct passwd *pwd;
114
115         result = lp_servicenumber(username);
116         if (result != -1) {
117                 DEBUG(3, ("Using static (or previously created) service for "
118                           "user '%s'; path = '%s'\n", username,
119                           lp_path(talloc_tos(), result)));
120                 return result;
121         }
122
123         pwd = Get_Pwnam_alloc(talloc_tos(), username);
124
125         if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
126                 DEBUG(3, ("No home directory defined for user '%s'\n",
127                           username));
128                 TALLOC_FREE(pwd);
129                 return -1;
130         }
131
132         DEBUG(3, ("Adding homes service for user '%s' using home directory: "
133                   "'%s'\n", username, pwd->pw_dir));
134
135         result = add_home_service(username, username, pwd->pw_dir);
136
137         TALLOC_FREE(pwd);
138         return result;
139 }