# HG changeset patch # User Matti Hamalainen # Date 1352743687 -7200 # Node ID 9b72d0060b8575978d2265e1a14d906f79f38fac # Parent 66d18a82d6b9e6dc41732d73b9a6b38a5db5eb53 Create configuration and log files with reduced permissions (rw for user only) under UNIX. diff -r 66d18a82d6b9 -r 9b72d0060b85 main.c --- a/main.c Mon Nov 12 20:03:18 2012 +0200 +++ b/main.c Mon Nov 12 20:08:07 2012 +0200 @@ -997,20 +997,35 @@ { (void) conn; (void) buf; + FILE *cfgfile = NULL; +#ifndef __WIN32 + int cfgfd = -1; +#endif - FILE *cfgfile = fopen(setConfigFile, "w"); - if (cfgfile == NULL) +#ifdef __WIN32 + if ((cfgfile = fopen(setConfigFile, "w")) == NULL) +#else + if ((cfgfd = open(setConfigFile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || + (cfgfile = fdopen(cfgfd, "w")) == NULL) +#endif { - printMsgQ(currWin, "Could not create configuration to file '%s': %s\n", - setConfigFile, strerror(errno)); - return 0; + printMsgQ(currWin, "Could not create configuration to file '%s', %d: %s\n", + setConfigFile, errno, strerror(errno)); + goto error; } printMsgQ(currWin, "Configuration saved in file '%s', res=%d\n", setConfigFile, th_cfg_write(cfgfile, setConfigFile, cfg)); - fclose(cfgfile); +error: + if (cfgfile != NULL) + fclose(cfgfile); +#ifndef __WIN32 + else + if (cfgfd >= 0) + close(cfgfd); +#endif return 0; } @@ -1533,6 +1548,9 @@ BOOL nn_log_open(nn_window_t *win) { char *path = NULL; +#ifndef __WIN32 + int logFd = -1; +#endif if (!optLogEnable) return FALSE; @@ -1588,9 +1606,15 @@ if (win->logFilename == NULL) goto error; +#ifdef __WIN32 if ((win->logFile = fopen(win->logFilename, "a")) == NULL) +#else + if ((logFd = open(win->logFilename, O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR)) == -1 || + (win->logFile = fdopen(logFd, "a")) == NULL) +#endif { - errorMsg("Could not open logfile '%s' for appending!\n", win->logFilename); + errorMsg("Could not open logfile '%s' for appending, %d: %s\n", + win->logFilename, errno, strerror(errno)); goto error; } @@ -1603,6 +1627,13 @@ th_free(path); th_free(win->logFilename); win->logFilename = NULL; + if (win->logFile != NULL) + fclose(win->logFile); +#ifndef __WIN32 + else + if (logFd >= 0) + close(logFd); +#endif return FALSE; }