2020-02-04 16:05:58 +00:00
|
|
|
/*
|
2020-02-27 23:26:58 +00:00
|
|
|
Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
|
2020-02-04 16:05:58 +00:00
|
|
|
|
|
|
|
All rights reserved. This program and the accompanying materials
|
2020-11-25 17:34:21 +00:00
|
|
|
are made available under the terms of the Eclipse Public License 2.0
|
2020-02-04 16:05:58 +00:00
|
|
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
|
|
|
|
The Eclipse Public License is available at
|
2020-11-25 17:34:21 +00:00
|
|
|
https://www.eclipse.org/legal/epl-2.0/
|
2020-02-04 16:05:58 +00:00
|
|
|
and the Eclipse Distribution License is available at
|
|
|
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
|
2021-01-20 11:46:18 +00:00
|
|
|
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
|
2020-12-01 18:21:59 +00:00
|
|
|
|
2020-02-04 16:05:58 +00:00
|
|
|
Contributors:
|
|
|
|
Roger Light - initial implementation and documentation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This contains general purpose utility functions that are not specific to
|
|
|
|
* Mosquitto/MQTT features. */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <winsock2.h>
|
|
|
|
# include <aclapi.h>
|
|
|
|
# include <io.h>
|
|
|
|
# include <lmcons.h>
|
|
|
|
#else
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
FILE *mosquitto__fopen(const char *path, const char *mode, bool restrict_read)
|
|
|
|
{
|
|
|
|
#ifdef WIN32
|
|
|
|
char buf[4096];
|
|
|
|
int rc;
|
2020-10-26 15:17:22 +00:00
|
|
|
rc = ExpandEnvironmentStringsA(path, buf, 4096);
|
2020-02-04 16:05:58 +00:00
|
|
|
if(rc == 0 || rc > 4096){
|
|
|
|
return NULL;
|
|
|
|
}else{
|
|
|
|
if (restrict_read) {
|
|
|
|
HANDLE hfile;
|
|
|
|
SECURITY_ATTRIBUTES sec;
|
2020-10-26 15:17:22 +00:00
|
|
|
EXPLICIT_ACCESS_A ea;
|
2020-02-04 16:05:58 +00:00
|
|
|
PACL pacl = NULL;
|
|
|
|
char username[UNLEN + 1];
|
2020-12-02 15:59:45 +00:00
|
|
|
DWORD ulen = UNLEN;
|
2020-02-04 16:05:58 +00:00
|
|
|
SECURITY_DESCRIPTOR sd;
|
|
|
|
DWORD dwCreationDisposition;
|
|
|
|
|
|
|
|
switch(mode[0]){
|
|
|
|
case 'a':
|
|
|
|
dwCreationDisposition = OPEN_ALWAYS;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
dwCreationDisposition = OPEN_EXISTING;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
dwCreationDisposition = CREATE_ALWAYS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-10-26 15:17:22 +00:00
|
|
|
GetUserNameA(username, &ulen);
|
2020-02-04 16:05:58 +00:00
|
|
|
if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-26 15:17:22 +00:00
|
|
|
BuildExplicitAccessWithNameA(&ea, username, GENERIC_ALL, SET_ACCESS, NO_INHERITANCE);
|
|
|
|
if (SetEntriesInAclA(1, &ea, NULL, &pacl) != ERROR_SUCCESS) {
|
2020-02-04 16:05:58 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!SetSecurityDescriptorDacl(&sd, TRUE, pacl, FALSE)) {
|
|
|
|
LocalFree(pacl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
sec.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sec.bInheritHandle = FALSE;
|
|
|
|
sec.lpSecurityDescriptor = &sd;
|
|
|
|
|
2020-10-26 15:17:22 +00:00
|
|
|
hfile = CreateFileA(buf, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
|
2020-02-04 16:05:58 +00:00
|
|
|
&sec,
|
|
|
|
dwCreationDisposition,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
LocalFree(pacl);
|
|
|
|
|
|
|
|
int fd = _open_osfhandle((intptr_t)hfile, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *fptr = _fdopen(fd, mode);
|
|
|
|
if (!fptr) {
|
|
|
|
_close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fptr;
|
|
|
|
|
|
|
|
}else {
|
|
|
|
return fopen(buf, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (restrict_read) {
|
|
|
|
FILE *fptr;
|
|
|
|
mode_t old_mask;
|
|
|
|
|
|
|
|
old_mask = umask(0077);
|
|
|
|
fptr = fopen(path, mode);
|
|
|
|
umask(old_mask);
|
|
|
|
|
|
|
|
return fptr;
|
|
|
|
}else{
|
|
|
|
return fopen(path, mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *misc__trimblanks(char *str)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
if(str == NULL) return NULL;
|
|
|
|
|
|
|
|
while(isspace(str[0])){
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
endptr = &str[strlen(str)-1];
|
|
|
|
while(endptr > str && isspace(endptr[0])){
|
|
|
|
endptr[0] = '\0';
|
|
|
|
endptr--;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *fgets_extending(char **buf, int *buflen, FILE *stream)
|
|
|
|
{
|
|
|
|
char *rc;
|
|
|
|
char endchar;
|
|
|
|
int offset = 0;
|
|
|
|
char *newbuf;
|
|
|
|
|
|
|
|
if(stream == NULL || buf == NULL || buflen == NULL || *buflen < 1){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do{
|
|
|
|
rc = fgets(&((*buf)[offset]), (*buflen)-offset, stream);
|
|
|
|
if(feof(stream)){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
endchar = (*buf)[strlen(*buf)-1];
|
|
|
|
if(endchar == '\n'){
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
/* No EOL char found, so extend buffer */
|
|
|
|
offset = (*buflen)-1;
|
|
|
|
*buflen += 1000;
|
2020-10-17 00:23:08 +00:00
|
|
|
newbuf = realloc(*buf, (size_t)*buflen);
|
2020-02-04 16:05:58 +00:00
|
|
|
if(!newbuf){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*buf = newbuf;
|
|
|
|
}while(1);
|
|
|
|
}
|