summaryrefslogtreecommitdiff
path: root/examples/libmsrpc/cacusermgr/mgr_group.c
blob: a936433e602d927bf481a26fcaec8a99c122d2b1 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Unix SMB/CIFS implementation. 
 * cacusermgr group implementation.
 *
 * Copyright (C) Chris Nicholls     2005
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 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 General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 675
 * Mass Ave, Cambridge, MA 02139, USA.  */

#include "cacusermgr.h"

CacGroupInfo *get_group_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *group_hnd) {
   struct SamGetGroupInfo getinfo;

   if(!hnd || !mem_ctx ||!group_hnd)
      return NULL;

   ZERO_STRUCT(getinfo);
   getinfo.in.group_hnd = group_hnd;

   if(!cac_SamGetGroupInfo(hnd, mem_ctx, &getinfo)) 
      printerr("Could not get group info.", hnd->status);

   return getinfo.out.info;
}

void print_group_info(CacGroupInfo *info) {
   if(!info)
      return;

   printf(" Group Name        : %s\n", info->name);
   printf(" Description       : %s\n", info->description);
   printf(" Number of Members : %d\n", info->num_members);
}

CacGroupInfo *modify_group_info(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *group_hnd) {
   struct SamSetGroupInfo setinfo;
   CacGroupInfo *info = NULL;
   fstring tmp;

   info = get_group_info(hnd, mem_ctx, group_hnd);

   if(!info)
      return NULL;

   printf("Description [%s]: ", info->description);
   mgr_getline(tmp);
   if(tmp[0] != '\0')
      info->description = talloc_strdup(mem_ctx, tmp);

   ZERO_STRUCT(setinfo);
   setinfo.in.group_hnd = group_hnd;
   setinfo.in.info = info;

   if(!cac_SamSetGroupInfo(hnd, mem_ctx, &setinfo)) {
      printerr("Could not set info.", hnd->status);
      info = NULL;
   }

   return info;
}

void group_menu(CacServerHandle *hnd, TALLOC_CTX *mem_ctx, POLICY_HND *dom_hnd, POLICY_HND *group_hnd) {
   CacGroupInfo *info = NULL;
   int rid_type = 0;

   fstring in;

   char *buf;

   struct SamGetGroupMembers getmem;
   struct SamGetNamesFromRids getnames;
   struct SamAddGroupMember add;
   struct SamRemoveGroupMember del;
 
   info = get_group_info(hnd, mem_ctx, group_hnd);

   printf("\n");
   print_group_info(info);

   while(in[0] != 'b' && in[0] != 'B' && in[0] != 'q' && in[0] != 'Q') {
      printf("\n");
      printf("[m] List Group Members\n");
      printf("[a] Add User To Group\n");
      printf("[r] Remove User From Group\n");
      printf("[l] List Users\n");
      printf("[v] View Group Info\n");
      printf("[d] Set Group Description\n");
      printf("[x] Delete Group\n");
      printf("[b] Back\n\n");
      
      printf("Command: ");
      mgr_getline(in);
      
      printf("\n");

      switch(in[0]) {
         case 'a': /*add member to group*/
         case 'A':
            ZERO_STRUCT(add);
            add.in.group_hnd = group_hnd;

            printf("Enter RID or Name: ");
            rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &add.in.rid, &buf);

            if(rid_type != CAC_USER_RID) {
               printf("Invalid User.\n");
               break;
            }

            if(!cac_SamAddGroupMember(hnd, mem_ctx, &add)) {
               printerr("Could not add user to group.", hnd->status);
            }
            break;

         case 'r': /*remove user from group*/
         case 'R':
            ZERO_STRUCT(del);
            del.in.group_hnd = group_hnd;

            printf("Enter RID or Name: ");
            rid_type = rid_or_name(hnd, mem_ctx, dom_hnd, &del.in.rid, &buf);

            if(rid_type != CAC_USER_RID) {
               printf("Invalid User.\n");
               break;
            }

            if(!cac_SamRemoveGroupMember(hnd, mem_ctx, &del)) {
               printerr("Could not remove use from group.", hnd->status);
            }
            break;

         case 'l': /*list users*/
         case 'L':
            list_users(hnd, mem_ctx, dom_hnd);
            break;

         case 'm': /*list members*/
         case 'M':
            ZERO_STRUCT(getmem);
            getmem.in.group_hnd = group_hnd;

            if(!cac_SamGetGroupMembers(hnd, mem_ctx, &getmem)) {
               printerr("Could not get members.", hnd->status);
               break;
            }

            ZERO_STRUCT(getnames);
            getnames.in.dom_hnd = dom_hnd;
            getnames.in.rids = getmem.out.rids;
            getnames.in.num_rids = getmem.out.num_members;

            if(!cac_SamGetNamesFromRids(hnd, mem_ctx, &getnames)) {
               printerr("Could not lookup names.", hnd->status);
               break;
            }

            printf("Group has %d members:\n", getnames.out.num_names);
            print_lookup_records(getnames.out.map, getnames.out.num_names);

            break;

         case 'd': /*set description*/
         case 'D':
            info = modify_group_info(hnd, mem_ctx, group_hnd);

            if(info)
               printf("Set Group Info.\n");
            break;

         case 'v': /*view info*/
         case 'V':
            info = get_group_info(hnd, mem_ctx, group_hnd);
            print_group_info(info);
            break;

         case 'x': /*delete group*/
         case 'X': 
            if(!cac_SamDeleteGroup(hnd, mem_ctx, group_hnd))
               printerr("Could Not Delete Group.", hnd->status);

            /*we want to go back to the main menu*/
            in[0] = 'b';
            break;

         case 'b': /*back*/
         case 'B':
         case 'q':
         case 'Q':
            break;

         default:
            printf("Invalid Command.\n");
      }
   }

   cac_SamClose(hnd, mem_ctx, group_hnd);
}