]>
Commit | Line | Data |
---|---|---|
17bbb3bc | 1 | /* $Id$ |
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 | ||
23 | RCSID("$Header$"); | |
24 | ||
25 | void RealDeleteACL(char **info, Bool one_item); | |
26 | void 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 | ||
34 | static 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 | ||
53 | static 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 | ||
78 | static char *PrintACLInfo(char **info) | |
79 | { | |
80 | static char name[BUFSIZ]; | |
81 | char buf[BUFSIZ]; | |
82 | int status; | |
83 | ||
84 | if (!info) /* If no informaion */ | |
85 | { | |
86 | Put_message("PrintACLInfo called with null info!"); | |
87 | return NULL; | |
88 | } | |
89 | Put_message(""); | |
90 | sprintf(buf, "Host: %s", info[ACL_HOST]); | |
91 | Put_message(buf); | |
92 | sprintf(buf, "Target file: %s", info[ACL_TARGET]); | |
93 | Put_message(buf); | |
94 | sprintf(buf, "Kind: %-20s List: %s", info[ACL_KIND], info[ACL_LIST]); | |
95 | Put_message(buf); | |
96 | ||
97 | sprintf(name, "%s:%s", info[ACL_HOST], info[ACL_TARGET]); | |
98 | return name; | |
99 | } | |
100 | ||
101 | /* Function Name: AskACLInfo. | |
102 | * Description: This function askes the user for information about an | |
103 | * ACL and saves it into a structure. | |
104 | * Arguments: info - a pointer the the structure to put the | |
105 | * info into. | |
106 | * Returns: none. | |
107 | */ | |
108 | ||
109 | static char **AskACLInfo(char **info) | |
110 | { | |
111 | char temp_buf[BUFSIZ]; | |
112 | char *args[3]; | |
113 | char *s, *d; | |
114 | int status; | |
115 | ||
116 | Put_message(""); | |
117 | info[ACL_HOST] = canonicalize_hostname(info[ACL_HOST]); | |
118 | sprintf(temp_buf, "ACL %s:%s.", info[ACL_HOST], info[ACL_TARGET]); | |
119 | Put_message(temp_buf); | |
120 | Put_message(""); | |
121 | ||
122 | if (GetTypeFromUser("Kind of ACL", "acl_kind", &info[ACL_KIND]) == | |
123 | SUB_ERROR) | |
124 | return NULL; | |
125 | if (GetValueFromUser("List name", &info[ACL_LIST]) == SUB_ERROR) | |
126 | return NULL; | |
127 | ||
128 | FreeAndClear(&info[ACL_MODTIME], TRUE); | |
129 | FreeAndClear(&info[ACL_MODBY], TRUE); | |
130 | FreeAndClear(&info[ACL_MODWITH], TRUE); | |
131 | ||
132 | return info; | |
133 | } | |
134 | ||
135 | /* ---------------- ACL Menu ------------------ */ | |
136 | ||
137 | /* Function Name: GetACL | |
138 | * Description: Get ACL information | |
139 | * Arguments: argc, argv - host and target file | |
140 | * Returns: DM_NORMAL. | |
141 | */ | |
142 | ||
143 | int GetACL(int argc, char **argv) | |
144 | { | |
145 | struct mqelem *top; | |
146 | ||
147 | top = GetACLInfo(argv[1], argv[2]); | |
148 | Loop(top, (void (*)(char **)) PrintACLInfo); | |
149 | FreeQueue(top); /* clean the queue. */ | |
150 | return DM_NORMAL; | |
151 | } | |
152 | ||
153 | /* Function Name: RealDeleteACL | |
154 | * Description: Does the real deletion work. | |
155 | * Arguments: info - array of char *'s containing all useful info. | |
156 | * one_item - a Boolean that is true if only one item | |
157 | * in queue that dumped us here. | |
158 | * Returns: none. | |
159 | */ | |
160 | ||
161 | void RealDeleteACL(char **info, Bool one_item) | |
162 | { | |
163 | int stat; | |
164 | ||
165 | if ((stat = do_mr_query("delete_acl", 2, &info[ACL_HOST], NULL, NULL))) | |
166 | com_err(program_name, stat, " ACL not deleted."); | |
167 | else | |
168 | Put_message("ACL deleted."); | |
169 | } | |
170 | ||
171 | /* Function Name: DeleteACL | |
172 | * Description: Delete an ACL given its name. | |
173 | * Arguments: argc, argv - host/target of the ACL | |
174 | * Returns: none. | |
175 | */ | |
176 | ||
177 | int DeleteACL(int argc, char **argv) | |
178 | { | |
179 | struct mqelem *elem = GetACLInfo(argv[1], argv[2]); | |
180 | QueryLoop(elem, PrintACLInfo, RealDeleteACL, "Delete ACL"); | |
181 | ||
182 | FreeQueue(elem); | |
183 | return DM_NORMAL; | |
184 | } | |
185 | ||
186 | /* Function Name: AddACL | |
187 | * Description: Add an ACL | |
188 | * Arguments: arc, argv - host/target of the ACL | |
189 | * Returns: DM_NORMAL. | |
190 | */ | |
191 | ||
192 | int AddACL(int argc, char **argv) | |
193 | { | |
194 | char *info[MAX_ARGS_SIZE], **args, *host; | |
195 | int stat; | |
196 | ||
197 | argv[1] = canonicalize_hostname(strdup(argv[1])); | |
198 | if (!(stat = do_mr_query("get_acl", 2, argv + 1, NULL, NULL))) | |
199 | { | |
200 | Put_message ("An ACL for that host and target already exists."); | |
201 | free(argv[1]); | |
202 | return DM_NORMAL; | |
203 | } | |
204 | else if (stat != MR_NO_MATCH) | |
205 | { | |
206 | com_err(program_name, stat, " in AddACL"); | |
207 | free(argv[1]); | |
208 | return DM_NORMAL; | |
209 | } | |
210 | ||
211 | args = AskACLInfo(SetDefaults(info, argv[1], argv[2])); | |
212 | free(argv[1]); | |
213 | if (!args) | |
214 | { | |
215 | Put_message("Aborted."); | |
216 | return DM_NORMAL; | |
217 | } | |
218 | ||
219 | if ((stat = do_mr_query("add_acl", CountArgs(args), args, NULL, NULL))) | |
220 | com_err(program_name, stat, " in AddACL"); | |
221 | ||
222 | FreeInfo(info); | |
223 | return DM_NORMAL; | |
224 | } |