# # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright 2009 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Copyright 2018 Nexenta Systems, Inc. All rights reserved. # Copyright (c) 2018, Joyent, Inc. # # # cmd/fs.d/smbclnt/smbutil/Makefile # PROG= smbutil OBJS= smbutil.o discon.o info.o login.o lookup.o print.o status.o view.o \ shares_rpc.o srvsvc1_clnt.o srvsvc1_ndr.o include $(SRC)/cmd/Makefile.cmd POFILE= smbutil_all.po POFILES= $(OBJS:%.o=%.po) CLEANFILES += srvsvc1_ndr.c CLOBBERFILES+= $(POFILE) $(POFILES) CSTD= $(CSTD_GNU99) LDLIBS += -lmlrpc -lsmbfs -lnsl # Hammerhead: GNU ld needs rpath-link to resolve mech_krb5.so.1 (transitive dep of libsmbfs) LDFLAGS += -Wl,-rpath-link,$(ROOT)/usr/lib/gss CPPFLAGS += -I$(SRC)/lib/libsmbfs \ -I$(SRC)/uts/common/smbclnt -I$(SRC)/uts/common # not linted SMATCH=off # Debugging $(NOT_RELEASE_BUILD)CPPFLAGS += -DDEBUG all: $(PROG) $(PROG): $(OBJS) $(LINK.c) -o $@ $(OBJS) $(LDLIBS) $(POST_PROCESS) install: all $(ROOTPROG) $(ROOTPROG): $(PROG) $(INS.file) catalog: $(POFILE) $(POFILE): $(POFILES) $(RM) $@ $(CAT) $(POFILES) > $@ clean: $(RM) $(OBJS) $(CLEANFILES) include ../../../Makefile.targ srvsvc1_ndr.c : srvsvc1.ndl $(NDRGEN) -Y $(ANSI_CPP) $(CPPFLAGS) srvsvc1.ndl .KEEP_STATE: /* * Copyright (c) 2000, 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. */ /* * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ #ifndef _SMBUTIL_COMMON_H #define _SMBUTIL_COMMON_H #ifdef __cplusplus extern "C" { #endif #include #include int cmd_crypt(int argc, char *argv[]); int cmd_discon(int argc, char *argv[]); int cmd_help(int argc, char *argv[]); int cmd_info(int argc, char *argv[]); int cmd_login(int argc, char *argv[]); int cmd_logout(int argc, char *argv[]); int cmd_logoutall(int argc, char *argv[]); int cmd_lookup(int argc, char *argv[]); int cmd_print(int argc, char *argv[]); int cmd_status(int argc, char *argv[]); int cmd_view(int argc, char *argv[]); void crypt_usage(void); void discon_usage(void); void help_usage(void); void info_usage(void); void login_usage(void); void logout_usage(void); void logoutall_usage(void); void lookup_usage(void); void print_usage(void); void status_usage(void); void view_usage(void); /* See view.c */ int share_enum_rap(struct smb_ctx *ctx); int share_enum_rpc(struct smb_ctx *ctx, char *server); void view_print_share(char *share, int type, char *comment); #ifdef __cplusplus } #endif #endif /* _SMBUTIL_COMMON_H */ /* * This file and its contents are supplied under the terms of the * Common Development and Distribution License ("CDDL"), version 1.0. * You may only use this file in accordance with the terms of version * 1.0 of the CDDL. * * A full copy of the text of the CDDL should have accompanied this * source. A copy of the CDDL is also available via the Internet at * http://www.illumos.org/license/CDDL. */ /* * Copyright 2018 Nexenta Systems, Inc. All rights reserved. */ /* * smbutil "discon" sub-command to disconnect a session * (mostly for usr/src/test/smbclient-tests) */ #include #include #include #include #include #include #include #include #include #include #include #include "common.h" void discon_usage(void) { printf(gettext("usage: smbutil discon [connection options] " "//[workgroup;][user@]server\n")); exit(1); } int cmd_discon(int argc, char *argv[]) { struct smb_ctx *ctx; int error, opt; if (argc < 2) discon_usage(); error = smb_ctx_alloc(&ctx); if (error != 0) return (error); error = smb_ctx_scan_argv(ctx, argc, argv, SMBL_SERVER, SMBL_SERVER, USE_WILDCARD); if (error != 0) goto out; error = smb_ctx_readrc(ctx); if (error != 0) goto out; while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) { if (opt == '?') discon_usage(); error = smb_ctx_opt(ctx, opt, optarg); if (error != 0) goto out; } /* * Resolve the server address, * setup derived defaults. */ error = smb_ctx_resolve(ctx); if (error != 0) goto out; /* * Have server, user, etc. from above: * smb_ctx_scan_argv, option settings. * * Lookup a session without creating. * (First part of smb_ctx_get_ssn) * If we find the session, kill it. */ error = smb_ctx_findvc(ctx); if (error == ENOENT) { /* Already gone. We're done. */ if (smb_debug) fprintf(stderr, "session not found\n"); error = 0; goto out; } if (error == 0) { /* Found session. Kill it. */ error = smb_ctx_kill(ctx); } if (error != 0) { smb_error(gettext("//%s: discon failed"), error, ctx->ct_fullserver); } out: smb_ctx_free(ctx); return (error); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ /* * Show information about the remote server, as offered by * NetServerGetInfo with SERVER_INFO_101 */ #include #include #include #include #include #include #include #include #include "srvsvc1_clnt.h" #include "common.h" static int get_info(smb_ctx_t *); void info_usage(void) { printf(gettext("usage: smbutil info [connection options] //" "[workgroup;][user[:password]@]server\n")); exit(1); } int cmd_info(int argc, char *argv[]) { struct smb_ctx *ctx; int error, err2, opt; if (argc < 2) info_usage(); error = smb_ctx_alloc(&ctx); if (error) return (error); error = smb_ctx_scan_argv(ctx, argc, argv, SMBL_SERVER, SMBL_SERVER, USE_WILDCARD); if (error) goto out; error = smb_ctx_readrc(ctx); if (error) goto out; while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) { if (opt == '?') info_usage(); error = smb_ctx_opt(ctx, opt, optarg); if (error) goto out; } smb_ctx_setshare(ctx, "IPC$", USE_IPC); /* * Resolve the server address, * setup derived defaults. */ error = smb_ctx_resolve(ctx); if (error) goto out; /* * Have server, share, etc. from above: * smb_ctx_scan_argv, option settings. * Get the session and tree. */ again: error = smb_ctx_get_ssn(ctx); if (error == EAUTH) { err2 = smb_get_authentication(ctx); if (err2 == 0) goto again; } if (error) { smb_error(gettext("//%s: login failed"), error, ctx->ct_fullserver); goto out; } error = smb_ctx_get_tree(ctx); if (error) { smb_error(gettext("//%s/%s: tree connect failed"), error, ctx->ct_fullserver, ctx->ct_origshare); goto out; } /* * Have IPC$ tcon. Get the server info. */ error = get_info(ctx); if (error) smb_error("cannot get server info.", error); out: smb_ctx_free(ctx); return (error); } int get_info(smb_ctx_t *ctx) { char pf_unk[32]; mlrpc_handle_t handle; ndr_service_t *svc; union mslm_NetServerGetInfo_ru res; struct mslm_SERVER_INFO_101 *sv101; char *platform_name; int err; /* * Create an RPC handle using the smb_ctx we already have. * Just local allocation and initialization. */ srvsvc1_initialize(); svc = ndr_svc_lookup_name("srvsvc"); if (svc == NULL) return (ENOENT); err = mlrpc_clh_create(&handle, ctx); if (err) return (err); /* * Try to bind to the RPC service. If it fails, * just return the error and the caller will * fall back to RAP. */ err = mlrpc_clh_bind(&handle, svc); if (err) goto out; err = srvsvc_net_server_getinfo(&handle, ctx->ct_fullserver, 101, &res); if (err) goto out; sv101 = res.info101; switch (sv101->sv101_platform_id) { case SV_PLATFORM_ID_DOS: platform_name = "DOS"; break; case SV_PLATFORM_ID_OS2: platform_name = "OS2"; break; case SV_PLATFORM_ID_NT: platform_name = "NT"; break; case SV_PLATFORM_ID_OSF: platform_name = "OSF"; break; case SV_PLATFORM_ID_VMS: platform_name = "VMS"; break; default: platform_name = pf_unk; snprintf(pf_unk, sizeof (pf_unk), "(%d)", sv101->sv101_platform_id); break; } printf("server info:\n"); printf(" platform_id %s\n", platform_name); printf(" vers.major %d\n", sv101->sv101_version_major); printf(" vers.minor %d\n", sv101->sv101_version_minor); if (smb_verbose) printf(" type_flags 0x%x\n", sv101->sv101_type); printf(" name \"%s\"\n", sv101->sv101_name); printf(" comment \"%s\"\n", sv101->sv101_comment); out: (void) mlrpc_clh_free(&handle); return (err); } /* * Copyright (c) 2000, 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. * * $Id: login.c,v 1.8 2004/03/19 01:49:48 lindak Exp $ */ #include #include #include #include #include #include #include #include #include #include #include #include "common.h" /* defaults */ static char def_dom[256]; static char def_usr[256]; static char tmp_arg[256]; /* * Parse the string: domuser, which may be any of: * "user@domain" or "domain/user" or "domain\\user" * and return pointers to the domain and user parts. * Modifies the string domuser in-place. Returned * string pointers are within the string domusr. */ int smbfs_parse_domuser(char *domuser, char **dom, char **usr) { const char sep[] = "@/\\"; char sc, *p, *s1, *s2; p = strpbrk(domuser, sep); if (p == NULL) { /* No separators - whole string is the user. */ *dom = NULL; *usr = domuser; return (0); } /* Have two strings. */ s1 = domuser; sc = *p; /* Save the sep. char */ *p++ = '\0'; /* zap it */ s2 = p; /* Enforce just one separator */ p = strpbrk(s2, sep); if (p) return (-1); /* * Now, which order are they? * "user@domain" or "domain/user" */ if (sc == '@') { *usr = s1; *dom = s2; } else { *dom = s1; *usr = s2; } return (0); } void login_usage(void) { printf(gettext("usage: smbutil login [-c] [[domain/]user]\n")); printf(gettext(" smbutil login [-c] [user[@domain]]\n")); exit(1); } int cmd_login(int argc, char *argv[]) { static char prompt[64]; char *dom, *usr, *pass; int err, opt; int check = 0; while ((opt = getopt(argc, argv, "c")) != EOF) { switch (opt) { case 'c': /* smbutil login -c ... */ check = 1; break; default: login_usage(); break; } } dom = usr = NULL; if (optind < argc) { strcpy(tmp_arg, argv[optind]); err = smbfs_parse_domuser(tmp_arg, &dom, &usr); if (err) errx(1, gettext("failed to parse %s"), argv[optind]); optind++; } if (optind != argc) login_usage(); if (dom == NULL || usr == NULL) { err = smbfs_default_dom_usr(NULL, NULL, def_dom, sizeof (def_dom), def_usr, sizeof (def_usr)); if (err) errx(1, gettext("failed to get defaults")); } if (dom == NULL) dom = def_dom; else nls_str_upper(dom, dom); if (usr == NULL) usr = def_usr; if (check) { err = smbfs_keychain_chk(dom, usr); if (!err) printf(gettext("Keychain entry exists.\n")); else printf(gettext("Keychain entry not found.\n")); return (0); } snprintf(prompt, sizeof (prompt), gettext("Password for %s/%s:"), dom, usr); pass = getpassphrase(prompt); err = smbfs_keychain_add((uid_t)-1, dom, usr, pass); if (err) errx(1, gettext("failed to add keychain entry")); return (0); } void logout_usage(void) { printf(gettext("usage: smbutil logout [[domain/]user]\n")); printf(gettext(" smbutil logout [user[@domain]]\n")); printf(gettext(" smbutil logout -a\n")); exit(1); } int cmd_logout(int argc, char *argv[]) { char *dom, *usr; int err, opt; while ((opt = getopt(argc, argv, "a")) != EOF) { switch (opt) { case 'a': /* smbutil logout -a */ if (optind != argc) logout_usage(); err = smbfs_keychain_del_owner(); if (err) errx(1, gettext("failed to delete keychain entries")); return (0); default: logout_usage(); break; } } /* This part is like login. */ dom = usr = NULL; if (optind < argc) { strcpy(tmp_arg, argv[optind]); err = smbfs_parse_domuser(tmp_arg, &dom, &usr); if (err) errx(1, gettext("failed to parse %s"), argv[optind]); optind++; } if (optind != argc) logout_usage(); if (dom == NULL || usr == NULL) { err = smbfs_default_dom_usr(NULL, NULL, def_dom, sizeof (def_dom), def_usr, sizeof (def_usr)); if (err) errx(1, gettext("failed to get defaults")); } if (dom == NULL) dom = def_dom; else nls_str_upper(dom, dom); if (usr == NULL) usr = def_usr; err = smbfs_keychain_del((uid_t)-1, dom, usr); if (err) errx(1, gettext("failed to delete keychain entry")); return (0); } void logoutall_usage(void) { printf(gettext("usage: smbutil logoutall\n")); exit(1); } int cmd_logoutall(int argc, char *argv[]) { int err; if (optind != argc) logoutall_usage(); err = smbfs_keychain_del_everyone(); if (err == EPERM) { errx(1, gettext("You must have super-user privileges to use this sub-command\n")); } if (err) { errx(1, gettext("Failed to delete all keychain entries: %s\n"), smb_strerror(err)); } return (0); } /* * Copyright (c) 2000, 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. * * $Id: lookup.c,v 1.1.1.1 2001/06/09 00:28:13 zarzycki Exp $ */ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" int cmd_lookup(int argc, char *argv[]) { struct nb_ctx *ctx; struct sockaddr *sap; char *hostname; int error, opt; if (argc < 2) lookup_usage(); error = nb_ctx_create(&ctx); if (error) { smb_error(gettext("unable to create nbcontext"), error); exit(1); } if (smb_open_rcfile(NULL) == 0) { if (nb_ctx_readrcsection(NULL, ctx, "default", 0) != 0) exit(1); smb_close_rcfile(); } if ((ctx->nb_flags & NBCF_NS_ENABLE) == 0) { fprintf(stderr, gettext("nbns_enable=false, cannot do lookup\n")); exit(1); } while ((opt = getopt(argc, argv, "w:")) != EOF) { switch (opt) { case 'w': nb_ctx_setns(ctx, optarg); break; default: lookup_usage(); /*NOTREACHED*/ } } if (optind >= argc) lookup_usage(); if (nb_ctx_resolve(ctx) != 0) exit(1); hostname = argv[argc - 1]; error = nbns_resolvename(hostname, ctx, &sap); if (error) { smb_error(gettext("unable to resolve %s"), error, hostname); exit(1); } printf(gettext("Got response from %s\n"), inet_ntoa(ctx->nb_lastns.sin_addr)); printf(gettext("IP address of %s: %s\n"), hostname, inet_ntoa(((struct sockaddr_in *)sap)->sin_addr)); return (0); } void lookup_usage(void) { printf(gettext("usage: smbutil lookup [-w host] name\n")); exit(1); } /* * Copyright (c) 2000, 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. * * from: Id: print.c,v 1.4 2001/01/28 07:35:01 bp Exp */ /* * Copyright 2011 Nexenta Systems, Inc. All rights reserved. * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include #include #include #include #include #include #include "common.h" static char titlebuf[256]; static char databuf[4096]; static int print_file(smb_ctx_t *, char *, int); void print_usage(void) { printf(gettext("usage: smbutil print [connection options] //" "[workgroup;][user[:password]@]" "server/share {print_file|-}\n")); exit(1); } int cmd_print(int argc, char *argv[]) { struct smb_ctx *ctx = NULL; char *filename; int error, opt; int file = -1; /* last arg is the print file. */ if (argc < 3) print_usage(); error = smb_ctx_alloc(&ctx); if (error) goto out; error = smb_ctx_scan_argv(ctx, argc-1, argv, SMBL_SHARE, SMBL_SHARE, USE_SPOOLDEV); if (error) goto out; error = smb_ctx_readrc(ctx); if (error) goto out; while ((opt = getopt(argc-1, argv, STDPARAM_OPT)) != EOF) { if (opt == '?') print_usage(); error = smb_ctx_opt(ctx, opt, optarg); if (error) goto out; } if (optind != argc-2) print_usage(); filename = argv[argc-1]; if (strcmp(filename, "-") == 0) { file = 0; /* stdin */ filename = "stdin"; } else { file = open(filename, O_RDONLY, 0); if (file < 0) { smb_error("could not open file %s\n", errno, filename); exit(1); } } /* * Resolve the server address, * setup derived defaults. */ error = smb_ctx_resolve(ctx); if (error) goto out; /* * Have server + share names, options etc. * Get the session and tree. */ again: error = smb_ctx_get_ssn(ctx); if (error == EAUTH) { int err2 = smb_get_authentication(ctx); if (err2 == 0) goto again; } if (error) { smb_error(gettext("//%s: login failed"), error, ctx->ct_fullserver); goto out; } error = smb_ctx_get_tree(ctx); if (error) { smb_error(gettext("//%s/%s: tree connect failed"), error, ctx->ct_fullserver, ctx->ct_origshare); goto out; } /* * Have the printer share connection. * Print the file. */ snprintf(titlebuf, sizeof (titlebuf), "%s %s", ctx->ct_user, filename); error = print_file(ctx, titlebuf, file); out: /* don't close stdin (file=0) */ if (file > 0) close(file); smb_ctx_free(ctx); return (error); } /* * Documentation for OPEN_PRINT_FILE is scarse. * It's in a 1996 MS doc. entitled: * SMB FILE SHARING PROTOCOL * * The extra parameters are: * SetupLength: what part of the file is printer setup * Mode: text or graphics (raw data) * IdentifierString: job title */ enum { MODE_TEXT = 0, /* TAB expansion, etc. */ MODE_GRAPHICS /* raw data */ }; static int print_file(smb_ctx_t *ctx, char *title, int file) { off_t offset; int rcnt, wcnt; int setup_len = 0; /* No printer setup data */ int mode = MODE_GRAPHICS; /* treat as raw data */ int error = 0; int pfd = -1; pfd = smb_open_printer(ctx, title, setup_len, mode); if (pfd < 0) { error = errno; smb_error("could not open print job", error); return (error); } offset = 0; for (;;) { rcnt = read(file, databuf, sizeof (databuf)); if (rcnt < 0) { error = errno; smb_error("error reading input file\n", error); break; } if (rcnt == 0) break; wcnt = smb_fh_write(pfd, offset, rcnt, databuf); if (wcnt < 0) { error = errno; smb_error("error writing spool file\n", error); break; } if (wcnt != rcnt) { smb_error("incomplete write to spool file\n", 0); error = EIO; break; } offset += wcnt; } (void) smb_fh_close(pfd); return (error); } /* * This file and its contents are supplied under the terms of the * Common Development and Distribution License ("CDDL"), version 1.0. * You may only use this file in accordance with the terms of version * 1.0 of the CDDL. * * A full copy of the text of the CDDL should have accompanied this * source. A copy of the CDDL is also available via the Internet at * http://www.illumos.org/license/CDDL. */ /* * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ /* * Share enumeration using Remote Procedure Call (RPC) */ #include #include #include #include #include #include #include #include "srvsvc1_clnt.h" #include "common.h" int share_enum_rpc(struct smb_ctx *ctx, char *server) { mlrpc_handle_t handle; ndr_service_t *svc; union mslm_NetShareEnum_ru res; struct mslm_NetShareInfo_1 *nsi1; int err, i, count; /* * Create an RPC handle using the smb_ctx we already have. * Just local allocation and initialization. */ srvsvc1_initialize(); svc = ndr_svc_lookup_name("srvsvc"); if (svc == NULL) return (ENOENT); err = mlrpc_clh_create(&handle, ctx); if (err) return (err); /* * Try to bind to the RPC service. If it fails, * just return the error and the caller will * fall back to RAP. */ err = mlrpc_clh_bind(&handle, svc); if (err) goto out; err = srvsvc_net_share_enum(&handle, server, 1, &res); if (err) goto out; /* Print the header line. */ view_print_share(NULL, 0, NULL); /* Print the share list. */ count = res.bufptr1->entriesread; i = 0, nsi1 = res.bufptr1->entries; while (i < count) { /* Convert UTF-8 to local code set? */ view_print_share((char *)nsi1->shi1_netname, nsi1->shi1_type, (char *)nsi1->shi1_comment); i++, nsi1++; } out: (void) mlrpc_clh_free(&handle); return (err); } /* * Copyright (c) 2000, 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. */ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * * Copyright 2018 Nexenta Systems, Inc. */ #include #include #include #include #include #include #include #include #include #include #include #include "common.h" #ifndef EX_DATAERR #define EX_DATAERR 1 #endif static void help(void) __NORETURN; typedef int cmd_fn_t (int argc, char *argv[]); typedef void cmd_usage_t (void); #define CMDFL_NO_KMOD 0x0001 static struct commands { const char *name; cmd_fn_t *fn; cmd_usage_t *usage; int flags; } commands[] = { {"crypt", cmd_crypt, NULL, CMDFL_NO_KMOD}, {"discon", cmd_discon, discon_usage, 0}, {"help", cmd_help, help_usage, CMDFL_NO_KMOD}, {"info", cmd_info, info_usage, 0}, {"login", cmd_login, login_usage, 0}, {"logout", cmd_logout, logout_usage, 0}, {"logoutall", cmd_logoutall, logoutall_usage, 0}, {"lookup", cmd_lookup, lookup_usage, CMDFL_NO_KMOD}, {"print", cmd_print, print_usage, 0}, {"status", cmd_status, status_usage, CMDFL_NO_KMOD}, {"view", cmd_view, view_usage, 0}, {NULL, NULL, NULL, 0} }; static struct commands * lookupcmd(const char *name) { struct commands *cmd; for (cmd = commands; cmd->name; cmd++) { if (strcmp(cmd->name, name) == 0) return (cmd); } return (NULL); } int cmd_crypt(int argc, char *argv[]) { char *cp, *psw; if (argc < 2) psw = getpassphrase(gettext("Password:")); else psw = argv[1]; /* XXX Better to embed malloc/free in smb_simplecrypt? */ cp = malloc(4 + 2 * strlen(psw)); if (cp == NULL) errx(EX_DATAERR, gettext("out of memory")); smb_simplecrypt(cp, psw); printf("%s\n", cp); free(cp); return (0); } int cmd_help(int argc, char *argv[]) { struct commands *cmd; char *cp; if (argc < 2) help_usage(); cp = argv[1]; cmd = lookupcmd(cp); if (cmd == NULL) errx(EX_DATAERR, gettext("unknown command %s"), cp); if (cmd->usage == NULL) errx(EX_DATAERR, gettext("no specific help for command %s"), cp); cmd->usage(); return (0); } int main(int argc, char *argv[]) { struct commands *cmd; char *cp; int err, opt; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); #ifdef APPLE dropsuid(); /* see libsmbfs */ #endif if (argc < 2) help(); while ((opt = getopt(argc, argv, "dhv")) != EOF) { switch (opt) { case 'd': smb_debug++; break; case 'h': help(); /* NOTREACHED */ case 'v': smb_verbose++; break; default: help(); /* NOTREACHED */ } } if (optind >= argc) help(); cp = argv[optind]; cmd = lookupcmd(cp); if (cmd == NULL) errx(EX_DATAERR, gettext("unknown command %s"), cp); if ((cmd->flags & CMDFL_NO_KMOD) == 0 && smb_lib_init() != 0) exit(1); argc -= optind; argv += optind; optind = 1; err = cmd->fn(argc, argv); return ((err) ? 1 : 0); } static void help(void) { printf("\n"); printf(gettext("usage: %s [-hv] subcommand [args]\n"), __progname); printf(gettext("where subcommands are:\n" " crypt slightly obscure password\n" " help display help on specified subcommand\n" " info display server type and version\n" " login login to specified host\n" " logout logout from specified host\n" " logoutall logout all users (requires privilege)\n" " lookup resolve NetBIOS name to IP address\n" " print print file to the specified remote printer\n" " status resolve IP address or DNS name to NetBIOS names\n" " view list resources on specified host\n" "\n")); exit(1); } void help_usage(void) { printf(gettext("usage: smbutil help command\n")); exit(1); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ /* * SRVSVC - Server Service (partial) * * This module needs only NetShareEnum (levels 0, 1) * and NetServerGetInfo (levels 100, 101) */ #include /* * SRVSVC - Server Service */ #define SRVSVC_OPNUM_NetShareEnum 0x0f #define SRVSVC_OPNUM_NetServerGetInfo 0x15 /* * SRVSVC NetShareEnum ( * IN LPTSTR servername, * IN DWORD level; * OUT union switch(level) { * case 0: struct { * DWORD entriesread; * [size_is(entriesread)] * _SHARE_INFO_0 *entries; * } *bufptr0; * case 1: struct { * DWORD entriesread; * [size_is(entriesread)] * _SHARE_INFO_1 *entries; * } *bufptr1; * ... * } bufptr, * IN DWORD prefmaxlen, * OUT DWORD totalentries, * IN OUT DWORD ?* resume_handle, * OUT DWORD status * ) */ struct mslm_NetShareInfo_0 { LPTSTR shi0_netname; }; struct mslm_NetShareInfo_0_result { DWORD entriesread; SIZE_IS(entriesread) struct mslm_NetShareInfo_0 *entries; }; struct mslm_NetShareInfo_1 { LPTSTR shi1_netname; DWORD shi1_type; /* type of resource such as IPC$ */ LPTSTR shi1_comment; }; struct mslm_NetShareInfo_1_result { DWORD entriesread; SIZE_IS(entriesread) struct mslm_NetShareInfo_1 *entries; }; union mslm_NetShareEnum_ru { CASE(0) struct mslm_NetShareInfo_0_result *bufptr0; CASE(1) struct mslm_NetShareInfo_1_result *bufptr1; DEFAULT char *nullptr; }; struct mslm_NetShareEnum_result { DWORD level; SWITCH(level) union mslm_NetShareEnum_ru ru; }; OPERATION(SRVSVC_OPNUM_NetShareEnum) struct mslm_NetShareEnum { IN LPTSTR servername; INOUT DWORD level; INOUT struct mslm_NetShareEnum_result result; IN DWORD prefmaxlen; OUT DWORD totalentries; INOUT DWORD *resume_handle; OUT DWORD status; }; /* * SRVSVC NetServerGetInfo ( * IN LPTSTR servername, * IN DWORD level, * OUT union switch(level) { * case 100: _SERVER_INFO_100 * p100; * case 101: _SERVER_INFO_101 * p101; * case 102: _SERVER_INFO_102 * p102; * } bufptr, * OUT DWORD status * ) */ /* for svX_platform (note: decimal!) */ #define SV_PLATFORM_ID_DOS 300 #define SV_PLATFORM_ID_OS2 400 #define SV_PLATFORM_ID_NT 500 #define SV_PLATFORM_ID_OSF 600 #define SV_PLATFORM_ID_VMS 700 struct mslm_SERVER_INFO_100 { DWORD sv100_platform_id; LPTSTR sv100_name; }; struct mslm_SERVER_INFO_101 { DWORD sv101_platform_id; LPTSTR sv101_name; DWORD sv101_version_major; DWORD sv101_version_minor; DWORD sv101_type; LPTSTR sv101_comment; }; union mslm_NetServerGetInfo_ru { CASE(100) struct mslm_SERVER_INFO_100 *info100; CASE(101) struct mslm_SERVER_INFO_101 *info101; DEFAULT char *nullptr; }; struct mslm_NetServerGetInfo_result { DWORD level; SWITCH(level) union mslm_NetServerGetInfo_ru ru; }; OPERATION(SRVSVC_OPNUM_NetServerGetInfo) struct mslm_NetServerGetInfo { IN LPTSTR servername; IN DWORD level; OUT struct mslm_NetServerGetInfo_result result; OUT DWORD status; }; /* * The SRVSVC interface */ INTERFACE(0) union srvsvc_interface { CASE(SRVSVC_OPNUM_NetShareEnum) struct mslm_NetShareEnum NetShareEnum; CASE(SRVSVC_OPNUM_NetServerGetInfo) struct mslm_NetServerGetInfo NetServerGetInfo; }; typedef union srvsvc_interface srvsvc_interface_t; EXTERNTYPEINFO(srvsvc_interface) /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ /* * A few excerpts from lib/smbsrv/libmlsvc * See longer comment in srvsvc1.ndl */ #include #include #include #include #include #include #include "srvsvc1_clnt.h" static ndr_service_t srvsvc_service = { "SRVSVC", /* name */ "Server services", /* desc */ "\\srvsvc", /* endpoint */ "\\PIPE\\ntsvcs", /* sec_addr_port */ "4b324fc8-1670-01d3-1278-5a47bf6ee188", 3, /* abstract */ NDR_TRANSFER_SYNTAX_UUID, 2, /* transfer */ 0, /* no bind_instance_size */ 0, /* no bind_req() */ 0, /* no unbind_and_close() */ 0, /* use generic_call_stub() */ &TYPEINFO(srvsvc_interface), /* interface_ti */ NULL /* stub_table */ }; /* * srvsvc_initialize * * This function registers the SRVSVC RPC interface with the RPC runtime * library. It must be called in order to use either the client side * or the server side functions. */ void srvsvc1_initialize(void) { static int init_done; if (init_done) return; init_done = 1; (void) ndr_svc_register(&srvsvc_service); } /* * Client-side stub for NetServerGetInfo */ int srvsvc_net_server_getinfo(mlrpc_handle_t *handle, char *server, int level, union mslm_NetServerGetInfo_ru *resp) { struct mslm_NetServerGetInfo arg; int len, opnum, rc; opnum = SRVSVC_OPNUM_NetServerGetInfo; bzero(&arg, sizeof (arg)); len = strlen(server) + 4; arg.servername = ndr_rpc_malloc(handle, len); if (arg.servername == NULL) return (ENOMEM); (void) snprintf((char *)arg.servername, len, "\\\\%s", server); arg.level = level; rc = ndr_rpc_call(handle, opnum, &arg); if ((rc != 0) || (arg.status != 0)) return (EIO); *resp = arg.result.ru; return (0); } /* * Client-side stub for NetShareEnum */ int srvsvc_net_share_enum(mlrpc_handle_t *handle, char *server, int level, union mslm_NetShareEnum_ru *resp) { /* Any enum result type is OK for nres. */ struct mslm_NetShareInfo_0_result nres; struct mslm_NetShareEnum arg; int len, opnum, rc; opnum = SRVSVC_OPNUM_NetShareEnum; bzero(&nres, sizeof (nres)); bzero(&arg, sizeof (arg)); len = strlen(server) + 4; arg.servername = ndr_rpc_malloc(handle, len); if (arg.servername == NULL) return (ENOMEM); (void) snprintf((char *)arg.servername, len, "\\\\%s", server); arg.level = level; arg.result.level = level; arg.result.ru.bufptr0 = &nres; arg.prefmaxlen = 0xFFFFFFFF; arg.resume_handle = NULL; rc = ndr_rpc_call(handle, opnum, &arg); if ((rc != 0) || (arg.status != 0)) return (EIO); *resp = arg.result.ru; return (0); } /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. * * Copyright 2013 Nexenta Systems, Inc. All rights reserved. */ #ifndef _SRVSVC1_CLNT_H #define _SRVSVC1_CLNT_H /* * Excerpts from lib/smbsrv/libmlsvc * Just enough for share enumeration. */ #include #include "srvsvc1.ndl" void srvsvc1_initialize(void); int srvsvc_net_share_enum(mlrpc_handle_t *handle, char *server, int level, union mslm_NetShareEnum_ru *resp); int srvsvc_net_server_getinfo(mlrpc_handle_t *handle, char *server, int level, union mslm_NetServerGetInfo_ru *resp); #endif /* _SRVSVC1_CLNT_H */ /* * Copyright (c) 2001, Apple Computer, Inc. * 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. * * $Id: status.c,v 1.2 2001/08/18 05:44:50 conrad Exp $ */ /* * Copyright 2009 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" int cmd_status(int argc, char *argv[]) { struct nb_ctx *ctx; struct in_addr ina; char *hostname; char servername[NB_NAMELEN]; char workgroupname[NB_NAMELEN]; int error, opt; if (argc < 2) status_usage(); error = nb_ctx_create(&ctx); if (error) { smb_error(gettext("unable to create nbcontext"), error); exit(1); } if (smb_open_rcfile(NULL) == 0) { if (nb_ctx_readrcsection(NULL, ctx, "default", 0) != 0) exit(1); smb_close_rcfile(); } while ((opt = getopt(argc, argv, "")) != EOF) { switch (opt) { default: status_usage(); /*NOTREACHED*/ } } if (optind >= argc) status_usage(); hostname = argv[argc - 1]; error = nb_resolvehost_in(hostname, &ina); if (error) { smb_error(gettext( "unable to resolve DNS hostname %s"), error, hostname); exit(1); } if ((ctx->nb_flags & NBCF_NS_ENABLE) == 0) { fprintf(stderr, gettext("nbns_enable=false, cannot get status\n")); exit(1); } servername[0] = (char)0; workgroupname[0] = (char)0; error = nbns_getnodestatus(ctx, &ina, servername, workgroupname); if (error) { smb_error( gettext("unable to get status from %s"), error, hostname); exit(1); } if (workgroupname[0]) { printf(gettext("Workgroup: %s\n"), workgroupname); } if (servername[0]) { printf(gettext("Server: %s\n"), servername); } return (0); } void status_usage(void) { printf(gettext("usage: smbutil status hostname\n")); exit(1); } /* * Copyright (c) 2000, 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. * * $Id: view.c,v 1.9 2004/12/13 00:25:39 lindak Exp $ */ /* * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright 2018 Nexenta Systems, Inc. All rights reserved. */ #include #include #include #include #include #include #include #include #include #include #include #include "common.h" void view_usage(void) { printf(gettext("usage: smbutil view [connection options] //" "[workgroup;][user[:password]@]server\n")); exit(1); } int cmd_view(int argc, char *argv[]) { struct smb_ctx *ctx; int error, err2, opt; if (argc < 2) view_usage(); error = smb_ctx_alloc(&ctx); if (error) return (error); error = smb_ctx_scan_argv(ctx, argc, argv, SMBL_SERVER, SMBL_SERVER, USE_WILDCARD); if (error) goto out; error = smb_ctx_readrc(ctx); if (error) goto out; while ((opt = getopt(argc, argv, STDPARAM_OPT)) != EOF) { if (opt == '?') view_usage(); error = smb_ctx_opt(ctx, opt, optarg); if (error) goto out; } smb_ctx_setshare(ctx, "IPC$", USE_IPC); /* * Resolve the server address, * setup derived defaults. */ error = smb_ctx_resolve(ctx); if (error) goto out; /* * Have server, share, etc. from above: * smb_ctx_scan_argv, option settings. * Get the session and tree. */ again: error = smb_ctx_get_ssn(ctx); if (error == EAUTH) { err2 = smb_get_authentication(ctx); if (err2 == 0) goto again; } if (error) { smb_error(gettext("//%s: login failed"), error, ctx->ct_fullserver); goto out; } error = smb_ctx_get_tree(ctx); if (error) { smb_error(gettext("//%s/%s: tree connect failed"), error, ctx->ct_fullserver, ctx->ct_origshare); goto out; } /* * Have IPC$ tcon, now list shares. */ error = share_enum_rpc(ctx, ctx->ct_fullserver); out: smb_ctx_free(ctx); return (error); } #ifdef I18N /* not defined, put here so xgettext(1) can find strings */ static char *shtype[] = { gettext("disk"), gettext("printer"), gettext("device"), /* Communications device */ gettext("IPC"), /* Inter process communication */ gettext("unknown") }; #else static char *shtype[] = { "disk", "printer", "device", /* Communications device */ "IPC", /* IPC Inter process communication */ "unknown" }; #endif /* * Print one line of the share list, or * if SHARE is null, print the header line. */ void view_print_share(char *share, int type, char *comment) { char *stname; int stindex; if (share == NULL) { printf(gettext("Share Type Comment\n")); printf("-------------------------------\n"); return; } stindex = type & STYPE_MASK; if (stindex > STYPE_UNKNOWN) stindex = STYPE_UNKNOWN; stname = gettext(shtype[stindex]); if (comment == NULL) comment = ""; printf("%-12s %-10s %s\n", share, stname, comment); }