File: goodpath.c

package info (click to toggle)
sudo 1.5.4-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 888 kB
  • ctags: 732
  • sloc: ansic: 4,405; sh: 1,589; makefile: 188; perl: 47
file content (99 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download
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
/*
 *  CU sudo version 1.5.4
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please send bugs, changes, problems to sudo-bugs@courtesan.com
 *
 *******************************************************************
 *
 *  This module contains sudo_goodpath(3)
 *
 *  sudo_goodpath(3) takes a path to check and returns its argument
 *  if the path is stat(2)'able, a regular file, and executable by
 *  root.  The string's size should be <= MAXPATHLEN.
 *
 *  Todd C. Miller (millert@colorado.edu) Sat Mar 25 21:58:17 MST 1995
 */

#ifndef lint
static char rcsid[] = "$Id: goodpath.c,v 1.15 1998/01/13 04:48:17 millert Exp $";
#endif /* lint */

#include "config.h"

#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <netinet/in.h>

#include "sudo.h"
#include <options.h>

#ifndef STDC_HEADERS
extern int stat		__P((const char *, struct stat *));
#endif /* !STDC_HEADERS */


/******************************************************************
 *
 *  sudo_goodpath()
 *
 *  this function takes a path and makes sure it describes a a file
 *  that is a normal file and executable by root.
 */

char * sudo_goodpath(path)
    const char * path;
{
    struct stat statbuf;		/* for stat(2) */
    int err;				/* if stat(2) got an error */

    /* check for brain damage */
    if (path == NULL || path[0] == '\0')
	return(NULL);

    /* we need to be root for the stat */
    set_perms(PERM_ROOT, 0);

    err = stat(path, &statbuf);

    /* discard root perms */
    set_perms(PERM_USER, 0);

    /* stat(3) failed */
    if (err)
	return(NULL);

    /* make sure path describes an executable regular file */
    if (S_ISREG(statbuf.st_mode) && (statbuf.st_mode & 0000111)) {
	return((char *)path);
    } else {
	/* file is not executable/regular */
	errno = EACCES;
	return(NULL);
    }
}