1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2000-2001 Boris Popov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 /* 32 * Two levels of connection hierarchy 33 */ 34 #define SMBL_SM 0 35 #define SMBL_VC 1 36 #define SMBL_SHARE 2 37 #define SMBL_NUM 3 38 #define SMBL_NONE (-1) 39 40 #define SMB_CS_NONE 0x0000 41 #define SMB_CS_UPPER 0x0001 /* convert passed string to upper case */ 42 #define SMB_CS_LOWER 0x0002 /* convert passed string to lower case */ 43 44 /* 45 * Common object flags 46 */ 47 #define SMBO_GONE 0x1000000 48 49 /* 50 * access modes 51 */ 52 #define SMBM_READ 0400 /* read conn attrs.(like list shares) */ 53 #define SMBM_WRITE 0200 /* modify conn attrs */ 54 #define SMBM_EXEC 0100 /* can send SMB requests */ 55 #define SMBM_READGRP 0040 56 #define SMBM_WRITEGRP 0020 57 #define SMBM_EXECGRP 0010 58 #define SMBM_READOTH 0004 59 #define SMBM_WRITEOTH 0002 60 #define SMBM_EXECOTH 0001 61 #define SMBM_MASK 0777 62 #define SMBM_EXACT 010000 /* check for specified mode exactly */ 63 #define SMBM_ALL (SMBM_READ | SMBM_WRITE | SMBM_EXEC) 64 #define SMBM_DEFAULT (SMBM_READ | SMBM_WRITE | SMBM_EXEC) 65 #define SMBM_ANY_OWNER ((uid_t)-1) 66 #define SMBM_ANY_GROUP ((gid_t)-1) 67 68 /* 69 * VC flags 70 */ 71 #define SMBV_PERMANENT 0x0002 72 #define SMBV_LONGNAMES 0x0004 /* connection is configured to use long names */ 73 #define SMBV_ENCRYPT 0x0008 /* server asked for encrypted password */ 74 #define SMBV_WIN95 0x0010 /* used to apply bugfixes for this OS */ 75 #define SMBV_PRIVATE 0x0020 /* connection can be used only by creator */ 76 #define SMBV_RECONNECTING 0x0040 /* conn is in the process of reconnection */ 77 #define SMBV_SINGLESHARE 0x0080 /* only one share connecting should be allowed */ 78 #define SMBV_CREATE 0x0100 /* lookup for create operation */ 79 /*#define SMBV_FAILED 0x0200*/ /* last reconnect attempt has failed */ 80 #define SMBV_UNICODE 0x0400 /* connection is configured to use Unicode */ 81 82 83 /* 84 * smb_share flags 85 */ 86 #define SMBS_PERMANENT 0x0001 87 #define SMBS_RECONNECTING 0x0002 88 #define SMBS_CONNECTED 0x0004 89 90 /* 91 * share types 92 */ 93 #define SMB_ST_DISK 0x0 /* A: */ 94 #define SMB_ST_PRINTER 0x1 /* LPT: */ 95 #define SMB_ST_PIPE 0x2 /* IPC */ 96 #define SMB_ST_COMM 0x3 /* COMM */ 97 #define SMB_ST_ANY 0x4 98 #define SMB_ST_MAX 0x4 99 #define SMB_ST_NONE 0xff /* not a part of protocol */ 100 101 /* 102 * Negotiated protocol parameters 103 */ 104 struct smb_sopt { 105 int sv_proto; 106 int16_t sv_tz; /* offset in min relative to UTC */ 107 u_int32_t sv_maxtx; /* maximum transmit buf size */ 108 u_char sv_sm; /* security mode */ 109 u_int16_t sv_maxmux; /* max number of outstanding rq's */ 110 u_int16_t sv_maxvcs; /* max number of VCs */ 111 u_int16_t sv_rawmode; 112 u_int32_t sv_maxraw; /* maximum raw-buffer size */ 113 u_int32_t sv_skey; /* session key */ 114 u_int32_t sv_caps; /* capabilities SMB_CAP_ */ 115 }; 116 117 /* 118 * network IO daemon states 119 */ 120 enum smbiod_state { 121 SMBIOD_ST_NOTCONN, /* no connect request was made */ 122 SMBIOD_ST_RECONNECT, /* a [re]connect attempt is in progress */ 123 SMBIOD_ST_TRANACTIVE, /* transport level is up */ 124 SMBIOD_ST_VCACTIVE, /* session established */ 125 SMBIOD_ST_DEAD /* connection broken, transport is down */ 126 }; 127 128 129 /* 130 * Info structures 131 */ 132 #define SMB_INFO_NONE 0 133 #define SMB_INFO_VC 2 134 #define SMB_INFO_SHARE 3 135 136 struct smb_vc_info { 137 int itype; 138 int usecount; 139 uid_t uid; /* user id of connection */ 140 gid_t gid; /* group of connection */ 141 mode_t mode; /* access mode */ 142 int flags; 143 enum smbiod_state iodstate; 144 struct smb_sopt sopt; 145 char srvname[SMB_MAXSRVNAMELEN + 1]; 146 char vcname[128]; 147 }; 148 149 struct smb_share_info { 150 int itype; 151 int usecount; 152 u_short tid; /* TID */ 153 int type; /* share type */ 154 uid_t uid; /* user id of connection */ 155 gid_t gid; /* group of connection */ 156 mode_t mode; /* access mode */ 157 int flags; 158 char sname[128]; 159 }; 160 161 #ifdef _KERNEL 162 163 #include <netsmb/smb_subr.h> 164 #include <sys/param.h> 165 #include <sys/lock.h> 166 #include <sys/condvar.h> 167 #include <sys/sx.h> 168 169 #define CONNADDREQ(a1,a2) ((a1)->sa_len == (a2)->sa_len && \ 170 bcmp(a1, a2, (a1)->sa_len) == 0) 171 172 struct smb_vc; 173 struct smb_share; 174 struct smb_cred; 175 struct smb_rq; 176 struct mbdata; 177 struct smbioc_oshare; 178 struct smbioc_ossn; 179 struct uio; 180 181 TAILQ_HEAD(smb_rqhead, smb_rq); 182 183 #define SMB_DEFRQTIMO 5 184 185 #define SMB_DIALECT(vcp) ((vcp)->vc_sopt.sv_proto) 186 187 struct smb_tran_desc; 188 189 /* 190 * Connection object 191 */ 192 struct smb_connobj; 193 194 typedef void smb_co_gone_t (struct smb_connobj *cp, struct smb_cred *scred); 195 typedef void smb_co_free_t (struct smb_connobj *cp); 196 197 struct smb_connobj { 198 struct cv co_lock; 199 struct thread *co_locker; 200 struct sx co_interlock; 201 int co_lockcnt; 202 int co_level; /* SMBL_ */ 203 int co_flags; 204 int co_usecount; 205 struct smb_connobj * co_parent; 206 SLIST_HEAD(,smb_connobj)co_children; 207 SLIST_ENTRY(smb_connobj)co_next; 208 smb_co_gone_t * co_gone; 209 smb_co_free_t * co_free; 210 }; 211 212 #define SMBCO_FOREACH(var, cp) SLIST_FOREACH((var), &(cp)->co_children, co_next) 213 214 /* 215 * Virtual Circuit (session) to a server. 216 * This is the most (over)complicated part of SMB protocol. 217 * For the user security level (usl), each session with different remote 218 * user name has its own VC. 219 * It is unclear however, should share security level (ssl) allow additional 220 * VCs, because user name is not used and can be the same. On other hand, 221 * multiple VCs allows us to create separate sessions to server on a per 222 * user basis. 223 */ 224 225 /* 226 * This lock protects vc_flags 227 */ 228 #define SMBC_ST_LOCK(vcp) smb_sl_lock(&(vcp)->vc_stlock) 229 #define SMBC_ST_UNLOCK(vcp) smb_sl_unlock(&(vcp)->vc_stlock) 230 231 struct smb_vc { 232 struct smb_connobj obj; 233 char * vc_srvname; 234 struct sockaddr*vc_paddr; /* server addr */ 235 struct sockaddr*vc_laddr; /* local addr, if any */ 236 char * vc_username; 237 char * vc_pass; /* password for usl case */ 238 char * vc_domain; /* workgroup/primary domain */ 239 240 u_int vc_timo; /* default request timeout */ 241 int vc_maxvcs; /* maximum number of VC per connection */ 242 243 void * vc_tolower; /* local charset */ 244 void * vc_toupper; /* local charset */ 245 void * vc_toserver; /* local charset to server one */ 246 void * vc_tolocal; /* server charset to local one */ 247 void * vc_cp_toserver; /* local charset to server one (using CodePage) */ 248 void * vc_cp_tolocal; /* server charset to local one (using CodePage) */ 249 void * vc_ucs_toserver; /* local charset to server one (using UCS-2) */ 250 void * vc_ucs_tolocal; /* server charset to local one (using UCS-2) */ 251 int vc_number; /* number of this VC from the client side */ 252 int vc_genid; 253 uid_t vc_uid; /* user id of connection */ 254 gid_t vc_grp; /* group of connection */ 255 mode_t vc_mode; /* access mode */ 256 u_short vc_smbuid; /* unique vc id assigned by server */ 257 258 u_char vc_hflags; /* or'ed with flags in the smb header */ 259 u_short vc_hflags2; /* or'ed with flags in the smb header */ 260 void * vc_tdata; /* transport control block */ 261 struct smb_tran_desc *vc_tdesc; 262 int vc_chlen; /* actual challenge length */ 263 u_char vc_ch[SMB_MAXCHALLENGELEN]; 264 u_short vc_mid; /* multiplex id */ 265 struct smb_sopt vc_sopt; /* server options */ 266 int vc_txmax; /* max tx/rx packet size */ 267 int vc_rxmax; /* max readx data size */ 268 int vc_wxmax; /* max writex data size */ 269 struct smbiod * vc_iod; 270 struct smb_slock vc_stlock; 271 u_int32_t vc_seqno; /* my next sequence number */ 272 u_int8_t *vc_mackey; /* MAC key */ 273 int vc_mackeylen; /* length of MAC key */ 274 }; 275 276 #define vc_maxmux vc_sopt.sv_maxmux 277 #define vc_flags obj.co_flags 278 279 #define SMB_UNICODE_STRINGS(vcp) ((vcp)->vc_hflags2 & SMB_FLAGS2_UNICODE) 280 281 #define SMB_UNICODE_NAME "UCS-2LE" 282 283 /* 284 * smb_share structure describes connection to the given SMB share (tree). 285 * Connection to share is always built on top of the VC. 286 */ 287 288 /* 289 * This lock protects ss_flags 290 */ 291 #define SMBS_ST_LOCK(ssp) smb_sl_lock(&(ssp)->ss_stlock) 292 #define SMBS_ST_LOCKPTR(ssp) (&(ssp)->ss_stlock) 293 #define SMBS_ST_UNLOCK(ssp) smb_sl_unlock(&(ssp)->ss_stlock) 294 295 struct smb_share { 296 struct smb_connobj obj; 297 char * ss_name; 298 u_short ss_tid; /* TID */ 299 int ss_type; /* share type */ 300 uid_t ss_uid; /* user id of connection */ 301 gid_t ss_grp; /* group of connection */ 302 mode_t ss_mode; /* access mode */ 303 int ss_vcgenid; 304 char * ss_pass; /* password to a share, can be null */ 305 struct smb_slock ss_stlock; 306 }; 307 308 #define ss_flags obj.co_flags 309 310 #define CPTOVC(cp) ((struct smb_vc*)(cp)) 311 #define VCTOCP(vcp) (&(vcp)->obj) 312 #define CPTOSS(cp) ((struct smb_share*)(cp)) 313 #define SSTOVC(ssp) CPTOVC(((ssp)->obj.co_parent)) 314 #define SSTOCP(ssp) (&(ssp)->obj) 315 316 struct smb_vcspec { 317 char * srvname; 318 struct sockaddr*sap; 319 struct sockaddr*lap; 320 int flags; 321 char * username; 322 char * pass; 323 char * domain; 324 mode_t mode; 325 mode_t rights; 326 uid_t owner; 327 gid_t group; 328 char * localcs; 329 char * servercs; 330 struct smb_sharespec *shspec; 331 struct smb_share *ssp; /* returned */ 332 /* 333 * The rest is an internal data 334 */ 335 struct smb_cred *scred; 336 }; 337 338 struct smb_sharespec { 339 char * name; 340 char * pass; 341 mode_t mode; 342 mode_t rights; 343 uid_t owner; 344 gid_t group; 345 int stype; 346 /* 347 * The rest is an internal data 348 */ 349 struct smb_cred *scred; 350 }; 351 352 /* 353 * Session level functions 354 */ 355 int smb_sm_init(void); 356 int smb_sm_done(void); 357 int smb_sm_lookup(struct smb_vcspec *vcspec, 358 struct smb_sharespec *shspec, struct smb_cred *scred, 359 struct smb_vc **vcpp); 360 361 /* 362 * Connection object 363 */ 364 void smb_co_ref(struct smb_connobj *cp); 365 void smb_co_rele(struct smb_connobj *cp, struct smb_cred *scred); 366 int smb_co_get(struct smb_connobj *cp, struct smb_cred *scred); 367 void smb_co_put(struct smb_connobj *cp, struct smb_cred *scred); 368 int smb_co_lock(struct smb_connobj *cp); 369 void smb_co_unlock(struct smb_connobj *cp); 370 371 /* 372 * session level functions 373 */ 374 int smb_vc_create(struct smb_vcspec *vcspec, 375 struct smb_cred *scred, struct smb_vc **vcpp); 376 int smb_vc_connect(struct smb_vc *vcp, struct smb_cred *scred); 377 int smb_vc_access(struct smb_vc *vcp, struct smb_cred *scred, mode_t mode); 378 int smb_vc_get(struct smb_vc *vcp, struct smb_cred *scred); 379 void smb_vc_put(struct smb_vc *vcp, struct smb_cred *scred); 380 void smb_vc_ref(struct smb_vc *vcp); 381 void smb_vc_rele(struct smb_vc *vcp, struct smb_cred *scred); 382 int smb_vc_lock(struct smb_vc *vcp); 383 void smb_vc_unlock(struct smb_vc *vcp); 384 int smb_vc_lookupshare(struct smb_vc *vcp, struct smb_sharespec *shspec, 385 struct smb_cred *scred, struct smb_share **sspp); 386 const char * smb_vc_getpass(struct smb_vc *vcp); 387 u_short smb_vc_nextmid(struct smb_vc *vcp); 388 389 /* 390 * share level functions 391 */ 392 int smb_share_create(struct smb_vc *vcp, struct smb_sharespec *shspec, 393 struct smb_cred *scred, struct smb_share **sspp); 394 int smb_share_access(struct smb_share *ssp, struct smb_cred *scred, mode_t mode); 395 void smb_share_ref(struct smb_share *ssp); 396 void smb_share_rele(struct smb_share *ssp, struct smb_cred *scred); 397 int smb_share_get(struct smb_share *ssp, struct smb_cred *scred); 398 void smb_share_put(struct smb_share *ssp, struct smb_cred *scred); 399 int smb_share_lock(struct smb_share *ssp); 400 void smb_share_unlock(struct smb_share *ssp); 401 void smb_share_invalidate(struct smb_share *ssp); 402 int smb_share_valid(struct smb_share *ssp); 403 const char * smb_share_getpass(struct smb_share *ssp); 404 405 /* 406 * SMB protocol level functions 407 */ 408 int smb_smb_negotiate(struct smb_vc *vcp, struct smb_cred *scred); 409 int smb_smb_ssnsetup(struct smb_vc *vcp, struct smb_cred *scred); 410 int smb_smb_ssnclose(struct smb_vc *vcp, struct smb_cred *scred); 411 int smb_smb_treeconnect(struct smb_share *ssp, struct smb_cred *scred); 412 int smb_smb_treedisconnect(struct smb_share *ssp, struct smb_cred *scred); 413 int smb_read(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 414 struct smb_cred *scred); 415 int smb_write(struct smb_share *ssp, u_int16_t fid, struct uio *uio, 416 struct smb_cred *scred); 417 int smb_smb_echo(struct smb_vc *vcp, struct smb_cred *scred); 418 419 /* 420 * smbiod thread 421 */ 422 423 #define SMBIOD_EV_NEWRQ 0x0001 424 #define SMBIOD_EV_SHUTDOWN 0x0002 425 #define SMBIOD_EV_CONNECT 0x0003 426 #define SMBIOD_EV_DISCONNECT 0x0004 427 #define SMBIOD_EV_TREECONNECT 0x0005 428 #define SMBIOD_EV_MASK 0x00ff 429 #define SMBIOD_EV_SYNC 0x0100 430 #define SMBIOD_EV_PROCESSING 0x0200 431 432 struct smbiod_event { 433 int ev_type; 434 int ev_error; 435 void * ev_ident; 436 STAILQ_ENTRY(smbiod_event) ev_link; 437 }; 438 439 #define SMBIOD_SHUTDOWN 0x0001 440 441 struct smbiod { 442 int iod_id; 443 int iod_flags; 444 enum smbiod_state iod_state; 445 int iod_muxcnt; /* number of active outstanding requests */ 446 int iod_sleeptimo; 447 struct smb_vc * iod_vc; 448 struct smb_slock iod_rqlock; /* iod_rqlist, iod_muxwant */ 449 struct smb_rqhead iod_rqlist; /* list of outstanding requests */ 450 int iod_muxwant; 451 struct proc * iod_p; 452 struct thread * iod_td; 453 struct smb_cred iod_scred; 454 struct smb_slock iod_evlock; /* iod_evlist */ 455 STAILQ_HEAD(,smbiod_event) iod_evlist; 456 struct timespec iod_lastrqsent; 457 struct timespec iod_pingtimo; 458 }; 459 460 int smb_iod_init(void); 461 int smb_iod_done(void); 462 int smb_iod_create(struct smb_vc *vcp); 463 int smb_iod_destroy(struct smbiod *iod); 464 int smb_iod_request(struct smbiod *iod, int event, void *ident); 465 int smb_iod_addrq(struct smb_rq *rqp); 466 int smb_iod_waitrq(struct smb_rq *rqp); 467 int smb_iod_removerq(struct smb_rq *rqp); 468 469 #endif /* _KERNEL */ 470