15331Samw /* 25331Samw * CDDL HEADER START 35331Samw * 45331Samw * The contents of this file are subject to the terms of the 55331Samw * Common Development and Distribution License (the "License"). 65331Samw * You may not use this file except in compliance with the License. 75331Samw * 85331Samw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 95331Samw * or http://www.opensolaris.org/os/licensing. 105331Samw * See the License for the specific language governing permissions 115331Samw * and limitations under the License. 125331Samw * 135331Samw * When distributing Covered Code, include this CDDL HEADER in each 145331Samw * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 155331Samw * If applicable, add the following below this CDDL HEADER, with the 165331Samw * fields enclosed by brackets "[]" replaced with your own identifying 175331Samw * information: Portions Copyright [yyyy] [name of copyright owner] 185331Samw * 195331Samw * CDDL HEADER END 205331Samw */ 215331Samw /* 225772Sas200622 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 235331Samw * Use is subject to license terms. 245331Samw */ 255331Samw 267588Samw@Sun.COM #include <sys/errno.h> 277588Samw@Sun.COM #include <stdlib.h> 285331Samw #include <unistd.h> 297588Samw@Sun.COM #include <strings.h> 307588Samw@Sun.COM #include <string.h> 317588Samw@Sun.COM #include <rpc/xdr.h> 327588Samw@Sun.COM #include <synch.h> 335772Sas200622 #include <pthread.h> 347588Samw@Sun.COM #include <smbsrv/smb_door_svc.h> 357588Samw@Sun.COM #include <smbsrv/smb_common_door.h> 367588Samw@Sun.COM #include <smbsrv/libsmb.h> 375331Samw #include <smbsrv/libmlsvc.h> 38*8334SJose.Borrego@Sun.COM #include <mlsvc.h> 395772Sas200622 405772Sas200622 static void *mlsvc_keepalive(void *); 415772Sas200622 425772Sas200622 static pthread_t mlsvc_keepalive_thr; 435772Sas200622 #define MLSVC_KEEPALIVE_INTERVAL (10 * 60) /* 10 minutes */ 445331Samw 455331Samw /* 467588Samw@Sun.COM * Door fd for downcalls to the smbsrv kernel door service. 477588Samw@Sun.COM * smbsrv will make an upcall to smbd during initialization to 487588Samw@Sun.COM * provide this file descriptor. 497588Samw@Sun.COM */ 507588Samw@Sun.COM static int mlsvc_door_fd = -1; 517588Samw@Sun.COM static mutex_t mlsvc_fd_mutex; 527588Samw@Sun.COM 537588Samw@Sun.COM /* 54*8334SJose.Borrego@Sun.COM * All NDR RPC service initialization is invoked from here. 555331Samw * Returns 0 upon success. Otherwise, returns -1. 565331Samw */ 575331Samw int 585331Samw mlsvc_init(void) 595331Samw { 605772Sas200622 pthread_attr_t tattr; 615772Sas200622 int rc; 625772Sas200622 63*8334SJose.Borrego@Sun.COM if ((rc = smb_dclocator_init()) != 0) 64*8334SJose.Borrego@Sun.COM return (rc); 65*8334SJose.Borrego@Sun.COM 665331Samw srvsvc_initialize(); 675331Samw wkssvc_initialize(); 685331Samw lsarpc_initialize(); 695331Samw netr_initialize(); 705331Samw dssetup_initialize(); 715331Samw samr_initialize(); 725331Samw svcctl_initialize(); 735331Samw winreg_initialize(); 745331Samw logr_initialize(); 75*8334SJose.Borrego@Sun.COM msgsvcsend_initialize(); 76*8334SJose.Borrego@Sun.COM spoolss_initialize(); 775331Samw 785772Sas200622 (void) pthread_attr_init(&tattr); 795772Sas200622 (void) pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); 805772Sas200622 rc = pthread_create(&mlsvc_keepalive_thr, &tattr, 815772Sas200622 mlsvc_keepalive, 0); 825772Sas200622 (void) pthread_attr_destroy(&tattr); 835772Sas200622 return (rc); 845331Samw } 855772Sas200622 865772Sas200622 /*ARGSUSED*/ 875772Sas200622 static void * 885772Sas200622 mlsvc_keepalive(void *arg) 895772Sas200622 { 905772Sas200622 unsigned long t; 915772Sas200622 925772Sas200622 for (;;) { 935772Sas200622 (void) sleep(MLSVC_KEEPALIVE_INTERVAL); 945772Sas200622 95*8334SJose.Borrego@Sun.COM if (smb_config_get_secmode() == SMB_SECMODE_DOMAIN) 967348SJose.Borrego@Sun.COM (void) srvsvc_gettime(&t); 975772Sas200622 } 985772Sas200622 995772Sas200622 /*NOTREACHED*/ 1005772Sas200622 return (NULL); 1015772Sas200622 } 1027588Samw@Sun.COM 1037588Samw@Sun.COM void 1047588Samw@Sun.COM mlsvc_set_door_fd(int fd) 1057588Samw@Sun.COM { 1067588Samw@Sun.COM (void) mutex_lock(&mlsvc_fd_mutex); 1077588Samw@Sun.COM mlsvc_door_fd = fd; 1087588Samw@Sun.COM (void) mutex_unlock(&mlsvc_fd_mutex); 1097588Samw@Sun.COM } 1107588Samw@Sun.COM 1117588Samw@Sun.COM int 1127588Samw@Sun.COM mlsvc_get_door_fd(void) 1137588Samw@Sun.COM { 1147588Samw@Sun.COM int fd; 1157588Samw@Sun.COM 1167588Samw@Sun.COM (void) mutex_lock(&mlsvc_fd_mutex); 1177588Samw@Sun.COM fd = mlsvc_door_fd; 1187588Samw@Sun.COM (void) mutex_unlock(&mlsvc_fd_mutex); 1197588Samw@Sun.COM 1207588Samw@Sun.COM return (fd); 1217588Samw@Sun.COM } 1227588Samw@Sun.COM 1237588Samw@Sun.COM uint64_t 1247588Samw@Sun.COM mlsvc_get_num_users(void) 1257588Samw@Sun.COM { 1267588Samw@Sun.COM door_arg_t arg; 1277588Samw@Sun.COM char *buf; 1287588Samw@Sun.COM size_t len; 1297588Samw@Sun.COM int64_t n_users = 0; 1307588Samw@Sun.COM int fd; 1317588Samw@Sun.COM 1327588Samw@Sun.COM if ((fd = mlsvc_get_door_fd()) < 0) 1337588Samw@Sun.COM return (0); 1347588Samw@Sun.COM 1357588Samw@Sun.COM if ((buf = smb_dr_set_opcode(SMB_KDR_USER_NUM, &len)) == NULL) 1367588Samw@Sun.COM return (0); 1377588Samw@Sun.COM 1387588Samw@Sun.COM smb_dr_clnt_setup(&arg, buf, len); 1397588Samw@Sun.COM 1407588Samw@Sun.COM if (smb_dr_clnt_call(fd, &arg) == 0) { 1417588Samw@Sun.COM buf = arg.rbuf + SMB_DR_DATA_OFFSET; 1427588Samw@Sun.COM len = arg.rsize - SMB_DR_DATA_OFFSET; 1437588Samw@Sun.COM 1447588Samw@Sun.COM if (smb_dr_decode_common(buf, len, xdr_uint32_t, &n_users) != 0) 1457588Samw@Sun.COM n_users = 0; 1467588Samw@Sun.COM } 1477588Samw@Sun.COM 1487588Samw@Sun.COM smb_dr_clnt_cleanup(&arg); 1497588Samw@Sun.COM return (n_users); 1507588Samw@Sun.COM } 1517588Samw@Sun.COM 1527588Samw@Sun.COM /* 1537588Samw@Sun.COM * The calling function must free the output parameter 'users'. 1547588Samw@Sun.COM */ 1557588Samw@Sun.COM int 1567588Samw@Sun.COM mlsvc_get_user_list(int offset, smb_dr_ulist_t *users) 1577588Samw@Sun.COM { 1587588Samw@Sun.COM door_arg_t arg; 1597588Samw@Sun.COM char *buf; 1607588Samw@Sun.COM size_t len; 1617588Samw@Sun.COM uint_t opcode = SMB_KDR_USER_LIST; 1627588Samw@Sun.COM int fd, rc = -1; 1637588Samw@Sun.COM 1647588Samw@Sun.COM bzero(users, sizeof (smb_dr_ulist_t)); 1657588Samw@Sun.COM 1667588Samw@Sun.COM if ((fd = mlsvc_get_door_fd()) < 0) 1677588Samw@Sun.COM return (-1); 1687588Samw@Sun.COM 1697588Samw@Sun.COM buf = smb_dr_encode_common(opcode, &offset, xdr_uint32_t, &len); 1707588Samw@Sun.COM if (buf == NULL) 1717588Samw@Sun.COM return (-1); 1727588Samw@Sun.COM 1737588Samw@Sun.COM smb_dr_clnt_setup(&arg, buf, len); 1747588Samw@Sun.COM 1757588Samw@Sun.COM if (smb_dr_clnt_call(fd, &arg) == 0) { 1767588Samw@Sun.COM buf = arg.rbuf + SMB_DR_DATA_OFFSET; 1777588Samw@Sun.COM len = arg.rsize - SMB_DR_DATA_OFFSET; 1787588Samw@Sun.COM 1797588Samw@Sun.COM rc = smb_dr_decode_common(buf, len, xdr_smb_dr_ulist_t, users); 1807588Samw@Sun.COM if (rc == 0) 1817588Samw@Sun.COM rc = users->dul_cnt; 1827588Samw@Sun.COM } 1837588Samw@Sun.COM 1847588Samw@Sun.COM smb_dr_clnt_cleanup(&arg); 1857588Samw@Sun.COM return (rc); 1867588Samw@Sun.COM } 1877588Samw@Sun.COM 1887588Samw@Sun.COM /* 1897588Samw@Sun.COM * Downcall to the kernel that is executed upon share enable and disable. 1907588Samw@Sun.COM */ 1917588Samw@Sun.COM int 1927588Samw@Sun.COM mlsvc_set_share(int shrop, char *path, char *sharename) 1937588Samw@Sun.COM { 1947588Samw@Sun.COM door_arg_t arg; 1957588Samw@Sun.COM char *buf; 1967588Samw@Sun.COM size_t len; 1977588Samw@Sun.COM smb_dr_kshare_t kshare; 1987588Samw@Sun.COM int fd, rc = 0; 1997588Samw@Sun.COM 2007588Samw@Sun.COM if ((shrop != SMB_SHROP_ADD) && (shrop != SMB_SHROP_DELETE)) 2017588Samw@Sun.COM return (EINVAL); 2027588Samw@Sun.COM 2037588Samw@Sun.COM if ((fd = mlsvc_get_door_fd()) < 0) 2047588Samw@Sun.COM return (EBADF); 2057588Samw@Sun.COM 2067588Samw@Sun.COM kshare.k_op = shrop; 2077588Samw@Sun.COM kshare.k_path = strdup(path); 2087588Samw@Sun.COM kshare.k_sharename = strdup(sharename); 2097588Samw@Sun.COM 2107588Samw@Sun.COM buf = smb_dr_encode_kshare(&kshare, &len); 2117588Samw@Sun.COM free(kshare.k_path); 2127588Samw@Sun.COM free(kshare.k_sharename); 2137588Samw@Sun.COM 2147588Samw@Sun.COM if (buf == NULL) 2157588Samw@Sun.COM return (ENOMEM); 2167588Samw@Sun.COM 2177588Samw@Sun.COM smb_dr_clnt_setup(&arg, buf, len); 2187588Samw@Sun.COM 2197588Samw@Sun.COM if (smb_dr_clnt_call(fd, &arg) == 0) { 2207588Samw@Sun.COM buf = arg.rbuf + SMB_DR_DATA_OFFSET; 2217588Samw@Sun.COM len = arg.rsize - SMB_DR_DATA_OFFSET; 2227588Samw@Sun.COM 2237588Samw@Sun.COM if (smb_dr_decode_common(buf, len, xdr_int32_t, &rc) != 0) 2247588Samw@Sun.COM rc = ENOMEM; 2257588Samw@Sun.COM } 2267588Samw@Sun.COM 2277588Samw@Sun.COM smb_dr_clnt_cleanup(&arg); 2287588Samw@Sun.COM return (rc); 2297588Samw@Sun.COM } 230