1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 #ifndef _SMBRDR_H_ 26 #define _SMBRDR_H_ 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <syslog.h> 31 #include <synch.h> 32 #include <sys/types.h> 33 34 #include <smbsrv/libsmb.h> 35 #include <smbsrv/libsmbrdr.h> 36 #include <smbsrv/smb.h> 37 #include <smbsrv/smbinfo.h> 38 #include <smbsrv/smb.h> 39 #include <smbsrv/wintypes.h> 40 41 #define SMBRDR_LOG_NAME "smbrdr" 42 #define SMBRDR_LOG_MAXCNT 1024 43 44 #define SMBRDR_REQ_BUFSZ 4096 45 46 #define MAX_ACCOUNT_NAME 32 47 #define MAX_SHARE_NAME 32 48 #define MAX_SCOPE_NAME 64 49 #define MAX_FILE_PATH 128 50 51 /* 52 * The number of shares and pipes is limited to 48 based on the note 53 * below. This really shouldn't cause a problem because we always 54 * our shares and named pipes are always opened and closed round every 55 * RPC transaction. This also tends to limit the number of active 56 * logons because we (currently) need two named pipes per logon. 57 * 58 * Q141709 Limit of 49 named pipe connections from a single workstation. 59 * If a named pipe server creates more than 49 distincly named pipes, a 60 * single client cannot connect more than 49 pipes on the named pipe 61 * server. Chapter 4, p113. Network Programming for Microsoft Windows 62 * Anthony Jones and Jim Ohlund, Microsoft Press, ISBN: 0-7356-0560-2 63 */ 64 #define N_NETUSE_TABLE 256 65 #define N_OFILE_TABLE 256 66 67 /* 68 * Logon's states 69 */ 70 #define SDB_LSTATE_START 0 71 #define SDB_LSTATE_INIT 1 72 #define SDB_LSTATE_LOGGING_OFF 2 73 #define SDB_LSTATE_SETUP 3 74 75 #define SDB_LOGON_NONE 0 76 #define SDB_LOGON_GUEST 1 77 #define SDB_LOGON_ANONYMOUS 2 78 #define SDB_LOGON_USER 3 79 80 typedef struct sdb_logon { 81 struct sdb_session *session; 82 char username[MAX_ACCOUNT_NAME]; 83 unsigned short uid; 84 unsigned int type; 85 unsigned short state; 86 smb_auth_info_t auth; 87 unsigned char ssn_key[SMBAUTH_SESSION_KEY_SZ]; 88 } sdb_logon_t; 89 90 /* 91 * Session's states 92 * 93 * SDB_SSTATE_START ready to be used 94 * SDB_SSTATE_INIT initialized 95 * SDB_SSTATE_STALE lost transport connection 96 * SDB_SSTATE_DISCONNECTING disconnecting: logoff the user 97 * disconnect trees, close files 98 * SDB_SSTATE_CLEANING was in STALE state now just 99 * cleaning up 100 * SDB_SSTATE_CONNECTED got transport connection 101 * SDB_SSTATE_NEGOTIATED did SMB negotiate 102 */ 103 #define SDB_SSTATE_START 0 104 #define SDB_SSTATE_INIT 1 105 #define SDB_SSTATE_STALE 2 106 #define SDB_SSTATE_DISCONNECTING 3 107 #define SDB_SSTATE_CLEANING 4 108 #define SDB_SSTATE_CONNECTED 5 109 #define SDB_SSTATE_NEGOTIATED 6 110 111 #define SDB_SLCK_READ 1 112 #define SDB_SLCK_WRITE 2 113 114 struct sdb_session { 115 char srv_name[MAXHOSTNAMELEN]; 116 smb_inaddr_t srv_ipaddr; 117 char domain[MAXHOSTNAMELEN]; 118 char scope[SMB_PI_MAX_SCOPE]; 119 char native_os[SMB_PI_MAX_NATIVE_OS]; 120 char native_lanman[SMB_PI_MAX_LANMAN]; 121 int sock; 122 short port; 123 uint16_t secmode; 124 uint32_t sesskey; 125 uint32_t challenge_len; 126 uint8_t challenge_key[32]; 127 uint8_t smb_flags; 128 uint16_t smb_flags2; 129 uint16_t vc; 130 uint32_t remote_caps; 131 uint8_t state; 132 uint32_t sid; /* session id */ 133 int remote_os; 134 int remote_lm; 135 int pdc_type; 136 smb_sign_ctx_t sign_ctx; 137 sdb_logon_t logon; 138 rwlock_t rwl; 139 }; 140 141 /* 142 * Netuse's states 143 */ 144 #define SDB_NSTATE_START 0 145 #define SDB_NSTATE_INIT 1 146 #define SDB_NSTATE_DISCONNECTING 2 147 #define SDB_NSTATE_CONNECTED 3 148 149 struct sdb_netuse { 150 struct sdb_session *session; 151 unsigned short state; 152 int letter; /* local identity */ 153 unsigned int sid; 154 unsigned short uid; 155 unsigned short tid; /* remote identity */ 156 char share[MAX_SHARE_NAME]; 157 mutex_t mtx; 158 }; 159 160 /* 161 * Ofile's states 162 */ 163 #define SDB_FSTATE_START 0 164 #define SDB_FSTATE_INIT 1 165 #define SDB_FSTATE_CLOSING 2 166 #define SDB_FSTATE_OPEN 3 167 168 struct sdb_ofile { 169 struct sdb_session *session; 170 struct sdb_netuse *netuse; 171 unsigned short state; 172 unsigned int sid; 173 unsigned short uid; 174 unsigned short tid; 175 unsigned short fid; /* remote identity */ 176 char path[MAX_FILE_PATH]; 177 mutex_t mtx; 178 }; 179 180 typedef struct smbrdr_handle { 181 unsigned char *srh_buf; 182 smb_msgbuf_t srh_mbuf; 183 unsigned int srh_mbflags; 184 unsigned char srh_cmd; 185 struct sdb_session *srh_session; 186 struct sdb_logon *srh_user; 187 struct sdb_netuse *srh_tree; 188 } smbrdr_handle_t; 189 190 typedef struct smb_nt_negotiate_rsp { 191 uint8_t word_count; 192 uint16_t dialect_index; 193 uint8_t security_mode; 194 uint16_t max_mpx; 195 uint16_t max_vc; 196 uint32_t max_buffer_size; 197 uint32_t max_raw_size; 198 uint32_t session_key; 199 uint32_t capabilities; 200 uint32_t time_low; 201 uint32_t time_high; 202 uint16_t server_tz; 203 uint8_t security_len; 204 uint16_t byte_count; 205 uint8_t *guid; 206 uint8_t *challenge; 207 uint8_t *oem_domain; 208 } smb_nt_negotiate_rsp_t; 209 210 /* 211 * SMB_COM_TRANSACTION 212 */ 213 typedef struct smb_transact_rsp { 214 uint8_t WordCount; /* Count of data bytes */ 215 /* value = 10 + SetupCount */ 216 uint16_t TotalParamCount; /* Total parameter bytes being sent */ 217 uint16_t TotalDataCount; /* Total data bytes being sent */ 218 uint16_t Reserved; 219 uint16_t ParamCount; /* Parameter bytes sent this buffer */ 220 uint16_t ParamOffset; /* Offset (from hdr start) to params */ 221 uint16_t ParamDisplacement; /* Displacement of these param bytes */ 222 uint16_t DataCount; /* Data bytes sent this buffer */ 223 uint16_t DataOffset; /* Offset (from hdr start) to data */ 224 uint16_t DataDisplacement; /* Displacement of these data bytes */ 225 uint8_t SetupCount; /* Count of setup words */ 226 uint16_t BCC; 227 #if 0 228 uint8_t Reserved2; /* Reserved (pad above to word) */ 229 uint8_t Buffer[1]; /* Buffer containing: */ 230 uint16_t Setup[]; /* Setup words (# = SetupWordCount) */ 231 uint16_t ByteCount; /* Count of data bytes */ 232 uint8_t Pad[]; /* Pad to SHORT or LONG */ 233 uint8_t Params[]; /* Param. bytes (# = ParamCount) */ 234 uint8_t Pad1[]; /* Pad to SHORT or LONG */ 235 uint8_t Data[]; /* Data bytes (# = DataCount) */ 236 #endif 237 } smb_transact_rsp_t; 238 239 /* 240 * SMBreadX 241 */ 242 typedef struct smb_read_andx_rsp { 243 uint8_t WordCount; 244 uint8_t AndXCmd; 245 uint8_t AndXReserved; 246 uint16_t AndXOffset; 247 uint16_t Remaining; 248 uint16_t DataCompactionMode; 249 uint16_t Reserved; 250 uint16_t DataLength; 251 uint16_t DataOffset; 252 uint32_t DataLengthHigh; 253 uint16_t Reserved2[3]; 254 uint16_t ByteCount; 255 #if 0 256 uint8_t Pad[]; 257 uint8_t Data[]; 258 #endif 259 } smb_read_andx_rsp_t; 260 261 /* 262 * smbrdr_netbios.c 263 */ 264 void nb_lock(void); 265 void nb_unlock(void); 266 void nb_close(int); 267 int nb_keep_alive(int, short); 268 269 int nb_send(int, unsigned char *, unsigned); 270 int nb_rcv(int, unsigned char *, unsigned, long); 271 int nb_exchange(int, unsigned char *, unsigned, 272 unsigned char *, unsigned, long); 273 int nb_session_request(int, char *, char *, char *, char *); 274 275 /* 276 * smbrdr_session.c 277 */ 278 int smbrdr_negotiate(char *, char *); 279 struct sdb_session *smbrdr_session_lock(const char *, int); 280 void smbrdr_session_unlock(struct sdb_session *); 281 282 /* 283 * smbrdr_logon.c 284 */ 285 int smbrdr_logoffx(struct sdb_logon *); 286 287 /* smbrdr_netuse.c */ 288 void smbrdr_netuse_logoff(unsigned short); 289 struct sdb_netuse *smbrdr_netuse_get(int); 290 DWORD smbrdr_tree_connect(char *, char *, char *, char *, unsigned short *); 291 int smbrdr_tree_disconnect(unsigned short); 292 void smbrdr_netuse_put(struct sdb_netuse *); 293 int smbrdr_tdcon(struct sdb_netuse *); 294 295 /* 296 * smbrdr_rpcpipe.c 297 */ 298 void smbrdr_ofile_end_of_share(unsigned short); 299 struct sdb_ofile *smbrdr_ofile_get(int); 300 void smbrdr_ofile_put(struct sdb_ofile *); 301 302 /* smbrdr_lib.c */ 303 DWORD smbrdr_request_init(smbrdr_handle_t *, unsigned char, 304 struct sdb_session *, struct sdb_logon *, struct sdb_netuse *); 305 DWORD smbrdr_send(smbrdr_handle_t *); 306 DWORD smbrdr_rcv(smbrdr_handle_t *, int); 307 DWORD smbrdr_exchange(smbrdr_handle_t *, smb_hdr_t *, long); 308 void smbrdr_handle_free(smbrdr_handle_t *); 309 int smbrdr_sign_init(struct sdb_session *, struct sdb_logon *); 310 void smbrdr_sign_fini(struct sdb_session *); 311 void smbrdr_sign_unset_key(struct sdb_session *); 312 313 void smbrdr_lock_transport(void); 314 void smbrdr_unlock_transport(void); 315 316 extern smb_log_hdl_t smbrdr_log_hdl; 317 318 #endif /* _SMBRDR_H_ */ 319