1 | /* $Id: main.c,v 1.43 2006-08-23 19:02:27 zacheiss Exp $ |
---|
2 | * |
---|
3 | * This is the file main.c for the Moira Client, which allows users |
---|
4 | * to quickly and easily maintain most parts of the Moira database. |
---|
5 | * It Contains: The main driver for the Moira Client. |
---|
6 | * |
---|
7 | * Created: 4/12/88 |
---|
8 | * By: Chris D. Peterson |
---|
9 | * |
---|
10 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
---|
11 | * For copying and distribution information, please see the file |
---|
12 | * <mit-copyright.h>. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <mit-copyright.h> |
---|
16 | #include <moira.h> |
---|
17 | #include <mrclient.h> |
---|
18 | #include "defs.h" |
---|
19 | #include "f_defs.h" |
---|
20 | #include "globals.h" |
---|
21 | |
---|
22 | #include <signal.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <string.h> |
---|
25 | #ifdef HAVE_UNISTD_H |
---|
26 | #include <unistd.h> |
---|
27 | #endif /* HAVE_UNISTD_H */ |
---|
28 | |
---|
29 | RCSID("$Header: /afs/athena.mit.edu/astaff/project/moiradev/repository/moira/clients/moira/main.c,v 1.43 2006-08-23 19:02:27 zacheiss Exp $"); |
---|
30 | |
---|
31 | static void ErrorExit(char *buf, int status); |
---|
32 | static void Usage(void); |
---|
33 | static void Signal_Handler(int sig); |
---|
34 | static void CatchInterrupt(int sig); |
---|
35 | static void SetHandlers(void); |
---|
36 | static char* get_program_name(char *arg); |
---|
37 | |
---|
38 | char *whoami; /* used by menu.c ugh!!! */ |
---|
39 | char *moira_server; |
---|
40 | int interrupt = 0; |
---|
41 | |
---|
42 | extern Menu moira_top_menu, list_menu, user_menu, dcm_menu; |
---|
43 | |
---|
44 | #ifdef HAVE_CURSES |
---|
45 | Bool use_menu = TRUE; /* whether or not we are using a menu. */ |
---|
46 | #endif |
---|
47 | |
---|
48 | /* Function Name: main |
---|
49 | * Description: The main driver for the Moira Client. |
---|
50 | * Arguments: argc, argv - standard command line args. |
---|
51 | * Returns: doesn't return. |
---|
52 | */ |
---|
53 | |
---|
54 | int main(int argc, char **argv) |
---|
55 | { |
---|
56 | int status; |
---|
57 | Menu *menu; |
---|
58 | char **arg; |
---|
59 | |
---|
60 | program_name = get_program_name(argv[0]); |
---|
61 | whoami = strdup(program_name); /* used by menu.c, ugh !!! */ |
---|
62 | |
---|
63 | user = mrcl_krb_user(); |
---|
64 | if (!user) |
---|
65 | exit(1); |
---|
66 | |
---|
67 | verbose = TRUE; |
---|
68 | arg = argv; |
---|
69 | moira_server = NULL; |
---|
70 | |
---|
71 | while (++arg - argv < argc) |
---|
72 | { |
---|
73 | if (**arg == '-') |
---|
74 | { |
---|
75 | if (!strcmp(*arg, "-nomenu")) |
---|
76 | { |
---|
77 | #ifdef HAVE_CURSES |
---|
78 | use_menu = FALSE; |
---|
79 | #else |
---|
80 | ; |
---|
81 | #endif |
---|
82 | } |
---|
83 | else if (!strcmp(*arg, "-menu")) |
---|
84 | { |
---|
85 | #ifdef HAVE_CURSES |
---|
86 | use_menu = TRUE; |
---|
87 | #else |
---|
88 | fprintf(stderr, "%s: No curses support. -menu option ignored\n", |
---|
89 | whoami); |
---|
90 | #endif |
---|
91 | } |
---|
92 | else if (!strcmp(*arg, "-db")) |
---|
93 | { |
---|
94 | if (arg - argv < argc - 1) |
---|
95 | { |
---|
96 | ++arg; |
---|
97 | moira_server = *arg; |
---|
98 | } else |
---|
99 | Usage(); |
---|
100 | } |
---|
101 | else |
---|
102 | Usage(); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0) |
---|
107 | != MRCL_SUCCESS) |
---|
108 | exit(1); |
---|
109 | |
---|
110 | if ((status = mr_krb5_auth(program_name))) |
---|
111 | { |
---|
112 | if (status == MR_UNKNOWN_PROC) |
---|
113 | status = mr_auth(program_name); |
---|
114 | |
---|
115 | if (status) { |
---|
116 | if (status == MR_USER_AUTH) |
---|
117 | { |
---|
118 | char buf[BUFSIZ]; |
---|
119 | com_err(program_name, status, "\nPress [RETURN] to continue"); |
---|
120 | fgets(buf, BUFSIZ, stdin); |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | if (status >= ERROR_TABLE_BASE_krb && |
---|
125 | status <= ERROR_TABLE_BASE_krb + 256) |
---|
126 | ErrorExit("\nAuthorization failed -- please run kinit", status); |
---|
127 | else |
---|
128 | ErrorExit("\nAuthorization failed.", status); |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * These signals should not be set until just before we fire up the menu |
---|
135 | * system. |
---|
136 | */ |
---|
137 | SetHandlers(); |
---|
138 | |
---|
139 | if (!strcmp(program_name, "listmaint")) |
---|
140 | menu = &list_menu; |
---|
141 | else if (!strcmp(program_name, "usermaint")) |
---|
142 | menu = &user_menu; |
---|
143 | else if (!strcmp(program_name, "dcmmaint")) |
---|
144 | menu = &dcm_menu; |
---|
145 | else |
---|
146 | menu = &moira_top_menu; |
---|
147 | |
---|
148 | #ifdef HAVE_CURSES |
---|
149 | if (use_menu) /* Start menus that execute program */ |
---|
150 | { |
---|
151 | Start_paging(); |
---|
152 | Start_menu(menu); |
---|
153 | Stop_paging(); |
---|
154 | } |
---|
155 | else /* Start program without menus. */ |
---|
156 | #endif |
---|
157 | Start_no_menu(menu); |
---|
158 | |
---|
159 | mr_disconnect(); |
---|
160 | exit(0); |
---|
161 | } |
---|
162 | |
---|
163 | /* Function Name: ErrorExit |
---|
164 | * Description: This function does the error handling and exits. |
---|
165 | * Arguments: buf - the error message to print. |
---|
166 | * status - the error code. |
---|
167 | * Returns: doesn't return. |
---|
168 | */ |
---|
169 | |
---|
170 | static void ErrorExit(char *buf, int status) |
---|
171 | { |
---|
172 | com_err(program_name, status, buf); |
---|
173 | mr_disconnect(); |
---|
174 | exit(1); |
---|
175 | } |
---|
176 | |
---|
177 | /* Function Name: usage |
---|
178 | * Description: Prints usage info and then exits. |
---|
179 | * Arguments: none |
---|
180 | * Returns: doesn't return. |
---|
181 | */ |
---|
182 | |
---|
183 | static void Usage(void) |
---|
184 | { |
---|
185 | fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n", |
---|
186 | program_name); |
---|
187 | exit(1); |
---|
188 | } |
---|
189 | |
---|
190 | /* Function Name: Signal_Handler |
---|
191 | * Description: This function cleans up from a signal interrupt. |
---|
192 | * Arguments: none. |
---|
193 | * Returns: doesn't |
---|
194 | */ |
---|
195 | |
---|
196 | static void Signal_Handler(int sig) |
---|
197 | { |
---|
198 | Put_message("Signal caught - exiting"); |
---|
199 | #ifdef HAVE_CURSES |
---|
200 | if (use_menu) |
---|
201 | Cleanup_menu(); |
---|
202 | #endif |
---|
203 | mr_disconnect(); |
---|
204 | exit(1); |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | static void CatchInterrupt(int sig) |
---|
209 | { |
---|
210 | Put_message("Interrupt! Press RETURN to continue"); |
---|
211 | interrupt = 1; |
---|
212 | } |
---|
213 | |
---|
214 | #ifdef HAVE_POSIX_SIGNALS |
---|
215 | static void SetHandlers(void) |
---|
216 | { |
---|
217 | struct sigaction act; |
---|
218 | |
---|
219 | sigemptyset(&act.sa_mask); |
---|
220 | act.sa_flags = 0; |
---|
221 | act.sa_handler = Signal_Handler; |
---|
222 | sigaction(SIGHUP, &act, NULL); |
---|
223 | sigaction(SIGQUIT, &act, NULL); |
---|
224 | #ifdef HAVE_CURSES |
---|
225 | if (use_menu) |
---|
226 | sigaction(SIGINT, &act, NULL); |
---|
227 | else |
---|
228 | #endif |
---|
229 | { |
---|
230 | act.sa_handler = CatchInterrupt; |
---|
231 | sigaction(SIGINT, &act, NULL); |
---|
232 | } |
---|
233 | } |
---|
234 | #else |
---|
235 | static void SetHandlers(void) |
---|
236 | { |
---|
237 | signal(SIGTERM, Signal_Handler); |
---|
238 | #ifdef HAVE_CURSES |
---|
239 | if (use_menu) |
---|
240 | signal(SIGINT, Signal_Handler); |
---|
241 | else |
---|
242 | #endif |
---|
243 | signal(SIGINT, CatchInterrupt); |
---|
244 | } |
---|
245 | #endif |
---|
246 | |
---|
247 | #ifdef _WIN32 |
---|
248 | static char* get_program_name(char *arg) |
---|
249 | { |
---|
250 | char* prog; |
---|
251 | char* ext; |
---|
252 | prog = max(max(strrchr(arg, '/'), strrchr(arg, '\\')) + 1, arg); |
---|
253 | prog = _strlwr(prog); |
---|
254 | prog = strdup(prog); |
---|
255 | ext = strrchr(prog, '.'); |
---|
256 | if (ext && !strcmp(ext, ".exe")) |
---|
257 | *ext = 0; |
---|
258 | return prog; |
---|
259 | } |
---|
260 | #else |
---|
261 | static char* get_program_name(char *arg) |
---|
262 | { |
---|
263 | char* prog; |
---|
264 | if (!(prog = strrchr(arg, '/'))) |
---|
265 | prog = arg; |
---|
266 | else |
---|
267 | prog++; |
---|
268 | return strdup(prog); |
---|
269 | } |
---|
270 | #endif |
---|