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 */
59_X_NORETURN
60static void
61usage(void)
62{
63    static const char *prefixmsg[] = {
64"",
65"where options include:",
66"    -f authfilename                name of authority file to use",
67"    -v                             turn on extra messages",
68"    -q                             turn off extra messages",
69"    -i                             ignore locks on authority file",
70"    -b                             break locks on authority file",
71"    -n                             do not resolve host names in authority file",
72"    -V                             show version number of xauth",
73"",
74"and commands have the following syntax:",
75"",
76NULL };
77    static const char *suffixmsg[] = {
78"A dash may be used with the \"merge\" and \"source\" to read from the",
79"standard input.  Commands beginning with \"n\" use numeric format.",
80"",
81NULL };
82
83    fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
84	     ProgramName);
85    for (const char **msg = prefixmsg; *msg; msg++) {
86	fprintf (stderr, "%s\n", *msg);
87    }
88    print_help (stderr, NULL, "    ");	/* match prefix indentation */
89    fprintf (stderr, "\n");
90    for (const char **msg = suffixmsg; *msg; msg++) {
91	fprintf (stderr, "%s\n", *msg);
92    }
93    exit (1);
94}
95
96
97/*
98 * The main routine - parses command line and calls action procedures
99 */
100int
101main(int argc, const char *argv[])
102{
103    const char *sourcename = defsource;
104    const char **arglist = defcmds;
105    int nargs = ndefcmds;
106    int status;
107
108    ProgramName = argv[0];
109
110    for (int i = 1; i < argc; i++) {
111	const char *arg = argv[i];
112
113	if (arg[0] == '-') {
114	    for (const char *flag = (arg + 1); *flag; flag++) {
115	        switch (*flag) {
116		  case 'f':		/* -f authfilename */
117		    if (++i >= argc) usage ();
118		    authfilename = argv[i];
119		    continue;
120		  case 'v':		/* -v */
121		    verbose = 1;
122		    continue;
123		  case 'q':		/* -q */
124		    verbose = 0;
125		    continue;
126		  case 'b':		/* -b */
127		    break_locks = True;
128		    continue;
129		  case 'i':		/* -i */
130		    ignore_locks = True;
131		    continue;
132		  case 'n':		/* -n */
133		    no_name_lookups = True;
134		    continue;
135		  case 'V':		/* -V */
136		    puts(PACKAGE_VERSION);
137	   	    exit(0);
138		  default:
139		    usage ();
140	        }
141	    }
142	} else {
143	    sourcename = "(argv)";
144	    nargs = argc - i;
145	    arglist = argv + i;
146	    if (verbose == -1) verbose = 0;
147	    break;
148	}
149    }
150
151    if (verbose == -1) {		/* set default, don't junk stdout */
152	verbose = (isatty(fileno(stdout)) != 0);
153    }
154
155    if (!authfilename) {
156	authfilename = XauFileName ();	/* static name, do not free */
157	if (!authfilename) {
158	    fprintf (stderr,
159		     "%s:  unable to generate an authority file name\n",
160		     ProgramName);
161	    exit (1);
162	}
163    }
164    if (auth_initialize (authfilename) != 0) {
165	/* error message printed in auth_initialize */
166	exit (1);
167    }
168
169    status = process_command (sourcename, 1, nargs, arglist);
170
171    (void) auth_finalize ();
172    exit ((status != 0) ? 1 : 0);
173}
174
175
176