r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[kai/samba.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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "cacusermgr.h"
21
22 CacGroupInfo *get_group_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *group_hnd) {
23    struct SamGetGroupInfo getinfo;
24
25    if(!hnd || !mem_ctx ||!group_hnd)
26       return NULL;
27
28    ZERO_STRUCT(getinfo);
29    getinfo.in.group_hnd = group_hnd;
30
31    if(!cac_SamGetGroupInfo(hnd, mem_ctx, &getinfo)) 
32       printerr("Could not get group info.", hnd->status);
33
34    return getinfo.out.info;
35 }
36
37 void print_group_info(CacGroupInfo *info) {
38    if(!info)
39       return;
40
41    printf(" Group Name        : %s\n", info->name);
42    printf(" Description       : %s\n", info->description);
43    printf(" Number of Members : %d\n", info->num_members);
44 }
45
46 CacGroupInfo *modify_group_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *group_hnd) {
47    struct SamSetGroupInfo setinfo;
48    CacGroupInfo *info = NULL;
49    fstring tmp;
50
51    info = get_group_info(hnd, mem_ctx, group_hnd);
52
53    if(!info)
54       return NULL;
55
56    printf("Description [%s]: ", info->description);
57    mgr_getline(tmp);
58    if(tmp[0] != '\0')
59       info->description = talloc_strdup(mem_ctx, tmp);
60
61    ZERO_STRUCT(setinfo);
62    setinfo.in.group_hnd = group_hnd;
63    setinfo.in.info = info;
64
65    if(!cac_SamSetGroupInfo(hnd, mem_ctx, &setinfo)) {
66       printerr("Could not set info.", hnd->status);
67       info = NULL;
68    }
69
70    return info;
71 }
72
73 void group_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd, POLICY_HND *group_hnd) {
74    CacGroupInfo *info = NULL;
75    int rid_type = 0;
76
77    fstring in;
78
79    char *buf;
80
81    struct SamGetGroupMembers getmem;
82    struct SamGetNamesFromRids getnames;
83    struct SamAddGroupMember add;
84    struct SamRemoveGroupMember del;
85  
86    info = get_group_info(hnd, mem_ctx, group_hnd);
87
88    printf("\n");
89    print_group_info(info);
90
91    while(in[0] != 'b' && in[0] != 'B' && in[0] != 'q' && in[0] != 'Q') {
92       printf("\n");
93       printf("[m] List Group Members\n");
94       printf("[a] Add User To Group\n");
95       printf("[r] Remove User From Group\n");
96       printf("[l] List Users\n");
97       printf("[v] View Group Info\n");
98       printf("[d] Set Group Description\n");
99       printf("[x] Delete Group\n");
100       printf("[b] Back\n\n");
101       
102       printf("Command: ");
103       mgr_getline(in);
104       
105       printf("\n");
106
107       switch(in[0]) {
108          case 'a': /*add member to group*/
109          case 'A':
110             ZERO_STRUCT(add);
111             add.in.group_hnd = group_hnd;
112
113             printf("Enter RID or Name: ");
114             rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &add.in.rid, &buf);
115
116             if(rid_type != CAC_USER_RID) {
117                printf("Invalid User.\n");
118                break;
119             }
120
121             if(!cac_SamAddGroupMember(hnd, mem_ctx, &add)) {
122                printerr("Could not add user to group.", hnd->status);
123             }
124             break;
125
126          case 'r': /*remove user from group*/
127          case 'R':
128             ZERO_STRUCT(del);
129             del.in.group_hnd = group_hnd;
130
131             printf("Enter RID or Name: ");
132             rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &del.in.rid, &buf);
133
134             if(rid_type != CAC_USER_RID) {
135                printf("Invalid User.\n");
136                break;
137             }
138
139             if(!cac_SamRemoveGroupMember(hnd, mem_ctx, &del)) {
140                printerr("Could not remove use from group.", hnd->status);
141             }
142             break;
143
144          case 'l': /*list users*/
145          case 'L':
146             list_users(hnd, mem_ctx, dom_hnd);
147             break;
148
149          case 'm': /*list members*/
150          case 'M':
151             ZERO_STRUCT(getmem);
152             getmem.in.group_hnd = group_hnd;
153
154             if(!cac_SamGetGroupMembers(hnd, mem_ctx, &getmem)) {
155                printerr("Could not get members.", hnd->status);
156                break;
157             }
158
159             ZERO_STRUCT(getnames);
160             getnames.in.dom_hnd = dom_hnd;
161             getnames.in.rids = getmem.out.rids;
162             getnames.in.num_rids = getmem.out.num_members;
163
164             if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &getnames)) {
165                printerr("Could not lookup names.", hnd->status);
166                break;
167             }
168
169             printf("Group has %d members:\n", getnames.out.num_names);
170             print_lookup_records(getnames.out.map, getnames.out.num_names);
171
172             break;
173
174          case 'd': /*set description*/
175          case 'D':
176             info = modify_group_info(hnd, mem_ctx, group_hnd);
177
178             if(info)
179                printf("Set Group Info.\n");
180             break;
181
182          case 'v': /*view info*/
183          case 'V':
184             info = get_group_info(hnd, mem_ctx, group_hnd);
185             print_group_info(info);
186             break;
187
188          case 'x': /*delete group*/
189          case 'X': 
190             if(!cac_SamDeleteGroup(hnd, mem_ctx, group_hnd))
191                printerr("Could Not Delete Group.", hnd->status);
192
193             /*we want to go back to the main menu*/
194             in[0] = 'b';
195             break;
196
197          case 'b': /*back*/
198          case 'B':
199          case 'q':
200          case 'Q':
201             break;
202
203          default:
204             printf("Invalid Command.\n");
205       }
206    }
207
208    cac_SamClose(hnd, mem_ctx, group_hnd);
209 }