include-afsconfig-before-param-h-20010712
[openafs.git] / src / libadmin / test / afscp.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 /* Test driver for admin functions. */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 RCSID("$Header$");
16
17 #include <afs/stds.h>
18
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include <pthread.h>
26
27 #include <afs/afs_Admin.h>
28 #include <afs/afs_utilAdmin.h>
29 #include <afs/afs_clientAdmin.h>
30
31 #include <afs/cellconfig.h>
32 #include <afs/cmd.h>
33 #include "common.h"
34 #include "bos.h"
35 #include "client.h"
36 #include "kas.h"
37 #include "pts.h"
38 #include "util.h"
39 #include "vos.h"
40
41 /*
42  * Globals
43  */
44
45 void *cellHandle;
46 void *tokenHandle;
47
48 /*
49  * Before processing any command, process the common arguments and
50  * get an appropriate cell handle
51  */
52
53 static int
54 MyBeforeProc(
55   struct cmd_syndesc *as,
56   char *arock)
57 {
58     afs_status_t st = 0;
59     int no_auth = 0;
60     int auth = 0;
61     char auth_cell[MAXCELLCHARS];
62     char exec_cell[MAXCELLCHARS];
63
64     /*
65      * Check what kind of authentication is necessary based upon
66      * the arguments passed
67      */
68
69     /*
70      * Check for noauth first
71      */
72
73     if (as->parms[NOAUTH_PARAM].items) {
74         no_auth = 1;
75         if (as->parms[USER_PARAM].items) {
76             ERR_EXT("you can't specify both -noauth and -authuser");
77         }
78         if (as->parms[PASSWORD_PARAM].items) {
79             ERR_EXT("you can't specify both -noauth and -authpassword");
80         }
81         if (as->parms[AUTHCELL_PARAM].items) {
82             ERR_EXT("you can't specify both -noauth and -authcell");
83         }
84     }
85
86     /*
87      * Check for user name password and auth cell
88      * It's ok to specify user name and password, but not auth cell
89      * in that case, we assume that the auth cell is the local cell.
90      */
91
92     if (as->parms[USER_PARAM].items) {
93         if (!as->parms[PASSWORD_PARAM].items) {
94             ERR_EXT("you must specify -authpassword if you specify -authuser");
95         }
96         if (as->parms[AUTHCELL_PARAM].items) {
97             strcpy(auth_cell, as->parms[AUTHCELL_PARAM].items->data);
98         } else {
99             if (!afsclient_LocalCellGet(auth_cell, &st)) {
100                 ERR_ST_EXT("can't get local cell name", st);
101             }
102         }
103     }
104
105     /*
106      * Get the execution cell.  If this parameter wasn't passed, we
107      * assume the command should execute in the local cell.
108      */
109
110     if (as->parms[EXECCELL_PARAM].items) {
111         strcpy(exec_cell, as->parms[EXECCELL_PARAM].items->data);
112     } else {
113         if (!afsclient_LocalCellGet(exec_cell, &st)) {
114             ERR_ST_EXT("can't get local cell name", st);
115         }
116     }
117
118     /*
119      * Get a token handle and a cell handle for this invocation
120      */
121
122     if (no_auth) {
123         if (!afsclient_TokenGetNew(auth_cell,
124                                    (const char *) 0,
125                                    (const char *) 0,
126                                    &tokenHandle,
127                                    &st)) {
128             ERR_ST_EXT("can't get noauth tokens", st);
129         }
130     } else {
131         if (!afsclient_TokenGetNew(auth_cell,
132                                    (const char *) as->parms[USER_PARAM].items->data,
133                                    (const char *) as->parms[PASSWORD_PARAM].items->data,
134                                    &tokenHandle,
135                                    &st)) {
136             ERR_ST_EXT("can't get tokens", st);
137         }
138     }
139
140     if (!afsclient_CellOpen(exec_cell, tokenHandle, &cellHandle, &st)) {
141         ERR_ST_EXT("can't open cell", st);
142     }
143
144     return 0;
145 }
146
147 static int MyAfterProc(
148     struct cmd_syndesc *as)
149 {
150
151     afsclient_CellClose(cellHandle, (afs_status_p) 0);
152     afsclient_TokenClose(tokenHandle, (afs_status_p) 0);
153     return 0;
154
155 }
156
157
158 void
159 SetupCommonCmdArgs(struct cmd_syndesc *as)
160 {
161     cmd_Seek(as, USER_PARAM);
162     cmd_AddParm(as, "-authuser", CMD_SINGLE, CMD_OPTIONAL,
163                  "user name to use for authentication");
164     cmd_AddParm(as, "-authpassword", CMD_SINGLE, CMD_OPTIONAL,
165                  "password to use for authentication");
166     cmd_AddParm(as, "-authcell", CMD_SINGLE, CMD_OPTIONAL,
167                  "cell to use for authentication");
168     cmd_AddParm(as, "-execcell", CMD_SINGLE, CMD_OPTIONAL,
169                  "cell where command will execute");
170     cmd_AddParm(as, "-noauth", CMD_FLAG, CMD_OPTIONAL,
171                  "run this command unauthenticated");
172 }
173
174 int main(int argc, char *argv[])
175 {
176     int code;
177     afs_status_t st;
178     char *whoami = argv[0];
179
180     /* perform client initialization */
181
182     if (!afsclient_Init(&st)) {
183         ERR_ST_EXT("can't init afs client", st);
184     }
185
186     /* initialize command syntax and globals */
187
188     cmd_SetBeforeProc(MyBeforeProc, (char *) 0);
189     cmd_SetAfterProc(MyAfterProc, (char *) 0);
190     SetupBosAdminCmd();
191     SetupClientAdminCmd();
192     SetupKasAdminCmd();
193     SetupPtsAdminCmd();
194     SetupUtilAdminCmd();
195     SetupVosAdminCmd();
196
197     /* execute command */
198
199     code = cmd_Dispatch(argc, argv);
200
201     return (code);
202 }