~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/lib/netapi/examples/share/share_enum.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Unix SMB/CIFS implementation.
 
3
 *  NetShareEnum query
 
4
 *  Copyright (C) Guenther Deschner 2008
 
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 <sys/types.h>
 
21
#include <inttypes.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
 
 
26
#include <netapi.h>
 
27
 
 
28
#include "common.h"
 
29
 
 
30
int main(int argc, const char **argv)
 
31
{
 
32
        NET_API_STATUS status;
 
33
        struct libnetapi_ctx *ctx = NULL;
 
34
        const char *hostname = NULL;
 
35
        uint32_t level = 0;
 
36
        uint8_t *buffer = NULL;
 
37
        uint32_t entries_read = 0;
 
38
        uint32_t total_entries = 0;
 
39
        uint32_t resume_handle = 0;
 
40
        int i;
 
41
 
 
42
        struct SHARE_INFO_0 *i0 = NULL;
 
43
        struct SHARE_INFO_1 *i1 = NULL;
 
44
        struct SHARE_INFO_2 *i2 = NULL;
 
45
 
 
46
        poptContext pc;
 
47
        int opt;
 
48
 
 
49
        struct poptOption long_options[] = {
 
50
                POPT_AUTOHELP
 
51
                POPT_COMMON_LIBNETAPI_EXAMPLES
 
52
                POPT_TABLEEND
 
53
        };
 
54
 
 
55
        status = libnetapi_init(&ctx);
 
56
        if (status != 0) {
 
57
                return status;
 
58
        }
 
59
 
 
60
        pc = poptGetContext("share_enum", argc, argv, long_options, 0);
 
61
 
 
62
        poptSetOtherOptionHelp(pc, "hostname level");
 
63
        while((opt = poptGetNextOpt(pc)) != -1) {
 
64
        }
 
65
 
 
66
        if (!poptPeekArg(pc)) {
 
67
                poptPrintHelp(pc, stderr, 0);
 
68
                goto out;
 
69
        }
 
70
        hostname = poptGetArg(pc);
 
71
 
 
72
        if (poptPeekArg(pc)) {
 
73
                level = atoi(poptGetArg(pc));
 
74
        }
 
75
 
 
76
        /* NetShareEnum */
 
77
 
 
78
        do {
 
79
                status = NetShareEnum(hostname,
 
80
                                      level,
 
81
                                      &buffer,
 
82
                                      (uint32_t)-1,
 
83
                                      &entries_read,
 
84
                                      &total_entries,
 
85
                                      &resume_handle);
 
86
                if (status == 0 || status == ERROR_MORE_DATA) {
 
87
                        printf("total entries: %d\n", total_entries);
 
88
                        switch (level) {
 
89
                                case 0:
 
90
                                        i0 = (struct SHARE_INFO_0 *)buffer;
 
91
                                        break;
 
92
                                case 1:
 
93
                                        i1 = (struct SHARE_INFO_1 *)buffer;
 
94
                                        break;
 
95
                                case 2:
 
96
                                        i2 = (struct SHARE_INFO_2 *)buffer;
 
97
                                        break;
 
98
                                default:
 
99
                                        break;
 
100
                        }
 
101
                        for (i=0; i<entries_read; i++) {
 
102
                                switch (level) {
 
103
                                        case 0:
 
104
                                                printf("#%d netname: %s\n", i, i0->shi0_netname);
 
105
                                                i0++;
 
106
                                                break;
 
107
                                        case 1:
 
108
                                                printf("#%d netname: %s\n", i, i1->shi1_netname);
 
109
                                                printf("#%d type: %d\n", i, i1->shi1_type);
 
110
                                                printf("#%d remark: %s\n", i, i1->shi1_remark);
 
111
                                                i1++;
 
112
                                                break;
 
113
                                        case 2:
 
114
                                                printf("#%d netname: %s\n", i, i2->shi2_netname);
 
115
                                                printf("#%d type: %d\n", i, i2->shi2_type);
 
116
                                                printf("#%d remark: %s\n", i, i2->shi2_remark);
 
117
                                                printf("#%d permissions: %d\n", i, i2->shi2_permissions);
 
118
                                                printf("#%d max users: %d\n", i, i2->shi2_max_uses);
 
119
                                                printf("#%d current users: %d\n", i, i2->shi2_current_uses);
 
120
                                                printf("#%d path: %s\n", i, i2->shi2_path);
 
121
                                                printf("#%d password: %s\n", i, i2->shi2_passwd);
 
122
                                                i2++;
 
123
                                                break;
 
124
                                        default:
 
125
                                                break;
 
126
                                }
 
127
                        }
 
128
                        NetApiBufferFree(buffer);
 
129
                }
 
130
        } while (status == ERROR_MORE_DATA);
 
131
 
 
132
        if (status != 0) {
 
133
                printf("NetShareEnum failed with: %s\n",
 
134
                        libnetapi_get_error_string(ctx, status));
 
135
        }
 
136
 
 
137
 out:
 
138
        libnetapi_free(ctx);
 
139
        poptFreeContext(pc);
 
140
 
 
141
        return status;
 
142
}