Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / lib / tt / bin / ttauth / ttauth.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $TOG: ttauth.c /main/1 1999/08/30 10:46:20 mgreess $
25  *
26  * xauth - manipulate authorization file
27  *
28  * 
29 Copyright 1989, 1998  The Open Group
30
31 All Rights Reserved.
32
33 The above copyright notice and this permission notice shall be included in
34 all copies or substantial portions of the Software.
35
36 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
39 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
40 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42
43 Except as contained in this notice, the name of The Open Group shall not be
44 used in advertising or otherwise to promote the sale, use or other dealings
45 in this Software without prior written authorization from The Open Group.
46  * *
47  * Original Author of "xauth" : Jim Fulton, MIT X Consortium
48  * Modified into "iceauth"    : Ralph Mor, X Consortium
49  * Modified into "ttauth"     : Mitch Greess, Solutions Atlantic
50  */
51
52 #include "ttauth.h"
53 #include "api/c/tt_c.h"
54
55
56 /*
57  * global data
58  */
59 char *ProgramName;                      /* argv[0], set at top of main() */
60 int verbose = -1;                       /* print certain messages */
61 Bool ignore_locks = False;              /* for error recovery */
62 Bool break_locks = False;               /* for error recovery */
63
64 /*
65  * local data
66  */
67
68 static char *authfilename = NULL;       /* filename of cookie file */
69 static char *defcmds[] = { "source", "-", NULL };  /* default command */
70 static int ndefcmds = 2;
71 static char *defsource = "(stdin)";
72
73 /*
74  * utility routines
75  */
76 static void usage ()
77 {
78     static char *prefixmsg[] = {
79 "",
80 "where options include:",
81 "    -f authfilename                name of authority file to use",
82 "    -v                             turn on extra messages",
83 "    -q                             turn off extra messages",
84 "    -i                             ignore locks on authority file",
85 "    -b                             break locks on authority file",
86 "",
87 "and commands have the following syntax:",
88 "",
89 NULL };
90     static char *suffixmsg[] = {
91 "A dash may be used with the \"merge\" and \"source\" to read from the",
92 "standard input.  Commands beginning with \"n\" use numeric format.",
93 "",
94 NULL };
95     char **msg;
96
97     fprintf (stderr, "usage:  %s [-options ...] [command arg ...]\n",
98              ProgramName);
99     for (msg = prefixmsg; *msg; msg++) {
100         fprintf (stderr, "%s\n", *msg);
101     }
102     print_help (stderr, NULL, "    ");  /* match prefix indentation */
103     fprintf (stderr, "\n");
104     for (msg = suffixmsg; *msg; msg++) {
105         fprintf (stderr, "%s\n", *msg);
106     }
107     exit (1);
108 }
109
110
111 /*
112  * The main routine - parses command line and calls action procedures
113  */
114 main (argc, argv)
115     int argc;
116     char *argv[];
117 {
118     int i;
119     char *sourcename = defsource;
120     char **arglist = defcmds;
121     int nargs = ndefcmds;
122     int status;
123
124     ProgramName = argv[0];
125
126     for (i = 1; i < argc; i++) {
127         char *arg = argv[i];
128
129         if (arg[0] == '-') {
130             char *flag;
131
132             for (flag = (arg + 1); *flag; flag++) {
133                 switch (*flag) {
134                   case 'f':             /* -f authfilename */
135                     if (++i >= argc) usage ();
136                     authfilename = argv[i];
137                     continue;
138                   case 'v':             /* -v */
139                     verbose = 1;
140                     continue;
141                   case 'q':             /* -q */
142                     verbose = 0;
143                     continue;
144                   case 'b':             /* -b */
145                     break_locks = True;
146                     continue;
147                   case 'i':             /* -i */
148                     ignore_locks = True;
149                     continue;
150                   default:
151                     usage ();
152                 }
153             }
154         } else {
155             sourcename = "(argv)";
156             nargs = argc - i;
157             arglist = argv + i;
158             if (verbose == -1) verbose = 0;
159             break;
160         }
161     }
162
163     if (verbose == -1) {                /* set default, don't junk stdout */
164         verbose = (isatty(fileno(stdout)) != 0);
165     }
166
167     if (!authfilename) {
168         authfilename = tt_AuthFileName ();      /* static name, do not free */
169         if (!authfilename) {
170             fprintf (stderr,
171                      "%s:  unable to generate an authority file name\n",
172                      ProgramName);
173             exit (1);
174         }
175     }
176     if (auth_initialize (authfilename) != 0) {
177         /* error message printed in auth_initialize */
178         exit (1);
179     }
180
181     status = process_command (sourcename, 1, nargs, arglist);
182
183     (void) auth_finalize ();
184     exit ((status != 0) ? 1 : 0);
185 }