Remove the RCSID macro
[openafs.git] / src / log / unlog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11         unlog -- Tell the Andrew Cache Manager to either clean up your connection completely
12                     or eliminate the caller's PAG.
13         February 1986
14
15        Usage:
16               unlog [cell ...]
17
18               where:
19                   cell is the name the pertinent cell.
20
21         If no cell is provided, unlog destroys all tokens.
22
23         If a cell, for which a token is not held, is provided it is ignored.
24 */
25
26 #define VIRTUE      1
27 #define VICE        1
28
29 #include <afsconfig.h>
30 #include <afs/param.h>
31
32
33 #include <stdio.h>
34 #include <potpourri.h>
35 #ifdef  AFS_AIX32_ENV
36 #include <signal.h>
37 #endif
38
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <errno.h>
43 #include <sys/ioctl.h>
44 #include <afs/vice.h>
45 #include <sys/file.h>
46
47 #include <afs/auth.h>
48 #include <afs/cellconfig.h>
49 #include <afs/afsutil.h>
50 #include <afs/cmd.h>
51
52 #undef VIRTUE
53 #undef VICE
54
55
56 struct tokenInfo {
57     struct ktc_token token;
58     struct ktc_principal service;
59     struct ktc_principal client;
60     int deleted;
61 };
62
63
64 int 
65 CommandProc(struct cmd_syndesc *as, void *arock)
66 {
67 #define MAXCELLS 20             /* XXX */
68     struct cmd_item *itp;
69     afs_int32 code, i = 0;
70     char *cells[20];
71
72     if (as->parms[0].items) {   /* A cell is provided */
73         for (itp = as->parms[0].items; itp; itp = itp->next) {
74             if (i > MAXCELLS) {
75                 printf
76                     ("The maximum number of cells (%d) is exceeded; the rest are ignored\n",
77                      MAXCELLS);
78                 break;
79             }
80             cells[i++] = itp->data;
81         }
82         code = unlog_ForgetCertainTokens(cells, i);
83     } else
84         code = ktc_ForgetAllTokens();
85     if (code) {
86         printf("unlog: could not discard tickets, code %d\n", code);
87         exit(1);
88     }
89     return 0;
90 }
91
92
93 #include "AFS_component_version_number.c"
94
95 int
96 main(int argc, char *argv[])
97 {                               /*Main routine */
98     struct cmd_syndesc *ts;
99     register afs_int32 code;
100
101 #ifdef  AFS_AIX32_ENV
102     /*
103      * The following signal action for AIX is necessary so that in case of a 
104      * crash (i.e. core is generated) we can include the user's data section 
105      * in the core dump. Unfortunately, by default, only a partial core is
106      * generated which, in many cases, isn't too useful.
107      */
108     struct sigaction nsa;
109
110     sigemptyset(&nsa.sa_mask);
111     nsa.sa_handler = SIG_DFL;
112     nsa.sa_flags = SA_FULLDUMP;
113     sigaction(SIGSEGV, &nsa, NULL);
114 #endif
115
116     ts = cmd_CreateSyntax(NULL, CommandProc, NULL,
117                           "Release Kerberos authentication");
118     cmd_AddParm(ts, "-cell", CMD_LIST, CMD_OPTIONAL, "cell name");
119
120     code = cmd_Dispatch(argc, argv);
121     exit(code != 0);
122 }                               /*Main routine */
123
124
125 /*
126  * Problem: only the KTC gives you the ability to selectively destroy
127  *          a specified token.
128  *
129  * Solution: Build a list of tokens, delete the bad ones (the ones to
130  *           remove from the permissions list,) destroy all tokens, and
131  *           then re-register the good ones.  Ugly, but it works.
132  */
133
134 int
135 unlog_ForgetCertainTokens(char **list, int listSize)
136 {
137     unsigned count, index, index2;
138     afs_int32 code;
139     struct ktc_principal serviceName;
140     struct tokenInfo *tokenInfoP;
141
142     /* normalize all the names in the list */
143     unlog_NormalizeCellNames(list, listSize);
144
145     /* figure out how many tokens exist */
146     count = 0;
147     do {
148         code = ktc_ListTokens(count, &count, &serviceName);
149     } while (!code);
150
151     tokenInfoP =
152         (struct tokenInfo *)malloc((sizeof(struct tokenInfo) * count));
153     if (!tokenInfoP) {
154         perror("unlog_ForgetCertainTokens -- osi_Alloc failed");
155         exit(1);
156     }
157
158     for (code = index = index2 = 0; (!code) && (index < count); index++) {
159         code =
160             ktc_ListTokens(index2, &index2, &(tokenInfoP + index)->service);
161
162         if (!code) {
163             code =
164                 ktc_GetToken(&(tokenInfoP + index)->service,
165                              &(tokenInfoP + index)->token,
166                              sizeof(struct ktc_token),
167                              &(tokenInfoP + index)->client);
168
169             if (!code)
170                 (tokenInfoP + index)->deleted =
171                     unlog_CheckUnlogList(list, listSize,
172                                          &(tokenInfoP + index)->client);
173         }
174     }
175
176     unlog_VerifyUnlog(list, listSize, tokenInfoP, count);
177     code = ktc_ForgetAllTokens();
178
179     if (code) {
180         printf("unlog: could not discard tickets, code %d\n", code);
181         exit(1);
182     }
183
184     for (code = index = 0; index < count; index++) {
185         if (!((tokenInfoP + index)->deleted)) {
186             code =
187                 ktc_SetToken(&(tokenInfoP + index)->service,
188                              &(tokenInfoP + index)->token,
189                              &(tokenInfoP + index)->client, 0);
190             if (code) {
191                 fprintf(stderr, "Couldn't re-register token, code = %d\n",
192                         code);
193             }
194         }
195     }
196     return 0;
197 }
198
199 /*
200  * 0 if not in list, 1 if in list
201  */
202 int
203 unlog_CheckUnlogList(char **list, int count, struct ktc_principal *principal)
204 {
205     do {
206         if (strcmp(*list, principal->cell) == 0)
207             return 1;
208         list++;
209         --count;
210     } while (count);
211
212     return 0;
213 }
214
215 /*
216  * Caveat: this routine does NOT free up the memory passed (and replaced).
217  *         because it assumes it isn't a problem.
218  */
219
220 int
221 unlog_NormalizeCellNames(char **list, int size)
222 {
223     char *newCellName, *lcstring();
224     unsigned index;
225     struct afsconf_dir *conf;
226     int code;
227     struct afsconf_cell cellinfo;
228
229     if (!(conf = afsconf_Open(AFSDIR_CLIENT_ETC_DIRPATH))) {
230         fprintf(stderr, "Cannot get cell configuration info!\n");
231         exit(1);
232     }
233
234     for (index = 0; index < size; index++, list++) {
235         newCellName = malloc(MAXKTCREALMLEN);
236         if (!newCellName) {
237             perror("unlog_NormalizeCellNames --- malloc failed");
238             exit(1);
239         }
240
241         lcstring(newCellName, *list, MAXKTCREALMLEN);
242         code = afsconf_GetCellInfo(conf, newCellName, 0, &cellinfo);
243         if (code) {
244             if (code == AFSCONF_NOTFOUND) {
245                 fprintf(stderr, "Unrecognized cell name %s\n", newCellName);
246             } else {
247                 fprintf(stderr,
248                         "unlog_NormalizeCellNames - afsconf_GetCellInfo");
249                 fprintf(stderr, " failed, code = %d\n", code);
250             }
251             exit(1);
252         }
253
254
255         strcpy(newCellName, cellinfo.name);
256
257         *list = newCellName;
258     }
259     afsconf_Close(conf);
260     return 0;
261 }
262
263 /*
264  * check given list to assure tokens were held for specified cells
265  * prints warning messages for those cells without such entries.
266  */
267 int
268 unlog_VerifyUnlog(char **cellList, int cellListSize, struct tokenInfo *tokenList, int tokenListSize)
269 {
270     int index;
271
272     for (index = 0; index < cellListSize; index++) {
273         int index2;
274         int found;
275
276         for (found = index2 = 0; !found && index2 < tokenListSize; index2++)
277             found =
278                 strcmp(cellList[index],
279                        (tokenList + index2)->client.cell) == 0;
280
281         if (!found)
282             fprintf(stderr, "unlog: Warning - no tokens held for cell %s\n",
283                     cellList[index]);
284     }
285     return 0;
286 }