1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/*
* Unix SMB/CIFS implementation.
* MS-RPC client library implementation
* Copyright (C) Chris Nicholls 2005.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "libmsrpc.h"
#include "libmsrpc_internal.h"
#include "libsmbclient.h"
#include "libsmb_internal.h"
/*this function is based on code found in smbc_init_context() (libsmb/libsmbclient.c)*/
void cac_Init(int debug) {
if(debug < 0 || debug > 99)
debug = 0;
DEBUGLEVEL = debug;
setup_logging("libmsrpc", True);
}
int cac_InitHandleMem(CacServerHandle *hnd) {
hnd->username = SMB_MALLOC_ARRAY(char, sizeof(fstring));
if(!hnd->username)
return CAC_FAILURE;
hnd->username[0] = '\0';
hnd->domain = SMB_MALLOC_ARRAY(char, sizeof(fstring));
if(!hnd->domain)
return CAC_FAILURE;
hnd->domain[0] = '\0';
hnd->netbios_name = SMB_MALLOC_ARRAY(char, sizeof(fstring));
if(!hnd->netbios_name)
return CAC_FAILURE;
hnd->netbios_name[0] = '\0';
hnd->password = SMB_MALLOC_ARRAY(char, sizeof(fstring));
if(!hnd->password)
return CAC_FAILURE;
hnd->password[0] = '\0';
hnd->server = SMB_MALLOC_ARRAY(char, sizeof(fstring));
if(!hnd->server)
return CAC_FAILURE;
hnd->server[0] = '\0';
return CAC_SUCCESS;
}
CacServerHandle *cac_NewServerHandle(BOOL allocate_fields) {
CacServerHandle * hnd;
hnd = SMB_MALLOC_P(CacServerHandle);
if(!hnd) {
errno = ENOMEM;
return NULL;
}
ZERO_STRUCTP(hnd);
if(allocate_fields == True) {
if(!cac_InitHandleMem(hnd)) {
SAFE_FREE(hnd);
return NULL;
}
}
hnd->_internal.ctx = smbc_new_context();
if(!hnd->_internal.ctx) {
cac_FreeHandle(hnd);
return NULL;
}
hnd->_internal.ctx->callbacks.auth_fn = cac_GetAuthDataFn;
/*add defaults*/
hnd->debug = 0;
/*start at the highest and it will fall down after trying the functions*/
hnd->_internal.srv_level = SRV_WIN_2K3;
hnd->_internal.user_supplied_ctx = False;
return hnd;
}
int cac_InitHandleData(CacServerHandle *hnd) {
/*store any automatically initialized values*/
if(!hnd->netbios_name) {
hnd->netbios_name = SMB_STRDUP(hnd->_internal.ctx->netbios_name);
}
else if(hnd->netbios_name[0] == '\0') {
strncpy(hnd->netbios_name, hnd->_internal.ctx->netbios_name, sizeof(fstring));
}
if(!hnd->username) {
hnd->username = SMB_STRDUP(hnd->_internal.ctx->user);
}
else if(hnd->username[0] == '\0') {
strncpy(hnd->username, hnd->_internal.ctx->user, sizeof(fstring));
}
if(!hnd->domain) {
hnd->domain = SMB_STRDUP(hnd->_internal.ctx->workgroup);
}
else if(hnd->domain[0] == '\0') {
strncpy(hnd->domain, hnd->_internal.ctx->workgroup, sizeof(fstring));
}
return CAC_SUCCESS;
}
void cac_SetAuthDataFn(CacServerHandle *hnd, smbc_get_auth_data_fn auth_fn) {
hnd->_internal.ctx->callbacks.auth_fn = auth_fn;
}
void cac_SetSmbcContext(CacServerHandle *hnd, SMBCCTX *ctx) {
SAFE_FREE(hnd->_internal.ctx);
hnd->_internal.user_supplied_ctx = True;
hnd->_internal.ctx = ctx;
/*_try_ to avoid any problems that might occur if cac_Connect() isn't called*/
/*cac_InitHandleData(hnd);*/
}
/*used internally*/
SMBCSRV *cac_GetServer(CacServerHandle *hnd) {
SMBCSRV *srv;
if(!hnd || !hnd->_internal.ctx) {
return NULL;
}
srv = smbc_attr_server(hnd->_internal.ctx, hnd->server, "IPC$", hnd->domain, hnd->username, hnd->password, NULL);
if(!srv) {
hnd->status=NT_STATUS_UNSUCCESSFUL;
DEBUG(1, ("cac_GetServer: Could not find server connection.\n"));
}
return srv;
}
int cac_Connect(CacServerHandle *hnd, const char *srv) {
if(!hnd) {
return CAC_FAILURE;
}
/*these values should be initialized by the user*/
if(!hnd->server && !srv) {
return CAC_FAILURE;
}
/*change the server name in the server handle if necessary*/
if(srv && hnd->server && strcmp(hnd->server, srv) == 0) {
SAFE_FREE(hnd->server);
hnd->server = SMB_STRDUP(srv);
}
/*first see if the context has already been setup*/
if( !(hnd->_internal.ctx->internal->_initialized) ) {
hnd->_internal.ctx->debug = hnd->debug;
/*initialize the context*/
if(!smbc_init_context(hnd->_internal.ctx)) {
return CAC_FAILURE;
}
}
/*copy any uninitialized values out of the smbc context into the handle*/
if(!cac_InitHandleData(hnd)) {
return CAC_FAILURE;
}
DEBUG(3, ("cac_Connect: Username: %s\n", hnd->username));
DEBUG(3, ("cac_Connect: Domain: %s\n", hnd->domain));
DEBUG(3, ("cac_Connect: Netbios Name: %s\n", hnd->netbios_name));
if(!cac_GetServer(hnd)) {
return CAC_FAILURE;
}
return CAC_SUCCESS;
}
void cac_FreeHandle(CacServerHandle * hnd) {
if(!hnd)
return;
/*only free the context if we created it*/
if(!hnd->_internal.user_supplied_ctx) {
smbc_free_context(hnd->_internal.ctx, True);
}
SAFE_FREE(hnd->netbios_name);
SAFE_FREE(hnd->domain);
SAFE_FREE(hnd->username);
SAFE_FREE(hnd->password);
SAFE_FREE(hnd->server);
SAFE_FREE(hnd);
}
void cac_InitCacTime(CacTime *cactime, NTTIME nttime) {
float high, low;
uint32 sec;
if(!cactime)
return;
ZERO_STRUCTP(cactime);
/*this code is taken from display_time() found in rpcclient/cmd_samr.c*/
if (nttime==0)
return;
if (nttime==0x80000000000000LL)
return;
high = 65536;
high = high/10000;
high = high*65536;
high = high/1000;
high = high * (~(nttime >> 32));
low = ~(nttime & 0xFFFFFFFF);
low = low/(1000*1000*10);
sec=high+low;
cactime->days=sec/(60*60*24);
cactime->hours=(sec - (cactime->days*60*60*24)) / (60*60);
cactime->minutes=(sec - (cactime->days*60*60*24) - (cactime->hours*60*60) ) / 60;
cactime->seconds=sec - (cactime->days*60*60*24) - (cactime->hours*60*60) - (cactime->minutes*60);
}
void cac_GetAuthDataFn(const char * pServer,
const char * pShare,
char * pWorkgroup,
int maxLenWorkgroup,
char * pUsername,
int maxLenUsername,
char * pPassword,
int maxLenPassword)
{
char temp[sizeof(fstring)];
static char authUsername[sizeof(fstring)];
static char authWorkgroup[sizeof(fstring)];
static char authPassword[sizeof(fstring)];
static char authSet = 0;
char *pass = NULL;
if (authSet)
{
strncpy(pWorkgroup, authWorkgroup, maxLenWorkgroup - 1);
strncpy(pUsername, authUsername, maxLenUsername - 1);
strncpy(pPassword, authPassword, maxLenPassword - 1);
}
else
{
d_printf("Domain: [%s] ", pWorkgroup);
fgets(temp, sizeof(fstring), stdin);
if (temp[strlen(temp) - 1] == '\n') /* A new line? */
{
temp[strlen(temp) - 1] = '\0';
}
if (temp[0] != '\0')
{
strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
strncpy(authWorkgroup, temp, maxLenWorkgroup - 1);
}
d_printf("Username: [%s] ", pUsername);
fgets(temp, sizeof(fstring), stdin);
if (temp[strlen(temp) - 1] == '\n') /* A new line? */
{
temp[strlen(temp) - 1] = '\0';
}
if (temp[0] != '\0')
{
strncpy(pUsername, temp, maxLenUsername - 1);
strncpy(authUsername, pUsername, maxLenUsername - 1);
}
pass = getpass("Password: ");
if (pass)
fstrcpy(temp, pass);
if (temp[strlen(temp) - 1] == '\n') /* A new line? */
{
temp[strlen(temp) - 1] = '\0';
}
if (temp[0] != '\0')
{
strncpy(pPassword, temp, maxLenPassword - 1);
strncpy(authPassword, pPassword, maxLenPassword - 1);
}
authSet = 1;
}
}
|