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