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