File: smb2-server-sync.c

package info (click to toggle)
libsmb2 6.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,992 kB
  • sloc: ansic: 33,026; sh: 221; makefile: 189; cpp: 98
file content (497 lines) | stat: -rw-r--r-- 16,711 bytes parent folder | download | duplicates (3)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
/*
   Copyright (C) 2024 by Brian Dodge <bdodge09@gmail.com>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define _GNU_SOURCE

#include <inttypes.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>

#include "smb2.h"
#include "libsmb2.h"
#include "libsmb2-raw.h"

#define PAD_TO_32BIT(len) ((len + 0x03) & 0xfffffffc)
#define PAD_TO_64BIT(len) ((len + 0x07) & 0xfffffff8)

static int
fill_file_info(struct smb2_context *smb2, uint8_t info_type, uint8_t file_info_class, void **out_info)
{
        uint8_t *info = NULL;
        int len = 0;

        switch (info_type) {
        case SMB2_0_INFO_FILE:
                switch (file_info_class) {
                case SMB2_FILE_BASIC_INFORMATION:
                {
                        struct smb2_file_basic_info *fs;

                        len = sizeof(struct smb2_file_basic_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        memset(fs, 0, len);
                        fs->file_attributes = 0;
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_STANDARD_INFORMATION:
                {
                        struct smb2_file_standard_info *fs;

                        len = sizeof(struct smb2_file_standard_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        memset(fs, 0, len);
                        fs->allocation_size = 32;
                        fs->end_of_file = 32;
                        fs->number_of_links = 0;
                        fs->delete_pending = 0;
                        fs->directory = 0;
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_RENAME_INFORMATION:
                        break;
                case SMB2_FILE_ALL_INFORMATION:
                {
                        struct smb2_file_all_info *fs;

                        len = sizeof(struct smb2_file_all_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        memset(fs, 0, len);
                        fs->basic.file_attributes = 0;
                        fs->standard.allocation_size = 32;
                        fs->standard.end_of_file = 32;
                        fs->standard.number_of_links = 0;
                        fs->standard.delete_pending = 0;
                        fs->standard.directory = 0;
                        fs->index_number = 1;
                        fs->ea_size = 0;
                        fs->access_flags = 0xFFFFFFFF;
                        fs->current_byte_offset = 0;
                        fs->mode = 0;
                        fs->alignment_requirement = 0;
                        fs->name = (uint8_t*)"junk.txt";
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_NETWORK_OPEN_INFORMATION:
                {
                        struct smb2_file_network_open_info *fs;

                        len = sizeof(struct smb2_file_network_open_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        memset(fs, 0, len);
                        fs->file_attributes = 0;
                        fs->allocation_size = 32;
                        fs->end_of_file = 32;
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_END_OF_FILE_INFORMATION:
                        break;
                default:
                        break;
                }
                break;
        case SMB2_0_INFO_FILESYSTEM:
                switch (file_info_class) {
                case SMB2_FILE_FS_VOLUME_INFORMATION:
                        break;
                case SMB2_FILE_FS_SIZE_INFORMATION:
                {
                        struct smb2_file_fs_size_info *fs;

                        len = sizeof(struct smb2_file_fs_size_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        fs->total_allocation_units = 0x100000;
                        fs->available_allocation_units = 0x10000;
                        fs->sectors_per_allocation_unit = 1;
                        fs->bytes_per_sector = 512;
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_FS_DEVICE_INFORMATION:
                {
                        struct smb2_file_fs_device_info *fs;

                        len = sizeof(struct smb2_file_fs_device_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        fs->device_type = FILE_DEVICE_DISK;
                        fs->characteristics = 0;
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_FS_ATTRIBUTE_INFORMATION:
                {
                        struct smb2_file_fs_attribute_info *fs;

                        len = sizeof(struct smb2_file_fs_attribute_info);
                        fs = malloc(len);
                        if (!fs) {
                                return -1;
                        }
                        fs->filesystem_attributes = 0x2; //FILE_CASE_PRESERVED_NAMES;
                        fs->maximum_component_name_length = 0x100;
                        fs->filesystem_name = (uint8_t*)"Sotero";
                        fs->filesystem_name_length = strlen((char*)fs->filesystem_name);
                        info = (uint8_t*)fs;
                        break;
                }
                case SMB2_FILE_FS_CONTROL_INFORMATION:
                        break;
                case SMB2_FILE_FS_FULL_SIZE_INFORMATION:
                        break;
                case SMB2_FILE_FS_SECTOR_SIZE_INFORMATION:
                        break;
                default:
                        break;
                }
                break;
        case SMB2_0_INFO_SECURITY:
                break;
        case SMB2_0_INFO_QUOTA:
                break;
        default:
                return 0;
        }

        *out_info = info;
        return len;
}

static int fill_dir_info(struct smb2_context *smb2, uint8_t **out_info)
{
        static const char *files[] = {
                "junk.txt",
                "crap.bin"
        };
        struct smb2_fileidbothdirectoryinformation *fsb;
        struct smb2_utf16 *fname = NULL;
        uint8_t *info;
        uint32_t fname_len;
        int len;
        int nfiles = sizeof(files)/sizeof(files[0]);
        int n;

        len = 0;
        for (n = 0; n < nfiles; n++) {
                len += PAD_TO_64BIT(sizeof(struct smb2_fileidbothdirectoryinformation) + 2 * strlen(files[n]));
        }

        info = malloc(len);
        if (!info) {
                //smb2_set_error(smb2, "can not alloc dir info", smb2_get_error(smb2));
                return -ENOMEM;
        }
        memset(info, 0, len);

        len = 0;
        for (n = 0; n < nfiles; n++) {
                fname_len = 0;
                fname = smb2_utf8_to_utf16(files[n]);
                if (fname == NULL) {
                        //smb2_set_error(smb2, "Could not convert name into UTF-16");
                        return -EINVAL;
                }
                fname_len = 2 * fname->len;

                fsb = (struct smb2_fileidbothdirectoryinformation *)(info + len);
                fsb->file_index = n;
                fsb->file_name_length = fname_len;
                fsb->short_name_length = sizeof(fsb->short_name);
                if (fsb->short_name_length > fname_len) {
                        fsb->short_name_length = fname_len;
                }
                memcpy(fsb->short_name, fname->val, fsb->short_name_length);
                fsb->name = files[n];
                len += PAD_TO_64BIT(sizeof(struct smb2_fileidbothdirectoryinformation));
                free(fname);
        }
        *out_info = info;
        return len;
}

static int session_handler(struct smb2_server *srvr, struct smb2_context *smb2)
{
        printf("Selected dialect %04x\n", smb2_get_dialect(smb2));
        return 0;
}

static int authorize_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                                const char *user,
                                const char *domain,
                                const char *workstation)
{
        if (user) {
                smb2_set_user(smb2, user);
                smb2_set_password_from_file(smb2);
                return 0;
        }
        return -1;
}

static int logoff_handler(struct smb2_server *srvr, struct smb2_context *smb2)
{
        return 0;
}

static int tree_connect_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_tree_connect_request *req,
                    struct smb2_tree_connect_reply *rep)
{
        rep->share_type = SMB2_SHARE_TYPE_DISK;
        rep->maximal_access = 0x101f01ff;

        if (req->path && req->path_length) {
                int ei = (req->path_length / 2) - 4;
                if (ei >= 0) {
                        if (req->path[ei] == 'I' && req->path[ei + 3] == '$') {
                                rep->share_type = SMB2_SHARE_TYPE_PIPE;
                                rep->maximal_access = 0x1f00a9;
                        }
                }
        }
        rep->share_flags = 0;
        rep->capabilities = 0;

        return 0;
}

static int tree_disconnect_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                            const uint32_t tree_id)
{
        return 0;
}

static int create_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_create_request *req,
                    struct smb2_create_reply *rep)
{
        rep->file_attributes = SMB2_FILE_ATTRIBUTE_NORMAL;
        return 0;
}

static int close_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_close_request *req,
                    struct smb2_close_reply *rep)
{
        memset(rep, 0, sizeof(*rep));
        return 0;
}

static int flush_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_flush_request *req)
{
        return 0;
}

static int read_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_read_request *req,
                    struct smb2_read_reply *rep)
{
        if (req->offset > 32) {
                rep->data = NULL;
                rep->data_length = 0;
                rep->data_remaining = 0;
                return 0;
        }
        rep->data_offset = req->offset;
        rep->data_length = 32;
        rep->data_remaining = 0;

        rep->data = malloc(rep->data_length);
        if (!rep->data) {
                return -ENOMEM;
        }
        for (uint32_t i = 0; i < rep->data_length; i++) {
                rep->data[i] = ('A' + i) & 0xff;
        }
        return 0;
}

static int write_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_write_request *req,
                    struct smb2_write_reply *rep)
{
        rep->count = req->length;
        rep->remaining = 0;

        return 0;
}

static int lock_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_lock_request *req)
{
        return 0;
}

int ioctl_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_ioctl_request *req,
                    struct smb2_ioctl_reply *rep)
{
        memset(rep, 0, sizeof(*rep));
        rep->ctl_code = req->ctl_code;
        memcpy(rep->file_id, req->file_id, SMB2_FD_SIZE);

        switch(rep->ctl_code) {
        case SMB2_FSCTL_VALIDATE_NEGOTIATE_INFO:
                break;
        default:
                return 1;
        }
        return 0;
}

static int cancel_handler(struct smb2_server *srvr, struct smb2_context *smb2)
{
        return 0;
}

static int echo_handler(struct smb2_server *srvr, struct smb2_context *smb2)
{
        return 0;
}

static int query_directory_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_query_directory_request *req,
                    struct smb2_query_directory_reply *rep)
{
        static int rets;

        if (rets++ == 0) {
                rep->output_buffer_length = fill_dir_info(smb2, &rep->output_buffer);
        }
        else
        {
                rep->output_buffer_length = 0;
                rep->output_buffer = NULL;
                rets = 0;
        }
        return 0;
}

static int query_info_handler(struct smb2_server *srvr, struct smb2_context *smb2,
                    struct smb2_query_info_request *req,
                    struct smb2_query_info_reply *rep)
{
        rep->output_buffer_length = fill_file_info(smb2, req->info_type, req->file_info_class, &rep->output_buffer);
        return rep->output_buffer_length > 0 ? 0 : -1;
}

struct smb2_server_request_handlers test_handlers = {
        NULL,
        authorize_handler,
        session_handler,
        logoff_handler,
        tree_connect_handler,
        tree_disconnect_handler,
        create_handler,
        close_handler,
        flush_handler,
        read_handler,
        write_handler,
        NULL,
        NULL,
        lock_handler,
        ioctl_handler,
        cancel_handler,
        echo_handler,
        query_directory_handler,
        NULL,
        query_info_handler,
        NULL
};

struct smb2_server server;

int usage(void)
{
    fprintf(stderr, "Usage:\n"
            "smbsever [port]\n\n");
    exit(1);
}

void on_smb2_error(struct smb2_context *smb2, const char *error_string)
{
        if (error_string) {
                fprintf(stderr, "%p: %s\n", smb2, error_string);
        }
}

void on_new_client(struct smb2_context *smb2, void *cb_data)
{
    struct smb2_context **pctx = (struct smb2_context **)cb_data;

    printf("New connection: %p\n", smb2);

    /* setup client context */
    smb2_set_version(smb2, SMB2_VERSION_ANY);
//  smb2_set_version(smb2, SMB2_VERSION_0210);

    smb2_register_error_callback(smb2, on_smb2_error);
    *pctx = smb2;
}

int main(int argc, char **argv)
{
    struct smb2_context *smb2 = NULL;
    int err;

    if (argc < 2) {
        usage();
    }

    memset(&server, 0, sizeof(server));
    server.handlers = &test_handlers;

    server.signing_enabled = 1;
    server.allow_anonymous = 1;

    server.port = strtoul(argv[1], NULL, 0);

    err = smb2_serve_port(&server, 1, on_new_client, (void *)&smb2);
    if (err) {
        fprintf(stderr, "smb2_serv_port failed %d\n", err);
        exit(err);
    }

    if (smb2) {
        fprintf(stderr, "client error %s\n", smb2_get_error(smb2));
    }

    return 0;
}