2 * Copyright (c) 1999, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 * $FreeBSD: src/usr.bin/ncplist/ncplist.c,v 1.1 1999/10/20 11:31:02 bp Exp $
34 #include <sys/param.h>
43 #include <netncp/ncp_lib.h>
45 extern char *__progname;
47 static struct ncp_conn_stat conndesc;
49 static void help(void);
50 static void show_connlist(void);
51 static void show_queuelist(char *, char *);
52 static void show_serverlist(char *);
53 static void show_userlist(char *);
54 static void list_bindery(char *, char *, char*);
55 static void list_volumes(char *);
56 static int ncp_get_connid(char *, int);
57 static void str_trim_right(char *, char);
61 ncp_get_connid(char *server, int justattach) {
63 struct ncp_conn_loginfo li;
65 connid = ncp_conn_find(server, NULL);
67 ncp_conn_getinfo(connid, &conndesc);
72 printf("You are not attached to server %s\n",server);
75 printf("You are not attached to any server\n");
78 ncp_li_init(&li, 0, NULL);
80 ncp_li_setserver(&li, server);
81 error = ncp_find_fileserver(&li, AF_IPX, NULL);
83 printf("Could not find server %s\n", li.server);
87 error = ncp_find_fileserver(&li, AF_IPX, NULL);
89 printf("Can't find any file server\n");
93 error = ncp_connect(&li, &connid);
95 printf("Can't attach to a nearest server\n");
98 ncp_conn_getinfo(connid, &conndesc);
102 static struct ncp_bitname conn_statenames [] = {
103 {NCPFL_INVALID, "invalid"},
104 {NCPFL_LOGGED, "active"},
105 {NCPFL_PERMANENT, "permanent"},
106 {NCPFL_PRIMARY, "primary"},
111 str_trim_right(char *s, char c) {
114 for(len = strlen(s) - 1; len > 0 && s[len] == c; len--)
119 show_connlist(void) {
123 struct ncp_conn_stat *ncsp;
125 printf("Active NCP connections:\n");
131 printf(" refid server:user(connid), owner:group(mode), refs, <state>\n");
133 ncsp = (struct ncp_conn_stat*)(((int*)p)+1);
135 printf("%6d %s:%s(%d), %s:%s(%o), %d, %s",
136 ncsp->connRef, ncsp->li.server,ncsp->user,ncsp->connid,
137 user_from_uid(ncsp->owner, 0),
138 group_from_gid(ncsp->group, 0),
139 ncsp->li.access_mode,
141 ncp_printb(buf, ncsp->flags, conn_statenames));
150 show_serverlist(char *server) {
151 int found = 0, connid;
152 struct ncp_bindery_object obj;
155 connid = ncp_get_connid(server, 1);
158 printf("Visible servers (from %s):\n", conndesc.li.server);
159 printf("Name Network Node Port\n");
160 printf("----------------------------------------------- -------- ------------ ----\n");
161 obj.object_id = 0xffffffff;
163 while (ncp_scan_bindery_object(connid, obj.object_id, NCP_BINDERY_FSERVER,
164 pattern, &obj) == 0) {
165 struct nw_property prop;
166 struct ipx_addr *naddr = (struct ipx_addr *) ∝
169 printf("%-48s", obj.object_name);
171 if (ncp_read_property_value(connid, NCP_BINDERY_FSERVER,
172 obj.object_name, 1, "NET_ADDRESS",
174 ipx_print_addr(naddr);
180 printf("No servers found\n");
187 show_userlist(char *server) {
188 int connid, error, i;
189 struct ncp_file_server_info info;
190 struct ncp_bindery_object user;
192 struct ipx_addr addr;
195 connid = ncp_get_connid(server, 0);
196 if (connid < 0) return;
197 if (ncp_get_file_server_information(connid, &info) != 0) {
198 perror("Could not get server information");
201 printf("User information for server %s\n",info.ServerName);
202 printf("\n%-6s%-21s%-27s%-12s\n"
203 "---------------------------------------------"
204 "---------------------------------\n",
209 for (i = 1; i <= info.MaximumServiceConnections; i++) {
212 error = ncp_get_stations_logged_info(connid, i, &user, &login_time);
214 memset(&addr, 0, sizeof(addr));
215 error = ncp_get_internet_address(connid, i, &addr, &conn_type);
217 memcpy(name, user.object_name, 48);
218 str_trim_right(name, ' ');
219 printf("%4d: %-20s ", i, name);
220 ipx_print_addr(&addr);
222 printf("%s", ctime(&login_time));
229 show_queuelist(char *server, char *patt) {
230 struct ncp_bindery_object q;
231 int found = 0, connid;
232 char default_pattern[] = "*";
233 char *pattern = default_pattern;
235 connid = ncp_get_connid(server, 1);
236 if (connid < 0) return;
239 ncp_str_upper(pattern);
241 printf("\nServer: %s\n", server);
242 printf("%-52s%-10s\n"
243 "-----------------------------------------------"
247 q.object_id = 0xffffffff;
249 while (ncp_scan_bindery_object(connid, q.object_id,
250 NCP_BINDERY_PQUEUE, pattern, &q) == 0)
253 printf("%-52s", q.object_name);
254 printf("%08X\n", (unsigned int) q.object_id);
258 printf("No queues found\n");
264 list_volumes(char *server) {
265 int found = 0, connid, i, error;
266 struct ncp_file_server_info si;
267 char volname[NCP_VOLNAME_LEN+1];
269 connid = ncp_get_connid(server, 1);
270 if (connid < 0) return;
272 error = ncp_get_file_server_information(connid, &si);
274 ncp_error("Can't get information for server %s", error, server);
278 printf("\nMounted volumes on server %s:\n", server);
279 printf("Number Name\n");
280 printf("------ -----------------------------------------------\n");
282 for(i = 0; i < si.NumberMountedVolumes; i++) {
283 if (NWGetVolumeName(connid, i, volname))
286 printf("%6d %s\n", i, volname);
290 printf("No volumes found ?\n");
294 struct ncp_bind_type {
299 static struct ncp_bind_type btypes[] = {
300 {NCP_BINDERY_USER, "USER"},
301 {NCP_BINDERY_UGROUP, "GROUP"},
302 {NCP_BINDERY_PSERVER, "PSERVER"},
308 list_bindery(char *server, char *type, char *patt) {
309 struct ncp_bindery_object q;
310 int i, found = 0, connid;
311 char default_pattern[] = "*";
312 char *pattern = default_pattern;
318 for(i = 0; btypes[i].type; i++) {
319 if (strcmp(btypes[i].name, type) == 0) {
320 objtype = btypes[i].type;
325 printf("Bindery object of type %s is unknown\n", type);
330 ncp_str_upper(pattern);
331 connid = ncp_get_connid(server, 1);
332 if (connid < 0) return;
334 connid = ncp_get_connid(server, 1);
335 if (connid < 0) return;
338 printf("\nServer: %s\n", server);
339 printf("%-52s%-10s\n"
340 "-----------------------------------------------"
345 q.object_id = 0xffffffff;
346 while (ncp_scan_bindery_object(connid, q.object_id,
347 objtype, pattern, &q) == 0)
350 printf("%-52s", q.object_name);
351 printf("%08X\n", (unsigned int) q.object_id);
355 printf("No bindery objects found\n");
361 LO_NONE, LO_SERVERS, LO_QUEUES, LO_BINDERY, LO_USERS, LO_VOLUMES
367 main(int argc, char *argv[]) {
368 int opt, wdone = 0, nargs = 0, i;
370 char *args[MAX_ARGS];
372 bzero(args, sizeof(args));
375 while ((opt = getopt(argc, argv, "h")) != -1) {
391 switch(argv[optind++][0]) {
415 printf("Unknown command %s\n", argv[optind-1]);
418 for (i = 0; i < MAX_ARGS; i++) {
420 args[i] = argv[optind++];
421 } else if (i < nargs) {
422 printf("Not enough arguments\n");
430 show_serverlist(args[0]);
434 show_userlist(args[0]);
438 show_queuelist(args[0], args[1]);
442 list_volumes(args[0]);
446 list_bindery(args[0], args[1], args[2]);
458 printf("usage: %s command [args]\n", __progname);
459 printf("where commands are:\n"
460 " b server user|group [pattern] list bindery objects on server\n"
461 " c display opened connections\n"
462 " s [server] display known servers\n"
463 " u server list logged-in users on server\n"
464 " q server [pattern] list print queues on server\n"
465 " v server list mounted volumes on a specified server\n"