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
|
#define _GNU_SOURCE
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>
int main( int argc, char *argv[] ) {
int err;
openlog( "piny-suid", LOG_ODELAY, LOG_AUTHPRIV );
if ( argc < 3 ) {
syslog( LOG_ERR, "Invalid usage" );
fprintf( stderr, "Usage: %s username command [args ...]\n", argv[0] );
return 1;
};
regex_t user_reg;
// Please note that these regular expressions should duplicate the language for usernames described in Piny::User.
if ( ( err = regcomp( &user_reg, "^[a-zA-Z][a-zA-Z0-9_.-]{0,30}$", REG_EXTENDED | REG_NOSUB ) ) != 0 ) {
size_t sz = regerror( err, &user_reg, NULL, 0 );
char buf[sz];
regerror( err, &user_reg, buf, sz );
syslog( LOG_CRIT, "Internal error; first regex, %s", buf );
fprintf( stderr, "Internal error compiling first regular expression: %s\n", buf );
return 1;
};
if ( regexec( &user_reg, argv[1], 0, NULL, 0 ) ) {
syslog( LOG_ERR, "Invalid username '%s' (first regex)", argv[1] );
fprintf( stderr, "'%s' does not appear to be a valid username!\n", argv[1] );
return 1;
};
regfree( &user_reg );
if ( ( err = regcomp( &user_reg, "^(git|iki)-", REG_EXTENDED | REG_NOSUB ) ) != 0 ) {
size_t sz = regerror( err, &user_reg, NULL, 0 );
char buf[sz];
regerror( err, &user_reg, buf, sz );
syslog( LOG_CRIT, "Internal error: second regex, %s", buf );
fprintf( stderr, "Internal error compiling second regular expression: %s\n", buf );
return 1;
};
if ( ! regexec( &user_reg, argv[1], 0, NULL, 0 ) ) {
syslog( LOG_ERR, "Invalid username '%s' (second regex)", argv[1] );
fprintf( stderr, "'%s' does not appear to be a valid username!\n", argv[1] );
return 1;
};
regfree( &user_reg );
regex_t cmd_reg;
if ( ( err = regcomp( &cmd_reg, "/", REG_EXTENDED | REG_NOSUB ) ) != 0 ) {
size_t sz = regerror( err, &cmd_reg, NULL, 0 );
char buf[sz];
regerror( err, &cmd_reg, buf, sz );
syslog( LOG_CRIT, "Internal error: third regex, %s", buf );
fprintf( stderr, "Internal error compiling third regular expression: %s\n", buf );
return 1;
};
if ( ! regexec( &cmd_reg, argv[2], 0, NULL, 0 ) ) {
syslog( LOG_ERR, "Invalid command '%s' (third regex)", argv[2] );
fprintf( stderr, "'%s' does not appear to be a valid command!\n", argv[2] );
return 1;
};
regfree( &cmd_reg );
struct passwd *pwd = getpwnam( argv[1] );
if ( ! pwd ) {
syslog( LOG_ERR, "Invalid username '%s' (getpwnam)", argv[1] );
fprintf( stderr, "'%s' does not appear to be a valid username!\n", argv[1] );
return 1;
};
if ( pwd->pw_uid < 1000 ) {
syslog( LOG_ERR, "Invalid username '%s' (uid)", argv[1] );
fprintf( stderr, "'%s' does not appear to be a valid username!\n", argv[1] );
return 1;
};
if ( initgroups( argv[1], pwd->pw_gid ) != 0 ) {
err = errno;
syslog( LOG_ERR, "Unable to initgroups: %s, %s", argv[1], strerror( err ) );
fprintf( stderr, "Unable to initgroups: %s\n", strerror( err ) );
return 1;
};
if ( setregid( pwd->pw_gid, pwd->pw_gid ) != 0 ) {
err = errno;
syslog( LOG_ERR, "Unable to change GID: %s, %s", argv[1], strerror( err ) );
fprintf( stderr, "Unable to change GID: %s\n", strerror( err ) );
return 1;
};
if ( setreuid( pwd->pw_uid, pwd->pw_uid ) != 0 ) {
err = errno;
syslog( LOG_ERR, "Unable to change UID: %s, %s", argv[1], strerror( err ) );
fprintf( stderr, "Unable to change UID: %s\n", strerror( err ) );
return 1;
};
size_t sz = snprintf( NULL, 0, "/usr/share/piny-suid/%s", argv[2] ) + 1;
char buf[sz];
snprintf( buf, sz, "/usr/share/piny-suid/%s", argv[2] );
char *path_info;
if ( asprintf( &path_info, "PATH_INFO=%s", getenv( "PATH_INFO" ) ) < 0 ) {
syslog( LOG_ERR, "Unable to allocate PATH_INFO space." );
fprintf( stderr, "Internal error preparing environment.\n" );
return 1;
};
char *script_name;
if ( asprintf( &script_name, "SCRIPT_NAME=%s", getenv( "SCRIPT_NAME" ) ) < 0 ) {
syslog( LOG_ERR, "Unable to allocate SCRIPT_NAME space." );
fprintf( stderr, "Internal error preparing environment.\n" );
return 1;
};
char *query_string;
if ( asprintf( &query_string, "QUERY_STRING=%s", getenv( "QUERY_STRING" ) ) < 0 ) {
syslog( LOG_ERR, "Unable to allocate QUERY_STRING space." );
fprintf( stderr, "Internal error preparing environment.\n" );
return 1;
};
char * const env[] =
{ path_info
, script_name
, query_string
, NULL
};
syslog( LOG_NOTICE, "Going to exec '%s' as '%s'", buf, argv[1] );
execve( buf, argv + 2, env );
err = errno;
syslog( LOG_ERR, "Invalid command '%s' (fell past exec, %s)", buf, strerror( err ) );
fprintf( stderr, "'%s' does not appear to be a valid command!\n", argv[2] );
return 1;
};
|