s4-auth log details about any token we fail to convert to a unix token
[kai/samba.git] / source4 / auth / unix_token.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Deal with unix elements in the security token
5
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Andrew Bartlett 2011
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "auth/auth.h"
25 #include "libcli/wbclient/wbclient.h"
26 #include "param/param.h"
27
28 /*
29   form a security_unix_token from the current security_token
30 */
31 NTSTATUS security_token_to_unix_token(TALLOC_CTX *mem_ctx,
32                                       struct wbc_context *wbc_ctx,
33                                       struct security_token *token,
34                                       struct security_unix_token **sec)
35 {
36         uint32_t s, g;
37         NTSTATUS status;
38         struct id_map *ids;
39         struct composite_context *ctx;
40
41         /* we can't do unix security without a user and group */
42         if (token->num_sids < 2) {
43                 return NT_STATUS_ACCESS_DENIED;
44         }
45
46         *sec = talloc_zero(mem_ctx, struct security_unix_token);
47         if (*sec == NULL) {
48                 return NT_STATUS_NO_MEMORY;
49         }
50
51         ids = talloc_zero_array(mem_ctx, struct id_map, token->num_sids);
52         NT_STATUS_HAVE_NO_MEMORY(ids);
53
54         for (s=0; s < token->num_sids; s++) {
55                 ids[s].sid = &token->sids[s];
56                 ids[s].status = ID_UNKNOWN;
57         }
58
59         ctx = wbc_sids_to_xids_send(wbc_ctx, ids, token->num_sids, ids);
60         NT_STATUS_HAVE_NO_MEMORY(ctx);
61
62         status = wbc_sids_to_xids_recv(ctx, &ids);
63         NT_STATUS_NOT_OK_RETURN(status);
64
65         g = token->num_sids;
66         if (ids[0].xid.type != ID_TYPE_BOTH) {
67                 g--;
68         }
69         (*sec)->ngroups = g;
70         (*sec)->groups = talloc_array(*sec, gid_t, (*sec)->ngroups);
71         NT_STATUS_HAVE_NO_MEMORY((*sec)->groups);
72
73         g=0;
74         if (ids[0].xid.type == ID_TYPE_BOTH) {
75                 (*sec)->uid = ids[0].xid.id;
76                 (*sec)->groups[g] = ids[0].xid.id;
77                 g++;
78         } else if (ids[0].xid.type == ID_TYPE_UID) {
79                 (*sec)->uid = ids[0].xid.id;
80         } else {
81                 char *sid_str = dom_sid_string(mem_ctx, ids[0].sid);
82                 DEBUG(0, ("Unable to convert first SID (%s) in user token to a UID.  Conversion was returned as type %d, full token:\n",
83                           sid_str, (int)ids[0].xid.type));
84                 security_token_debug(0, 0, token);
85                 talloc_free(sid_str);
86                 return NT_STATUS_INVALID_SID;
87         }
88
89         if (ids[1].xid.type == ID_TYPE_BOTH ||
90             ids[1].xid.type == ID_TYPE_GID) {
91                 (*sec)->gid = ids[1].xid.id;
92                 (*sec)->groups[g] = ids[1].xid.id;
93                 g++;
94         } else {
95                 char *sid_str = dom_sid_string(mem_ctx, ids[1].sid);
96                 DEBUG(0, ("Unable to convert second SID (%s) in user token to a GID.  Conversion was returned as type %d, full token:\n",
97                           sid_str, (int)ids[1].xid.type));
98                 security_token_debug(0, 0, token);
99                 talloc_free(sid_str);
100                 return NT_STATUS_INVALID_SID;
101         }
102
103         for (s=2; s < token->num_sids; s++) {
104                 if (ids[s].xid.type == ID_TYPE_BOTH ||
105                     ids[s].xid.type == ID_TYPE_GID) {
106                         (*sec)->groups[g] = ids[s].xid.id;
107                         g++;
108                 } else {
109                         char *sid_str = dom_sid_string(mem_ctx, ids[s].sid);
110                         DEBUG(0, ("Unable to convert SID (%s) at index %u in user token to a GID.  Conversion was returned as type %d, full token:\n",
111                                   sid_str, (unsigned int)s, (int)ids[s].xid.type));
112                         security_token_debug(0, 0, token);
113                         talloc_free(sid_str);
114                         return NT_STATUS_INVALID_SID;
115                 }
116         }
117
118         DEBUG(5, ("Successfully converted security token to a unix token:"));
119         security_token_debug(0, 5, token);
120         TALLOC_FREE(ids);
121
122         return NT_STATUS_OK;
123 }
124
125 /*
126   Fill in the auth_user_info_unix and auth_unix_token elements in a struct session_info
127 */
128 NTSTATUS auth_session_info_fill_unix( struct wbc_context *wbc_ctx,
129                                      struct loadparm_context *lp_ctx,
130                                      struct auth_session_info *session_info)
131 {
132         char *su;
133         size_t len;
134         NTSTATUS status = security_token_to_unix_token(session_info, wbc_ctx,
135                                                        session_info->security_token,
136                                                        &session_info->unix_token);
137         if (!NT_STATUS_IS_OK(status)) {
138                 return status;
139         }
140
141         session_info->unix_info = talloc_zero(session_info, struct auth_user_info_unix);
142         NT_STATUS_HAVE_NO_MEMORY(session_info->unix_info);
143
144         session_info->unix_info->system = security_token_is_system(session_info->security_token);
145
146         session_info->unix_info->unix_name = talloc_asprintf(session_info->unix_info,
147                                                              "%s%s%s", session_info->info->domain_name,
148                                                              lpcfg_winbind_separator(lp_ctx),
149                                                              session_info->info->account_name);
150         NT_STATUS_HAVE_NO_MEMORY(session_info->unix_info->unix_name);
151
152         len = strlen(session_info->info->account_name) + 1;
153         session_info->unix_info->sanitized_username = su = talloc_array(session_info->unix_info, char, len);
154         NT_STATUS_HAVE_NO_MEMORY(su);
155
156         alpha_strcpy(su, session_info->info->account_name,
157                      ". _-$", len);
158
159         return NT_STATUS_OK;
160 }