comparison mcabber/src/settings.c @ 1192:7b8765c10abb

New command: /source
author Mikael Berthe <mikael@lilotux.net>
date Sat, 21 Apr 2007 12:06:13 +0200
parents 16abe7ec3056
children 6f602d3270a4
comparison
equal deleted inserted replaced
1191:b2ed413d8f3d 1192:7b8765c10abb
59 #ifdef HAVE_GPGME 59 #ifdef HAVE_GPGME
60 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal); 60 pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal);
61 #endif 61 #endif
62 } 62 }
63 63
64 // cfg_read_file(filename) 64 // cfg_read_file(filename, mainfile)
65 // Read and parse config file "filename". If filename is NULL, 65 // Read and parse config file "filename". If filename is NULL,
66 // try to open the configuration file at the default locations. 66 // try to open the configuration file at the default locations.
67 // mainfile must be set to TRUE for the startup config file.
68 // If mainfile is TRUE, the permissions of the configuration file will
69 // be fixed if they're insecure.
67 // 70 //
68 int cfg_read_file(char *filename) 71 int cfg_read_file(char *filename, guint mainfile)
69 { 72 {
70 FILE *fp; 73 FILE *fp;
71 char *buf; 74 char *buf;
72 char *line, *eol; 75 char *line, *eol;
73 unsigned int ln = 0; 76 unsigned int ln = 0;
74 int err = 0; 77 int err = 0;
75 78
76 if (!filename) { 79 if (!filename) {
77 // Use default config file locations 80 // Use default config file locations
78 char *home = getenv("HOME"); 81 char *home;
82
83 if (!mainfile) {
84 scr_LogPrint(LPRINT_LOGNORM, "No file name provided");
85 return -1;
86 }
87
88 home = getenv("HOME");
79 if (!home) { 89 if (!home) {
80 scr_LogPrint(LPRINT_LOG, "Can't find home dir!"); 90 scr_LogPrint(LPRINT_LOG, "Can't find home dir!");
81 fprintf(stderr, "Can't find home dir!\n"); 91 fprintf(stderr, "Can't find home dir!\n");
82 return -1; 92 return -1;
83 } 93 }
93 } 103 }
94 } 104 }
95 // Check configuration file permissions 105 // Check configuration file permissions
96 // As it could contain sensitive data, we make it user-readable only 106 // As it could contain sensitive data, we make it user-readable only
97 checkset_perm(filename, TRUE); 107 checkset_perm(filename, TRUE);
108 scr_LogPrint(LPRINT_LOGNORM, "Reading %s", filename);
98 // Check mcabber dir. There we just warn, we don't change the modes 109 // Check mcabber dir. There we just warn, we don't change the modes
99 sprintf(filename, "%s/.mcabber/", home); 110 sprintf(filename, "%s/.mcabber/", home);
100 checkset_perm(filename, FALSE); 111 checkset_perm(filename, FALSE);
101 g_free(filename); 112 g_free(filename);
113 filename = NULL;
102 } else { 114 } else {
103 if ((fp = fopen(filename, "r")) == NULL) { 115 if ((fp = fopen(filename, "r")) == NULL) {
104 perror("Cannot open configuration file"); 116 const char *msg = "Cannot open configuration file";
117 if (mainfile)
118 perror(msg);
119 else
120 scr_LogPrint(LPRINT_LOGNORM, "%s (%s).", msg, filename);
105 return -2; 121 return -2;
106 } 122 }
107 // Check configuration file permissions (see above) 123 // Check configuration file permissions (see above)
108 checkset_perm(filename, TRUE); 124 // We don't change the permissions if that's not the main file.
125 checkset_perm(filename, mainfile);
126 scr_LogPrint(LPRINT_LOGNORM, "Reading %s", filename);
109 } 127 }
110 128
111 buf = g_new(char, 512); 129 buf = g_new(char, 512);
112 130
113 while (fgets(buf+1, 511, fp) != NULL) { 131 while (fgets(buf+1, 511, fp) != NULL) {
131 149
132 // Ignore empty lines and comments 150 // Ignore empty lines and comments
133 if ((*line == '\n') || (*line == '\0') || (*line == '#')) 151 if ((*line == '\n') || (*line == '\0') || (*line == '#'))
134 continue; 152 continue;
135 153
136 if ((strchr(line, '=') != NULL) || !strncmp(line, "pgp ", strlen("pgp "))) { 154 // We only allow assignments line, except for commands "pgp" and "source"
137 // Only accept the set, alias, bind and pgp commands 155 if ((strchr(line, '=') != NULL) ||
138 if (strncmp(line, "set ", strlen("set ")) && 156 startswith(line, "pgp ") || startswith(line, "source ")) {
139 strncmp(line, "pgp ", strlen("pgp ")) && 157 // Only accept the set, alias, bind, pgp and source commands
140 strncmp(line, "bind ", strlen("bind ")) && 158 if (!startswith(line, "set ") && !startswith(line, "bind ") &&
141 strncmp(line, "alias ", strlen("alias "))) { 159 !startswith(line, "alias ") && !startswith(line, "pgp ") &&
160 !startswith(line, "source ")) {
142 scr_LogPrint(LPRINT_LOGNORM, 161 scr_LogPrint(LPRINT_LOGNORM,
143 "Error in configuration file (l. %d): bad command", ln); 162 "Error in configuration file (l. %d): bad command", ln);
144 err++; 163 err++;
145 continue; 164 continue;
146 } 165 }
154 err++; 173 err++;
155 } 174 }
156 } 175 }
157 g_free(buf); 176 g_free(buf);
158 fclose(fp); 177 fclose(fp);
178
179 if (filename)
180 scr_LogPrint(LPRINT_LOGNORM, "Loaded %s.", filename);
159 return err; 181 return err;
160 } 182 }
161 183
162 // parse_assigment(assignment, pkey, pval) 184 // parse_assigment(assignment, pkey, pval)
163 // Read assignment and split it to key, value 185 // Read assignment and split it to key, value