~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to source3/lib/netapi/examples/group/group_setusers.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
 *  NetGroupSetUsers 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
        const char *groupname = NULL;
 
36
        uint32_t level = 0;
 
37
        uint8_t *buffer = NULL;
 
38
        uint32_t num_entries = 0;
 
39
        const char **names = NULL;
 
40
        int i = 0;
 
41
        size_t buf_size = 0;
 
42
 
 
43
        struct GROUP_USERS_INFO_0 *g0 = NULL;
 
44
        struct GROUP_USERS_INFO_1 *g1 = 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("group_setusers", argc, argv, long_options, 0);
 
61
 
 
62
        poptSetOtherOptionHelp(pc, "hostname groupname 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
                poptPrintHelp(pc, stderr, 0);
 
74
                goto out;
 
75
        }
 
76
        groupname = poptGetArg(pc);
 
77
 
 
78
        if (!poptPeekArg(pc)) {
 
79
                poptPrintHelp(pc, stderr, 0);
 
80
                goto out;
 
81
        }
 
82
 
 
83
        names = poptGetArgs(pc);
 
84
        for (i=0; names[i] != NULL; i++) {
 
85
                num_entries++;
 
86
        }
 
87
 
 
88
        switch (level) {
 
89
                case 0:
 
90
                        buf_size = sizeof(struct GROUP_USERS_INFO_0) * num_entries;
 
91
 
 
92
                        status = NetApiBufferAllocate(buf_size, (void **)&g0);
 
93
                        if (status) {
 
94
                                printf("NetApiBufferAllocate failed with: %s\n",
 
95
                                        libnetapi_get_error_string(ctx, status));
 
96
                                goto out;
 
97
                        }
 
98
 
 
99
                        for (i=0; i<num_entries; i++) {
 
100
                                g0[i].grui0_name = names[i];
 
101
                        }
 
102
 
 
103
                        buffer = (uint8_t *)g0;
 
104
                        break;
 
105
                case 1:
 
106
                        buf_size = sizeof(struct GROUP_USERS_INFO_1) * num_entries;
 
107
 
 
108
                        status = NetApiBufferAllocate(buf_size, (void **)&g1);
 
109
                        if (status) {
 
110
                                printf("NetApiBufferAllocate failed with: %s\n",
 
111
                                        libnetapi_get_error_string(ctx, status));
 
112
                                goto out;
 
113
                        }
 
114
 
 
115
                        for (i=0; i<num_entries; i++) {
 
116
                                g1[i].grui1_name = names[i];
 
117
                        }
 
118
 
 
119
                        buffer = (uint8_t *)g1;
 
120
                        break;
 
121
                default:
 
122
                        break;
 
123
        }
 
124
 
 
125
        /* NetGroupSetUsers */
 
126
 
 
127
        status = NetGroupSetUsers(hostname,
 
128
                                  groupname,
 
129
                                  level,
 
130
                                  buffer,
 
131
                                  num_entries);
 
132
        if (status != 0) {
 
133
                printf("NetGroupSetUsers 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
}