Fix: memory leaks detected by valingrid

Fix: dont't change user/group if startup user is not root
This commit is contained in:
jcorporation 2019-01-30 23:10:16 +01:00
parent c5cac48ac7
commit c7a093ae1d
1 changed files with 19 additions and 15 deletions

View File

@ -194,6 +194,7 @@ static void mympd_parse_env(struct t_config *config, const char *envvar) {
mympd_inihandler(config, section, name, value); mympd_inihandler(config, section, name, value);
} }
value = NULL; value = NULL;
free(var);
} }
} }
@ -319,21 +320,23 @@ int main(int argc, char **argv) {
} }
//drop privileges //drop privileges
if (config->user != NULL) { if (getuid() == 0) {
printf("Droping privileges to %s\n", config->user); if (config->user != NULL || strlen(config->user) != 0) {
struct passwd *pw; printf("Droping privileges to %s\n", config->user);
if ((pw = getpwnam(config->user)) == NULL) { struct passwd *pw;
printf("ERROR: getpwnam() failed, unknown user\n"); if ((pw = getpwnam(config->user)) == NULL) {
goto cleanup; printf("ERROR: getpwnam() failed, unknown user\n");
} else if (setgroups(0, NULL) != 0) { goto cleanup;
printf("ERROR: setgroups() failed\n"); } else if (setgroups(0, NULL) != 0) {
goto cleanup; printf("ERROR: setgroups() failed\n");
} else if (setgid(pw->pw_gid) != 0) { goto cleanup;
printf("ERROR: setgid() failed\n"); } else if (setgid(pw->pw_gid) != 0) {
goto cleanup; printf("ERROR: setgid() failed\n");
} else if (setuid(pw->pw_uid) != 0) { goto cleanup;
printf("ERROR: setuid() failed\n"); } else if (setuid(pw->pw_uid) != 0) {
goto cleanup; printf("ERROR: setuid() failed\n");
goto cleanup;
}
} }
} }
@ -423,5 +426,6 @@ int main(int argc, char **argv) {
tiny_queue_free(mpd_client_queue); tiny_queue_free(mpd_client_queue);
tiny_queue_free(mympd_api_queue); tiny_queue_free(mympd_api_queue);
mympd_free_config(config); mympd_free_config(config);
free(configfile);
return rc; return rc;
} }