s3:auth Make Samba3 use the new common struct auth_usersupplied_info
[samba.git] / source3 / auth / user_info.c
1 /*
2    Unix SMB/CIFS implementation.
3    Authentication utility functions
4    Copyright (C) Volker Lendecke 2010
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_AUTH
24
25 /****************************************************************************
26  Create an auth_usersupplied_data structure
27 ****************************************************************************/
28
29 NTSTATUS make_user_info(struct auth_usersupplied_info **user_info,
30                         const char *smb_name,
31                         const char *internal_username,
32                         const char *client_domain,
33                         const char *domain,
34                         const char *workstation_name,
35                         const DATA_BLOB *lm_pwd,
36                         const DATA_BLOB *nt_pwd,
37                         const struct samr_Password *lm_interactive_pwd,
38                         const struct samr_Password *nt_interactive_pwd,
39                         const char *plaintext_password,
40                         enum auth_password_state password_state)
41 {
42
43         DEBUG(5,("attempting to make a user_info for %s (%s)\n", internal_username, smb_name));
44
45         *user_info = SMB_MALLOC_P(struct auth_usersupplied_info);
46         if (*user_info == NULL) {
47                 DEBUG(0,("malloc failed for user_info (size %lu)\n", (unsigned long)sizeof(*user_info)));
48                 return NT_STATUS_NO_MEMORY;
49         }
50
51         ZERO_STRUCTP(*user_info);
52
53         DEBUG(5,("making strings for %s's user_info struct\n", internal_username));
54
55         (*user_info)->client.account_name = SMB_STRDUP(smb_name);
56         if ((*user_info)->client.account_name == NULL) {
57                 free_user_info(user_info);
58                 return NT_STATUS_NO_MEMORY;
59         }
60
61         (*user_info)->mapped.account_name = SMB_STRDUP(internal_username);
62         if ((*user_info)->mapped.account_name == NULL) {
63                 free_user_info(user_info);
64                 return NT_STATUS_NO_MEMORY;
65         }
66
67         (*user_info)->mapped.domain_name = SMB_STRDUP(domain);
68         if ((*user_info)->mapped.domain_name == NULL) {
69                 free_user_info(user_info);
70                 return NT_STATUS_NO_MEMORY;
71         }
72
73         (*user_info)->client.domain_name = SMB_STRDUP(client_domain);
74         if ((*user_info)->client.domain_name == NULL) {
75                 free_user_info(user_info);
76                 return NT_STATUS_NO_MEMORY;
77         }
78
79         (*user_info)->workstation_name = SMB_STRDUP(workstation_name);
80         if ((*user_info)->workstation_name == NULL) {
81                 free_user_info(user_info);
82                 return NT_STATUS_NO_MEMORY;
83         }
84
85         DEBUG(5,("making blobs for %s's user_info struct\n", internal_username));
86
87         if (lm_pwd)
88                 (*user_info)->password.response.lanman = data_blob(lm_pwd->data, lm_pwd->length);
89         if (nt_pwd)
90                 (*user_info)->password.response.nt = data_blob(nt_pwd->data, nt_pwd->length);
91         if (lm_interactive_pwd) {
92                 (*user_info)->password.hash.lanman = SMB_MALLOC_P(struct samr_Password);
93                 memcpy((*user_info)->password.hash.lanman->hash, lm_interactive_pwd->hash, sizeof((*user_info)->password.hash.lanman->hash));
94         }
95
96         if (nt_interactive_pwd) {
97                 (*user_info)->password.hash.nt = SMB_MALLOC_P(struct samr_Password);
98                 memcpy((*user_info)->password.hash.nt->hash, nt_interactive_pwd->hash, sizeof((*user_info)->password.hash.nt->hash));
99         }
100
101         if (plaintext_password)
102                 (*user_info)->password.plaintext = SMB_STRDUP(plaintext_password);
103
104         (*user_info)->password_state = password_state;
105
106         (*user_info)->logon_parameters = 0;
107
108         DEBUG(10,("made a user_info for %s (%s)\n", internal_username, smb_name));
109
110         return NT_STATUS_OK;
111 }
112
113 /***************************************************************************
114  Free a user_info struct
115 ***************************************************************************/
116
117 void free_user_info(struct auth_usersupplied_info **user_info)
118 {
119         DEBUG(5,("attempting to free (and zero) a user_info structure\n"));
120         if (*user_info != NULL) {
121                 if ((*user_info)->client.account_name) {
122                         DEBUG(10,("structure was created for %s\n",
123                                   (*user_info)->client.account_name));
124                 }
125                 SAFE_FREE((*user_info)->client.account_name);
126                 SAFE_FREE((*user_info)->mapped.account_name);
127                 SAFE_FREE((*user_info)->client.domain_name);
128                 SAFE_FREE((*user_info)->mapped.domain_name);
129                 SAFE_FREE((*user_info)->workstation_name);
130                 data_blob_free(&(*user_info)->password.response.lanman);
131                 data_blob_free(&(*user_info)->password.response.nt);
132                 if ((*user_info)->password.hash.lanman) {
133                         ZERO_STRUCTP((*user_info)->password.hash.lanman);
134                         SAFE_FREE((*user_info)->password.hash.lanman);
135                 }
136                 if ((*user_info)->password.hash.nt) {
137                         ZERO_STRUCTP((*user_info)->password.hash.nt);
138                         SAFE_FREE((*user_info)->password.hash.nt);
139                 }
140                 if ((*user_info)->password.plaintext) {
141                         memset((*user_info)->password.plaintext, '\0', strlen(((*user_info)->password.plaintext)));
142                         SAFE_FREE((*user_info)->password.plaintext);
143                 }
144                 ZERO_STRUCT(**user_info);
145         }
146         SAFE_FREE(*user_info);
147 }