File: preferences.c

package info (click to toggle)
muscleframework 1.1.6-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,328 kB
  • ctags: 1,593
  • sloc: ansic: 13,594; sh: 11,523; makefile: 219
file content (343 lines) | stat: -rw-r--r-- 9,750 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/********************************************************************
 *                                                                  *
 * File: preferences.c                                              *
 *                                                                  *
 ********************************************************************
 *                                                                  *
 * Authors:                                                         *
 *   Chris Osgod          <oznet@mac.com>                           *
 *   Eirik A. Herskedal   <ehersked@cs.purdue.edu>                  *
 *   Bruce Barnett        <muscle040302@grymoire.com>
 *                                                                  *
 ********************************************************************
 *                                                                  *
 * Parses the configuration file.                                   *
 * This file is a modification of a parser originally written       *
 * by Chris Osgod (oznet@mac.com).                                  *
 *                                                                  *
 ********************************************************************/

#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include "preferences.h"

/* The third parameter was /home, but this overrides the default value
   of the home directory specified by the passwd file */

struct preferences pr = {
  DEBUGOFF,
  0,
  1,
  "",   /* was "/home/" */
  "user.cert",
  "/etc/musclepam/root.cert",
  "",
  "",
  ROOTCERT
};

/******************************************************************************
** Function: util_ParsePreference
**
** Parse a preference item from a line of text.  The text should be null
** terminated and buf_size keeps overruns from happening.
**
** If a preference item is successfully parsed then it is stored in the
** st.prefs settings.
**
** This whole function is fairly verbose and could be broken into smaller
** pieces to handle things like "get true/false pref" but at least this is
** very straightforward.
**
** Fixme: put this in p11x_prefs.c
**
** Parameters:
**  buf      - Null terminated text buffer
**  buf_size - Size of buffer
**
** Returns:
**  none
******************************************************************************/
void util_ParsePreference(char *buf, int buf_size)
{
  char sep[] = "=\t\r\n ";
  char *token;

  /* We will be using many unsafe string functions so force a NULL at the */
  /* end of the buffer to protect ourselves                               */
  buf[buf_size - 1] = 0;
  token = (char *) strchr(buf, '#');

  if (token)
    *token = 0;

  token = (char *) strtok(buf, sep);
  if (token)
    {
      if (!strcasecmp("Debug", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"Debug\" failed");
	  else
            {
	      if (!strcasecmp("OFF", token))
		pr.debug = DEBUGOFF;
	      else
		pr.debug = DEBUGON;
            }
        }
      else if (!strcasecmp("CertNumber", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"CertNumber\" failed");
	  else
            {
	      pr.certnumber = (int) (*token - '0');
            }
        }
      else if (!strcasecmp("PinNumber", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"PinNumber\" failed");
	  else
            {
	      pr.pinnumber = (int) (*token - '0');
            }
        }
      else if (!strcasecmp("CertName", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"CertName\" failed");
	  else
            {
	      strncpy(pr.certname, token, 200);
            }
        }
      else if (!strcasecmp("UserPath", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"UserPath\" failed");
	  else
            {
	      strncpy(pr.userpath, token, 200);
            }
        }
      else if (!strcasecmp("RootCACert", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"RootCACert\" failed");
	  else
            {
	      strncpy(pr.rootcacert, token, 200);
            }
        }
      else if (!strcasecmp("LDAPHost", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"LDAPHost\" failed");
	  else
            {
	      strncpy(pr.ldaphost, token, 200);
            }
        }
      else if (!strcasecmp("LDAPPath", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"LDAPPath\" failed");
	  else
            {
	      strncpy(pr.ldappath, token, 200);
            }
        }
      else if (!strcasecmp("AuthMode", token))
        {
	  token = (char *) strtok(0, sep);
	  if (!token)
	    syslog(LOG_ERR, "Config option \"AuthMode\" failed");
	  else
            {
	      if (!strcasecmp("UserCert", token))
		pr.authmode = USERCERT;
	      else if (!strcasecmp("RootCert", token))
		pr.authmode = ROOTCERT;
            }
        }
    }
}

/******************************************************************************
** Function: util_ReadPreferences
**
** Gets preferences, if available.  On UNIX, looks for .pkcs11rc
** in the $HOME directory, or root directory if $HOME is not
** defined.  Having a preferences file is optional and it is assumed
** that most of the time users will not have one unless debug/logging
** or other special settings are required.
**
** Parameters:
**  none
**
** Returns:
**  none
******************************************************************************/

int util_ReadPreferences()
{
  int rv = 0;
  FILE *fp;
  char rcfilename[] = "/etc/musclepam/pam-muscle.conf";
  char buf[1024];

  if (pr.debug)
	syslog(LOG_INFO, "Reading the preferences from file '%s'", rcfilename);

  /* unsafe preference file? */
  if (util_CheckFile(rcfilename, "root") != 0)
	return -1;

  fp = fopen(rcfilename, "rb");
  if (fp)
    {
      while (fgets(buf, sizeof(buf), fp))
	util_ParsePreference(buf, sizeof(buf));
      fclose(fp);
    }
  return rv;
}

/******************************************************************************
** Function: util_CheckFile
**
** Checks to make sure the file is not modifyable by someone other
** than the owner This utility is given the complete name for the file
** and recursively walks up the direwctory tree until it reaches the
** root directory.
**
**
** Parameters:
**  file (string)
**   user (string)
**
** Returns:
**  zero if no problem
**  -1 if there is a security risk
**  -2 if the file does not exist
**
******************************************************************************/

int util_CheckFile (char *file, char *user)
{
	char path[1024];
	int i;
	char *l;

	if (strstr(file, "..")) {
	  /* This would probably never happen, and the owner of the
		 /etc/musclepam/pam-muscle.conf file has to put it
		 there. . But it's better to be paranoid and safe than sorry */
	  syslog(LOG_ERR, "File '%s' contains the string '..'  - unsafe place to put configuration file", file);
	  return -1;
	}

	if (file[0] != '/') {
	  syslog(LOG_ERR, "File '%s' is relative. Must use absolute path  - unsafe place to put configuration file", file);
	  return -1;
	}

	/* Now - check the entire file */
	if ((i = util_CheckFileComponent(file,user)) < 0)
		return i;

	/* looks okay - now check all of the paths */
	strncpy(path, file, sizeof(path));

	while ((l = strrchr(path,'/')) != NULL) {
	  /* chop off all characters after the '/' */
	  *l = '\0';
	  if (strlen(path) > 0)
		if ((i = util_CheckFileComponent(path,user)) < 0)
			return i;
	}

	if (pr.debug)
	  syslog(LOG_INFO, "File '%s' and user %s look okay", file, user);

	return 0;
}

/******************************************************************************
** Function: util_CheckFileComponent
**
** Checks to make sure the file component (either a single file or
** directory) is not modifyable by someone other than the owner
**
** Parameters:
**  file (string)
**   user (string)
**
** Returns:
**  zero if no problem
**  -1 if there is a security risk
**  -2 if the file or directory does not exist
**
******************************************************************************/

int util_CheckFileComponent (char *file, char *user)
{
    struct stat buf;
  	struct passwd *pw;
	uid_t uid;

	/* Check if the file is a symbolic link */
	if (lstat(file, &buf)) {
	  /* if (pr.debug) printf("File does not exist %s - done\n", file); */
	  return -2; /* doesn't exist */
	}

	if (S_ISLNK(buf.st_mode)) {
	  syslog(LOG_ERR, "File '%s' is a symbolic link - unsafe place to put configuration file", file);
	  return -1;
	}

	/* Check the real file */
	if (stat(file,&buf)) {
	  /* if (pr.debug) printf("File does not exist (2) %s - done\n", file); */
	  return -2; /* I can't check it, so it's okay */
	}

	/* Find the user's UID number */
	if ((pw = getpwnam(user)) == NULL) {
	  syslog(LOG_ERR, "User '%s' does not exist", user);
	  return -1;
	}

	uid = pw->pw_uid;

	if ((buf.st_uid != uid) && (buf.st_uid != 0)) {
	  syslog(LOG_ERR, "File '%s' is  owned by UID %d, and should be owned by %d (%s) - unsafe operation", file, buf.st_uid, uid, user);
	  return -1;
	}

	if ((buf.st_mode & 0022) != 0) {
	  /* if (pr.debug) printf("File mode is BAD,  %o vs. %o\n", buf.st_mode, (buf.st_mode&022)); */
	  syslog(LOG_ERR, "File '%s' is group or world writable - may be unsafe operation", file);
	  return -1;
	}

	return 0;
}