build(waf): fix a typo
[Samba.git] / libcli / smb / util.c
blobb02ae00d0d95f24105a7abc3702def68cc628a09
1 /*
2 Unix SMB/CIFS implementation.
3 client file operations
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Jeremy Allison 2001-2002
6 Copyright (C) James Myers 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "libcli/smb/smb_common.h"
24 #include "system/filesys.h"
26 /**
27 Return a string representing a CIFS attribute for a file.
28 **/
29 char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib)
31 int i, len;
32 const struct {
33 char c;
34 uint16_t attr;
35 } attr_strs[] = {
36 {'V', FILE_ATTRIBUTE_VOLUME},
37 {'D', FILE_ATTRIBUTE_DIRECTORY},
38 {'A', FILE_ATTRIBUTE_ARCHIVE},
39 {'H', FILE_ATTRIBUTE_HIDDEN},
40 {'S', FILE_ATTRIBUTE_SYSTEM},
41 {'N', FILE_ATTRIBUTE_NORMAL},
42 {'R', FILE_ATTRIBUTE_READONLY},
43 {'d', FILE_ATTRIBUTE_DEVICE},
44 {'t', FILE_ATTRIBUTE_TEMPORARY},
45 {'s', FILE_ATTRIBUTE_SPARSE},
46 {'r', FILE_ATTRIBUTE_REPARSE_POINT},
47 {'c', FILE_ATTRIBUTE_COMPRESSED},
48 {'o', FILE_ATTRIBUTE_OFFLINE},
49 {'n', FILE_ATTRIBUTE_NONINDEXED},
50 {'e', FILE_ATTRIBUTE_ENCRYPTED}
52 char *ret;
54 ret = talloc_array(mem_ctx, char, ARRAY_SIZE(attr_strs)+1);
55 if (!ret) {
56 return NULL;
59 for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {
60 if (attrib & attr_strs[i].attr) {
61 ret[len++] = attr_strs[i].c;
65 ret[len] = 0;
67 talloc_set_name_const(ret, ret);
69 return ret;
72 /****************************************************************************
73 Map standard UNIX permissions onto wire representations.
74 ****************************************************************************/
76 uint32_t unix_perms_to_wire(mode_t perms)
78 unsigned int ret = 0;
80 ret |= ((perms & S_IXOTH) ? UNIX_X_OTH : 0);
81 ret |= ((perms & S_IWOTH) ? UNIX_W_OTH : 0);
82 ret |= ((perms & S_IROTH) ? UNIX_R_OTH : 0);
83 ret |= ((perms & S_IXGRP) ? UNIX_X_GRP : 0);
84 ret |= ((perms & S_IWGRP) ? UNIX_W_GRP : 0);
85 ret |= ((perms & S_IRGRP) ? UNIX_R_GRP : 0);
86 ret |= ((perms & S_IXUSR) ? UNIX_X_USR : 0);
87 ret |= ((perms & S_IWUSR) ? UNIX_W_USR : 0);
88 ret |= ((perms & S_IRUSR) ? UNIX_R_USR : 0);
89 #ifdef S_ISVTX
90 ret |= ((perms & S_ISVTX) ? UNIX_STICKY : 0);
91 #endif
92 #ifdef S_ISGID
93 ret |= ((perms & S_ISGID) ? UNIX_SET_GID : 0);
94 #endif
95 #ifdef S_ISUID
96 ret |= ((perms & S_ISUID) ? UNIX_SET_UID : 0);
97 #endif
98 return ret;
101 /****************************************************************************
102 Map wire permissions to standard UNIX.
103 ****************************************************************************/
105 mode_t wire_perms_to_unix(uint32_t perms)
107 mode_t ret = (mode_t)0;
109 ret |= ((perms & UNIX_X_OTH) ? S_IXOTH : 0);
110 ret |= ((perms & UNIX_W_OTH) ? S_IWOTH : 0);
111 ret |= ((perms & UNIX_R_OTH) ? S_IROTH : 0);
112 ret |= ((perms & UNIX_X_GRP) ? S_IXGRP : 0);
113 ret |= ((perms & UNIX_W_GRP) ? S_IWGRP : 0);
114 ret |= ((perms & UNIX_R_GRP) ? S_IRGRP : 0);
115 ret |= ((perms & UNIX_X_USR) ? S_IXUSR : 0);
116 ret |= ((perms & UNIX_W_USR) ? S_IWUSR : 0);
117 ret |= ((perms & UNIX_R_USR) ? S_IRUSR : 0);
118 #ifdef S_ISVTX
119 ret |= ((perms & UNIX_STICKY) ? S_ISVTX : 0);
120 #endif
121 #ifdef S_ISGID
122 ret |= ((perms & UNIX_SET_GID) ? S_ISGID : 0);
123 #endif
124 #ifdef S_ISUID
125 ret |= ((perms & UNIX_SET_UID) ? S_ISUID : 0);
126 #endif
127 return ret;
130 /****************************************************************************
131 Return the file type from the wire filetype for UNIX extensions.
132 ****************************************************************************/
134 mode_t unix_filetype_from_wire(uint32_t wire_type)
136 switch (wire_type) {
137 case UNIX_TYPE_FILE:
138 return S_IFREG;
139 case UNIX_TYPE_DIR:
140 return S_IFDIR;
141 #ifdef S_IFLNK
142 case UNIX_TYPE_SYMLINK:
143 return S_IFLNK;
144 #endif
145 #ifdef S_IFCHR
146 case UNIX_TYPE_CHARDEV:
147 return S_IFCHR;
148 #endif
149 #ifdef S_IFBLK
150 case UNIX_TYPE_BLKDEV:
151 return S_IFBLK;
152 #endif
153 #ifdef S_IFIFO
154 case UNIX_TYPE_FIFO:
155 return S_IFIFO;
156 #endif
157 #ifdef S_IFSOCK
158 case UNIX_TYPE_SOCKET:
159 return S_IFSOCK;
160 #endif
161 default:
162 return (mode_t)0;
166 bool smb_buffer_oob(uint32_t bufsize, uint32_t offset, uint32_t length)
168 if ((offset + length < offset) || (offset + length < length)) {
169 /* wrap */
170 return true;
172 if ((offset > bufsize) || (offset + length > bufsize)) {
173 /* overflow */
174 return true;
176 return false;