s3/smbd: update some more DEBUG macros in smbd_smb2_create_send
[nivanova/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 /* Fix up prototypes for OSX 10.4, where they're missing */
30 #ifndef HAVE_SETNETGRENT_PROTOTYPE
31 extern int setnetgrent(const char* netgroup);
32 #endif
33 #ifndef HAVE_GETNETGRENT_PROTOTYPE
34 extern int getnetgrent(char **host, char **user, char **domain);
35 #endif
36 #ifndef HAVE_ENDNETGRENT_PROTOTYPE
37 extern void endnetgrent(void);
38 #endif
39
40 enum server_allocated_state { SERVER_ALLOCATED_REQUIRED_YES,
41                                 SERVER_ALLOCATED_REQUIRED_NO,
42                                 SERVER_ALLOCATED_REQUIRED_ANY};
43
44 static struct user_struct *get_valid_user_struct_internal(
45                         struct smbd_server_connection *sconn,
46                         uint64_t vuid,
47                         enum server_allocated_state server_allocated)
48 {
49         struct user_struct *usp;
50         int count=0;
51
52         if (vuid == UID_FIELD_INVALID)
53                 return NULL;
54
55         usp=sconn->users;
56         for (;usp;usp=usp->next,count++) {
57                 if (vuid == usp->vuid) {
58                         switch (server_allocated) {
59                                 case SERVER_ALLOCATED_REQUIRED_YES:
60                                         if (usp->session_info == NULL) {
61                                                 continue;
62                                         }
63                                         break;
64                                 case SERVER_ALLOCATED_REQUIRED_NO:
65                                         if (usp->session_info != NULL) {
66                                                 continue;
67                                         }
68                                 case SERVER_ALLOCATED_REQUIRED_ANY:
69                                         break;
70                         }
71                         if (count > 10) {
72                                 DLIST_PROMOTE(sconn->users, usp);
73                         }
74                         return usp;
75                 }
76         }
77
78         return NULL;
79 }
80
81 /****************************************************************************
82  Check if a uid has been validated, and return an pointer to the user_struct
83  if it has. NULL if not. vuid is biased by an offset. This allows us to
84  tell random client vuid's (normally zero) from valid vuids.
85 ****************************************************************************/
86
87 struct user_struct *get_valid_user_struct(struct smbd_server_connection *sconn,
88                                           uint64_t vuid)
89 {
90         return get_valid_user_struct_internal(sconn, vuid,
91                         SERVER_ALLOCATED_REQUIRED_YES);
92 }
93
94 /****************************************************************************
95  Invalidate a uid.
96 ****************************************************************************/
97
98 void invalidate_vuid(struct smbd_server_connection *sconn, uint64_t vuid)
99 {
100         struct user_struct *vuser = NULL;
101
102         vuser = get_valid_user_struct_internal(sconn, vuid,
103                         SERVER_ALLOCATED_REQUIRED_ANY);
104         if (vuser == NULL) {
105                 return;
106         }
107
108         session_yield(vuser->session);
109
110         DLIST_REMOVE(sconn->users, vuser);
111         SMB_ASSERT(sconn->num_users > 0);
112         sconn->num_users--;
113
114         /* clear the vuid from the 'cache' on each connection, and
115            from the vuid 'owner' of connections */
116         conn_clear_vuid_caches(sconn, vuid);
117
118         TALLOC_FREE(vuser);
119 }
120
121 int register_homes_share(const char *username)
122 {
123         int result;
124         struct passwd *pwd;
125
126         result = lp_servicenumber(username);
127         if (result != -1) {
128                 DEBUG(3, ("Using static (or previously created) service for "
129                           "user '%s'; path = '%s'\n", username,
130                           lp_path(talloc_tos(), result)));
131                 return result;
132         }
133
134         pwd = Get_Pwnam_alloc(talloc_tos(), username);
135
136         if ((pwd == NULL) || (pwd->pw_dir[0] == '\0')) {
137                 DEBUG(3, ("No home directory defined for user '%s'\n",
138                           username));
139                 TALLOC_FREE(pwd);
140                 return -1;
141         }
142
143         DEBUG(3, ("Adding homes service for user '%s' using home directory: "
144                   "'%s'\n", username, pwd->pw_dir));
145
146         result = add_home_service(username, username, pwd->pw_dir);
147
148         TALLOC_FREE(pwd);
149         return result;
150 }