1*6007Sthurlow /*
2*6007Sthurlow  * Copyright (c) 2001, Apple Computer, Inc.
3*6007Sthurlow  * All rights reserved.
4*6007Sthurlow  *
5*6007Sthurlow  * Redistribution and use in source and binary forms, with or without
6*6007Sthurlow  * modification, are permitted provided that the following conditions
7*6007Sthurlow  * are met:
8*6007Sthurlow  * 1. Redistributions of source code must retain the above copyright
9*6007Sthurlow  *    notice, this list of conditions and the following disclaimer.
10*6007Sthurlow  * 2. Redistributions in binary form must reproduce the above copyright
11*6007Sthurlow  *    notice, this list of conditions and the following disclaimer in the
12*6007Sthurlow  *    documentation and/or other materials provided with the distribution.
13*6007Sthurlow  * 3. All advertising materials mentioning features or use of this software
14*6007Sthurlow  *    must display the following acknowledgement:
15*6007Sthurlow  *    This product includes software developed by Boris Popov.
16*6007Sthurlow  * 4. Neither the name of the author nor the names of any co-contributors
17*6007Sthurlow  *    may be used to endorse or promote products derived from this software
18*6007Sthurlow  *    without specific prior written permission.
19*6007Sthurlow  *
20*6007Sthurlow  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*6007Sthurlow  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*6007Sthurlow  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*6007Sthurlow  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*6007Sthurlow  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*6007Sthurlow  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*6007Sthurlow  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*6007Sthurlow  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*6007Sthurlow  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*6007Sthurlow  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*6007Sthurlow  * SUCH DAMAGE.
31*6007Sthurlow  *
32*6007Sthurlow  * $Id: status.c,v 1.2 2001/08/18 05:44:50 conrad Exp $
33*6007Sthurlow  */
34*6007Sthurlow 
35*6007Sthurlow #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*6007Sthurlow 
37*6007Sthurlow #include <sys/param.h>
38*6007Sthurlow #include <sys/errno.h>
39*6007Sthurlow #include <sys/stat.h>
40*6007Sthurlow #include <sys/socket.h>
41*6007Sthurlow #include <stdio.h>
42*6007Sthurlow #include <err.h>
43*6007Sthurlow #include <unistd.h>
44*6007Sthurlow #include <strings.h>
45*6007Sthurlow #include <stdlib.h>
46*6007Sthurlow #include <sysexits.h>
47*6007Sthurlow #include <libintl.h>
48*6007Sthurlow 
49*6007Sthurlow #include <netinet/in.h>
50*6007Sthurlow #include <arpa/inet.h>
51*6007Sthurlow 
52*6007Sthurlow #include <cflib.h>
53*6007Sthurlow 
54*6007Sthurlow #include <netsmb/netbios.h>
55*6007Sthurlow #include <netsmb/smb_lib.h>
56*6007Sthurlow #include <netsmb/nb_lib.h>
57*6007Sthurlow 
58*6007Sthurlow #include "common.h"
59*6007Sthurlow 
60*6007Sthurlow 
61*6007Sthurlow int
62*6007Sthurlow cmd_status(int argc, char *argv[])
63*6007Sthurlow {
64*6007Sthurlow 	struct nb_ctx *ctx;
65*6007Sthurlow 	struct sockaddr *sap;
66*6007Sthurlow 	char *hostname;
67*6007Sthurlow 	char servername[SMB_MAXSRVNAMELEN + 1];
68*6007Sthurlow 	char workgroupname[SMB_MAXUSERNAMELEN + 1];
69*6007Sthurlow 	int error, opt;
70*6007Sthurlow 
71*6007Sthurlow 	if (argc < 2)
72*6007Sthurlow 		status_usage();
73*6007Sthurlow 	error = nb_ctx_create(&ctx);
74*6007Sthurlow 	if (error) {
75*6007Sthurlow 		smb_error(gettext("unable to create nbcontext"), error);
76*6007Sthurlow 		exit(1);
77*6007Sthurlow 	}
78*6007Sthurlow 	if (smb_open_rcfile(NULL) == 0) {
79*6007Sthurlow 		if (nb_ctx_readrcsection(smb_rc, ctx, "default", 0) != 0)
80*6007Sthurlow 			exit(1);
81*6007Sthurlow 		rc_close(smb_rc);
82*6007Sthurlow 	}
83*6007Sthurlow 	while ((opt = getopt(argc, argv, "")) != EOF) {
84*6007Sthurlow 		switch (opt) {
85*6007Sthurlow 		default:
86*6007Sthurlow 			status_usage();
87*6007Sthurlow 			/*NOTREACHED*/
88*6007Sthurlow 		}
89*6007Sthurlow 	}
90*6007Sthurlow 	if (optind >= argc)
91*6007Sthurlow 		status_usage();
92*6007Sthurlow 
93*6007Sthurlow 	hostname = argv[argc - 1];
94*6007Sthurlow 	error = nb_resolvehost_in(hostname, &sap);
95*6007Sthurlow 	if (error) {
96*6007Sthurlow 		smb_error(gettext(
97*6007Sthurlow 		    "unable to resolve DNS hostname %s"), error, hostname);
98*6007Sthurlow 		exit(1);
99*6007Sthurlow 	}
100*6007Sthurlow 	if ((ctx->nb_flags & NBCF_NS_ENABLE) == 0) {
101*6007Sthurlow 		fprintf(stderr,
102*6007Sthurlow 		    gettext("nbns_enable=false, cannot get status\n"));
103*6007Sthurlow 		exit(1);
104*6007Sthurlow 	}
105*6007Sthurlow 	servername[0] = (char)0;
106*6007Sthurlow 	workgroupname[0] = (char)0;
107*6007Sthurlow 	error = nbns_getnodestatus(sap, ctx, servername, workgroupname);
108*6007Sthurlow 	if (error) {
109*6007Sthurlow 		smb_error(
110*6007Sthurlow 		    gettext("unable to get status from %s"), error, hostname);
111*6007Sthurlow 		exit(1);
112*6007Sthurlow 	}
113*6007Sthurlow 
114*6007Sthurlow 	if (workgroupname[0]) {
115*6007Sthurlow 		printf(gettext("Workgroup: %s\n"), workgroupname);
116*6007Sthurlow 	}
117*6007Sthurlow 	if (servername[0]) {
118*6007Sthurlow 		printf(gettext("Server: %s\n"), servername);
119*6007Sthurlow 	}
120*6007Sthurlow 
121*6007Sthurlow 	return (0);
122*6007Sthurlow }
123*6007Sthurlow 
124*6007Sthurlow 
125*6007Sthurlow void
126*6007Sthurlow status_usage(void)
127*6007Sthurlow {
128*6007Sthurlow 	printf(gettext("usage: smbutil status hostname\n"));
129*6007Sthurlow 	exit(1);
130*6007Sthurlow }
131