]>
Commit | Line | Data |
---|---|---|
c441a31a | 1 | /* $Id$ |
7ac48069 | 2 | * |
3 | * This is the file main.c for the Moira Client, which allows users | |
59ec8dae | 4 | * to quickly and easily maintain most parts of the Moira database. |
1931dc22 | 5 | * It Contains: The main driver for the Moira Client. |
5eaef520 | 6 | * |
08345b74 | 7 | * Created: 4/12/88 |
8 | * By: Chris D. Peterson | |
9 | * | |
7ac48069 | 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>. | |
08345b74 | 13 | */ |
14 | ||
7ac48069 | 15 | #include <mit-copyright.h> |
16 | #include <moira.h> | |
acde26f0 | 17 | #include <mrclient.h> |
7ac48069 | 18 | #include "defs.h" |
19 | #include "f_defs.h" | |
20 | #include "globals.h" | |
21 | ||
08345b74 | 22 | #include <signal.h> |
23 | #include <stdio.h> | |
f071d8a7 | 24 | #include <string.h> |
533bacb3 | 25 | #ifdef HAVE_UNISTD_H |
7ac48069 | 26 | #include <unistd.h> |
533bacb3 | 27 | #endif /* HAVE_UNISTD_H */ |
7a40dcd4 | 28 | |
7ac48069 | 29 | RCSID("$Header$"); |
30 | ||
31 | static void ErrorExit(char *buf, int status); | |
32 | static void Usage(void); | |
533bacb3 | 33 | static void Signal_Handler(int sig); |
34 | static void CatchInterrupt(int sig); | |
35 | static void SetHandlers(void); | |
80ea9aef | 36 | static char* get_program_name(char *arg); |
08345b74 | 37 | |
5eaef520 | 38 | char *whoami; /* used by menu.c ugh!!! */ |
39 | char *moira_server; | |
facd6784 | 40 | int interrupt = 0; |
85ca828a | 41 | |
8defc06b | 42 | extern Menu moira_top_menu, list_menu, user_menu, dcm_menu; |
85ca828a | 43 | |
ea0caf4a | 44 | #ifdef HAVE_CURSES |
08345b74 | 45 | Bool use_menu = TRUE; /* whether or not we are using a menu. */ |
ea0caf4a | 46 | #endif |
08345b74 | 47 | |
48 | /* Function Name: main | |
1931dc22 | 49 | * Description: The main driver for the Moira Client. |
08345b74 | 50 | * Arguments: argc, argv - standard command line args. |
51 | * Returns: doesn't return. | |
52 | */ | |
53 | ||
3bad18e8 | 54 | int main(int argc, char **argv) |
08345b74 | 55 | { |
5eaef520 | 56 | int status; |
57 | Menu *menu; | |
533bacb3 | 58 | char **arg; |
5eaef520 | 59 | |
80ea9aef | 60 | program_name = get_program_name(argv[0]); |
7ac48069 | 61 | whoami = strdup(program_name); /* used by menu.c, ugh !!! */ |
5eaef520 | 62 | |
acde26f0 | 63 | user = mrcl_krb_user(); |
64 | if (!user) | |
65 | exit(1); | |
66 | ||
5eaef520 | 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")) | |
ea0caf4a | 76 | { |
77 | #ifdef HAVE_CURSES | |
78 | use_menu = FALSE; | |
79 | #else | |
80 | ; | |
81 | #endif | |
82 | } | |
5eaef520 | 83 | else if (!strcmp(*arg, "-menu")) |
ea0caf4a | 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 | } | |
5eaef520 | 92 | else if (!strcmp(*arg, "-db")) |
93 | { | |
94 | if (arg - argv < argc - 1) | |
95 | { | |
576becad | 96 | ++arg; |
1931dc22 | 97 | moira_server = *arg; |
5eaef520 | 98 | } else |
7ac48069 | 99 | Usage(); |
5eaef520 | 100 | } |
101 | else | |
7ac48069 | 102 | Usage(); |
576becad | 103 | } |
08345b74 | 104 | } |
85ca828a | 105 | |
f91b70c1 | 106 | if (mrcl_connect(moira_server, program_name, QUERY_VERSION, 0) |
107 | != MRCL_SUCCESS) | |
acde26f0 | 108 | exit(1); |
262ca740 | 109 | |
991417e4 | 110 | if ((status = mr_krb5_auth(program_name))) |
5eaef520 | 111 | { |
b78528d8 | 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 | } | |
1931dc22 | 131 | } |
08345b74 | 132 | |
5eaef520 | 133 | /* |
134 | * These signals should not be set until just before we fire up the menu | |
135 | * system. | |
136 | */ | |
533bacb3 | 137 | SetHandlers(); |
5eaef520 | 138 | |
5eaef520 | 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 | ||
ea0caf4a | 148 | #ifdef HAVE_CURSES |
5eaef520 | 149 | if (use_menu) /* Start menus that execute program */ |
150 | { | |
151 | Start_paging(); | |
152 | Start_menu(menu); | |
153 | Stop_paging(); | |
08345b74 | 154 | } |
5eaef520 | 155 | else /* Start program without menus. */ |
ea0caf4a | 156 | #endif |
5eaef520 | 157 | Start_no_menu(menu); |
08345b74 | 158 | |
5eaef520 | 159 | mr_disconnect(); |
160 | exit(0); | |
08345b74 | 161 | } |
162 | ||
461c03b6 | 163 | /* Function Name: ErrorExit |
08345b74 | 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 | ||
5eaef520 | 170 | static void ErrorExit(char *buf, int status) |
08345b74 | 171 | { |
5eaef520 | 172 | com_err(program_name, status, buf); |
173 | mr_disconnect(); | |
174 | exit(1); | |
08345b74 | 175 | } |
176 | ||
177 | /* Function Name: usage | |
178 | * Description: Prints usage info and then exits. | |
179 | * Arguments: none | |
180 | * Returns: doesn't return. | |
181 | */ | |
182 | ||
5eaef520 | 183 | static void Usage(void) |
08345b74 | 184 | { |
5eaef520 | 185 | fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n", |
186 | program_name); | |
187 | exit(1); | |
08345b74 | 188 | } |
189 | ||
7798ebc3 | 190 | /* Function Name: Signal_Handler |
08345b74 | 191 | * Description: This function cleans up from a signal interrupt. |
192 | * Arguments: none. | |
193 | * Returns: doesn't | |
194 | */ | |
195 | ||
533bacb3 | 196 | static void Signal_Handler(int sig) |
08345b74 | 197 | { |
5eaef520 | 198 | Put_message("Signal caught - exiting"); |
ea0caf4a | 199 | #ifdef HAVE_CURSES |
5eaef520 | 200 | if (use_menu) |
201 | Cleanup_menu(); | |
ea0caf4a | 202 | #endif |
5eaef520 | 203 | mr_disconnect(); |
204 | exit(1); | |
08345b74 | 205 | } |
facd6784 | 206 | |
207 | ||
533bacb3 | 208 | static void CatchInterrupt(int sig) |
facd6784 | 209 | { |
5eaef520 | 210 | Put_message("Interrupt! Press RETURN to continue"); |
211 | interrupt = 1; | |
facd6784 | 212 | } |
533bacb3 | 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 | |
80ea9aef | 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 |