1 /* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
3 Copyright (C) 2016 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
39 * Generic callback for completion of smb2_*_async().
40 * command_data depends on status.
42 typedef void (*smb2_command_cb)(struct smb2_context *smb2, int status,
43 void *command_data, void *cb_data);
46 #define SMB2_TYPE_FILE 0x00000000
47 #define SMB2_TYPE_DIRECTORY 0x00000001
48 #define SMB2_TYPE_LINK 0x00000002
55 uint64_t smb2_atime_nsec;
57 uint64_t smb2_mtime_nsec;
59 uint64_t smb2_ctime_nsec;
61 uint64_t smb2_btime_nsec;
80 struct smb2_stat_64 st;
85 typedef SOCKET t_socket;
91 * Create an SMB2 context.
93 * NULL : Failed to create a context.
94 * *smb2 : A pointer to an smb2 context.
96 struct smb2_context *smb2_init_context(void);
99 * Destroy an smb2 context.
101 * Any open "struct smb2fh" will automatically be freed. You can not reference
102 * any "struct smb2fh" after the context is destroyed.
103 * Any open "struct smb2dir" will automatically be freed. You can not reference
104 * any "struct smb2dir" after the context is destroyed.
105 * Any pending async commands will be aborted with -ECONNRESET.
107 void smb2_destroy_context(struct smb2_context *smb2);
110 * The following three functions are used to integrate libsmb2 in an event
114 * Returns the file descriptor that libsmb2 uses.
116 t_socket smb2_get_fd(struct smb2_context *smb2);
118 * Returns which events that we need to poll for for the smb2 file descriptor.
120 int smb2_which_events(struct smb2_context *smb2);
122 * Called to process the events when events become available for the smb2
127 * <0 : Unrecoverable failure. At this point the context can no longer be
128 * used and must be freed by calling smb2_destroy_context().
131 int smb2_service(struct smb2_context *smb2, int revents);
134 * Set the security mode for the connection.
135 * This is a combination of the flags SMB2_NEGOTIATE_SIGNING_ENABLED
136 * and SMB2_NEGOTIATE_SIGNING_REQUIRED
139 void smb2_set_security_mode(struct smb2_context *smb2, uint16_t security_mode);
142 * Set whether smb3 encryption should be used or not.
143 * 0 : disable encryption. This is the default.
144 * !0 : enable encryption.
146 void smb2_set_seal(struct smb2_context *smb2, int val);
149 * Set authentication method.
150 * SMB2_SEC_UNDEFINED (use KRB if available or NTLM if not)
154 void smb2_set_authentication(struct smb2_context *smb2, int val);
157 * Set the username that we will try to authenticate as.
158 * Default is to try to authenticate as the current user.
160 void smb2_set_user(struct smb2_context *smb2, const char *user);
162 * Set the password that we will try to authenticate as.
163 * This function is only needed when libsmb2 is built --without-libkrb5
165 void smb2_set_password(struct smb2_context *smb2, const char *password);
167 * Set the domain when authenticating.
168 * This function is only needed when libsmb2 is built --without-libkrb5
170 void smb2_set_domain(struct smb2_context *smb2, const char *domain);
172 * Set the workstation when authenticating.
173 * This function is only needed when libsmb2 is built --without-libkrb5
175 void smb2_set_workstation(struct smb2_context *smb2, const char *workstation);
179 * Returns the client_guid for this context.
181 const char *smb2_get_client_guid(struct smb2_context *smb2);
184 * Asynchronous call to connect a TCP connection to the server
187 * 0 if the call was initiated and a connection will be attempted. Result of
188 * the connection will be reported through the callback function.
189 * <0 if there was an error. The callback function will not be invoked.
191 * Callback parameters :
192 * status can be either of :
193 * 0 : Connection was successful. Command_data is NULL.
195 * <0 : Failed to establish the connection. Command_data is NULL.
197 int smb2_connect_async(struct smb2_context *smb2, const char *server,
198 smb2_command_cb cb, void *cb_data);
201 * Async call to connect to a share.
202 * On unix, if user is NULL then default to the current user.
205 * 0 if the call was initiated and a connection will be attempted. Result of
206 * the connection will be reported through the callback function.
207 * -errno if there was an error. The callback function will not be invoked.
209 * Callback parameters :
210 * status can be either of :
211 * 0 : Connection was successful. Command_data is NULL.
213 * -errno : Failed to connect to the share. Command_data is NULL.
215 int smb2_connect_share_async(struct smb2_context *smb2,
219 smb2_command_cb cb, void *cb_data);
222 * Sync call to connect to a share.
223 * On unix, if user is NULL then default to the current user.
226 * 0 : Connected to the share successfully.
229 int smb2_connect_share(struct smb2_context *smb2,
235 * Async call to disconnect from a share/
238 * 0 if the call was initiated and a connection will be attempted. Result of
239 * the disconnect will be reported through the callback function.
240 * -errno if there was an error. The callback function will not be invoked.
242 * Callback parameters :
243 * status can be either of :
244 * 0 : Connection was successful. Command_data is NULL.
246 * -errno : Failed to disconnect the share. Command_data is NULL.
248 int smb2_disconnect_share_async(struct smb2_context *smb2,
249 smb2_command_cb cb, void *cb_data);
252 * Sync call to disconnect from a share/
255 * 0 : Disconnected from the share successfully.
258 int smb2_disconnect_share(struct smb2_context *smb2);
261 * This function returns a description of the last encountered error.
263 const char *smb2_get_error(struct smb2_context *smb2);
273 /* Convert an smb2/nt error code into a string */
274 const char *nterror_to_str(uint32_t status);
276 /* Convert an smb2/nt error code into an errno value */
277 int nterror_to_errno(uint32_t status);
280 * This function is used to parse an SMB2 URL into as smb2_url structure.
282 * smb2://[<domain;][<username>@]<server>/<share>/<path>
283 * where <server> has the format:
286 * Function will return a pointer to an iscsi smb2 structure if successful,
287 * or it will return NULL and set smb2_get_error() accordingly if there was
288 * a problem with the URL.
290 * The returned structure is freed by calling smb2_destroy_url()
292 struct smb2_url *smb2_parse_url(struct smb2_context *smb2, const char *url);
293 void smb2_destroy_url(struct smb2_url *url);
297 * The functions are used when creating compound low level commands.
298 * The general pattern for compound chains is
299 * 1, pdu = smb2_cmd_*_async(smb2, ...)
301 * 2, next = smb2_cmd_*_async(smb2, ...)
302 * 3, smb2_add_compound_pdu(smb2, pdu, next);
304 * 4, next = smb2_cmd_*_async(smb2, ...)
305 * 5, smb2_add_compound_pdu(smb2, pdu, next);
307 * *, smb2_queue_pdu(smb2, pdu);
309 * See libsmb2.c and smb2-raw-stat-async.c for examples on how to use
312 void smb2_add_compound_pdu(struct smb2_context *smb2,
313 struct smb2_pdu *pdu, struct smb2_pdu *next_pdu);
314 void smb2_free_pdu(struct smb2_context *smb2, struct smb2_pdu *pdu);
315 void smb2_queue_pdu(struct smb2_context *smb2, struct smb2_pdu *pdu);
325 * 0 : The operation was initiated. Result of the operation will be reported
326 * through the callback function.
327 * <0 : There was an error. The callback function will not be invoked.
329 * When the callback is invoked, status indicates the result:
331 * Command_data is struct smb2dir.
332 * This structure is freed using smb2_closedir().
333 * -errno : An error occured.
334 * Command_data is NULL.
336 int smb2_opendir_async(struct smb2_context *smb2, const char *path,
337 smb2_command_cb cb, void *cb_data);
342 * Returns NULL on failure.
344 struct smb2dir *smb2_opendir(struct smb2_context *smb2, const char *path);
350 * smb2_closedir() never blocks, thus no async version is needed.
352 void smb2_closedir(struct smb2_context *smb2, struct smb2dir *smb2dir);
358 * smb2_readdir() never blocks, thus no async version is needed.
360 struct smb2dirent *smb2_readdir(struct smb2_context *smb2,
361 struct smb2dir *smb2dir);
367 * smb2_rewinddir() never blocks, thus no async version is needed.
369 void smb2_rewinddir(struct smb2_context *smb2, struct smb2dir *smb2dir);
375 * smb2_telldir() never blocks, thus no async version is needed.
377 long smb2_telldir(struct smb2_context *smb2, struct smb2dir *smb2dir);
383 * smb2_seekdir() never blocks, thus no async version is needed.
385 void smb2_seekdir(struct smb2_context *smb2, struct smb2dir *smb2dir,
395 * Opens or creates a file.
396 * Supported flags are:
405 * 0 : The operation was initiated. Result of the operation will be
406 * reported through the callback function.
407 * -errno : There was an error. The callback function will not be invoked.
409 * When the callback is invoked, status indicates the result:
411 * Command_data is struct smb2fh.
412 * This structure is freed using smb2_close().
413 * -errno : An error occured.
414 * Command_data is NULL.
416 int smb2_open_async(struct smb2_context *smb2, const char *path, int flags,
417 smb2_command_cb cb, void *cb_data);
422 * Returns NULL on failure.
424 struct smb2fh *smb2_open(struct smb2_context *smb2, const char *path, int flags);
433 * 0 : The operation was initiated. Result of the operation will be
434 * reported through the callback function.
435 * -errno : There was an error. The callback function will not be invoked.
437 * When the callback is invoked, status indicates the result:
439 * -errno : An error occured.
441 * Command_data is always NULL.
443 int smb2_close_async(struct smb2_context *smb2, struct smb2fh *fh,
444 smb2_command_cb cb, void *cb_data);
449 int smb2_close(struct smb2_context *smb2, struct smb2fh *fh);
458 * 0 : The operation was initiated. Result of the operation will be
459 * reported through the callback function.
460 * -errno : There was an error. The callback function will not be invoked.
462 * When the callback is invoked, status indicates the result:
464 * -errno : An error occured.
466 * Command_data is always NULL.
468 int smb2_fsync_async(struct smb2_context *smb2, struct smb2fh *fh,
469 smb2_command_cb cb, void *cb_data);
474 int smb2_fsync(struct smb2_context *smb2, struct smb2fh *fh);
477 * GetMaxReadWriteSize
478 * SMB2 servers have a maximum size for read/write data that they support.
480 uint32_t smb2_get_max_read_size(struct smb2_context *smb2);
481 uint32_t smb2_get_max_write_size(struct smb2_context *smb2);
488 * Use smb2_get_max_read_size to discover the maximum data size that the
492 * 0 : The operation was initiated. Result of the operation will be
493 * reported through the callback function.
494 * -errno : There was an error. The callback function will not be invoked.
496 * When the callback is invoked, status indicates the result:
497 * >=0 : Number of bytes read.
498 * -errno : An error occured.
500 * Command_data is always NULL.
502 int smb2_pread_async(struct smb2_context *smb2, struct smb2fh *fh,
503 uint8_t *buf, uint32_t count, uint64_t offset,
504 smb2_command_cb cb, void *cb_data);
508 * Use smb2_get_max_read_size to discover the maximum data size that the
511 int smb2_pread(struct smb2_context *smb2, struct smb2fh *fh,
512 uint8_t *buf, uint32_t count, uint64_t offset);
519 * Use smb2_get_max_write_size to discover the maximum data size that the
523 * 0 : The operation was initiated. Result of the operation will be
524 * reported through the callback function.
525 * -errno : There was an error. The callback function will not be invoked.
527 * When the callback is invoked, status indicates the result:
528 * >=0 : Number of bytes written.
529 * -errno : An error occured.
531 * Command_data is always NULL.
533 int smb2_pwrite_async(struct smb2_context *smb2, struct smb2fh *fh,
534 uint8_t *buf, uint32_t count, uint64_t offset,
535 smb2_command_cb cb, void *cb_data);
539 * Use smb2_get_max_write_size to discover the maximum data size that the
542 int smb2_pwrite(struct smb2_context *smb2, struct smb2fh *fh,
543 uint8_t *buf, uint32_t count, uint64_t offset);
552 * 0 : The operation was initiated. Result of the operation will be
553 * reported through the callback function.
554 * -errno : There was an error. The callback function will not be invoked.
556 * When the callback is invoked, status indicates the result:
557 * >=0 : Number of bytes read.
558 * -errno : An error occured.
560 * Command_data is always NULL.
562 int smb2_read_async(struct smb2_context *smb2, struct smb2fh *fh,
563 uint8_t *buf, uint32_t count,
564 smb2_command_cb cb, void *cb_data);
569 int smb2_read(struct smb2_context *smb2, struct smb2fh *fh,
570 uint8_t *buf, uint32_t count);
579 * 0 : The operation was initiated. Result of the operation will be
580 * reported through the callback function.
581 * -errno : There was an error. The callback function will not be invoked.
583 * When the callback is invoked, status indicates the result:
584 * >=0 : Number of bytes written.
585 * -errno : An error occured.
587 * Command_data is always NULL.
589 int smb2_write_async(struct smb2_context *smb2, struct smb2fh *fh,
590 uint8_t *buf, uint32_t count,
591 smb2_command_cb cb, void *cb_data);
596 int smb2_write(struct smb2_context *smb2, struct smb2fh *fh,
597 uint8_t *buf, uint32_t count);
603 * smb2_seek() never blocks, thus no async version is needed.
605 int64_t smb2_lseek(struct smb2_context *smb2, struct smb2fh *fh,
606 int64_t offset, int whence, uint64_t *current_offset);
615 * 0 : The operation was initiated. Result of the operation will be
616 * reported through the callback function.
617 * -errno : There was an error. The callback function will not be invoked.
619 * When the callback is invoked, status indicates the result:
621 * -errno : An error occured.
623 * Command_data is always NULL.
625 int smb2_unlink_async(struct smb2_context *smb2, const char *path,
626 smb2_command_cb cb, void *cb_data);
631 int smb2_unlink(struct smb2_context *smb2, const char *path);
640 * 0 : The operation was initiated. Result of the operation will be
641 * reported through the callback function.
642 * -errno : There was an error. The callback function will not be invoked.
644 * When the callback is invoked, status indicates the result:
646 * -errno : An error occured.
648 * Command_data is always NULL.
650 int smb2_rmdir_async(struct smb2_context *smb2, const char *path,
651 smb2_command_cb cb, void *cb_data);
656 int smb2_rmdir(struct smb2_context *smb2, const char *path);
665 * 0 : The operation was initiated. Result of the operation will be
666 * reported through the callback function.
667 * -errno : There was an error. The callback function will not be invoked.
669 * When the callback is invoked, status indicates the result:
671 * -errno : An error occured.
673 * Command_data is always NULL.
675 int smb2_mkdir_async(struct smb2_context *smb2, const char *path,
676 smb2_command_cb cb, void *cb_data);
681 int smb2_mkdir(struct smb2_context *smb2, const char *path);
690 * 0 : The operation was initiated. Result of the operation will be
691 * reported through the callback function.
692 * -errno : There was an error. The callback function will not be invoked.
694 * When the callback is invoked, status indicates the result:
695 * 0 : Success. Command_data is struct smb2_statvfs
696 * -errno : An error occured.
698 int smb2_statvfs_async(struct smb2_context *smb2, const char *path,
699 struct smb2_statvfs *statvfs,
700 smb2_command_cb cb, void *cb_data);
704 int smb2_statvfs(struct smb2_context *smb2, const char *path,
705 struct smb2_statvfs *statvfs);
714 * 0 : The operation was initiated. Result of the operation will be
715 * reported through the callback function.
716 * -errno : There was an error. The callback function will not be invoked.
718 * When the callback is invoked, status indicates the result:
719 * 0 : Success. Command_data is struct smb2_stat_64
720 * -errno : An error occured.
722 int smb2_fstat_async(struct smb2_context *smb2, struct smb2fh *fh,
723 struct smb2_stat_64 *st,
724 smb2_command_cb cb, void *cb_data);
728 int smb2_fstat(struct smb2_context *smb2, struct smb2fh *fh,
729 struct smb2_stat_64 *st);
735 * 0 : The operation was initiated. Result of the operation will be
736 * reported through the callback function.
737 * -errno : There was an error. The callback function will not be invoked.
739 * When the callback is invoked, status indicates the result:
740 * 0 : Success. Command_data is struct smb2_stat_64
741 * -errno : An error occured.
743 int smb2_stat_async(struct smb2_context *smb2, const char *path,
744 struct smb2_stat_64 *st,
745 smb2_command_cb cb, void *cb_data);
749 int smb2_stat(struct smb2_context *smb2, const char *path,
750 struct smb2_stat_64 *st);
756 * 0 : The operation was initiated. Result of the operation will be
757 * reported through the callback function.
758 * -errno : There was an error. The callback function will not be invoked.
760 * When the callback is invoked, status indicates the result:
762 * -errno : An error occured.
764 int smb2_rename_async(struct smb2_context *smb2, const char *oldpath,
765 const char *newpath, smb2_command_cb cb, void *cb_data);
770 int smb2_rename(struct smb2_context *smb2, const char *oldpath,
771 const char *newpath);
777 * 0 : The operation was initiated. Result of the operation will be
778 * reported through the callback function.
779 * -errno : There was an error. The callback function will not be invoked.
781 * When the callback is invoked, status indicates the result:
783 * -errno : An error occured.
785 int smb2_truncate_async(struct smb2_context *smb2, const char *path,
786 uint64_t length, smb2_command_cb cb, void *cb_data);
791 * -errno : An error occured.
793 int smb2_truncate(struct smb2_context *smb2, const char *path,
800 * 0 : The operation was initiated. Result of the operation will be
801 * reported through the callback function.
802 * -errno : There was an error. The callback function will not be invoked.
804 * When the callback is invoked, status indicates the result:
806 * -errno : An error occured.
808 int smb2_ftruncate_async(struct smb2_context *smb2, struct smb2fh *fh,
809 uint64_t length, smb2_command_cb cb, void *cb_data);
814 * -errno : An error occured.
816 int smb2_ftruncate(struct smb2_context *smb2, struct smb2fh *fh,
827 * 0 : The operation was initiated. The link content will be
828 * reported through the callback function.
829 * -errno : There was an error. The callback function will not be invoked.
831 * When the callback is invoked, status indicates the result:
832 * 0 : Success. Command_data is the link content.
833 * -errno : An error occured.
835 int smb2_readlink_async(struct smb2_context *smb2, const char *path,
836 smb2_command_cb cb, void *cb_data);
841 int smb2_readlink(struct smb2_context *smb2, const char *path, char *buf, uint32_t bufsiz);
847 * 0 : The operation was initiated. Result of the operation will be
848 * reported through the callback function.
849 * -errno : There was an error. The callback function will not be invoked.
851 * When the callback is invoked, status indicates the result:
853 * -errno : An error occured.
855 int smb2_echo_async(struct smb2_context *smb2,
856 smb2_command_cb cb, void *cb_data);
862 * 0 : successfully send the message and received a reply.
865 int smb2_echo(struct smb2_context *smb2);
871 /* Low 2 bits desctibe the type */
872 #define SHARE_TYPE_DISKTREE 0
873 #define SHARE_TYPE_PRINTQ 1
874 #define SHARE_TYPE_DEVICE 2
875 #define SHARE_TYPE_IPC 3
877 #define SHARE_TYPE_TEMPORARY 0x40000000
878 #define SHARE_TYPE_HIDDEN 0x80000000
880 struct srvsvc_netshareinfo1 {
886 struct srvsvc_netsharectr1 {
888 struct srvsvc_netshareinfo1 *array;
891 struct srvsvc_netsharectr {
894 struct srvsvc_netsharectr1 ctr1;
898 struct srvsvc_netshareenumall_req {
901 struct srvsvc_netsharectr *ctr;
903 uint32_t resume_handle;
906 struct srvsvc_netshareenumall_rep {
908 struct srvsvc_netsharectr *ctr;
909 uint32_t total_entries;
910 uint32_t resume_handle;
917 * This function only works when connected to the IPC$ share.
920 * 0 : The operation was initiated. Result of the operation will be
921 * reported through the callback function.
922 * -errno : There was an error. The callback function will not be invoked.
924 * When the callback is invoked, status indicates the result:
925 * 0 : Success. Command_data is struct srvsvc_netshareenumall_rep *
926 * This pointer must be freed using smb2_free_data().
927 * -errno : An error occured.
929 int smb2_share_enum_async(struct smb2_context *smb2,
930 smb2_command_cb cb, void *cb_data);
932 #endif /* !_LIBSMB2_H_ */