Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / ncplogin / ncplogin.c
1 /*
2  * Copyright (c) 1999, Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD: src/usr.bin/ncplogin/ncplogin.c,v 1.1 1999/10/31 02:14:59 bp Exp $
33  */
34 #include <sys/types.h>
35 #include <sys/errno.h>
36 #include <sys/stat.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <strings.h>
41 #include <stdlib.h>
42 #include <sysexits.h>
43
44 #include <netncp/ncp_lib.h>
45 #include <netncp/ncp_rcfile.h>
46
47 extern char *__progname;
48
49 static void
50 login_usage() {
51         printf("usage: %s [-Dh] [-A host] [-BCN] [-I level] [-M mode] \n"
52                "       [-R retrycount] [-W timeout] /server:user\n", __progname);
53         exit(1);
54 }
55
56 static void
57 logout_usage() {
58         printf("usage: %s [-c handle] [-h] [/server:user]\n", __progname);
59         exit(1);
60 }
61
62 static void
63 login(int argc, char *argv[], struct ncp_conn_loginfo *li) {
64         int error = 0, connid, opt, setprimary = 0;
65
66         while ((opt = getopt(argc, argv, STDPARAM_OPT"D")) != EOF){
67                 switch(opt){
68                     case STDPARAM_ARGS:
69                         if (ncp_li_arg(li, opt, optarg))        
70                                 exit(1);
71                         break;
72                     case 'D':
73                         setprimary = 1;
74                         break;
75                     default:
76                         login_usage();
77                         /*NOTREACHED*/
78                 }
79         }
80         if (li->access_mode == 0)
81                 li->access_mode = S_IRWXU;
82         if (ncp_li_check(li))
83                 exit(1);
84         li->opt |= NCP_OPT_WDOG | NCP_OPT_PERMANENT;
85         /* now we can try to login, or use already established connection */
86         error = ncp_li_login(li, &connid);
87         if (error) {
88                 ncp_error("Could not login to server %s", error, li->server);
89                 exit(1);
90         }
91         error = ncp_setpermanent(connid, 1);
92         if (error && errno != EACCES){
93                 ncp_error("Can't make connection permanent", error);
94                 exit(1);
95         }
96         if (setprimary && ncp_setprimary(connid, 1) != 0)
97                 ncp_error("Warning: can't make connection primary", errno);
98         printf("Logged in with conn handle:%d\n", connid);
99         return;
100 }
101
102 static void
103 logout(int argc, char *argv[], struct ncp_conn_loginfo *li) {
104         int error = 0, connid, opt;
105
106         connid = -1;
107         while ((opt = getopt(argc, argv, STDPARAM_OPT"c:")) != EOF){
108                 switch (opt) {
109                     case 'c':
110                         connid = atoi(optarg);
111                         break;
112                     case STDPARAM_ARGS:
113                         if (ncp_li_arg(li, opt, optarg))
114                                 exit(1);
115                         break;
116                     default:
117                         logout_usage();
118                         /*NOTREACHED*/
119                 }
120         }
121         if (connid == -1) {
122                 if (li->server[0] == 0)
123                         errx(EX_USAGE, "no server name specified");
124                 if (li->user == 0) 
125                         errx(EX_USAGE, "no user name specified");
126                 if (ncp_conn_scan(li, &connid))
127                         errx(EX_OSERR, "You are not attached to server %s",
128                              li->server);
129         }
130         if (ncp_setpermanent(connid, 0) < 0 && errno != EACCES) {
131                 ncp_error("Connection isn't valid", errno);
132                 exit(EX_OSERR);
133         }
134         error = ncp_disconnect(connid);
135         if (error) {
136                 if (errno == EACCES) {
137                         warnx("you logged out, but connection belongs"
138                               "to other user and not closed");
139                 } else {
140                         ncp_error("Can't logout with connid %d", error, connid);
141                         error = 1;
142                 }
143         }
144         exit(error ? 1 : 0);
145 }
146
147 int
148 main(int argc, char *argv[]) {
149         int islogin, error;
150         char *p, *p1;
151         struct ncp_conn_loginfo li;
152
153         islogin = strcmp(__progname, "ncplogin") == 0;
154
155         if (argc == 2) {
156                 if (strcmp(argv[1], "-h") == 0) {
157                         if (islogin)
158                                 login_usage();
159                         else
160                                 logout_usage();
161                 }
162         }
163
164         if (ncp_initlib())
165                 exit(1);
166         if (ncp_li_init(&li, argc, argv))
167                 return 1;
168
169         if (argc >= 2 && argv[argc - 1][0] == '/') {
170                 p = argv[argc - 1];
171                 error = 1;
172                 do {
173                         if (*p++ != '/')
174                                 break;
175                         p1 = strchr(p, ':');
176                         if (p1 == NULL)
177                                 break;
178                         *p1++ = 0;
179                         if (ncp_li_setserver(&li, p))
180                                 break;
181                         if (*p1 == 0)
182                                 break;
183                         if (ncp_li_setuser(&li, p1)) break;
184                         error = 0;
185                 } while(0);
186                 if (error)
187                         errx(EX_DATAERR, 
188                             "an error occured while parsing '%s'",
189                             argv[argc - 1]);
190         }
191
192         if (ncp_li_readrc(&li))
193                 return 1;
194         if (ncp_rc)
195                 rc_close(ncp_rc);
196         if (islogin)
197                 login(argc, argv, &li);
198         else
199                 logout(argc, argv, &li);
200         return 0;
201 }