]> andersk Git - moira.git/blob - clients/moira/main.c
deal with new get_pobox output
[moira.git] / clients / moira / main.c
1 /* $Id$
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 "defs.h"
18 #include "f_defs.h"
19 #include "globals.h"
20
21 #include <pwd.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include <krb.h>
28
29 RCSID("$Header$");
30
31 static void ErrorExit(char *buf, int status);
32 static void Usage(void);
33 static void Signal_Handler(void);
34 static void CatchInterrupt(void);
35
36 char *whoami;                   /* used by menu.c ugh!!! */
37 char *moira_server;
38 int interrupt = 0;
39
40 extern Menu moira_top_menu, list_menu, user_menu, dcm_menu;
41
42 #ifdef HAVE_CURSES
43 Bool use_menu = TRUE;           /* whether or not we are using a menu. */
44 #endif
45
46 /*      Function Name: main
47  *      Description: The main driver for the Moira Client.
48  *      Arguments: argc, argv - standard command line args.
49  *      Returns: doesn't return.
50  */
51
52 int main(int argc, char **argv)
53 {
54   int status;
55   Menu *menu;
56   char *motd, **arg, pname[ANAME_SZ];
57   struct sigaction act;
58
59   if ((status = tf_init(TKT_FILE, R_TKT_FIL)) ||
60       (status = tf_get_pname(pname)))
61     {
62       initialize_krb_error_table();
63       com_err(whoami, status + ERROR_TABLE_BASE_krb,
64               "reading Kerberos ticket file");
65       exit(1);
66     }
67   tf_close();
68   user = pname;
69
70   if (!(program_name = strrchr(argv[0], '/')))
71     program_name = argv[0];
72   else
73     program_name++;
74   program_name = strdup(program_name);
75   whoami = strdup(program_name); /* used by menu.c,  ugh !!! */
76
77   verbose = TRUE;
78   arg = argv;
79   moira_server = NULL;
80
81   while (++arg - argv < argc)
82     {
83       if (**arg == '-')
84         {
85           if (!strcmp(*arg, "-nomenu"))
86             {
87 #ifdef HAVE_CURSES
88               use_menu = FALSE;
89 #else
90               ;
91 #endif
92             }
93           else if (!strcmp(*arg, "-menu"))
94             {
95 #ifdef HAVE_CURSES
96               use_menu = TRUE;
97 #else
98               fprintf(stderr, "%s: No curses support. -menu option ignored\n",
99                       whoami);
100 #endif
101             }
102           else if (!strcmp(*arg, "-db"))
103             {
104               if (arg - argv < argc - 1)
105                 {
106                   ++arg;
107                   moira_server = *arg;
108                 } else
109                   Usage();
110             }
111           else
112             Usage();
113         }
114     }
115
116   if ((status = mr_connect(moira_server)))
117     ErrorExit("\nConnection to Moira server failed", status);
118
119   if ((status = mr_motd(&motd)))
120     ErrorExit("\nUnable to check server status", status);
121   if (motd)
122     {
123       fprintf(stderr, "The Moira server is currently unavailable:\n%s\n",
124               motd);
125       mr_disconnect();
126       exit(1);
127     }
128
129   if ((status = mr_auth(program_name)))
130     {
131       if (status == MR_USER_AUTH)
132         {
133           char buf[BUFSIZ];
134           com_err(program_name, status, "\nPress [RETURN] to continue");
135           fgets(buf, BUFSIZ, stdin);
136         }
137       else
138         {
139           if (status >= ERROR_TABLE_BASE_krb &&
140               status <= ERROR_TABLE_BASE_krb + 256)
141             ErrorExit("\nAuthorization failed -- please run kinit", status);
142           else
143             ErrorExit("\nAuthorization failed.", status);
144         }
145     }
146
147   /*
148    * These signals should not be set until just before we fire up the menu
149    * system.
150    */
151
152   sigemptyset(&act.sa_mask);
153   act.sa_flags = 0;
154   act.sa_handler = Signal_Handler;
155   sigaction(SIGHUP, &act, NULL);
156   sigaction(SIGQUIT, &act, NULL);
157 #ifdef HAVE_CURSES
158   if (use_menu)
159     sigaction(SIGINT, &act, NULL);
160   else
161 #endif
162     {
163       act.sa_handler = CatchInterrupt;
164       sigaction(SIGINT, &act, NULL);
165     }
166
167   initialize_gdss_error_table();
168
169   if (!strcmp(program_name, "listmaint"))
170     menu = &list_menu;
171   else if (!strcmp(program_name, "usermaint"))
172     menu = &user_menu;
173   else if (!strcmp(program_name, "dcmmaint"))
174     menu = &dcm_menu;
175   else
176     menu = &moira_top_menu;
177
178 #ifdef HAVE_CURSES
179   if (use_menu)         /* Start menus that execute program */
180     {
181       Start_paging();
182       Start_menu(menu);
183       Stop_paging();
184     }
185   else                  /* Start program without menus. */
186 #endif
187     Start_no_menu(menu);
188
189   mr_disconnect();
190   exit(0);
191 }
192
193 /*      Function Name: ErrorExit
194  *      Description: This function does the error handling and exits.
195  *      Arguments: buf - the error message to print.
196  *                 status - the error code.
197  *      Returns: doesn't return.
198  */
199
200 static void ErrorExit(char *buf, int status)
201 {
202   com_err(program_name, status, buf);
203   mr_disconnect();
204   exit(1);
205 }
206
207 /*      Function Name: usage
208  *      Description: Prints usage info and then exits.
209  *      Arguments: none
210  *      Returns: doesn't return.
211  */
212
213 static void Usage(void)
214 {
215   fprintf(stderr, "Usage: %s [-nomenu | -menu] [-db server[:port]]\n",
216           program_name);
217   exit(1);
218 }
219
220 /*      Function Name: Signal_Handler
221  *      Description: This function cleans up from a signal interrupt.
222  *      Arguments: none.
223  *      Returns: doesn't
224  */
225
226 static void Signal_Handler(void)
227 {
228   Put_message("Signal caught - exiting");
229 #ifdef HAVE_CURSES
230   if (use_menu)
231     Cleanup_menu();
232 #endif
233   mr_disconnect();
234   exit(1);
235 }
236
237
238 static void CatchInterrupt(void)
239 {
240   Put_message("Interrupt! Press RETURN to continue");
241   interrupt = 1;
242 }
This page took 0.077423 seconds and 5 git commands to generate.