Skip to content

Commit

Permalink
Add support for PUID/PGID environment variables
Browse files Browse the repository at this point in the history
These arefor setting the user/group to drop privileges to.

Closes #2441. Thanks to matthewparkes.
  • Loading branch information
ralight committed Aug 11, 2022
1 parent a2f91ba commit 95e2c4d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Broker:
before trying to use it in a live broker. Closes #2521.
- Print messages in mosquitto_passwd when adding/updating passwords.
Closes #2544.
- Add support for PUID/PGID environment variables for setting the user/group
to drop privileges to. Closes #2441.

Plugins / plugin interface:
- Add persist-sqlite plugin.
Expand Down
8 changes: 7 additions & 1 deletion docker/local/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ set -e

# Set permissions
user="$(id -u)"
if [ "$PUID" = "" ]; then
PUID="mosquitto"
fi
if [ "$PGID" = "" ]; then
PGID="mosquitto"
fi
if [ "$user" = '0' ]; then
[ -d "/mosquitto" ] && chown -R mosquitto:mosquitto /mosquitto || true
[ -d "/mosquitto" ] && chown -R ${PUID}:${PGID} /mosquitto || true
fi

exec "$@"
65 changes: 64 additions & 1 deletion src/mosquitto.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
# include <unistd.h>
# include <grp.h>
# include <assert.h>
/* For umask() */
# include <sys/stat.h>
#endif

#ifndef WIN32
Expand Down Expand Up @@ -65,6 +67,35 @@ int allow_severity = LOG_INFO;
int deny_severity = LOG_INFO;
#endif

static int set_umask(void)
{
#if !defined(__CYGWIN__) && !defined(WIN32)
/* This affects files that are written to, apart from those that are
* created using mosquitto_fopen(..., restrict_read=true), which sets a
* umask of 077. */
const char *mask_s;
char *endptr = NULL;
long mask;


mask_s = getenv("UMASK_SET");
if(mask_s){
errno = 0;
mask = strtol(mask_s, &endptr, 8);
if(errno || endptr == mask_s || *endptr != '\0'){
log__printf(NULL, MOSQ_LOG_ERR, "Error: UMASK_SET environment variable not a valid octal number.");
return MOSQ_ERR_INVAL;
}
if(mask < 000 || mask > 0777){
log__printf(NULL, MOSQ_LOG_ERR, "Error: UMASK_SET environment variable out of range.");
return MOSQ_ERR_INVAL;
}
umask((mode_t)mask);
}
#endif
return MOSQ_ERR_SUCCESS;
}

/* mosquitto shouldn't run as root.
* This function will attempt to change to an unprivileged user and group if
* running as root. The user is given in config->user.
Expand All @@ -79,15 +110,43 @@ static int drop_privileges(struct mosquitto__config *config)
struct passwd *pwd;
char *err;
int rc;
const char *puid_s, *pgid_s;
uid_t puid;
gid_t pgid;

const char *snap = getenv("SNAP_NAME");
if(snap && !strcmp(snap, "mosquitto")){
/* Don't attempt to drop privileges if running as a snap */
return MOSQ_ERR_SUCCESS;
}

/* PUID and PGID are docker custom user mappings */
puid_s = getenv("PUID");
pgid_s = getenv("PGID");

if(geteuid() == 0){
if(config->user && strcmp(config->user, "root")){
if(puid_s || pgid_s){
if(pgid_s){
pgid = (gid_t)atoi(pgid_s);

rc = setgid(pgid);
if(rc == -1){
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting gid whilst dropping privileges: %s.", err);
return 1;
}
}
if(puid_s){
puid = (uid_t)atoi(puid_s);

rc = setuid(puid);
if(rc == -1){
err = strerror(errno);
log__printf(NULL, MOSQ_LOG_ERR, "Error setting uid whilst dropping privileges: %s.", err);
return 1;
}
}
}else if(config->user && strcmp(config->user, "root")){
pwd = getpwnam(config->user);
if(!pwd){
if(strcmp(config->user, "mosquitto")){
Expand Down Expand Up @@ -283,6 +342,10 @@ int main(int argc, char *argv[])
*/
rc = drop_privileges(&config);
if(rc != MOSQ_ERR_SUCCESS) return rc;
/* Set umask based on environment variable */
rc = set_umask();
if(rc != MOSQ_ERR_SUCCESS) return rc;


if(config.daemon){
mosquitto__daemonise();
Expand Down

0 comments on commit 95e2c4d

Please sign in to comment.