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