xauth.c revision 96402570
1/*
2 *
3 * xauth - manipulate authorization file
4 *
5 *
6Copyright 1989,1998  The Open Group
7
8Permission to use, copy, modify, distribute, and sell this software and its
9documentation for any purpose is hereby granted without fee, provided that
10the above copyright notice appear in all copies and that both that
11copyright notice and this permission notice appear in supporting
12documentation.
13
14The above copyright notice and this permission notice shall be included in
15all copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24Except as contained in this notice, the name of The Open Group shall not be
25used in advertising or otherwise to promote the sale, use or other dealings
26in this Software without prior written authorization from The Open Group.
27 * *
28 * Author:  Jim Fulton, MIT X Consortium
29 */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include "xauth.h"
36
37
38/*
39 * global data
40 */
41const char *ProgramName;		/* argv[0], set at top of main() */
42int verbose = -1;			/* print certain messages */
43Bool ignore_locks = False;		/* for error recovery */
44Bool break_locks = False;		/* for error recovery */
45Bool no_name_lookups = False;		/* show addresses instead of names */
46
47/*
48 * local data
49 */
50
51static const char *authfilename = NULL;	/* filename of cookie file */
52static const char *defcmds[] = { "source", "-", NULL };  /* default command */
53static int ndefcmds = 2;
54static const char *defsource = "(stdin)";
55
56/*
57 * utility routines
58 */
59static void
60usage(void)
61{
62    static const char *prefixmsg[] = {
63"",
64"where options include:",
65"    -f authfilename                name of authority file to use",
66"    -v                             turn on extra messages",
67"    -q                             turn off extra messages",
68"    -i                             ignore locks on authority file",
69"    -b                             break locks on authority file",
70"",
71"and commands have the following syntax:",
72"",
73NULL };
74    static const char *suffixmsg[] = {
75"A dash may be used with the \"merge\" and \"source\" to read from the",
76"standard input.  Commands beginning with \"n\" use numeric format.",
77"",
78NULL };
79    const char **msg;
80
81    fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
82	     ProgramName);
83    for (msg = prefixmsg; *msg; msg++) {
84	fprintf (stderr, "%s\n", *msg);
85    }
86    print_help (stderr, NULL, "    ");	/* match prefix indentation */
87    fprintf (stderr, "\n");
88    for (msg = suffixmsg; *msg; msg++) {
89	fprintf (stderr, "%s\n", *msg);
90    }
91    exit (1);
92}
93
94
95/*
96 * The main routine - parses command line and calls action procedures
97 */
98int
99main(int argc, const char *argv[])
100{
101    int i;
102    const char *sourcename = defsource;
103    const char **arglist = defcmds;
104    int nargs = ndefcmds;
105    int status;
106
107    ProgramName = argv[0];
108
109    for (i = 1; i < argc; i++) {
110	const char *arg = argv[i];
111
112	if (arg[0] == '-') {
113	    const char *flag;
114
115	    for (flag = (arg + 1); *flag; flag++) {
116		switch (*flag) {
117		  case 'f':		/* -f authfilename */
118		    if (++i >= argc) usage ();
119		    authfilename = argv[i];
120		    continue;
121		  case 'v':		/* -v */
122		    verbose = 1;
123		    continue;
124		  case 'q':		/* -q */
125		    verbose = 0;
126		    continue;
127		  case 'b':		/* -b */
128		    break_locks = True;
129		    continue;
130		  case 'i':		/* -i */
131		    ignore_locks = True;
132		    continue;
133		  case 'n':		/* -n */
134		    no_name_lookups = True;
135		    continue;
136		  default:
137		    usage ();
138		}
139	    }
140	} else {
141	    sourcename = "(argv)";
142	    nargs = argc - i;
143	    arglist = argv + i;
144	    if (verbose == -1) verbose = 0;
145	    break;
146	}
147    }
148
149    if (verbose == -1) {		/* set default, don't junk stdout */
150	verbose = (isatty(fileno(stdout)) != 0);
151    }
152
153    if (!authfilename) {
154	authfilename = XauFileName ();	/* static name, do not free */
155	if (!authfilename) {
156	    fprintf (stderr,
157		     "%s:  unable to generate an authority file name\n",
158		     ProgramName);
159	    exit (1);
160	}
161    }
162    if (auth_initialize (authfilename) != 0) {
163	/* error message printed in auth_initialize */
164	exit (1);
165    }
166
167    status = process_command (sourcename, 1, nargs, arglist);
168
169    (void) auth_finalize ();
170    exit ((status != 0) ? 1 : 0);
171}
172
173
174