libsmb2.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. /* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
  2. /*
  3. Copyright (C) 2016 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef _LIBSMB2_H_
  16. #define _LIBSMB2_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #define LIBSMB2_SHARE_ENUM_V2 1
  21. struct smb2_iovec {
  22. uint8_t *buf;
  23. size_t len;
  24. void (*free)(void *);
  25. };
  26. struct smb2_context;
  27. /*
  28. * Generic callback for completion of smb2_*_async().
  29. * command_data depends on status.
  30. */
  31. typedef void (*smb2_command_cb)(struct smb2_context *smb2, int status,
  32. void *command_data, void *cb_data);
  33. /*
  34. * callback for getting error information when errors are set
  35. * command_data depends on status.
  36. */
  37. typedef void (*smb2_error_cb)(struct smb2_context *smb2,
  38. const char *error_string);
  39. /*
  40. * callback for server accepting a new connection
  41. */
  42. typedef int (*smb2_accepted_cb)(const int fd, void *cb_data);
  43. /*
  44. * callback when a new connection is made to setup context
  45. */
  46. typedef void (*smb2_client_connection)(struct smb2_context *smb2, void *cb_data);
  47. /*
  48. * callback when a server notifies of an oplock or lease break
  49. * the type of break is determined from the stuct_size in the request
  50. * (notification) passed in. the app can set the new oplock level
  51. * or new lease state for the acknowledgement that will be sent back
  52. */
  53. struct smb2_oplock_or_lease_break_reply;
  54. typedef void (*smb2_oplock_or_lease_break_cb)(struct smb2_context *smb2,
  55. int status,
  56. struct smb2_oplock_or_lease_break_reply *rep,
  57. uint8_t *new_oplock_level,
  58. uint32_t *new_lease_state);
  59. /* Stat structure */
  60. #define SMB2_TYPE_FILE 0x00000000
  61. #define SMB2_TYPE_DIRECTORY 0x00000001
  62. #define SMB2_TYPE_LINK 0x00000002
  63. struct smb2_stat_64 {
  64. uint32_t smb2_type;
  65. uint32_t smb2_nlink;
  66. uint64_t smb2_ino;
  67. uint64_t smb2_size;
  68. uint64_t smb2_atime;
  69. uint64_t smb2_atime_nsec;
  70. uint64_t smb2_mtime;
  71. uint64_t smb2_mtime_nsec;
  72. uint64_t smb2_ctime;
  73. uint64_t smb2_ctime_nsec;
  74. uint64_t smb2_btime;
  75. uint64_t smb2_btime_nsec;
  76. };
  77. struct smb2_statvfs {
  78. uint32_t f_bsize;
  79. uint32_t f_frsize;
  80. uint64_t f_blocks;
  81. uint64_t f_bfree;
  82. uint64_t f_bavail;
  83. uint32_t f_files;
  84. uint32_t f_ffree;
  85. uint32_t f_favail;
  86. uint32_t f_fsid;
  87. uint32_t f_flag;
  88. uint32_t f_namemax;
  89. };
  90. struct smb2dirent {
  91. const char *name;
  92. struct smb2_stat_64 st;
  93. };
  94. #if defined(_WINDOWS)
  95. #ifdef __USE_WINSOCK__
  96. #include <winsock.h>
  97. #else
  98. #include <ws2tcpip.h>
  99. #include <winsock2.h>
  100. #endif
  101. #elif defined(_XBOX)
  102. #include <xtl.h>
  103. #include <winsockx.h>
  104. #endif
  105. #if defined(_WINDOWS) || defined(_XBOX)
  106. typedef SOCKET t_socket;
  107. #else
  108. typedef int t_socket;
  109. #endif
  110. /*
  111. * Create an SMB2 context.
  112. * Function returns
  113. * NULL : Failed to create a context.
  114. * *smb2 : A pointer to an smb2 context.
  115. */
  116. struct smb2_context *smb2_init_context(void);
  117. /*
  118. * Close an SMB2 context
  119. *
  120. * closes socket if open, and clears keys but leave
  121. * context allocated. the context will be destroyed
  122. * at a time later when it won't be in-use
  123. */
  124. void smb2_close_context(struct smb2_context *smb2);
  125. /*
  126. * Destroy an smb2 context.
  127. *
  128. * Any open "struct smb2fh" will automatically be freed. You can not reference
  129. * any "struct smb2fh" after the context is destroyed.
  130. * Any open "struct smb2dir" will automatically be freed. You can not reference
  131. * any "struct smb2dir" after the context is destroyed.
  132. * Any pending async commands will be aborted with -ECONNRESET.
  133. */
  134. void smb2_destroy_context(struct smb2_context *smb2);
  135. /*
  136. * Get the list of currently allocated contexts
  137. */
  138. struct smb2_context *smb2_active_contexts(void);
  139. /*
  140. * Determine of the context is currently active. This lets
  141. * code know if the context was destroyed in a callback for example
  142. */
  143. int smb2_context_active(struct smb2_context *smb2);
  144. /*
  145. * EVENT SYSTEM INTEGRATION
  146. * ========================
  147. * The following functions are used to integrate libsmb2 in an event
  148. * system.
  149. *
  150. * The simplest way is by using smb2_get_fd() and smb2_which_events()
  151. * in every loop of the event system to detect which fd to use (it can change)
  152. * and which events should be waited for.
  153. * This is very simple to use but has the drawback of the overhead having to
  154. * call these two functions for every loop.
  155. *
  156. * This is suitable for trivial apps where you roll your event system
  157. * using select() or poll().
  158. *
  159. * See for example smb2-cat-async.c for an example on how to use these
  160. * two functions in an event loop.
  161. */
  162. /*
  163. * Returns the file descriptor that libsmb2 uses.
  164. */
  165. t_socket smb2_get_fd(struct smb2_context *smb2);
  166. /*
  167. * Returns which events that we need to poll for for the smb2 file descriptor.
  168. */
  169. int smb2_which_events(struct smb2_context *smb2);
  170. /*
  171. * Returns file descriptors that libsmb2 use or is trying to connect to
  172. *
  173. * This function should be used when trying to connect with more than one
  174. * addresses in parallel, cf. rfc8305: Happy Eyeballs.
  175. *
  176. * The timeout, in ms, is valid during the socket connection step. The caller
  177. * should call smb2_service_fd() with fd = -1 when the timeout is reached.
  178. * This will trigger a new socket connection on the next resolved address. All
  179. * connecting fds will be closed when the first fd is connected. The timeout
  180. * will be -1 (infinite) once connected or if there is no next addresses to
  181. * connect to.
  182. */
  183. const t_socket *
  184. smb2_get_fds(struct smb2_context *smb2, size_t *fd_count, int *timeout);
  185. /*
  186. * A much more scalable way to use smb2_fd_event_callbacks() to register
  187. * callbacks for libsmb2 to call anytime a filedescriptor is changed or when
  188. * the events we are waiting for changes.
  189. * This way libsmb2 will do callbacks back into the application to inform
  190. * when fd or events change.
  191. *
  192. * This is suitable when you want to plug libsmb2 into a more sophisticated
  193. * eventsystem or if you use epoll() or similar.
  194. *
  195. * See for smb2-ls-async.c for a trivial example of using these callbacks.
  196. */
  197. #define SMB2_ADD_FD 0
  198. #define SMB2_DEL_FD 1
  199. typedef void (*smb2_change_fd_cb)(struct smb2_context *smb2, t_socket fd, int cmd);
  200. typedef void (*smb2_change_events_cb)(struct smb2_context *smb2, t_socket fd,
  201. int events);
  202. void smb2_fd_event_callbacks(struct smb2_context *smb2,
  203. smb2_change_fd_cb change_fd,
  204. smb2_change_events_cb change_events);
  205. /*
  206. * Called to process the events when events become available for the smb2
  207. * file descriptor.
  208. *
  209. * Returns:
  210. * 0 : Success
  211. * <0 : Unrecoverable failure. At this point the context can no longer be
  212. * used and must be freed by calling smb2_destroy_context().
  213. *
  214. */
  215. int smb2_service(struct smb2_context *smb2, int revents);
  216. /*
  217. * Called to process the events when events become available for the smb2
  218. * file descriptor.
  219. *
  220. * Behave like smb2_service() with some differences:
  221. * - must be called with a fd returned by smb2_get_fd() or smb2_get_fds(),
  222. * - passing -1 as fd will trigger a new connection attempt on the next
  223. * resolved address, cf. smb2_get_fds().
  224. *
  225. * Returns:
  226. * 0 : Success
  227. * <0 : Unrecoverable failure. At this point the context can no longer be
  228. * used and must be freed by calling smb2_destroy_context().
  229. *
  230. */
  231. int smb2_service_fd(struct smb2_context *smb2, t_socket fd, int revents);
  232. /*
  233. * Set the timeout in seconds after which a command will be aborted with
  234. * SMB2_STATUS_IO_TIMEOUT.
  235. * If you use timeouts with the async API you must make sure to call
  236. * smb2_service() at least once every second.
  237. *
  238. * Default is 0: No timeout.
  239. */
  240. void smb2_set_timeout(struct smb2_context *smb2, int seconds);
  241. /*
  242. * Set passthrough-enable. Passthrough allows command packers
  243. * and unpackers to keep the extra data on complex commands
  244. * in its on-the-wire format without any interpretation. this
  245. * is useful for proxy use cases where there might be no need
  246. * to fully parse things like query-info replies or ioctl
  247. * requests. If passthrough is not set, any command that
  248. * that is processed that can't interpret the data will fail
  249. * Most client use case will not need this
  250. *
  251. * Default is 0: no-passthrough
  252. */
  253. void smb2_set_passthrough(struct smb2_context *smb2,
  254. int passthrough);
  255. /*
  256. * Get the current passthrough setting
  257. */
  258. void smb2_get_passthrough(struct smb2_context *smb2,
  259. int *passthrough);
  260. /*
  261. * Set which version of SMB to negotiate.
  262. * Default is to let the server pick the version.
  263. */
  264. enum smb2_negotiate_version {
  265. SMB2_VERSION_ANY = 0,
  266. SMB2_VERSION_ANY2 = 2,
  267. SMB2_VERSION_ANY3 = 3,
  268. SMB2_VERSION_0202 = 0x0202,
  269. SMB2_VERSION_0210 = 0x0210,
  270. SMB2_VERSION_0300 = 0x0300,
  271. SMB2_VERSION_0302 = 0x0302,
  272. SMB2_VERSION_0311 = 0x0311,
  273. };
  274. #define SMB2_VERSION_WILDCARD 0x02FF
  275. void smb2_set_version(struct smb2_context *smb2,
  276. enum smb2_negotiate_version version);
  277. /*
  278. * Sets which version libsmb2 uses.
  279. */
  280. #define LIBSMB2_MAJOR_VERSION 4
  281. #define LIBSMB2_MINOR_VERSION 0
  282. #define LIBSMB2_PATCH_VERSION 0
  283. struct smb2_libversion
  284. {
  285. uint8_t major_version;
  286. uint8_t minor_version;
  287. uint8_t patch_version;
  288. };
  289. /*
  290. * Gets the libsmb2 version being linked while used.
  291. * This function will be available on 5.x
  292. * @param struct smb2_libversion
  293. */
  294. void smb2_get_libsmb2Version(struct smb2_libversion *smb2_ver);
  295. /*
  296. * gets the (currently) negotiated dialect
  297. */
  298. uint16_t smb2_get_dialect(struct smb2_context *smb2);
  299. /*
  300. * Set the security mode for the connection.
  301. * This is a combination of the flags SMB2_NEGOTIATE_SIGNING_ENABLED
  302. * and SMB2_NEGOTIATE_SIGNING_REQUIRED
  303. * Default is 0.
  304. */
  305. void smb2_set_security_mode(struct smb2_context *smb2, uint16_t security_mode);
  306. /*
  307. * Set whether smb3 encryption should be used or not.
  308. * 0 : disable encryption. This is the default.
  309. * !0 : enable encryption.
  310. */
  311. void smb2_set_seal(struct smb2_context *smb2, int val);
  312. /*
  313. * Set whether smb2 signing should be required or not
  314. * 0 : do not require signing. This is the default.
  315. * !0 : require signing.
  316. */
  317. void smb2_set_sign(struct smb2_context *smb2, int val);
  318. enum smb2_sec {
  319. SMB2_SEC_UNDEFINED = 0,
  320. SMB2_SEC_NTLMSSP,
  321. SMB2_SEC_KRB5,
  322. };
  323. /*
  324. * Set authentication method.
  325. * SMB2_SEC_UNDEFINED (use KRB if available or NTLM if not)
  326. * SMB2_SEC_NTLMSSP
  327. * SMB2_SEC_KRB5
  328. */
  329. void smb2_set_authentication(struct smb2_context *smb2, int val);
  330. /*
  331. * Set the username that we will try to authenticate as.
  332. * Default is to try to authenticate as the current user.
  333. */
  334. void smb2_set_user(struct smb2_context *smb2, const char *user);
  335. /*
  336. * Get the username associated with a context.
  337. * returns NULL if none
  338. */
  339. const char *smb2_get_user(struct smb2_context *smb2);
  340. /*
  341. * Set the password that we will try to authenticate as.
  342. * This function is only needed when libsmb2 is built --without-libkrb5
  343. */
  344. void smb2_set_password(struct smb2_context *smb2, const char *password);
  345. /*
  346. * Convert a win timestamp to a unix timeval
  347. */
  348. void smb2_win_to_timeval(uint64_t smb2_time, struct smb2_timeval *tv);
  349. /*
  350. * Convert unit timeval to a win timestamp
  351. */
  352. uint64_t smb2_timeval_to_win(struct smb2_timeval *tv);
  353. /*
  354. * set the context error string
  355. */
  356. void smb2_set_error(struct smb2_context *smb2,
  357. const char *error_string, ...);
  358. /*
  359. * Register an error callback, so any calls to smb2_set_error will call this
  360. * function with the error string generated
  361. */
  362. void smb2_register_error_callback(struct smb2_context *smb,
  363. smb2_error_cb error_cb);
  364. /*
  365. * register for oplock or lease break callbacks
  366. */
  367. void smb2_set_oplock_or_lease_break_callback(struct smb2_context *smb2,
  368. smb2_oplock_or_lease_break_cb cb);
  369. /*
  370. * Set the smb2 context passworkd from a file (see NTLM_USER_FILE)
  371. * depends on user/domain being already set in smb2 context
  372. */
  373. void smb2_set_password_from_file(struct smb2_context *smb2);
  374. /*
  375. * Set the domain when authenticating.
  376. * This function is only needed when libsmb2 is built --without-libkrb5
  377. */
  378. void smb2_set_domain(struct smb2_context *smb2, const char *domain);
  379. /*
  380. * Get the domain associated with a context.
  381. * returns NULL if none
  382. */
  383. const char *smb2_get_domain(struct smb2_context *smb2);
  384. /*
  385. * Set the workstation when authenticating.
  386. * This function is only needed when libsmb2 is built --without-libkrb5
  387. */
  388. void smb2_set_workstation(struct smb2_context *smb2, const char *workstation);
  389. /*
  390. * Get the workstation associated with a context.
  391. * returns NULL if none
  392. */
  393. const char *smb2_get_workstation(struct smb2_context *smb2);
  394. /*
  395. * Sets the address to some user defined object. May be used to make
  396. * additional context data available in the async callbacks.
  397. */
  398. void smb2_set_opaque(struct smb2_context *smb2, void *opaque);
  399. /*
  400. * Returns the opaque pointer set with smb2_set_opaque.
  401. */
  402. void *smb2_get_opaque(struct smb2_context *smb2);
  403. /*
  404. * copy credential handle from one context to another (and set
  405. * it NULL in source context)
  406. * returns 0 if handle transferred,
  407. * or -1 if not (no handle or no context, or not applicable)
  408. */
  409. int smb2_delegate_credentials(struct smb2_context *in, struct smb2_context *out);
  410. /*
  411. * Sets the client_guid for this context.
  412. */
  413. void smb2_set_client_guid(struct smb2_context *smb2, const uint8_t guid[SMB2_GUID_SIZE]);
  414. /*
  415. * Returns the client_guid for this context.
  416. */
  417. const char *smb2_get_client_guid(struct smb2_context *smb2);
  418. /*
  419. * Asynchronous call to connect a TCP connection to the server
  420. *
  421. * Returns:
  422. * 0 if the call was initiated and a connection will be attempted. Result of
  423. * the connection will be reported through the callback function.
  424. * <0 if there was an error. The callback function will not be invoked.
  425. *
  426. * Callback parameters :
  427. * status can be either of :
  428. * 0 : Connection was successful. Command_data is NULL.
  429. *
  430. * -errno : Failed to establish the connection. Command_data is NULL.
  431. */
  432. int smb2_connect_async(struct smb2_context *smb2, const char *server,
  433. smb2_command_cb cb, void *cb_data);
  434. /*
  435. * Async call to connect to a share.
  436. * On unix, if user is NULL then default to the current user.
  437. *
  438. * Returns:
  439. * 0 if the call was initiated and a connection will be attempted. Result of
  440. * the connection will be reported through the callback function.
  441. * -errno if there was an error. The callback function will not be invoked.
  442. *
  443. * Callback parameters :
  444. * status can be either of :
  445. * 0 : Connection was successful. Command_data is NULL.
  446. *
  447. * -errno : Failed to connect to the share. Command_data is NULL.
  448. */
  449. int smb2_connect_share_async(struct smb2_context *smb2,
  450. const char *server,
  451. const char *share,
  452. const char *user,
  453. smb2_command_cb cb, void *cb_data);
  454. /*
  455. * Sync call to connect to a share.
  456. * On unix, if user is NULL then default to the current user.
  457. *
  458. * Returns:
  459. * 0 : Connected to the share successfully.
  460. * -errno : Failure.
  461. */
  462. int smb2_connect_share(struct smb2_context *smb2,
  463. const char *server,
  464. const char *share,
  465. const char *user);
  466. /*
  467. * Async call to disconnect from a share/
  468. *
  469. * Returns:
  470. * 0 if the call was initiated and a connection will be attempted. Result of
  471. * the disconnect will be reported through the callback function.
  472. * -errno if there was an error. The callback function will not be invoked.
  473. *
  474. * Callback parameters :
  475. * status can be either of :
  476. * 0 : Connection was successful. Command_data is NULL.
  477. *
  478. * -errno : Failed to disconnect the share. Command_data is NULL.
  479. */
  480. int smb2_disconnect_share_async(struct smb2_context *smb2,
  481. smb2_command_cb cb, void *cb_data);
  482. /*
  483. * Sync call to disconnect from a share/
  484. *
  485. * Returns:
  486. * 0 : Disconnected from the share successfully.
  487. * -errno : Failure.
  488. */
  489. int smb2_disconnect_share(struct smb2_context *smb2);
  490. /*
  491. * Select a tree id that was previously connected. Sets the tree_id
  492. * in the context to be used for subsequent requests
  493. *
  494. * Returns:
  495. * 0 : OK
  496. * -errno : tree wasn't connected
  497. */
  498. int smb2_select_tree_id(struct smb2_context *smb2, uint32_t tree_id);
  499. struct smb2_pdu;
  500. /*
  501. * Get/Set the tree id of a pdu
  502. *
  503. * Returns:
  504. * 0 : OK
  505. * -errno : tree wasn't connected | no pdu | no context
  506. */
  507. int smb2_get_tree_id_for_pdu(struct smb2_context *smb2, struct smb2_pdu *pdu, uint32_t *tree_id);
  508. int smb2_set_tree_id_for_pdu(struct smb2_context *smb2, struct smb2_pdu *pdu, uint32_t tree_id);
  509. /*
  510. * Get session id
  511. *
  512. * Returns:
  513. * 0 : OK
  514. * -errno :
  515. */
  516. int smb2_get_session_id(struct smb2_context *smb2, uint64_t *session_id);
  517. /*
  518. * This function returns a description of the last encountered error.
  519. */
  520. const char *smb2_get_error(struct smb2_context *smb2);
  521. int smb2_get_nterror(struct smb2_context *smb2);
  522. struct smb2_url {
  523. const char *domain;
  524. const char *user;
  525. const char *server;
  526. const char *share;
  527. const char *path;
  528. };
  529. /* Convert an smb2/nt error code into a string */
  530. const char *nterror_to_str(uint32_t status);
  531. /* Convert an smb2/nt error code into an errno value */
  532. int nterror_to_errno(uint32_t status);
  533. /*
  534. * This function is used to parse an SMB2 URL into as smb2_url structure.
  535. * SMB2 URL format:
  536. * smb2://[<domain;][<username>@]<server>/<share>/<path>
  537. * where <server> has the format:
  538. * <host>[:<port>].
  539. *
  540. * Function will return a pointer to an iscsi smb2 structure if successful,
  541. * or it will return NULL and set smb2_get_error() accordingly if there was
  542. * a problem with the URL.
  543. *
  544. * The returned structure is freed by calling smb2_destroy_url()
  545. */
  546. struct smb2_url *smb2_parse_url(struct smb2_context *smb2, const char *url);
  547. void smb2_destroy_url(struct smb2_url *url);
  548. /*
  549. * These functions are used when creating compound low level commands.
  550. * The general pattern for compound chains is
  551. * 1, pdu = smb2_cmd_*_async(smb2, ...)
  552. *
  553. * 2, next = smb2_cmd_*_async(smb2, ...)
  554. * 3, smb2_add_compound_pdu(smb2, pdu, next);
  555. *
  556. * 4, next = smb2_cmd_*_async(smb2, ...)
  557. * 5, smb2_add_compound_pdu(smb2, pdu, next);
  558. * ...
  559. * *, smb2_queue_pdu(smb2, pdu);
  560. *
  561. * See libsmb2.c and smb2-raw-stat-async.c for examples on how to use
  562. * this interface.
  563. */
  564. void smb2_add_compound_pdu(struct smb2_context *smb2,
  565. struct smb2_pdu *pdu, struct smb2_pdu *next_pdu);
  566. void smb2_free_pdu(struct smb2_context *smb2, struct smb2_pdu *pdu);
  567. void smb2_queue_pdu(struct smb2_context *smb2, struct smb2_pdu *pdu);
  568. /*
  569. * These are used to access/modify pdus from application level
  570. * useful for proxies, etc.
  571. */
  572. struct smb2_pdu *smb2_get_compound_pdu(struct smb2_context *smb2,
  573. struct smb2_pdu *pdu);
  574. void smb2_set_pdu_status(struct smb2_context *smb2, struct smb2_pdu *pdu, int status);
  575. void smb2_set_pdu_message_id(struct smb2_context *smb2, struct smb2_pdu *pdu, uint64_t message_id);
  576. uint64_t smb2_get_pdu_message_id(struct smb2_context *smb2, struct smb2_pdu *pdu);
  577. uint64_t smb2_get_last_request_message_id(struct smb2_context *smb2);
  578. uint64_t smb2_get_last_reply_message_id(struct smb2_context *smb2);
  579. int smb2_pdu_is_compound(struct smb2_context *smb2);
  580. /*
  581. * OPENDIR
  582. */
  583. struct smb2dir;
  584. /*
  585. * Async opendir()
  586. *
  587. * Returns
  588. * pdu : The operation was initiated. Result of the operation will be reported
  589. * through the callback function.
  590. * The pdu must be freed by smb2_free_pdu().
  591. * The pdu can be cancelled before the callback is invoked by freeing
  592. * it.
  593. * NULL : There was an error. The callback function will not be invoked.
  594. *
  595. * When the callback is invoked, status indicates the result:
  596. * 0 : Success.
  597. * Command_data is struct smb2dir.
  598. * This structure is freed using smb2_closedir().
  599. * -errno : An error occurred.
  600. * Command_data is NULL.
  601. */
  602. struct smb2_pdu *
  603. smb2_opendir_async_pdu(struct smb2_context *smb2, const char *path,
  604. smb2_command_cb cb, void *cb_data, void (*free_cb)(void *));
  605. /*
  606. * Sync opendir()
  607. *
  608. * Returns NULL on failure.
  609. */
  610. struct smb2dir *smb2_opendir(struct smb2_context *smb2, const char *path);
  611. int smb2_opendir_async(struct smb2_context *smb2, const char *path,
  612. smb2_command_cb cb, void *cb_data);
  613. /*
  614. * closedir()
  615. */
  616. /*
  617. * smb2_closedir() never blocks, thus no async version is needed.
  618. */
  619. void smb2_closedir(struct smb2_context *smb2, struct smb2dir *smb2dir);
  620. /*
  621. * readdir()
  622. */
  623. /*
  624. * smb2_readdir() never blocks, thus no async version is needed.
  625. */
  626. struct smb2dirent *smb2_readdir(struct smb2_context *smb2,
  627. struct smb2dir *smb2dir);
  628. /*
  629. * rewinddir()
  630. */
  631. /*
  632. * smb2_rewinddir() never blocks, thus no async version is needed.
  633. */
  634. void smb2_rewinddir(struct smb2_context *smb2, struct smb2dir *smb2dir);
  635. /*
  636. * telldir()
  637. */
  638. /*
  639. * smb2_telldir() never blocks, thus no async version is needed.
  640. */
  641. long smb2_telldir(struct smb2_context *smb2, struct smb2dir *smb2dir);
  642. /*
  643. * seekdir()
  644. */
  645. /*
  646. * smb2_seekdir() never blocks, thus no async version is needed.
  647. */
  648. void smb2_seekdir(struct smb2_context *smb2, struct smb2dir *smb2dir,
  649. long loc);
  650. /*
  651. * OPEN
  652. */
  653. struct smb2fh;
  654. /*
  655. * Async open()
  656. *
  657. * Returns
  658. * pdu : The operation was initiated. Result of the operation will be reported
  659. * through the callback function.
  660. * The pdu must be freed by smb2_free_pdu().
  661. * The pdu can be cancelled before the callback is invoked by freeing
  662. * it.
  663. * NULL : There was an error. The callback function will not be invoked.
  664. *
  665. * Opens or creates a file.
  666. * Supported flags are:
  667. * O_RDONLY
  668. * O_WRONLY
  669. * O_RDWR
  670. * O_SYNC
  671. * O_CREAT
  672. * O_EXCL
  673. *
  674. * When the callback is invoked, status indicates the result:
  675. * 0 : Success.
  676. * Command_data is struct smb2fh.
  677. * This structure is freed using smb2_close().
  678. * -errno : An error occurred.
  679. * Command_data is NULL.
  680. */
  681. struct smb2_pdu *
  682. smb2_open_async_pdu(struct smb2_context *smb2, const char *path, int flags,
  683. smb2_command_cb cb, void *cb_data, void (*free_cb)(void *));
  684. /*
  685. * Returns
  686. * 0 : The operation was initiated. Result of the operation will be
  687. * reported through the callback function.
  688. * -errno : There was an error. The callback function will not be invoked.
  689. *
  690. */
  691. int smb2_open_async_with_oplock_or_lease(struct smb2_context *smb2, const char *path, int flags,
  692. uint8_t oplock_level, uint32_t lease_state, smb2_lease_key lease_key,
  693. smb2_command_cb cb, void *cb_data);
  694. int smb2_open_async(struct smb2_context *smb2, const char *path, int flags,
  695. smb2_command_cb cb, void *cb_data);
  696. /*
  697. * Sync open()
  698. *
  699. * Returns NULL on failure.
  700. */
  701. struct smb2fh *smb2_open(struct smb2_context *smb2, const char *path, int flags);
  702. /*
  703. * CLOSE
  704. */
  705. /*
  706. * Async close()
  707. *
  708. * Returns
  709. * 0 : The operation was initiated. Result of the operation will be
  710. * reported through the callback function.
  711. * -errno : There was an error. The callback function will not be invoked.
  712. *
  713. * When the callback is invoked, status indicates the result:
  714. * 0 : Success.
  715. * -errno : An error occurred.
  716. *
  717. * Command_data is always NULL.
  718. */
  719. int smb2_close_async(struct smb2_context *smb2, struct smb2fh *fh,
  720. smb2_command_cb cb, void *cb_data);
  721. /*
  722. * Sync close()
  723. */
  724. int smb2_close(struct smb2_context *smb2, struct smb2fh *fh);
  725. /*
  726. * FSYNC
  727. */
  728. /*
  729. * Async fsync()
  730. *
  731. * Returns
  732. * 0 : The operation was initiated. Result of the operation will be
  733. * reported through the callback function.
  734. * -errno : There was an error. The callback function will not be invoked.
  735. *
  736. * When the callback is invoked, status indicates the result:
  737. * 0 : Success.
  738. * -errno : An error occurred.
  739. *
  740. * Command_data is always NULL.
  741. */
  742. int smb2_fsync_async(struct smb2_context *smb2, struct smb2fh *fh,
  743. smb2_command_cb cb, void *cb_data);
  744. /*
  745. * Sync fsync()
  746. */
  747. int smb2_fsync(struct smb2_context *smb2, struct smb2fh *fh);
  748. /*
  749. * GetMaxReadWriteSize
  750. * SMB2 servers have a maximum size for read/write data that they support.
  751. */
  752. uint32_t smb2_get_max_read_size(struct smb2_context *smb2);
  753. uint32_t smb2_get_max_write_size(struct smb2_context *smb2);
  754. struct smb2_read_cb_data {
  755. struct smb2fh *fh;
  756. uint8_t *buf;
  757. uint32_t count;
  758. uint64_t offset;
  759. };
  760. struct smb2_write_cb_data {
  761. struct smb2fh *fh;
  762. const uint8_t *buf;
  763. uint32_t count;
  764. uint64_t offset;
  765. };
  766. /*
  767. * PREAD
  768. */
  769. /*
  770. * Async pread()
  771. * Use smb2_get_max_read_size to discover the maximum data size that the
  772. * server supports.
  773. *
  774. * Returns
  775. * 0 : The operation was initiated. Result of the operation will be
  776. * reported through the callback function.
  777. * -errno : There was an error. The callback function will not be invoked.
  778. *
  779. * When the callback is invoked, status indicates the result:
  780. * >=0 : Number of bytes read.
  781. * -errno : An error occurred.
  782. *
  783. * Command_data is struct smb2_read_cb_data, which holds the arguments
  784. * that were given to smb2_pread_async.
  785. * This structure is automatically freed.
  786. */
  787. int smb2_pread_async(struct smb2_context *smb2, struct smb2fh *fh,
  788. uint8_t *buf, uint32_t count, uint64_t offset,
  789. smb2_command_cb cb, void *cb_data);
  790. /*
  791. * Sync pread()
  792. * Use smb2_get_max_read_size to discover the maximum data size that the
  793. * server supports.
  794. */
  795. int smb2_pread(struct smb2_context *smb2, struct smb2fh *fh,
  796. uint8_t *buf, uint32_t count, uint64_t offset);
  797. /*
  798. * PWRITE
  799. */
  800. /*
  801. * Async pwrite()
  802. * Use smb2_get_max_write_size to discover the maximum data size that the
  803. * server supports.
  804. *
  805. * Returns
  806. * 0 : The operation was initiated. Result of the operation will be
  807. * reported through the callback function.
  808. * -errno : There was an error. The callback function will not be invoked.
  809. *
  810. * When the callback is invoked, status indicates the result:
  811. * >=0 : Number of bytes written.
  812. * -errno : An error occurred.
  813. *
  814. * Command_data is struct smb2_write_cb_data, which holds the arguments
  815. * that were given to smb2_pwrite_async.
  816. * This structure is automatically freed.
  817. */
  818. int smb2_pwrite_async(struct smb2_context *smb2, struct smb2fh *fh,
  819. const uint8_t *buf, uint32_t count, uint64_t offset,
  820. smb2_command_cb cb, void *cb_data);
  821. /*
  822. * Sync pwrite()
  823. * Use smb2_get_max_write_size to discover the maximum data size that the
  824. * server supports.
  825. */
  826. int smb2_pwrite(struct smb2_context *smb2, struct smb2fh *fh,
  827. const uint8_t *buf, uint32_t count, uint64_t offset);
  828. /*
  829. * READ
  830. */
  831. /*
  832. * Async read()
  833. *
  834. * Returns
  835. * 0 : The operation was initiated. Result of the operation will be
  836. * reported through the callback function.
  837. * -errno : There was an error. The callback function will not be invoked.
  838. *
  839. * When the callback is invoked, status indicates the result:
  840. * >=0 : Number of bytes read.
  841. * -errno : An error occurred.
  842. *
  843. * Command_data is struct smb2_read_cb_data, which holds the arguments
  844. * that were given to smb2_read_async. offset denotes the offset in the file
  845. * at which the read took place.
  846. * This structure is automatically freed.
  847. */
  848. int smb2_read_async(struct smb2_context *smb2, struct smb2fh *fh,
  849. uint8_t *buf, uint32_t count,
  850. smb2_command_cb cb, void *cb_data);
  851. /*
  852. * Sync read()
  853. */
  854. int smb2_read(struct smb2_context *smb2, struct smb2fh *fh,
  855. uint8_t *buf, uint32_t count);
  856. /*
  857. * WRITE
  858. */
  859. /*
  860. * Async write()
  861. *
  862. * Returns
  863. * 0 : The operation was initiated. Result of the operation will be
  864. * reported through the callback function.
  865. * -errno : There was an error. The callback function will not be invoked.
  866. *
  867. * When the callback is invoked, status indicates the result:
  868. * >=0 : Number of bytes written.
  869. * -errno : An error occurred.
  870. *
  871. * Command_data is struct smb2_write_cb_data, which holds the arguments
  872. * that were given to smb2_write_async. offset denotes the offset in the file
  873. * at which the write took place.
  874. * This structure is automatically freed.
  875. */
  876. int smb2_write_async(struct smb2_context *smb2, struct smb2fh *fh,
  877. const uint8_t *buf, uint32_t count,
  878. smb2_command_cb cb, void *cb_data);
  879. /*
  880. * Sync write()
  881. */
  882. int smb2_write(struct smb2_context *smb2, struct smb2fh *fh,
  883. const uint8_t *buf, uint32_t count);
  884. /*
  885. * Sync lseek()
  886. */
  887. /*
  888. * smb2_seek() SEEK_SET and SEEK_CUR are fully supported.
  889. * SEEK_END only returns the end-of-file from the original open.
  890. * (it will not call fstat to discover the current file size and will not block)
  891. */
  892. int64_t smb2_lseek(struct smb2_context *smb2, struct smb2fh *fh,
  893. int64_t offset, int whence, uint64_t *current_offset);
  894. /*
  895. * UNLINK
  896. */
  897. /*
  898. * Async unlink()
  899. *
  900. * Returns
  901. * 0 : The operation was initiated. Result of the operation will be
  902. * reported through the callback function.
  903. * -errno : There was an error. The callback function will not be invoked.
  904. *
  905. * When the callback is invoked, status indicates the result:
  906. * 0 : Success.
  907. * -errno : An error occurred.
  908. *
  909. * Command_data is always NULL.
  910. */
  911. int smb2_unlink_async(struct smb2_context *smb2, const char *path,
  912. smb2_command_cb cb, void *cb_data);
  913. /*
  914. * Sync unlink()
  915. */
  916. int smb2_unlink(struct smb2_context *smb2, const char *path);
  917. /*
  918. * RMDIR
  919. */
  920. /*
  921. * Async rmdir()
  922. *
  923. * Returns
  924. * 0 : The operation was initiated. Result of the operation will be
  925. * reported through the callback function.
  926. * -errno : There was an error. The callback function will not be invoked.
  927. *
  928. * When the callback is invoked, status indicates the result:
  929. * 0 : Success.
  930. * -errno : An error occurred.
  931. *
  932. * Command_data is always NULL.
  933. */
  934. int smb2_rmdir_async(struct smb2_context *smb2, const char *path,
  935. smb2_command_cb cb, void *cb_data);
  936. /*
  937. * Sync rmdir()
  938. */
  939. int smb2_rmdir(struct smb2_context *smb2, const char *path);
  940. /*
  941. * MKDIR
  942. */
  943. /*
  944. * Async mkdir()
  945. *
  946. * Returns
  947. * 0 : The operation was initiated. Result of the operation will be
  948. * reported through the callback function.
  949. * -errno : There was an error. The callback function will not be invoked.
  950. *
  951. * When the callback is invoked, status indicates the result:
  952. * 0 : Success.
  953. * -errno : An error occurred.
  954. *
  955. * Command_data is always NULL.
  956. */
  957. int smb2_mkdir_async(struct smb2_context *smb2, const char *path,
  958. smb2_command_cb cb, void *cb_data);
  959. /*
  960. * Sync mkdir()
  961. */
  962. int smb2_mkdir(struct smb2_context *smb2, const char *path);
  963. /*
  964. * STATVFS
  965. */
  966. /*
  967. * Async statvfs()
  968. *
  969. * Returns
  970. * 0 : The operation was initiated. Result of the operation will be
  971. * reported through the callback function.
  972. * -errno : There was an error. The callback function will not be invoked.
  973. *
  974. * When the callback is invoked, status indicates the result:
  975. * 0 : Success. Command_data is struct smb2_statvfs
  976. * -errno : An error occurred.
  977. */
  978. int smb2_statvfs_async(struct smb2_context *smb2, const char *path,
  979. struct smb2_statvfs *statvfs,
  980. smb2_command_cb cb, void *cb_data);
  981. /*
  982. * Sync statvfs()
  983. */
  984. int smb2_statvfs(struct smb2_context *smb2, const char *path,
  985. struct smb2_statvfs *statvfs);
  986. /*
  987. * FSTAT
  988. */
  989. /*
  990. * Async fstat()
  991. *
  992. * Returns
  993. * 0 : The operation was initiated. Result of the operation will be
  994. * reported through the callback function.
  995. * -errno : There was an error. The callback function will not be invoked.
  996. *
  997. * When the callback is invoked, status indicates the result:
  998. * 0 : Success. Command_data is struct smb2_stat_64
  999. * -errno : An error occurred.
  1000. */
  1001. int smb2_fstat_async(struct smb2_context *smb2, struct smb2fh *fh,
  1002. struct smb2_stat_64 *st,
  1003. smb2_command_cb cb, void *cb_data);
  1004. /*
  1005. * Sync fstat()
  1006. */
  1007. int smb2_fstat(struct smb2_context *smb2, struct smb2fh *fh,
  1008. struct smb2_stat_64 *st);
  1009. /*
  1010. * Async stat()
  1011. *
  1012. * Returns
  1013. * 0 : The operation was initiated. Result of the operation will be
  1014. * reported through the callback function.
  1015. * -errno : There was an error. The callback function will not be invoked.
  1016. *
  1017. * When the callback is invoked, status indicates the result:
  1018. * 0 : Success. Command_data is struct smb2_stat_64
  1019. * -errno : An error occurred.
  1020. */
  1021. int smb2_stat_async(struct smb2_context *smb2, const char *path,
  1022. struct smb2_stat_64 *st,
  1023. smb2_command_cb cb, void *cb_data);
  1024. /*
  1025. * Sync stat()
  1026. */
  1027. int smb2_stat(struct smb2_context *smb2, const char *path,
  1028. struct smb2_stat_64 *st);
  1029. /*
  1030. * Async rename()
  1031. *
  1032. * Returns
  1033. * 0 : The operation was initiated. Result of the operation will be
  1034. * reported through the callback function.
  1035. * -errno : There was an error. The callback function will not be invoked.
  1036. *
  1037. * When the callback is invoked, status indicates the result:
  1038. * 0 : Success.
  1039. * -errno : An error occurred.
  1040. */
  1041. int smb2_rename_async(struct smb2_context *smb2, const char *oldpath,
  1042. const char *newpath, smb2_command_cb cb, void *cb_data);
  1043. /*
  1044. * Sync rename()
  1045. */
  1046. int smb2_rename(struct smb2_context *smb2, const char *oldpath,
  1047. const char *newpath);
  1048. /*
  1049. * Async truncate()
  1050. *
  1051. * Returns
  1052. * 0 : The operation was initiated. Result of the operation will be
  1053. * reported through the callback function.
  1054. * -errno : There was an error. The callback function will not be invoked.
  1055. *
  1056. * When the callback is invoked, status indicates the result:
  1057. * 0 : Success.
  1058. * -errno : An error occurred.
  1059. */
  1060. int smb2_truncate_async(struct smb2_context *smb2, const char *path,
  1061. uint64_t length, smb2_command_cb cb, void *cb_data);
  1062. /*
  1063. * Sync truncate()
  1064. * Function returns
  1065. * 0 : Success
  1066. * -errno : An error occurred.
  1067. */
  1068. int smb2_truncate(struct smb2_context *smb2, const char *path,
  1069. uint64_t length);
  1070. /*
  1071. * Async ftruncate()
  1072. *
  1073. * Returns
  1074. * 0 : The operation was initiated. Result of the operation will be
  1075. * reported through the callback function.
  1076. * -errno : There was an error. The callback function will not be invoked.
  1077. *
  1078. * When the callback is invoked, status indicates the result:
  1079. * 0 : Success.
  1080. * -errno : An error occurred.
  1081. */
  1082. int smb2_ftruncate_async(struct smb2_context *smb2, struct smb2fh *fh,
  1083. uint64_t length, smb2_command_cb cb, void *cb_data);
  1084. /*
  1085. * Sync ftruncate()
  1086. * Function returns
  1087. * 0 : Success
  1088. * -errno : An error occurred.
  1089. */
  1090. int smb2_ftruncate(struct smb2_context *smb2, struct smb2fh *fh,
  1091. uint64_t length);
  1092. /*
  1093. * READLINK
  1094. */
  1095. /*
  1096. * Async readlink()
  1097. *
  1098. * Returns
  1099. * 0 : The operation was initiated. The link content will be
  1100. * reported through the callback function.
  1101. * -errno : There was an error. The callback function will not be invoked.
  1102. *
  1103. * When the callback is invoked, status indicates the result:
  1104. * 0 : Success. Command_data is the link content.
  1105. * -errno : An error occurred.
  1106. */
  1107. int smb2_readlink_async(struct smb2_context *smb2, const char *path,
  1108. smb2_command_cb cb, void *cb_data);
  1109. /*
  1110. * Sync readlink()
  1111. */
  1112. int smb2_readlink(struct smb2_context *smb2, const char *path, char *buf, uint32_t bufsiz);
  1113. /*
  1114. * Async echo()
  1115. *
  1116. * Returns
  1117. * 0 : The operation was initiated. Result of the operation will be
  1118. * reported through the callback function.
  1119. * -errno : There was an error. The callback function will not be invoked.
  1120. *
  1121. * When the callback is invoked, status indicates the result:
  1122. * 0 : Success.
  1123. * -errno : An error occurred.
  1124. */
  1125. int smb2_echo_async(struct smb2_context *smb2,
  1126. smb2_command_cb cb, void *cb_data);
  1127. /*
  1128. * Sync echo()
  1129. *
  1130. * Returns:
  1131. * 0 : successfully send the message and received a reply.
  1132. * -errno : Failure.
  1133. */
  1134. int smb2_echo(struct smb2_context *smb2);
  1135. void
  1136. free_smb2_file_notify_change_information(struct smb2_context *smb2, struct smb2_file_notify_change_information *fnc);
  1137. int smb2_notify_change_async(struct smb2_context *smb2, const char *path, uint16_t flags, uint32_t filter, int loop,
  1138. smb2_command_cb cb, void *cb_data);
  1139. int smb2_notify_change_filehandle_async(struct smb2_context *smb2, struct smb2fh *smb2_dir_fh, uint16_t flags, uint32_t filter, int loop,
  1140. smb2_command_cb cb, void *cb_data);
  1141. /*
  1142. * Sync notify_change()
  1143. *
  1144. */
  1145. struct smb2_file_notify_change_information *smb2_notify_change(struct smb2_context *smb2, const char *path, uint16_t flags, uint32_t filter);
  1146. /* Utilities that help by being public
  1147. */
  1148. /* SMB's UTF-16 is always in Little Endian */
  1149. struct smb2_utf16 {
  1150. int len;
  1151. uint16_t val[1];
  1152. };
  1153. /* Returns a string converted to UTF-16 format. Use free() to release
  1154. * the utf16 string.
  1155. */
  1156. struct smb2_utf16 *smb2_utf8_to_utf16(const char *utf8);
  1157. /* Returns a string converted to UTF8 format. Use free() to release
  1158. * the utf8 string.
  1159. */
  1160. const char *smb2_utf16_to_utf8(const uint16_t *str, size_t len);
  1161. /************* Server-side API **********************************************/
  1162. struct smb2_server;
  1163. /* pdu handlers in general take the request from the client, and return
  1164. * < 0 on error, and the library should create an error reply
  1165. * == 0 on OK, and the library should use the reply struct (if needed) to create a reply
  1166. * > 0 if the handler created and queued a reply itself
  1167. */
  1168. struct smb2_server_request_handlers {
  1169. int (*destruction_event)(struct smb2_server *srvr, struct smb2_context *smb2);
  1170. int (*authorize_user)(struct smb2_server *srvr, struct smb2_context *smb2,
  1171. const char *user,
  1172. const char *domain,
  1173. const char *workstation);
  1174. int (*session_established)(struct smb2_server *srvr, struct smb2_context *smb2);
  1175. int (*logoff_cmd)(struct smb2_server *srvr, struct smb2_context *smb2);
  1176. int (*tree_connect_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1177. struct smb2_tree_connect_request *req,
  1178. struct smb2_tree_connect_reply *rep);
  1179. int (*tree_disconnect_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1180. const uint32_t tree_id);
  1181. int (*create_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1182. struct smb2_create_request *req,
  1183. struct smb2_create_reply *rep);
  1184. int (*close_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1185. struct smb2_close_request *req,
  1186. struct smb2_close_reply *rep);
  1187. int (*flush_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1188. struct smb2_flush_request *req);
  1189. int (*read_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1190. struct smb2_read_request *req,
  1191. struct smb2_read_reply *rep);
  1192. int (*write_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1193. struct smb2_write_request *req,
  1194. struct smb2_write_reply *rep);
  1195. int (*oplock_break_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1196. struct smb2_oplock_break_acknowledgement *req);
  1197. int (*lease_break_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1198. struct smb2_lease_break_acknowledgement *req);
  1199. int (*lock_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1200. struct smb2_lock_request *req);
  1201. int (*ioctl_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1202. struct smb2_ioctl_request *req,
  1203. struct smb2_ioctl_reply *rep);
  1204. int (*cancel_cmd)(struct smb2_server *srvr, struct smb2_context *smb2);
  1205. int (*echo_cmd)(struct smb2_server *srvr, struct smb2_context *smb2);
  1206. int (*query_directory_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1207. struct smb2_query_directory_request *req,
  1208. struct smb2_query_directory_reply *rep);
  1209. int (*change_notify_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1210. struct smb2_change_notify_request *req,
  1211. struct smb2_change_notify_reply *rep);
  1212. int (*query_info_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1213. struct smb2_query_info_request *req,
  1214. struct smb2_query_info_reply *rep);
  1215. int (*set_info_cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1216. struct smb2_set_info_request *req);
  1217. /*
  1218. int (*oplock_break cmd)(struct smb2_server *srvr, struct smb2_context *smb2,
  1219. struct smb2_oplock_break_request *req);
  1220. */
  1221. };
  1222. struct smb2_server {
  1223. uint8_t guid[16];
  1224. char hostname[128];
  1225. char domain[128];
  1226. int fd;
  1227. uint16_t port;
  1228. uint64_t session_counter;
  1229. struct smb2_server_request_handlers *handlers;
  1230. uint32_t max_transact_size;
  1231. uint32_t max_read_size;
  1232. uint32_t max_write_size;
  1233. int signing_enabled;
  1234. int allow_anonymous;
  1235. /* this can be set non-0 to delegate client authentication to
  1236. * another client and allow any authentication to this server */
  1237. int proxy_authentication;
  1238. /* saved from negotiate to be used in validate negotiate info */
  1239. uint32_t capabilities;
  1240. uint32_t security_mode;
  1241. /* for kerberos, credential context */
  1242. char keytab_path[256];
  1243. char error[128];
  1244. void *auth_data;
  1245. };
  1246. int smb2_bind_and_listen(const uint16_t port, const int max_connections, int *out_fd);
  1247. int smb2_accept_connection_async(const int fd, const int to_msecs, smb2_accepted_cb cb, void *cb_data);
  1248. int smb2_serve_port_async(const int fd, const int to_msecs, struct smb2_context **out_smb2);
  1249. /*
  1250. * Sync serve port()
  1251. *
  1252. * Returns
  1253. * 0 : The server is complete by exiting its loop normally (shouldnt happen)
  1254. * -errno : There was an error causing server loop to exit
  1255. */
  1256. int smb2_serve_port(struct smb2_server *server, const int max_connections, smb2_client_connection cb, void *cb_data);
  1257. /*
  1258. * Some symbols have moved over to a different header file to allow better
  1259. * separation between dcerpc and smb2, so we need to include this header
  1260. * here to retain compatibility for apps that depend on those symbols.
  1261. */
  1262. #include <smb2/libsmb2-dcerpc-srvsvc.h>
  1263. #ifdef __cplusplus
  1264. }
  1265. #endif
  1266. #endif /* !_LIBSMB2_H_ */