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