1 /*=====================================================================
2 Unix SMB/Netbios implementation.
4 SMB client library API definitions
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000
7 Copyright (C) John Terpsra 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 =====================================================================*/
24 #ifndef SMBCLIENT_H_INCLUDED
25 #define SMBCLIENT_H_INCLUDED
27 /*-------------------------------------------------------------------*/
28 /* The following are special comments to instruct DOXYGEN (automated
31 /** \defgroup structure Data Structures Type and Constants
32 * Data structures, types, and constants
34 /** \defgroup file File Functions
35 * Functions used to access individual file contents
37 /** \defgroup directory Directory Functions
38 * Functions used to access directory entries
40 /** \defgroup attribute Attributes Functions
41 * Functions used to view or change file and directory attributes
43 /** \defgroup print Print Functions
44 * Functions used to access printing functionality
46 /** \defgroup attribute Miscellaneous Functions
47 * Functions that don't fit in to other categories
49 /*-------------------------------------------------------------------*/
51 /* Make sure we have the following includes for now ... */
52 #include <sys/types.h>
56 #define SMBC_MAX_NAME 1023
57 #define SMBC_WORKGROUP 1
59 #define SMBC_FILE_SHARE 3
60 #define SMBC_PRINTER_SHARE 4
61 #define SMBC_COMMS_SHARE 5
62 #define SMBC_IPC_SHARE 6
67 #define SMBC_FILE_MODE (S_IFREG | 0444)
68 #define SMBC_DIR_MODE (S_IFDIR | 0555)
70 #define SMBC_MAX_FD 10000
74 * Structure that represents a directory entry.
91 /** Length of this smbc_dirent in bytes
94 /** The length of the comment string in bytes (includes null
98 /** Points to the null terminated comment string
101 /** The length of the name string in bytes (includes null
105 /** Points to the null terminated name string
112 typedef unsigned short uint16;
114 /**@ingroup structure
115 * Structure that represents a print job.
118 struct print_job_info
120 /** numeric ID of the print job
124 /** represents print job priority (lower numbers mean higher priority)
128 /** Size of the print job
132 /** Name of the user that owns the print job
136 /** Name of the print job. This will have no name if an anonymous print
137 * file was opened. Ie smb://server/printer
141 /** Time the print job was spooled
148 /**@ingroup structure
149 * Authentication callback function type.
151 * Type for the the authentication function called by the library to
152 * obtain authentication credentals
154 * @param srv Server being authenticated to
156 * @param shr Share being authenticated to
158 * @param wg Pointer to buffer containing a "hint" for the
159 * workgroup to be authenticated. Should be filled in
160 * with the correct workgroup if the hint is wrong.
162 * @param wglen The size of the workgroup buffer in bytes
164 * @param un Pointer to buffer containing a "hint" for the
165 * user name to be use for authentication. Should be
166 * filled in with the correct workgroup if the hint is
169 * @param unlen The size of the username buffer in bytes
171 * @param pw Pointer to buffer containing to which password
174 * @param pwlen The size of the password buffer in bytes
177 typedef void (*smbc_get_auth_data_fn)(const char *srv,
181 char *pw, int pwlen);
184 /**@ingroup structure
185 * Print job info callback function type.
187 * @param i pointer to print job information structure
190 typedef void (*smbc_get_print_job_info)(struct print_job_info *i);
194 * Initialize the samba client library.
196 * Must be called before using any of the smbclient API function
198 * @param fn The function that will be called to obtaion
199 * authentication credentials.
201 * @param debug Allows caller to set the debug level. Can be
202 * changed in smb.conf file. Allows caller to set
203 * debugging if no smb.conf.
205 * @return 0 on success, < 0 on error with errno set:
206 * - ENOMEM Out of memory
207 * - ENOENT The smb.conf file would not load
210 int smbc_init(smbc_get_auth_data_fn fn, int debug);
214 * Open a file on an SMB server.
216 * @param furl The smb url of the file to be opened.
218 * @param flags Is one of O_RDONLY, O_WRONLY or O_RDWR which
219 * request opening the file read-only,write-only
220 * or read/write. flags may also be bitwise-or'd with
221 * one or more of the following:
222 * O_CREAT - If the file does not exist it will be
224 * O_EXCL - When used with O_CREAT, if the file
225 * already exists it is an error and the open will
227 * O_TRUNC - If the file already exists it will be
229 * O_APPEND The file is opened in append mode
231 * @param mode mode specifies the permissions to use if a new
232 * file is created. It is modified by the
233 * process's umask in the usual way: the permissions
234 * of the created file are (mode & ~umask)
236 * Not currently use, but there for future use.
237 * We will map this to SYSTEM, HIDDEN, etc bits
238 * that reverses the mapping that smbc_fstat does.
240 * @return Valid file handle, < 0 on error with errno set:
241 * - ENOMEM Out of memory
242 * - EINVAL if an invalid parameter passed, like no
243 * file, or smbc_init not called.
244 * - EEXIST pathname already exists and O_CREAT and
246 * - EISDIR pathname refers to a directory and
247 * the access requested involved writing.
248 * - EACCES The requested access to the file is not
250 * - ENODEV The requested share does not exist
251 * - ENOTDIR A file on the path is not a directory
252 * - ENOENT A directory component in pathname does
257 * @note This call uses an underlying routine that may create
258 * a new connection to the server specified in the URL.
259 * If the credentials supplied in the URL, or via the
260 * auth_fn in the smbc_init call, fail, this call will
261 * try again with an empty username and password. This
262 * often gets mapped to the guest account on some machines.
264 int smbc_open(const char *furl, int flags, mode_t mode);
268 * Create a file on an SMB server.
270 * Same as calling smbc_open() with flags = O_CREAT|O_WRONLY|O_TRUNC
272 * @param furl The smb url of the file to be created
274 * @param mode mode specifies the permissions to use if a new
275 * file is created. It is modified by the
276 * process's umask in the usual way: the permissions
277 * of the created file are (mode & ~umask)
279 * NOTE, the above is not true. We are dealing with
280 * an SMB server, which has no concept of a umask!
282 * @return Valid file handle, < 0 on error with errno set:
283 * - ENOMEM Out of memory
284 * - EINVAL if an invalid parameter passed, like no
285 * file, or smbc_init not called.
286 * - EEXIST pathname already exists and O_CREAT and
288 * - EISDIR pathname refers to a directory and
289 * the access requested involved writing.
290 * - EACCES The requested access to the file is not
292 * - ENOENT A directory component in pathname does
294 * - ENODEV The requested share does not exist.
298 int smbc_creat(const char *furl, mode_t mode);
302 * Read from a file using an opened file handle.
304 * @param fd Open file handle from smbc_open() or smbc_creat()
306 * @param buf Pointer to buffer to recieve read data
308 * @param bufsize Size of buf in bytes
310 * @return Number of bytes read, < 0 on error with errno set:
311 * - EISDIR fd refers to a directory
312 * - EBADF fd is not a valid file descriptor or
313 * is not open for reading.
314 * - EINVAL fd is attached to an object which is
315 * unsuitable for reading, or no buffer passed or
316 * smbc_init not called.
318 * @see smbc_open(), smbc_write()
321 ssize_t smbc_read(int fd, void *buf, size_t bufsize);
325 * Write to a file using an opened file handle.
327 * @param fd Open file handle from smbc_open() or smbc_creat()
329 * @param buf Pointer to buffer to recieve read data
331 * @param bufsize Size of buf in bytes
333 * @return Number of bytes written, < 0 on error with errno set:
334 * - EISDIR fd refers to a directory.
335 * - EBADF fd is not a valid file descriptor or
336 * is not open for reading.
337 * - EINVAL fd is attached to an object which is
338 * unsuitable for reading, or no buffer passed or
339 * smbc_init not called.
341 * @see smbc_open(), smbc_read()
344 ssize_t smbc_write(int fd, void *buf, size_t bufsize);
348 * Seek to a specific location in a file.
350 * @param fd Open file handle from smbc_open() or smbc_creat()
352 * @param offset Offset in bytes from whence
354 * @param whence A location in the file:
355 * - SEEK_SET The offset is set to offset bytes from
356 * the beginning of the file
357 * - SEEK_CUR The offset is set to current location
359 * - SEEK_END The offset is set to the size of the
360 * file plus offset bytes.
362 * @return Upon successful completion, lseek returns the
363 * resulting offset location as measured in bytes
364 * from the beginning of the file. Otherwise, a value
365 * of (off_t)-1 is returned and errno is set to
366 * indicate the error:
367 * - EBADF Fildes is not an open file descriptor.
368 * - EINVAL Whence is not a proper value or smbc_init
371 * @todo Are all the whence values really supported?
373 * @todo Are errno values complete and correct?
375 off_t smbc_lseek(int fd, off_t offset, int whence);
379 * Close an open file handle.
381 * @param fd The file handle to close
383 * @return 0 on success, < 0 on error with errno set:
384 * - EBADF fd isn't a valid open file descriptor
385 * - EINVAL smbc_init() failed or has not been called
387 * @see smbc_open(), smbc_creat()
389 int smbc_close(int fd);
392 /**@ingroup directory
393 * Unlink (delete) a file or directory.
395 * @param furl The smb url of the file to delete
397 * @return 0 on success, < 0 on error with errno set:
398 * - EACCES or EPERM Write access to the directory
399 * containing pathname is not allowed or one
400 * of the directories in pathname did not allow
401 * search (execute) permission
402 * - ENOENT A directory component in pathname does
404 * - EINVAL NULL was passed in the file param or
405 * smbc_init not called.
406 * - EACCES You do not have access to the file
407 * - ENOMEM Insufficient kernel memory was available
411 * @todo Are errno values complete and correct?
413 int smbc_unlink(const char *furl);
416 /**@ingroup directory
417 * Rename or move a file or directory.
419 * @param ourl The original smb url (source url) of file or
420 * directory to be moved
422 * @param nurl The new smb url (destination url) of the file
423 * or directory after the move. Currently nurl must
424 * be on the same share as ourl.
426 * @return 0 on success, < 0 on error with errno set:
427 * - EISDIR nurl is an existing directory, but ourl is
429 * - EEXIST nurl is a non-empty directory,
430 * i.e., contains entries other than "." and ".."
431 * - EINVAL The new url contained a path prefix
432 * of the old, or, more generally, an attempt was
433 * made to make a directory a subdirectory of itself
434 * or smbc_init not called.
435 * - ENOTDIR A component used as a directory in ourl
436 * or nurl path is not, in fact, a directory. Or,
437 * ourl is a directory, and newpath exists but is not
439 * - EACCES or EPERM Write access to the directory
440 * containing ourl or nurl is not allowed for the
441 * process's effective uid, or one of the
442 * directories in ourl or nurl did not allow search
443 * (execute) permission, or ourl was a directory
444 * and did not allow write permission.
445 * - ENOENT A directory component in ourl or nurl
447 * - EXDEV Rename across shares not supported.
448 * - ENOMEM Insufficient kernel memory was available.
449 * - EEXIST The target file, nurl, already exists.
452 * @todo Are we going to support copying when urls are not on the same
453 * share? I say no... NOTE. I agree for the moment.
456 int smbc_rename(const char *ourl, const char *nurl);
459 /**@ingroup directory
460 * Open a directory used to obtain directory entries.
462 * @param durl The smb url of the directory to open
464 * @return Valid directory handle. < 0 on error with errno set:
465 * - EACCES Permission denied.
466 * - EINVAL A NULL file/URL was passed, or the URL would
467 * not parse, or was of incorrect form or smbc_init not
469 * - ENOENT durl does not exist, or name is an
470 * - ENOMEM Insufficient memory to complete the
472 * - ENOTDIR name is not a directory.
473 * - EPERM the workgroup could not be found.
474 * - ENODEV the workgroup or server could not be found.
476 * @see smbc_getdents(), smbc_readdir(), smbc_closedir()
479 int smbc_opendir(const char *durl);
482 /**@ingroup directory
483 * Close a directory handle opened by smbc_opendir().
485 * @param dh Directory handle to close
487 * @return 0 on success, < 0 on error with errno set:
488 * - EBADF dh is an invalid directory handle
490 * @see smbc_opendir()
492 int smbc_closedir(int dh);
495 /**@ingroup directory
496 * Get multiple directory entries.
498 * smbc_getdents() reads as many dirent structures from the an open
499 * directory handle into a specified memory area as will fit.
501 * @param dh Valid directory as returned by smbc_opendir()
503 * @param dirp pointer to buffer that will receive the directory
506 * @param count The size of the dirp buffer in bytes
508 * @returns If any dirents returned, return will indicate the
509 * total size. If there were no more dirents available,
510 * 0 is returned. < 0 indicates an error.
511 * - EBADF Invalid directory handle
512 * - EINVAL Result buffer is too small or smbc_init
514 * - ENOENT No such directory.
515 * @see , smbc_dirent, smbc_readdir(), smbc_open()
517 * @todo Are errno values complete and correct?
519 * @todo Add example code so people know how to parse buffers.
521 int smbc_getdents(unsigned int dh, struct smbc_dirent *dirp, int count);
524 /**@ingroup directory
525 * Get a single directory entry.
527 * @param dh Valid directory as returned by smbc_opendir()
529 * @return A pointer to a smbc_dirent structure, or NULL if an
530 * error occurs or end-of-directory is reached:
531 * - EBADF Invalid directory handle
532 * - EINVAL smbc_init() failed or has not been called
534 * @see smbc_dirent, smbc_getdents(), smbc_open()
536 struct smbc_dirent* smbc_readdir(unsigned int dh);
539 /**@ingroup directory
540 * Get the current directory offset.
542 * smbc_telldir() may be used in conjunction with smbc_readdir() and
545 * @param dh Valid directory as returned by smbc_opendir()
547 * @return The current location in the directory stream or -1
548 * if an error occur. The current location is not
549 * an offset. Becuase of the implementation, it is a
550 * handle that allows the library to find the entry
552 * - EBADF dh is not a valid directory handle
553 * - EINVAL smbc_init() failed or has not been called
554 * - ENOTDIR if dh is not a directory
556 * @see smbc_readdir()
559 off_t smbc_telldir(int dh);
562 /**@ingroup directory
563 * lseek on directories.
565 * smbc_lseekdir() may be used in conjunction with smbc_readdir() and
566 * smbc_telldir(). (rewind by smbc_lseekdir(fd, NULL))
568 * @param fd Valid directory as returned by smbc_opendir()
570 * @param offset The offset (as returned by smbc_telldir). Can be
571 * NULL, in which case we will rewind
573 * @return 0 on success, -1 on failure
574 * - EBADF dh is not a valid directory handle
575 * - ENOTDIR if dh is not a directory
576 * - EINVAL offset did not refer to a valid dirent or
577 * smbc_init not called.
579 * @see smbc_telldir()
582 * @todo In what does the reture and errno values mean?
584 int smbc_lseekdir(int fd, off_t offset);
586 /**@ingroup directory
587 * Create a directory.
589 * @param durl The url of the directory to create
591 * @param mode Specifies the permissions to use. It is modified
592 * by the process's umask in the usual way: the
593 * permissions of the created file are (mode & ~umask).
595 * @return 0 on success, < 0 on error with errno set:
596 * - EEXIST directory url already exists
597 * - EACCES The parent directory does not allow write
598 * permission to the process, or one of the directories
599 * - ENOENT A directory component in pathname does not
601 * - EINVAL NULL durl passed or smbc_init not called.
602 * - ENOMEM Insufficient memory was available.
607 int smbc_mkdir(const char *durl, mode_t mode);
610 /**@ingroup directory
611 * Remove a directory.
613 * @param durl The smb url of the directory to remove
615 * @return 0 on success, < 0 on error with errno set:
616 * - EACCES or EPERM Write access to the directory
617 * containing pathname was not allowed.
618 * - EINVAL durl is NULL or smbc_init not called.
619 * - ENOENT A directory component in pathname does not
621 * - ENOTEMPTY directory contains entries.
622 * - ENOMEM Insufficient kernel memory was available.
624 * @see smbc_mkdir(), smbc_unlink()
626 * @todo Are errno values complete and correct?
628 int smbc_rmdir(const char *durl);
631 /**@ingroup attribute
632 * Get information about a file or directory.
634 * @param url The smb url to get information for
636 * @param st pointer to a buffer that will be filled with
637 * standard Unix struct stat information.
639 * @return 0 on success, < 0 on error with errno set:
640 * - ENOENT A component of the path file_name does not
642 * - EINVAL a NULL url was passed or smbc_init not called.
643 * - EACCES Permission denied.
644 * - ENOMEM Out of memory
645 * - ENOTDIR The target dir, url, is not a directory.
650 int smbc_stat(const char *url, struct stat *st);
653 /**@ingroup attribute
654 * Get file information via an file descriptor.
656 * @param fd Open file handle from smbc_open() or smbc_creat()
658 * @param st pointer to a buffer that will be filled with
659 * standard Unix struct stat information.
661 * @return EBADF filedes is bad.
662 * - EACCES Permission denied.
663 * - EBADF fd is not a valid file descriptor
664 * - EINVAL Problems occurred in the underlying routines
665 * or smbc_init not called.
666 * - ENOMEM Out of memory
668 * @see smbc_stat(), Unix stat()
671 int smbc_fstat(int fd, struct stat *st);
675 * Change the ownership of a file or directory.
677 * @param url The smb url of the file or directory to change
680 * @param owner I have no idea?
682 * @param group I have not idea?
684 * @return 0 on success, < 0 on error with errno set:
685 * - EPERM The effective UID does not match the owner
686 * of the file, and is not zero; or the owner or group
687 * were specified incorrectly.
688 * - ENOENT The file does not exist.
689 * - ENOMEM Insufficient was available.
690 * - ENOENT file or directory does not exist
692 * @todo Are we actually going to be able to implement this function
694 * @todo How do we abstract owner and group uid and gid?
697 int smbc_chown(const char *url, uid_t owner, gid_t group);
700 /**@ingroup attribute
701 * Change the permissions of a file.
703 * @param url The smb url of the file or directory to change
706 * @param mode The permissions to set:
707 * - Put good explaination of permissions here!
709 * @return 0 on success, < 0 on error with errno set:
710 * - EPERM The effective UID does not match the owner
711 * of the file, and is not zero
712 * - ENOENT The file does not exist.
713 * - ENOMEM Insufficient was available.
714 * - ENOENT file or directory does not exist
716 * @todo Actually implement this fuction?
718 * @todo Are errno values complete and correct?
720 int smbc_chmod(const char *url, mode_t mode);
724 * Print a file given the name in fname. It would be a URL ...
726 * @param fname The URL of a file on a remote SMB server that the
727 * caller wants printed
729 * @param printq The URL of the print share to print the file to.
731 * @return 0 on success, < 0 on error with errno set:
733 * - EINVAL fname or printq was NULL or smbc_init not
735 * and errors returned by smbc_open
738 int smbc_print_file(const char *fname, const char *printq);
741 * Open a print file that can be written to by other calls. This simply
742 * does an smbc_open call after checking if there is a file name on the
743 * URI. If not, a temporary name is added ...
745 * @param fname The URL of the print share to print to?
747 * @returns A file handle for the print file if successful.
748 * Returns -1 if an error ocurred and errno has the values
749 * - EINVAL fname was NULL or smbc_init not called.
750 * - all errors returned by smbc_open
753 int smbc_open_print_job(const char *fname);
756 * List the print jobs on a print share, for the moment, pass a callback
758 * @param purl The url of the print share to list the jobs of
760 * @param fn Callback function the receives printjob info
762 * @return 0 on success, < 0 on error with errno set:
763 * - EINVAL fname was NULL or smbc_init not called
766 int smbc_list_print_jobs(const char *purl, smbc_get_print_job_info fn);
771 * @param purl Url of the print share
773 * @param id The id of the job to delete
775 * @return 0 on success, < 0 on error with errno set:
776 * - EINVAL fname was NULL or smbc_init not called
778 * @todo what errno values are possible here?
780 int smbc_unlink_print_job(const char *purl, int id);
783 #endif /* SMBCLIENT_H_INCLUDED */