CVE-2022-2031 s4:kdc: Reject tickets during the last two minutes of their life
[Samba.git] / lib / util / util_str.c
blobda1989f80f298f718f8fd7e58a91a2ef49bdca54
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2005
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "system/locale.h"
26 #include "smb_strtox.h"
27 #undef strncasecmp
28 #undef strcasemp
30 /**
31 * @file
32 * @brief String utilities.
33 **/
35 /**
36 * Parse a string containing a boolean value.
38 * val will be set to the read value.
40 * @retval true if a boolean value was parsed, false otherwise.
42 _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
44 char * end = NULL;
45 long lval;
47 if (str == NULL || *str == '\0') {
48 return false;
51 lval = strtol(str, &end, 10 /* base */);
52 if (end == NULL || *end != '\0' || end == str) {
53 return set_boolean(str, val);
56 *val = (lval) ? true : false;
57 return true;
60 /**
61 * Convert a size specification like 16K into an integral number of bytes.
62 **/
63 _PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val)
65 char * end = NULL;
66 unsigned long long lval;
67 int error = 0;
69 if (str == NULL || *str == '\0') {
70 return false;
73 lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
74 if (error != 0) {
75 return false;
78 if (*end) {
79 if (strwicmp(end, "K") == 0) {
80 lval *= 1024ULL;
81 } else if (strwicmp(end, "M") == 0) {
82 lval *= (1024ULL * 1024ULL);
83 } else if (strwicmp(end, "G") == 0) {
84 lval *= (1024ULL * 1024ULL * 1024ULL);
85 } else if (strwicmp(end, "T") == 0) {
86 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
87 } else if (strwicmp(end, "P") == 0) {
88 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
89 } else {
90 return false;
94 *val = (uint64_t)lval;
95 return true;
98 /**
99 * Parse a uint64_t value from a string
101 * val will be set to the value read.
103 * @retval true if parsing was successful, false otherwise
105 _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
107 unsigned long long lval;
108 int error = 0;
110 if (str == NULL || *str == '\0') {
111 return false;
114 lval = smb_strtoull(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
115 if (error != 0) {
116 return false;
119 *val = (uint64_t)lval;
120 return true;
124 * Compare 2 strings.
126 * @note The comparison is case-insensitive.
128 _PUBLIC_ bool strequal(const char *s1, const char *s2)
130 if (s1 == s2)
131 return true;
132 if (!s1 || !s2)
133 return false;
135 return strcasecmp_m(s1,s2) == 0;
139 * @file
140 * @brief String utilities.
143 static bool next_token_internal_talloc(TALLOC_CTX *ctx,
144 const char **ptr,
145 char **pp_buff,
146 const char *sep,
147 bool ltrim)
149 const char *s;
150 const char *saved_s;
151 char *pbuf;
152 bool quoted;
153 size_t len=1;
155 *pp_buff = NULL;
156 if (!ptr) {
157 return(false);
160 s = *ptr;
162 /* default to simple separators */
163 if (!sep) {
164 sep = " \t\n\r";
167 /* find the first non sep char, if left-trimming is requested */
168 if (ltrim) {
169 while (*s && strchr_m(sep,*s)) {
170 s++;
174 /* nothing left? */
175 if (!*s) {
176 return false;
179 /* When restarting we need to go from here. */
180 saved_s = s;
182 /* Work out the length needed. */
183 for (quoted = false; *s &&
184 (quoted || !strchr_m(sep,*s)); s++) {
185 if (*s == '\"') {
186 quoted = !quoted;
187 } else {
188 len++;
192 /* We started with len = 1 so we have space for the nul. */
193 *pp_buff = talloc_array(ctx, char, len);
194 if (!*pp_buff) {
195 return false;
198 /* copy over the token */
199 pbuf = *pp_buff;
200 s = saved_s;
201 for (quoted = false; *s &&
202 (quoted || !strchr_m(sep,*s)); s++) {
203 if ( *s == '\"' ) {
204 quoted = !quoted;
205 } else {
206 *pbuf++ = *s;
210 *ptr = (*s) ? s+1 : s;
211 *pbuf = 0;
213 return true;
216 bool next_token_talloc(TALLOC_CTX *ctx,
217 const char **ptr,
218 char **pp_buff,
219 const char *sep)
221 return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
225 * Get the next token from a string, return false if none found. Handles
226 * double-quotes. This version does not trim leading separator characters
227 * before looking for a token.
230 bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
231 const char **ptr,
232 char **pp_buff,
233 const char *sep)
235 return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
239 * Get the next token from a string, return False if none found.
240 * Handles double-quotes.
242 * Based on a routine by GJC@VILLAGE.COM.
243 * Extensively modified by Andrew.Tridgell@anu.edu.au
245 _PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
247 const char *s;
248 bool quoted;
249 size_t len=1;
251 if (!ptr)
252 return false;
254 s = *ptr;
256 /* default to simple separators */
257 if (!sep)
258 sep = " \t\n\r";
260 /* find the first non sep char */
261 while (*s && strchr_m(sep,*s))
262 s++;
264 /* nothing left? */
265 if (!*s)
266 return false;
268 /* copy over the token */
269 for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
270 if (*s == '\"') {
271 quoted = !quoted;
272 } else {
273 len++;
274 *buff++ = *s;
278 *ptr = (*s) ? s+1 : s;
279 *buff = 0;
281 return true;
285 Set a boolean variable from the text value stored in the passed string.
286 Returns true in success, false if the passed string does not correctly
287 represent a boolean.
290 _PUBLIC_ bool set_boolean(const char *boolean_string, bool *boolean)
292 if (strwicmp(boolean_string, "yes") == 0 ||
293 strwicmp(boolean_string, "true") == 0 ||
294 strwicmp(boolean_string, "on") == 0 ||
295 strwicmp(boolean_string, "1") == 0) {
296 *boolean = true;
297 return true;
298 } else if (strwicmp(boolean_string, "no") == 0 ||
299 strwicmp(boolean_string, "false") == 0 ||
300 strwicmp(boolean_string, "off") == 0 ||
301 strwicmp(boolean_string, "0") == 0) {
302 *boolean = false;
303 return true;
305 return false;
308 _PUBLIC_ int memcmp_const_time(const void *s1, const void *s2, size_t n)
310 const uint8_t *p1 = s1, *p2 = s2;
311 size_t i, sum = 0;
313 for (i = 0; i < n; i++) {
314 sum |= (p1[i] ^ p2[i]);
317 return sum != 0;