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