comparison mcabber/src/utils.c @ 362:d8f147d6e872

Check directory and config file permissions * Check history directory and configuration file permissions, and correct them to 0700 and 0600 if necessary. * Warn when mcabber main directory ($HOME/.mcabber) has bad permissions * Reset UseFileLogging & FileLoadLogs when the log dir does not exist
author Mikael Berthe <mikael@lilotux.net>
date Sun, 24 Jul 2005 14:37:27 +0100
parents f562b9af2de7
children 33b8e801ffa6
comparison
equal deleted inserted replaced
361:51ff319947c3 362:d8f147d6e872
24 #include <stdio.h> 24 #include <stdio.h>
25 #include <stdlib.h> 25 #include <stdlib.h>
26 #include <string.h> 26 #include <string.h>
27 #include <stdarg.h> 27 #include <stdarg.h>
28 #include <time.h> 28 #include <time.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
29 32
30 #include <config.h> 33 #include <config.h>
34 #include "screen.h"
31 35
32 static int DebugEnabled; 36 static int DebugEnabled;
33 static char *FName; 37 static char *FName;
34 38
35 void ut_InitDebug(unsigned int level, const char *filename) 39 void ut_InitDebug(unsigned int level, const char *filename)
88 free(buffer); 92 free(buffer);
89 fclose(fp); 93 fclose(fp);
90 } 94 }
91 } 95 }
92 96
97 // checkset_perm(name, setmode)
98 // Check the permissions of the "name" file/dir
99 // If setmode is true, correct the permissions if they are wrong
100 // Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct
101 int checkset_perm(const char *name, unsigned int setmode)
102 {
103 int fd;
104 struct stat buf;
105
106 fd = lstat(name, &buf);
107 if (fd == -1) return -1;
108
109 if (buf.st_uid != geteuid()) {
110 scr_LogPrint("Wrong file owner [%s]", name);
111 return 1;
112 }
113
114 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) ||
115 buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) {
116 if (setmode) {
117 mode_t newmode = 0;
118 scr_LogPrint("Bad permissions [%s]", name);
119 if (S_ISDIR(buf.st_mode))
120 newmode |= S_IXUSR;
121 newmode |= S_IRUSR | S_IWUSR;
122 if (chmod(name, newmode)) {
123 scr_LogPrint("WARNING: Failed to correct permissions!");
124 return 1;
125 }
126 scr_LogPrint("Permissions have been corrected");
127 } else {
128 scr_LogPrint("WARNING: Bad permissions [%s]", name);
129 return 1;
130 }
131 }
132
133 return 0;
134 }
135
93 // to_iso8601(dststr, timestamp) 136 // to_iso8601(dststr, timestamp)
94 // Convert timestamp to iso8601 format, and store it in dststr. 137 // Convert timestamp to iso8601 format, and store it in dststr.
95 // NOTE: dststr should be at last 19 chars long. 138 // NOTE: dststr should be at last 19 chars long.
96 // Return should be 0 139 // Return should be 0
97 int to_iso8601(char *dststr, time_t timestamp) 140 int to_iso8601(char *dststr, time_t timestamp)
183 226
184 retval += tzoff; 227 retval += tzoff;
185 228
186 return retval; 229 return retval;
187 } 230 }
188