r10003: in the rush for 10k, I forgot to run add the rest of Chris' libmsrpc files
[kai/samba-autobuild/.git] / examples / libmsrpc / cacusermgr / mgr_group.c
1 /*
2  * Unix SMB/CIFS implementation. 
3  * cacusermgr group implementation.
4  *
5  * Copyright (C) Chris Nicholls     2005
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  * 
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 675
19  * Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "cacusermgr.h"
22
23 CacGroupInfo *get_group_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *group_hnd) {
24    struct SamGetGroupInfo getinfo;
25
26    if(!hnd || !mem_ctx ||!group_hnd)
27       return NULL;
28
29    ZERO_STRUCT(getinfo);
30    getinfo.in.group_hnd = group_hnd;
31
32    if(!cac_SamGetGroupInfo(hnd, mem_ctx, &getinfo)) 
33       printerr("Could not get group info.", hnd->status);
34
35    return getinfo.out.info;
36 }
37
38 void print_group_info(CacGroupInfo *info) {
39    if(!info)
40       return;
41
42    printf(" Group Name        : %s\n", info->name);
43    printf(" Description       : %s\n", info->description);
44    printf(" Number of Members : %d\n", info->num_members);
45 }
46
47 CacGroupInfo *modify_group_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *group_hnd) {
48    struct SamSetGroupInfo setinfo;
49    CacGroupInfo *info = NULL;
50    fstring tmp;
51
52    info = get_group_info(hnd, mem_ctx, group_hnd);
53
54    if(!info)
55       return NULL;
56
57    printf("Description [%s]: ", info->description);
58    mgr_getline(tmp);
59    if(tmp[0] != '\0')
60       info->description = talloc_strdup(mem_ctx, tmp);
61
62    ZERO_STRUCT(setinfo);
63    setinfo.in.group_hnd = group_hnd;
64    setinfo.in.info = info;
65
66    if(!cac_SamSetGroupInfo(hnd, mem_ctx, &setinfo)) {
67       printerr("Could not set info.", hnd->status);
68       info = NULL;
69    }
70
71    return info;
72 }
73
74 void group_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd, POLICY_HND *group_hnd) {
75    CacGroupInfo *info = NULL;
76    int rid_type = 0;
77
78    fstring in;
79
80    char *buf;
81
82    struct SamGetGroupMembers getmem;
83    struct SamGetNamesFromRids getnames;
84    struct SamAddGroupMember add;
85    struct SamRemoveGroupMember del;
86  
87    info = get_group_info(hnd, mem_ctx, group_hnd);
88
89    printf("\n");
90    print_group_info(info);
91
92    while(in[0] != 'b' && in[0] != 'B' && in[0] != 'q' && in[0] != 'Q') {
93       printf("\n");
94       printf("[m] List Group Members\n");
95       printf("[a] Add User To Group\n");
96       printf("[r] Remove User From Group\n");
97       printf("[l] List Users\n");
98       printf("[v] View Group Info\n");
99       printf("[d] Set Group Description\n");
100       printf("[x] Delete Group\n");
101       printf("[b] Back\n\n");
102       
103       printf("Command: ");
104       mgr_getline(in);
105       
106       printf("\n");
107
108       switch(in[0]) {
109          case 'a': /*add member to group*/
110          case 'A':
111             ZERO_STRUCT(add);
112             add.in.group_hnd = group_hnd;
113
114             printf("Enter RID or Name: ");
115             rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &add.in.rid, &buf);
116
117             if(rid_type != CAC_USER_RID) {
118                printf("Invalid User.\n");
119                break;
120             }
121
122             if(!cac_SamAddGroupMember(hnd, mem_ctx, &add)) {
123                printerr("Could not add user to group.", hnd->status);
124             }
125             break;
126
127          case 'r': /*remove user from group*/
128          case 'R':
129             ZERO_STRUCT(del);
130             del.in.group_hnd = group_hnd;
131
132             printf("Enter RID or Name: ");
133             rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &del.in.rid, &buf);
134
135             if(rid_type != CAC_USER_RID) {
136                printf("Invalid User.\n");
137                break;
138             }
139
140             if(!cac_SamRemoveGroupMember(hnd, mem_ctx, &del)) {
141                printerr("Could not remove use from group.", hnd->status);
142             }
143             break;
144
145          case 'l': /*list users*/
146          case 'L':
147             list_users(hnd, mem_ctx, dom_hnd);
148             break;
149
150          case 'm': /*list members*/
151          case 'M':
152             ZERO_STRUCT(getmem);
153             getmem.in.group_hnd = group_hnd;
154
155             if(!cac_SamGetGroupMembers(hnd, mem_ctx, &getmem)) {
156                printerr("Could not get members.", hnd->status);
157                break;
158             }
159
160             ZERO_STRUCT(getnames);
161             getnames.in.dom_hnd = dom_hnd;
162             getnames.in.rids = getmem.out.rids;
163             getnames.in.num_rids = getmem.out.num_members;
164
165             if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &getnames)) {
166                printerr("Could not lookup names.", hnd->status);
167                break;
168             }
169
170             printf("Group has %d members:\n", getnames.out.num_names);
171             print_lookup_records(getnames.out.map, getnames.out.num_names);
172
173             break;
174
175          case 'd': /*set description*/
176          case 'D':
177             info = modify_group_info(hnd, mem_ctx, group_hnd);
178
179             if(info)
180                printf("Set Group Info.\n");
181             break;
182
183          case 'v': /*view info*/
184          case 'V':
185             info = get_group_info(hnd, mem_ctx, group_hnd);
186             print_group_info(info);
187             break;
188
189          case 'x': /*delete group*/
190          case 'X': 
191             if(!cac_SamDeleteGroup(hnd, mem_ctx, group_hnd))
192                printerr("Could Not Delete Group.", hnd->status);
193
194             /*we want to go back to the main menu*/
195             in[0] = 'b';
196             break;
197
198          case 'b': /*back*/
199          case 'B':
200          case 'q':
201          case 'Q':
202             break;
203
204          default:
205             printf("Invalid Command.\n");
206       }
207    }
208
209    cac_SamClose(hnd, mem_ctx, group_hnd);
210 }