ncpl_conn.c revision 52153
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: head/lib/libncp/ncpl_conn.c 52153 1999-10-12 11:56:41Z bp $
33 */
34
35/*
36 *
37 * Current scheme to create/open connection:
38 * 1. ncp_li_init() - lookup -S [-U] options in command line
39 * 2. ncp_li_init() - try to find existing connection
40 * 3. ncp_li_init() - if no server name and no accessible connections - bail out
41 * 4. This is connection candidate, read .rc file, override with command line
42 *    and go ahead
43 * Note: connection referenced only via ncp_login() call. Although it is
44 * possible to get connection handle in other way, it will be unwise to use
45 * it, since conn can be destroyed at any time.
46 *
47 */
48#include <sys/param.h>
49#include <sys/sysctl.h>
50#include <sys/ioctl.h>
51#include <sys/time.h>
52#include <sys/mount.h>
53#include <fcntl.h>
54#include <ctype.h>
55#include <errno.h>
56#include <stdio.h>
57#include <string.h>
58#include <stdlib.h>
59#include <pwd.h>
60#include <grp.h>
61#include <unistd.h>
62
63#include <netncp/ncp_lib.h>
64#include <netncp/ncp_rcfile.h>
65#include <nwfs/nwfs.h>
66
67static char *server_name; /* need a better way ! */
68
69
70
71int
72ncp_li_setserver(struct ncp_conn_loginfo *li, const char *arg) {
73	if (strlen(arg) >= NCP_BINDERY_NAME_LEN) {
74		fprintf(stderr, "Server name too long:%s\n", arg);
75		return ENAMETOOLONG;
76	}
77	ncp_str_upper(strcpy(li->server, arg));
78	return 0;
79}
80
81int
82ncp_li_setuser(struct ncp_conn_loginfo *li, char *arg) {
83	if (arg && strlen(arg) >= NCP_BINDERY_NAME_LEN) {
84		fprintf(stderr, "User name too long:%s\n", arg);
85		return ENAMETOOLONG;
86	}
87	if (li->user)
88		free(li->user);
89	if (arg) {
90		li->user = strdup(arg);
91		if (li->user == NULL)
92			return ENOMEM;
93		ncp_str_upper(li->user);
94	} else
95		li->user = NULL;
96	return 0;
97}
98
99int
100ncp_li_setpassword(struct ncp_conn_loginfo *li, const char *passwd) {
101	if (passwd && strlen(passwd) >= 127) {
102		fprintf(stderr, "Password too long:%s\n", passwd);
103		return ENAMETOOLONG;
104	}
105	if (li->password) {
106		bzero(li->password, strlen(li->password));
107		free(li->password);
108	}
109	if (passwd) {
110		li->password = strdup(passwd);
111		if (li->password == NULL)
112			return ENOMEM;
113	} else
114		li->password = NULL;
115	return 0;
116}
117/*
118 * Prescan command line for [-S server] [-U user] arguments
119 * and fill li structure with defaults
120 */
121int
122ncp_li_init(struct ncp_conn_loginfo *li, int argc, char *argv[]) {
123	int  opt, error = 0;
124	char *arg;
125
126	bzero(li,sizeof(*li));
127	li->timeout = 15;	/* these values should be large enough to handle */
128	li->retry_count = 4;	/* slow servers, even on ethernet */
129	li->access_mode = 0;
130	li->password = NULL;
131	li->sig_level = 1;
132	li->objtype = NCP_BINDERY_USER;
133	li->owner = NCP_DEFAULT_OWNER;
134	li->group = NCP_DEFAULT_GROUP;
135	server_name = NULL;
136	if (argv == NULL) return 0;
137	while ((opt = ncp_getopt(argc, argv, ":S:U:")) != -1) {
138		arg = ncp_optarg;
139		switch (opt) {
140		    case 'S':
141			error = ncp_li_setserver(li, arg);
142			break;
143		    case 'U':
144			error = ncp_li_setuser(li, arg);
145			break;
146		}
147	}
148	ncp_optind = ncp_optreset = 1;
149	return error;
150}
151
152void
153ncp_li_done(struct ncp_conn_loginfo *li) {
154	if (li->user)
155		free(li->user);
156	if (li->password)
157		free(li->password);
158}
159
160/*
161 * Lookup existing connection based on li structure, if connection
162 * found, it will be referenced. Otherwise full login sequence performed.
163 */
164int
165ncp_li_login(struct ncp_conn_loginfo *li, int *aconnid) {
166	int connHandle, error;
167
168	if ((error = ncp_conn_scan(li, &connHandle)) == 0) {
169		*aconnid = connHandle;
170		return 0;
171	}
172	error = ncp_connect(li, &connHandle);
173	if (error) return errno;
174	error = ncp_login(connHandle, li->user, li->objtype, li->password);
175	if (error) {
176		ncp_disconnect(connHandle);
177	} else
178		*aconnid = connHandle;
179	return error;
180}
181
182/*
183 * read rc file as follows:
184 * 1. read [server] section
185 * 2. override with [server:user] section
186 * Since abcence of rcfile is not a bug, silently ignore that fact.
187 * rcfile never closed to reduce number of open/close operations.
188 */
189int
190ncp_li_readrc(struct ncp_conn_loginfo *li) {
191	int i, val, error;
192	char uname[NCP_BINDERY_NAME_LEN*2+1];
193	char *sect = NULL, *p;
194
195	/*
196	 * if info from cmd line incomplete, try to find existing
197	 * connection and fill server/user from it.
198	 */
199	if (li->server[0] == 0 || li->user == NULL) {
200		int connHandle;
201		struct ncp_conn_stat cs;
202
203		if ((error = ncp_conn_scan(li, &connHandle)) != 0) {
204			fprintf(stderr, "no default connection found: %s\n",strerror(errno));
205			return error;
206		}
207		ncp_conn_getinfo(connHandle, &cs);
208		ncp_li_setserver(li, cs.li.server);
209		ncp_li_setuser(li, cs.user);
210		ncp_li_setpassword(li, "");
211		ncp_disconnect(connHandle);
212	}
213	if (ncp_open_rcfile()) 	return 0;
214
215	for (i = 0; i < 2; i++) {
216		switch (i) {
217		    case 0:
218			sect = li->server;
219			break;
220		    case 1:
221			strcat(strcat(strcpy(uname,li->server),":"),li->user ? li->user : "default");
222			sect = uname;
223			break;
224		}
225		rc_getstringptr(ncp_rc, sect, "password", &p);
226		if (p)
227			ncp_li_setpassword(li, p);
228		rc_getint(ncp_rc,sect, "timeout", &li->timeout);
229		rc_getint(ncp_rc,sect, "retry_count", &li->retry_count);
230		rc_getint(ncp_rc,sect, "sig_level", &li->sig_level);
231		if (rc_getint(ncp_rc,sect,"access_mode",&val) == 0)
232			li->access_mode = val;
233		if(rc_getbool(ncp_rc,sect,"bindery",&val) == 0 && val) {
234			li->opt |= NCP_OPT_BIND;
235		}
236	}
237	return 0;
238}
239
240/*
241 * check for all uncompleted fields
242 */
243int
244ncp_li_check(struct ncp_conn_loginfo *li) {
245	int error = 0;
246	char *p;
247
248	do {
249		if (li->server[0] == 0) {
250			fprintf(stderr, "no server name specified\n");
251			error = 1;
252			break;
253		}
254		error = ncp_find_fileserver(li,
255		    (server_name==NULL) ? AF_IPX : AF_INET, server_name);
256		if (error) {
257			fprintf(stderr,"Can't find server %s, error=%s\n",li->server,strerror(errno));
258			break;
259		}
260		if (li->user == NULL || li->user[0] == 0) {
261			fprintf(stderr, "no user name specified for server %s\n",li->server);
262			error = 1;
263			break;
264		}
265		if (li->password == NULL) {
266			p = getpass("Netware password:");
267			error = ncp_li_setpassword(li, p) ? 1 : 0;
268		}
269	} while (0);
270	return error;
271}
272
273int
274ncp_conn_cnt(void) {
275	int error, cnt = 0, len = sizeof(cnt);
276
277#if __FreeBSD_version < 400001
278	error = sysctlbyname("net.ipx.ncp.conn_cnt", &cnt, &len, NULL, 0);
279#else
280	error = sysctlbyname("net.ncp.conn_cnt", &cnt, &len, NULL, 0);
281#endif
282	if (error) cnt = 0;
283	return cnt;
284}
285
286/*
287 * Find an existing connection and reference it
288 */
289int
290ncp_conn_find(char *server,char *user) {
291	struct ncp_conn_args ca;
292	int connid, error;
293
294	if (server == NULL && user == NULL) {
295		error = ncp_conn_scan(NULL,&connid);
296		if (error) return -2;
297		return connid;
298	}
299	if (server == NULL)
300		return -2;
301	ncp_str_upper(server);
302	if (user) ncp_str_upper(user);
303	bzero(&ca, sizeof(ca));
304	ncp_li_setserver(&ca, server);
305	ncp_li_setuser(&ca, user);
306	error = ncp_conn_scan(&ca,&connid);
307	if (error)
308		connid = -1;
309	return connid;
310}
311
312int
313ncp_li_arg(struct ncp_conn_loginfo *li, int opt, char *arg) {
314	int error = 0, sig_level;
315	char *p, *cp;
316	struct group *gr;
317	struct passwd *pw;
318
319	switch(opt) {
320	    case 'S': /* we already fill server/[user] pair */
321	    case 'U':
322		break;
323	    case 'A':
324		server_name = arg;
325		break;
326	    case 'B':
327		li->opt |= NCP_OPT_BIND;
328		break;
329	    case 'C':
330		li->opt |= NCP_OPT_NOUPCASEPASS;
331		break;
332	    case 'I':
333		sig_level = atoi(arg);
334		if (sig_level < 0 || sig_level > 3) {
335			fprintf(stderr, "Invalid NCP signature level option `%s' (must be number between 0 and 3)\n", arg);
336			error = 1;
337		}
338		li->sig_level = sig_level;
339		if (sig_level > 1) li->opt |= NCP_OPT_SIGN;
340		break;
341	    case 'M':
342		li->access_mode = strtol(arg, NULL, 8);
343		break;
344	    case 'N':
345		ncp_li_setpassword(li, "");
346		break;
347	    case 'O':
348		p = strdup(arg);
349		cp = strchr(p, ':');
350		if (cp) {
351			*cp++ = '\0';
352			if (*cp) {
353				gr = getgrnam(cp);
354				if (gr) {
355					li->group = gr->gr_gid;
356				} else
357					ncp_error("Invalid group name %s, ignored",
358					    0, cp);
359			}
360		}
361		if (*p) {
362			pw = getpwnam(p);
363			if (pw) {
364				li->owner = pw->pw_uid;
365			} else
366				ncp_error("Invalid user name %s, ignored", 0, p);
367		}
368		endpwent();
369		free(p);
370		break;
371	    case 'P':
372		li->opt |= NCP_OPT_PERMANENT;
373		break;
374	    case 'R':
375		li->retry_count = atoi(arg);
376		break;
377	    case 'W':
378		li->timeout = atoi(arg);
379		break;
380	}
381	return error;
382}
383
384void *
385ncp_conn_list(void) {
386	int error, cnt = 0, len;
387	void *p;
388
389	cnt = ncp_conn_cnt();
390	if (cnt == 0) return NULL;
391	len = cnt*(sizeof(struct ncp_conn_stat))+sizeof(int);
392	p = malloc(len);
393	if (p == NULL) return NULL;
394#if __FreeBSD_version < 400001
395	error = sysctlbyname("net.ipx.ncp.conn_stat", p, &len, NULL, 0);
396#else
397	error = sysctlbyname("net.ncp.conn_stat", p, &len, NULL, 0);
398#endif
399	if (error) {
400		free(p);
401		p = NULL;
402	}
403	return p;
404}
405
406
407int
408ncp_conn_setflags(int connid, u_int16_t mask, u_int16_t flags) {
409	int error;
410	DECLARE_RQ;
411
412	ncp_init_request(conn);
413	ncp_add_byte(conn, NCP_CONN_SETFLAGS);
414	ncp_add_word_lh(conn, mask);
415	ncp_add_word_lh(conn, flags);
416	if ((error = ncp_conn_request(connid, conn)) < 0)
417		return -1;
418	return error;
419}
420
421int
422ncp_login(int connHandle, const char *user, int objtype, const char *password) {
423	int error;
424	struct ncp_conn_login *p;
425	DECLARE_RQ;
426
427	ncp_init_request(conn);
428	ncp_add_byte(conn, NCP_CONN_LOGIN);
429	p = (struct ncp_conn_login *)&conn->packet[conn->rqsize];
430	(const char*)p->username = user;
431	p->objtype = objtype;
432	(const char*)p->password = password;
433	conn->rqsize += sizeof(*p);
434	if ((error = ncp_conn_request(connHandle, conn)) < 0)
435		return -1;
436	return error;
437}
438
439int
440ncp_connect_addr(struct sockaddr *sa, NWCONN_HANDLE *chp) {
441	int error;
442	struct ncp_conn_args li;
443
444	bzero(&li, sizeof(li));
445	bcopy(sa, &li.addr, sa->sa_len);
446	/*
447	 * XXX Temporary !!!. server will be filled in kernel !!!
448	 */
449	strcpy(li.server,ipx_ntoa(li.ipxaddr.sipx_addr));
450	error = ncp_connect(&li, chp);
451	return error;
452}
453
454int
455ncp_conn_getinfo(int connHandle, struct ncp_conn_stat *ps) {
456	int error;
457	DECLARE_RQ;
458
459	ncp_init_request(conn);
460	ncp_add_byte(conn, NCP_CONN_GETINFO);
461	if ((error = ncp_conn_request(connHandle, conn)) < 0)
462		return -1;
463	memcpy(ps, ncp_reply_data(conn,0), sizeof(*ps));
464	return error;
465}
466
467int
468ncp_conn_getuser(int connHandle, char **user) {
469	int error;
470	DECLARE_RQ;
471
472	ncp_init_request(conn);
473	ncp_add_byte(conn, NCP_CONN_GETUSER);
474	if ((error = ncp_conn_request(connHandle, conn)) < 0)
475		return -1;
476	*user = strdup(ncp_reply_data(conn,0));
477	return error;
478}
479
480int
481ncp_conn2ref(int connHandle, int *connRef) {
482	int error;
483	DECLARE_RQ;
484
485	ncp_init_request(conn);
486	ncp_add_byte(conn, NCP_CONN_CONN2REF);
487	if ((error = ncp_conn_request(connHandle, conn)) < 0)
488		return -1;
489	*connRef = *((int*)ncp_reply_data(conn,0));
490	return error;
491}
492
493int
494ncp_path2conn(char *path, int *connHandle) {
495	struct statfs st;
496	int d, error;
497
498	if ((error = statfs(path, &st)) != 0) return errno;
499	if (strcmp(st.f_fstypename,"nwfs") != 0) return EINVAL;
500	if ((d = open(path, O_RDONLY)) < 0) return errno;
501	if ((error = ioctl(d,NWFSIOC_GETCONN, connHandle)) != 0) return errno;
502	close(d);
503	return 0;
504}
505
506int
507ncp_conn_dup(NWCONN_HANDLE org, NWCONN_HANDLE *res) {
508	int error;
509	DECLARE_RQ;
510
511	ncp_init_request(conn);
512	ncp_add_byte(conn, NCP_CONN_DUP);
513	if ((error = ncp_conn_request(org, conn)) < 0)
514		return errno;
515	*res = *((int*)ncp_reply_data(conn, 0));
516	return 0;
517}
518