1
1
Signed-Off-By: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Этот коммит содержится в:
Andreas Schneider 2019-12-10 18:28:03 +01:00
родитель 08f4469e21
Коммит 5317ebf0fc
2 изменённых файлов: 52 добавлений и 0 удалений

Просмотреть файл

@ -96,5 +96,6 @@ int ssh_mkdirs(const char *pathname, mode_t mode);
int ssh_quote_file_name(const char *file_name, char *buf, size_t buf_len);
int ssh_newline_vis(const char *string, char *buf, size_t buf_len);
int ssh_tmpname(char *template);
#endif /* MISC_H_ */

Просмотреть файл

@ -1735,4 +1735,55 @@ int ssh_newline_vis(const char *string, char *buf, size_t buf_len)
return out - buf;
}
/**
* @internal
*
* @brief Replaces the last 6 characters of a string from 'X' to 6 random hexdigits.
*
* @param[in] template Any input string with last 6 characters as 'X'.
* @returns -1 as error when the last 6 characters of the input to be replaced are not 'X'
* 0 otherwise.
*/
int ssh_tmpname(char *template)
{
char *tmp = NULL;
size_t i = 0;
if (template == NULL) {
goto err;
}
tmp = template + strlen(template) - 6;
if (tmp < template) {
goto err;
}
for (i = 0; i < 6; i++) {
if (tmp[i] != 'X') {
SSH_LOG(SSH_LOG_WARNING,
"Invalid input. Last six characters of the input must be \'X\'");
goto err;
}
}
srand(time(NULL));
for (i = 0; i < 6; ++i) {
#ifdef _WIN32
/* in win32 MAX_RAND is 32767, thus we can not shift that far,
* otherwise the last three chars are 0 */
int hexdigit = (rand() >> (i * 2)) & 0x1f;
#else
int hexdigit = (rand() >> (i * 5)) & 0x1f;
#endif
tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
}
return 0;
err:
errno = EINVAL;
return -1;
}
/** @} */