source: trunk/third/moira/clients/moira/acl.c @ 24319

Revision 24319, 5.6 KB checked in by broder, 14 years ago (diff)
New Moira snapshot from SVN.
Line 
1/* $Id: acl.c 3956 2010-01-05 20:56:56Z zacheiss $
2 *
3 *      This is the file acl.c for the Moira Client, which allows users
4 *      to quickly and easily maintain most parts of the Moira database.
5 *      It Contains: Functions for handling generic ACLs.
6 *
7 * Copyright (C) 1999 by the Massachusetts Institute of Technology.
8 * For copying and distribution information, please see the file
9 * <mit-copyright.h>.
10 */
11
12#include <mit-copyright.h>
13#include <moira.h>
14#include <moira_site.h>
15#include "defs.h"
16#include "f_defs.h"
17#include "globals.h"
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/clients/moira/acl.c $ $Id: acl.c 3956 2010-01-05 20:56:56Z zacheiss $");
24
25void RealDeleteACL(char **info, Bool one_item);
26void ChangeACL(char **info, Bool one_item);
27
28/*      Function Name: SetDefaults
29 *      Description: sets the default values for ACL additions.
30 *      Arguments: info - an array of char pointers to recieve defaults.
31 *      Returns: char ** (this array, now filled).
32 */
33
34static char **SetDefaults(char **info, char *host, char *target)
35{
36  info[ACL_HOST] = strdup(host);
37  info[ACL_TARGET] = strdup(target);
38  info[ACL_KIND] = strdup("");
39  info[ACL_LIST] = strdup("");
40  info[ACL_MODTIME] = info[ACL_MODBY] = info[ACL_MODWITH] = NULL;
41
42  info[ACL_END] = NULL;
43  return info;
44}
45
46/*      Function Name: GetACLInfo
47 *      Description: Stores the info in a queue.
48 *      Arguments: host, target - ACL to get info on
49 *      Returns: a pointer to the first element in the queue or null
50 *              if ACL not found.
51 */
52
53static struct mqelem *GetACLInfo(char *host, char *target)
54{
55  int stat;
56  struct mqelem *elem = NULL;
57  char *argv[2];
58
59  argv[0] = canonicalize_hostname(strdup(host));
60  argv[1] = target;
61  stat = do_mr_query("get_acl", 2, argv, StoreInfo, &elem);
62  free(argv[0]);
63
64  if (stat)
65    {
66      com_err(program_name, stat, " in GetACLInfo");
67      return NULL;
68    }
69  return QueueTop(elem);
70}
71
72/*      Function Name: PrintACLInfo
73 *      Description: Yet another specialized print function.
74 *      Arguments: info - all info about this ACL.
75 *      Returns: a static buffer...
76 */
77
78static char *PrintACLInfo(char **info)
79{
80  static char name[BUFSIZ];
81  char buf[BUFSIZ];
82
83  if (!info)            /* If no informaion */
84    {
85      Put_message("PrintACLInfo called with null info!");
86      return NULL;
87    }
88  Put_message("");
89  sprintf(buf, "Host: %s", info[ACL_HOST]);
90  Put_message(buf);
91  sprintf(buf, "Target file: %s", info[ACL_TARGET]);
92  Put_message(buf);
93  sprintf(buf, "Kind: %-20s List: %s", info[ACL_KIND], info[ACL_LIST]);
94  Put_message(buf);
95
96  sprintf(name, "%s:%s", info[ACL_HOST], info[ACL_TARGET]);
97  return name;
98}
99
100/*      Function Name: AskACLInfo.
101 *      Description: This function askes the user for information about an
102 *                   ACL and saves it into a structure.
103 *      Arguments: info - a pointer the the structure to put the
104 *                        info into.
105 *      Returns: none.
106 */
107
108static char **AskACLInfo(char **info)
109{
110  char temp_buf[BUFSIZ];
111
112  Put_message("");
113  info[ACL_HOST] = canonicalize_hostname(info[ACL_HOST]);
114  sprintf(temp_buf, "ACL %s:%s.", info[ACL_HOST], info[ACL_TARGET]);
115  Put_message(temp_buf);
116  Put_message("");
117
118  if (GetTypeFromUser("Kind of ACL", "acl_kind", &info[ACL_KIND]) ==
119      SUB_ERROR)
120    return NULL;
121  if (GetValueFromUser("List name", &info[ACL_LIST]) == SUB_ERROR)
122    return NULL;
123
124  FreeAndClear(&info[ACL_MODTIME], TRUE);
125  FreeAndClear(&info[ACL_MODBY], TRUE);
126  FreeAndClear(&info[ACL_MODWITH], TRUE);
127
128  return info;
129}
130
131/* ---------------- ACL Menu ------------------ */
132
133/*      Function Name: GetACL
134 *      Description: Get ACL information
135 *      Arguments: argc, argv - host and target file
136 *      Returns: DM_NORMAL.
137 */
138
139int GetACL(int argc, char **argv)
140{
141  struct mqelem *top;
142
143  top = GetACLInfo(argv[1], argv[2]);
144  Loop(top, (void (*)(char **)) PrintACLInfo);
145  FreeQueue(top);               /* clean the queue. */
146  return DM_NORMAL;
147}
148
149/*      Function Name: RealDeleteACL
150 *      Description: Does the real deletion work.
151 *      Arguments: info - array of char *'s containing all useful info.
152 *                 one_item - a Boolean that is true if only one item
153 *                              in queue that dumped us here.
154 *      Returns: none.
155 */
156
157void RealDeleteACL(char **info, Bool one_item)
158{
159  int stat;
160
161  if ((stat = do_mr_query("delete_acl", 2, &info[ACL_HOST], NULL, NULL)))
162    com_err(program_name, stat, " ACL not deleted.");
163  else
164    Put_message("ACL deleted.");
165}
166
167/*      Function Name: DeleteACL
168 *      Description: Delete an ACL given its name.
169 *      Arguments: argc, argv - host/target of the ACL
170 *      Returns: none.
171 */
172
173int DeleteACL(int argc, char **argv)
174{
175  struct mqelem *elem = GetACLInfo(argv[1], argv[2]);
176  QueryLoop(elem, PrintACLInfo, RealDeleteACL, "Delete ACL");
177
178  FreeQueue(elem);
179  return DM_NORMAL;
180}
181
182/*      Function Name: AddACL
183 *      Description: Add an ACL
184 *      Arguments: arc, argv - host/target of the ACL
185 *      Returns: DM_NORMAL.
186 */
187
188int AddACL(int argc, char **argv)
189{
190  char *info[MAX_ARGS_SIZE], **args;
191  int stat;
192
193  argv[1] = canonicalize_hostname(strdup(argv[1]));
194  if (!(stat = do_mr_query("get_acl", 2, argv + 1, NULL, NULL)))
195    {
196      Put_message ("An ACL for that host and target already exists.");
197      free(argv[1]);
198      return DM_NORMAL;
199    }
200  else if (stat != MR_NO_MATCH)
201    {
202      com_err(program_name, stat, " in AddACL");
203      free(argv[1]);
204      return DM_NORMAL;
205    }
206
207  args = AskACLInfo(SetDefaults(info, argv[1], argv[2]));
208  free(argv[1]);
209  if (!args)
210    {
211      Put_message("Aborted.");
212      return DM_NORMAL;
213    }
214
215  if ((stat = do_mr_query("add_acl", CountArgs(args), args, NULL, NULL)))
216    com_err(program_name, stat, " in AddACL");
217
218  FreeInfo(info);
219  return DM_NORMAL;
220}
Note: See TracBrowser for help on using the repository browser.