File: smb2-share-enum.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 (194 lines) | stat: -rw-r--r-- 5,496 bytes parent folder | download | duplicates (2)
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
/* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
/*
   Copyright (C) 2018 by Ronnie Sahlberg <ronniesahlberg@gmail.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef STDC_HEADERS
#include <stddef.h>
#endif

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_UNISTD_H
#include <sys/unistd.h>
#endif

#include <errno.h>
#include <stdio.h>

#include "compat.h"

#include "smb2.h"
#include "libsmb2.h"
#include "libsmb2-dcerpc.h"
#include "libsmb2-dcerpc-srvsvc.h"
#include "libsmb2-raw.h"
#include "libsmb2-private.h"

struct smb2nse {
        smb2_command_cb cb;
        void *cb_data;
        union {
                struct srvsvc_NetrShareEnum_req se_req;
        };
};

static void
nse_free(struct smb2nse *nse)
{
        free(discard_const(nse->se_req.ServerName.utf8));
        free(nse);
}

static void
srvsvc_ioctl_cb(struct dcerpc_context *dce, int status,
                void *command_data, void *cb_data)
{
        struct smb2nse *nse = cb_data;
        struct srvsvc_rep *rep = command_data;
        struct smb2_context *smb2 = dcerpc_get_smb2_context(dce);

        if (status != SMB2_STATUS_SUCCESS) {
                nse->cb(smb2, status, NULL, nse->cb_data);
                nse_free(nse);
                dcerpc_destroy_context(dce);
                return;
        }
        
        nse->cb(smb2, rep->status, rep, nse->cb_data);
        nse_free(nse);
        dcerpc_destroy_context(dce);
}

static void
share_enum_bind_cb(struct dcerpc_context *dce, int status,
                      void *command_data, void *cb_data)
{
        struct smb2nse *nse = cb_data;
        struct smb2_context *smb2 = dcerpc_get_smb2_context(dce);

        if (status != SMB2_STATUS_SUCCESS) {
                nse->cb(smb2, status, NULL, nse->cb_data);
                nse_free(nse);
                dcerpc_destroy_context(dce);
                return;
        }

        status = dcerpc_call_async(dce,
                                   SRVSVC_NETRSHAREENUM,
                                   srvsvc_NetrShareEnum_req_coder, &nse->se_req,
                                   srvsvc_NetrShareEnum_rep_coder,
                                   sizeof(struct srvsvc_NetrShareEnum_rep),
                                   srvsvc_ioctl_cb, nse);
        if (status) {
                nse->cb(smb2, status, NULL, nse->cb_data);
                nse_free(nse);
                dcerpc_destroy_context(dce);
                return;
        }
}

int
smb2_share_enum_async(struct smb2_context *smb2,
                      enum SHARE_INFO_enum  level,
                      smb2_command_cb cb, void *cb_data)
{
        struct dcerpc_context *dce;
        struct smb2nse *nse;
        int rc;
        char *server;

        dce = dcerpc_create_context(smb2);
        if (dce == NULL) {
                return -ENOMEM;
        }
        
        nse = calloc(1, sizeof(struct smb2nse));
        if (nse == NULL) {
                smb2_set_error(smb2, "Failed to allocate nse");
                dcerpc_destroy_context(dce);
                return -ENOMEM;
        }
        nse->cb = cb;
        nse->cb_data = cb_data;

        server = malloc(strlen(smb2->server) + 3);
        if (server == NULL) {
                free(nse);
                smb2_set_error(smb2, "Failed to allocate server");
                dcerpc_destroy_context(dce);
                return -ENOMEM;
        }
        
        sprintf(server, "\\\\%s", smb2->server);
        nse->se_req.ServerName.utf8 = server;

        switch (level) {
        case SHARE_INFO_0:
                nse->se_req.ses.Level = level;
                nse->se_req.ses.ShareInfo.Level = level;
                nse->se_req.ses.ShareInfo.Level0.EntriesRead = 0;
                nse->se_req.ses.ShareInfo.Level0.Buffer = NULL;
                break;
        case SHARE_INFO_1:
                nse->se_req.ses.Level = level;
                nse->se_req.ses.ShareInfo.Level = level;
                nse->se_req.ses.ShareInfo.Level1.EntriesRead = 0;
                nse->se_req.ses.ShareInfo.Level1.Buffer = NULL;
                break;
        }
        nse->se_req.PreferedMaximumLength = 0xffffffff;
        nse->se_req.ResumeHandle = 0;

        rc = dcerpc_connect_context_async(dce, "srvsvc", &srvsvc_interface,
                                          share_enum_bind_cb, nse);
        if (rc) {
                nse_free(nse);
                dcerpc_destroy_context(dce);
                return rc;
        }
        
        return 0;
}