iceauth.c revision e8ac26b0
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    int i;
89    const char *sourcename = defsource;
90    const char **arglist = defcmds;
91    int nargs = ndefcmds;
92    int status;
93
94    ProgramName = argv[0];
95
96    for (i = 1; i < argc; i++) {
97	const char *arg = argv[i];
98
99	if (arg[0] == '-') {
100	    const char *flag;
101
102	    for (flag = (arg + 1); *flag; flag++) {
103		switch (*flag) {
104		  case 'f':		/* -f authfilename */
105		    if (++i >= argc) {
106			fprintf(stderr, "%s: -f requires an argument\n",
107				ProgramName);
108			usage (1);
109		    }
110		    authfilename = argv[i];
111		    continue;
112		  case 'V':		/* -V */
113		    printf("%s\n", PACKAGE_STRING);
114		    exit(0);
115		  case 'v':		/* -v */
116		    verbose = 1;
117		    continue;
118		  case 'q':		/* -q */
119		    verbose = 0;
120		    continue;
121		  case 'b':		/* -b */
122		    break_locks = True;
123		    continue;
124		  case 'i':		/* -i */
125		    ignore_locks = True;
126		    continue;
127		  case 'u':		/* -u */
128		    usage (0);
129		  default:
130		    fprintf(stderr, "%s: unrecognized option '%s'\n",
131			    ProgramName, flag);
132		    usage (1);
133		}
134	    }
135	} else {
136	    sourcename = "(argv)";
137	    nargs = argc - i;
138	    arglist = (const char **) argv + i;
139	    if (verbose == -1) verbose = 0;
140	    break;
141	}
142    }
143
144    if (verbose == -1) {		/* set default, don't junk stdout */
145	verbose = (isatty(fileno(stdout)) != 0);
146    }
147
148    if (!authfilename) {
149	authfilename = IceAuthFileName ();	/* static name, do not free */
150	if (!authfilename) {
151	    fprintf (stderr,
152		     "%s:  unable to generate an authority file name\n",
153		     ProgramName);
154	    exit (1);
155	}
156    }
157    if (auth_initialize (authfilename) != 0) {
158	/* error message printed in auth_initialize */
159	exit (1);
160    }
161
162    status = process_command (sourcename, 1, nargs, arglist);
163
164    (void) auth_finalize ();
165    exit ((status != 0) ? 1 : 0);
166}
167