2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <strings.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <libnvpair.h>
2N/A#include <door.h>
2N/A#include <errno.h>
2N/A#include <sys/mman.h>
2N/A#include <alloca.h>
2N/A
2N/A#include <netsmb/smb_dev.h>
2N/A#include "smbfs_lib.h"
2N/A
2N/Astatic int smbfs_door_encode(int, smbfs_passwd_t *, char **, size_t *);
2N/Astatic int smbfs_door_call(char *, size_t, door_arg_t *);
2N/A
2N/A/*
2N/A * Request the creation of our per-user smbiod
2N/A * via door call to the "main" IOD service.
2N/A */
2N/Aint
2N/Asmbfs_iod_start(void)
2N/A{
2N/A door_arg_t da;
2N/A char *buf = NULL;
2N/A size_t buflen;
2N/A int err;
2N/A int32_t rc;
2N/A
2N/A if ((err = smbfs_door_encode(SMBIOD_START, NULL, &buf, &buflen)) != 0)
2N/A return (err);
2N/A
2N/A bzero(&da, sizeof (door_arg_t));
2N/A da.rbuf = (void *) &rc;
2N/A da.rsize = sizeof (rc);
2N/A
2N/A if ((err = smbfs_door_call(buf, buflen, &da)) != 0) {
2N/A free(buf);
2N/A return (err);
2N/A }
2N/A free(buf);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Adds passwd info to the smbfspasswd file.
2N/A */
2N/Aint
2N/Asmbfs_iod_pwdadd(smbfs_passwd_t *pwdinfo)
2N/A{
2N/A door_arg_t da;
2N/A char *buf = NULL;
2N/A size_t buflen;
2N/A int err;
2N/A int32_t rc;
2N/A
2N/A if (pwdinfo == NULL)
2N/A return (EINVAL);
2N/A
2N/A if ((err = smbfs_door_encode(SMBIOD_PWDFILE_ADD, pwdinfo,
2N/A &buf, &buflen)) != 0)
2N/A return (err);
2N/A
2N/A bzero(&da, sizeof (door_arg_t));
2N/A da.rbuf = (void *) &rc;
2N/A da.rsize = sizeof (rc);
2N/A
2N/A if ((err = smbfs_door_call(buf, buflen, &da)) != 0) {
2N/A free(buf);
2N/A return (err);
2N/A }
2N/A free(buf);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Removes passwd info(s) from the smbfspasswd file. If pwdinfo is NULL,
2N/A * removes all passwd info for the same user ID.
2N/A */
2N/Aint
2N/Asmbfs_iod_pwddel(smbfs_passwd_t *pwdinfo)
2N/A{
2N/A door_arg_t da;
2N/A char *buf = NULL;
2N/A size_t buflen;
2N/A int err, cmd;
2N/A int32_t rc;
2N/A
2N/A if (pwdinfo == NULL)
2N/A cmd = SMBIOD_PWDFILE_DELALL;
2N/A else
2N/A cmd = SMBIOD_PWDFILE_DEL;
2N/A
2N/A if ((err = smbfs_door_encode(cmd, pwdinfo, &buf, &buflen)) != 0)
2N/A return (err);
2N/A
2N/A bzero(&da, sizeof (door_arg_t));
2N/A da.rbuf = (void *) &rc;
2N/A da.rsize = sizeof (rc);
2N/A
2N/A if ((err = smbfs_door_call(buf, buflen, &da)) != 0) {
2N/A free(buf);
2N/A return (err);
2N/A }
2N/A free(buf);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Decodes the door call arguments which is encoded in a buffer via
2N/A * nvlist_pack(). The packed data contains the command and may contain
2N/A * password info.
2N/A *
2N/A * Returns the command and password info if a buffer is supplied to store
2N/A * the password info.
2N/A */
2N/Aint
2N/Asmbfs_door_decode(char *buf, size_t buflen, int *cmd, smbfs_passwd_t *pwdinfo)
2N/A{
2N/A nvlist_t *nvl;
2N/A uint8_t *lm, *nt;
2N/A uint_t lm_sz, nt_sz;
2N/A char *dom, *usr;
2N/A int err;
2N/A
2N/A if ((err = nvlist_unpack(buf, buflen, &nvl, 0)) != 0)
2N/A return (err);
2N/A
2N/A if ((err = nvlist_lookup_int32(nvl, "cmd", cmd)) != 0) {
2N/A nvlist_free(nvl);
2N/A return (err);
2N/A }
2N/A
2N/A if (pwdinfo != NULL) {
2N/A if ((nvlist_lookup_string(nvl, "dom", &dom) != 0) ||
2N/A (nvlist_lookup_string(nvl, "usr", &usr) != 0) ||
2N/A (nvlist_lookup_uint8_array(nvl, "lm", &lm, &lm_sz) != 0) ||
2N/A (nvlist_lookup_uint8_array(nvl, "nt", &nt, &nt_sz) != 0)) {
2N/A nvlist_free(nvl);
2N/A return (EINVAL);
2N/A }
2N/A
2N/A (void) strlcpy(pwdinfo->pw_dom, dom, sizeof (pwdinfo->pw_dom));
2N/A (void) strlcpy(pwdinfo->pw_usr, usr, sizeof (pwdinfo->pw_usr));
2N/A (void) bcopy(lm, pwdinfo->pw_lmhash, lm_sz);
2N/A (void) bcopy(nt, pwdinfo->pw_nthash, nt_sz);
2N/A }
2N/A
2N/A nvlist_free(nvl);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Encodes the door arguments, the command and password info, if supplied.
2N/A *
2N/A * To have the nvpair library allocate memory for the pack data, the caller
2N/A * should set the buf pointer to NULL. The memory then should be freed by
2N/A * the caller.
2N/A */
2N/Astatic int
2N/Asmbfs_door_encode(int cmd, smbfs_passwd_t *pwdinfo, char **buf, size_t *buflen)
2N/A{
2N/A nvlist_t *nvl;
2N/A int err;
2N/A smbfs_passwd_t newpwd, *pwdinfop;
2N/A
2N/A if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0)) != 0)
2N/A return (err);
2N/A
2N/A if (pwdinfo == NULL) {
2N/A bzero(&newpwd, sizeof (newpwd));
2N/A pwdinfop = &newpwd;
2N/A } else {
2N/A pwdinfop = pwdinfo;
2N/A }
2N/A
2N/A if ((nvlist_add_int32(nvl, "cmd", cmd) != 0) ||
2N/A (nvlist_add_string(nvl, "dom", pwdinfop->pw_dom) != 0) ||
2N/A (nvlist_add_string(nvl, "usr", pwdinfop->pw_usr) != 0) ||
2N/A (nvlist_add_uint8_array(nvl, "lm", pwdinfop->pw_lmhash,
2N/A SMBIOC_HASH_SZ) != 0) ||
2N/A (nvlist_add_uint8_array(nvl, "nt", pwdinfop->pw_nthash,
2N/A SMBIOC_HASH_SZ) != 0)) {
2N/A nvlist_free(nvl);
2N/A return (EINVAL);
2N/A }
2N/A
2N/A if ((err = nvlist_size(nvl, buflen, NV_ENCODE_XDR)) != 0) {
2N/A nvlist_free(nvl);
2N/A return (err);
2N/A }
2N/A
2N/A err = nvlist_pack(nvl, buf, buflen, NV_ENCODE_XDR, 0);
2N/A
2N/A nvlist_free(nvl);
2N/A return (err);
2N/A}
2N/A
2N/Astatic int
2N/Asmbfs_door_call(char *buf, size_t buflen, door_arg_t *da)
2N/A{
2N/A const char *svc_door = SMBIOD_SVC_DOOR;
2N/A int32_t err;
2N/A int fd, rc;
2N/A
2N/A fd = open(svc_door, O_RDONLY, 0);
2N/A if (fd < 0) {
2N/A err = errno;
2N/A return (err);
2N/A }
2N/A da->data_ptr = (void *) buf;
2N/A da->data_size = buflen;
2N/A rc = door_call(fd, da);
2N/A (void) close(fd);
2N/A if (rc < 0) {
2N/A err = errno;
2N/A return (err);
2N/A }
2N/A
2N/A return (0);
2N/A}