style: compare return value of getopt() against -1 rather than EOF
[games.git] / usr.bin / ncplogin / ncplogin.c
CommitLineData
984263bc
MD
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 $
9b0ec895 33 * $DragonFly: src/usr.bin/ncplogin/ncplogin.c,v 1.5 2008/07/10 18:29:52 swildner Exp $
984263bc
MD
34 */
35#include <sys/types.h>
36#include <sys/errno.h>
37#include <sys/stat.h>
38#include <err.h>
39#include <stdio.h>
40#include <unistd.h>
41#include <strings.h>
42#include <stdlib.h>
43#include <sysexits.h>
44
45#include <netncp/ncp_lib.h>
46#include <netncp/ncp_rcfile.h>
47
48extern char *__progname;
49
50static void
1d1731fa 51login_usage(void) {
984263bc
MD
52 printf("usage: %s [-Dh] [-A host] [-BCN] [-I level] [-M mode] \n"
53 " [-R retrycount] [-W timeout] /server:user\n", __progname);
54 exit(1);
55}
56
57static void
1d1731fa 58logout_usage(void) {
984263bc
MD
59 printf("usage: %s [-c handle] [-h] [/server:user]\n", __progname);
60 exit(1);
61}
62
63static void
64login(int argc, char *argv[], struct ncp_conn_loginfo *li) {
65 int error = 0, connid, opt, setprimary = 0;
66
9b0ec895 67 while ((opt = getopt(argc, argv, STDPARAM_OPT"D")) != -1){
984263bc
MD
68 switch(opt){
69 case STDPARAM_ARGS:
70 if (ncp_li_arg(li, opt, optarg))
71 exit(1);
72 break;
73 case 'D':
74 setprimary = 1;
75 break;
76 default:
77 login_usage();
78 /*NOTREACHED*/
79 }
80 }
81 if (li->access_mode == 0)
82 li->access_mode = S_IRWXU;
83 if (ncp_li_check(li))
84 exit(1);
85 li->opt |= NCP_OPT_WDOG | NCP_OPT_PERMANENT;
86 /* now we can try to login, or use already established connection */
87 error = ncp_li_login(li, &connid);
88 if (error) {
89 ncp_error("Could not login to server %s", error, li->server);
90 exit(1);
91 }
92 error = ncp_setpermanent(connid, 1);
93 if (error && errno != EACCES){
94 ncp_error("Can't make connection permanent", error);
95 exit(1);
96 }
97 if (setprimary && ncp_setprimary(connid, 1) != 0)
98 ncp_error("Warning: can't make connection primary", errno);
99 printf("Logged in with conn handle:%d\n", connid);
100 return;
101}
102
103static void
104logout(int argc, char *argv[], struct ncp_conn_loginfo *li) {
105 int error = 0, connid, opt;
106
107 connid = -1;
9b0ec895 108 while ((opt = getopt(argc, argv, STDPARAM_OPT"c:")) != -1){
984263bc
MD
109 switch (opt) {
110 case 'c':
111 connid = atoi(optarg);
112 break;
113 case STDPARAM_ARGS:
114 if (ncp_li_arg(li, opt, optarg))
115 exit(1);
116 break;
117 default:
118 logout_usage();
119 /*NOTREACHED*/
120 }
121 }
122 if (connid == -1) {
123 if (li->server[0] == 0)
124 errx(EX_USAGE, "no server name specified");
125 if (li->user == 0)
126 errx(EX_USAGE, "no user name specified");
127 if (ncp_conn_scan(li, &connid))
128 errx(EX_OSERR, "You are not attached to server %s",
129 li->server);
130 }
131 if (ncp_setpermanent(connid, 0) < 0 && errno != EACCES) {
132 ncp_error("Connection isn't valid", errno);
133 exit(EX_OSERR);
134 }
135 error = ncp_disconnect(connid);
136 if (error) {
137 if (errno == EACCES) {
138 warnx("you logged out, but connection belongs"
139 "to other user and not closed");
140 } else {
141 ncp_error("Can't logout with connid %d", error, connid);
142 error = 1;
143 }
144 }
145 exit(error ? 1 : 0);
146}
147
148int
149main(int argc, char *argv[]) {
150 int islogin, error;
151 char *p, *p1;
152 struct ncp_conn_loginfo li;
153
154 islogin = strcmp(__progname, "ncplogin") == 0;
155
156 if (argc == 2) {
157 if (strcmp(argv[1], "-h") == 0) {
158 if (islogin)
159 login_usage();
160 else
161 logout_usage();
162 }
163 }
164
165 if (ncp_initlib())
166 exit(1);
167 if (ncp_li_init(&li, argc, argv))
168 return 1;
169
170 if (argc >= 2 && argv[argc - 1][0] == '/') {
171 p = argv[argc - 1];
172 error = 1;
173 do {
174 if (*p++ != '/')
175 break;
176 p1 = strchr(p, ':');
177 if (p1 == NULL)
178 break;
179 *p1++ = 0;
180 if (ncp_li_setserver(&li, p))
181 break;
182 if (*p1 == 0)
183 break;
184 if (ncp_li_setuser(&li, p1)) break;
185 error = 0;
186 } while(0);
187 if (error)
188 errx(EX_DATAERR,
3f625015 189 "an error occurred while parsing '%s'",
984263bc
MD
190 argv[argc - 1]);
191 }
192
193 if (ncp_li_readrc(&li))
194 return 1;
195 if (ncp_rc)
196 rc_close(ncp_rc);
197 if (islogin)
198 login(argc, argv, &li);
199 else
200 logout(argc, argv, &li);
201 return 0;
202}