-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
193 lines (160 loc) · 4.67 KB
/
server.c
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
#include <krb5.h>
#include <com_err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "afsadm.h"
krb5_preauthtype *preauth = NULL;
krb5_preauthtype preauth_list[2] = { KRB5_PADATA_ENC_TIMESTAMP, -1 };
krb5_data tgtname = {
0,
KRB5_TGS_NAME_SIZE,
KRB5_TGS_NAME
};
/*********************************************************************
*
* 0 - Help printed
* 1 - Command
*
*********************************************************************/
int gethelp(char *str) {
char *list;
char *p;
static char helpstr[] = "help [command]\nquit/exit";
static char help[] = "help";
if (strncmp(str, help, 4) == 0) {
/* Skip word "help" */
p = str + 4;
while (isspace((int)*p))
p++;
if ((*p != '\0') && (list = get_help(p))) {
if (write(1, list, strlen(list)) == -1)
printf("Failed write to stdout.\n");
free(list);
return 0;
} else {
if ((list = get_list_of_cmd()) == NULL) {
if (debug)
syslog(LOG_DEBUG, "NULL cmdlist");
if (write(1, helpstr, strlen(helpstr)) == -1)
printf("Failed write to stdout.\n");
return 0;
}
printf("%s%s", list, helpstr);
free(list);
return 0;
}
}
return 1;
}
int do_command(krb5_context context, krb5_keytab keytab, krb5_principal me, char *princ, char *cmd, char *cmddir) {
char *p;
char *answer;
static char answer_exec[] = "Cannot execute command.";
static char answer_priv[] = "You are not privileged to execute this command.";
static char answer_regexp[] = "Command doesn't match any allowed regexp.";
int result;
if (debug)
syslog(LOG_DEBUG, "Principal %s is trying to execute command %s", princ, cmd);
/* Replace \n with \0 */
p = cmd;
while (*p != '\0' && *p != '\n')
p++;
*p = '\0';
if (gethelp(cmd) == 0)
return 0;
if ((result = chk_user_cmd(princ, cmd)) != 0) {
switch(result) {
case CHK_GRP:
answer = answer_priv;
break;
case CHK_REGEXP:
answer = answer_regexp;
break;
default:
answer = answer_exec;
}
if (debug)
syslog(LOG_DEBUG, "%s", answer);
if (write(1, answer, strlen(answer)) == -1)
printf("Failed write to stdout.\n");
return 0;
} else {
char *localcmd, *pathenv;
char ccname[255];
krb5_ccache ccache;
krb5_creds creds;
krb5_principal tgtserver;
krb5_error_code retval;
krb5_get_init_creds_opt opts;
pathenv = malloc((strlen(cmddir) + 6) * sizeof(char));
if (pathenv == NULL) {
syslog(LOG_ERR, "Not enough memory (env)");
exit(1);
}
sprintf(pathenv, "PATH=%s", cmddir);
preauth = preauth_list;
#ifdef __osf__
sprintf(ccname, "FILE:/tmp/afsadm_%d", getpid());
#else
snprintf(ccname, 255, "FILE:/tmp/afsadm_%d", getpid());
#endif
if (retval = krb5_cc_resolve(context, ccname, &ccache)) {
syslog(LOG_ERR, "%s while resolving ccache", error_message(retval));
exit(1);
}
#ifdef __osf__
sprintf(ccname, "KRB5CCNAME=FILE:/tmp/afsadm_%d", getpid());
#else
snprintf(ccname, 255, "KRB5CCNAME=FILE:/tmp/afsadm_%d", getpid());
#endif
putenv(ccname);
if (retval = krb5_cc_initialize(context, ccache, me)) {
syslog(LOG_ERR, "%s while initialize ccache", error_message(retval));
exit(1);
}
memset((char *)&creds, 0, sizeof(creds));
creds.client = me;
krb5_data *realm = krb5_princ_realm(context, me);
if ((retval = krb5_build_principal_ext(context, &tgtserver, realm->length, realm->data, tgtname.length, tgtname.data, realm->length, realm->data, 0))) {
syslog(LOG_ERR, "%s while building server name", error_message(retval));
krb5_cc_destroy(context, ccache);
exit(1);
}
creds.server = tgtserver;
krb5_get_init_creds_opt_init(&opts);
opts.preauth_list = preauth;
if (retval = krb5_get_init_creds_keytab(context, &creds, me, keytab, 0, NULL, &opts)) {
syslog(LOG_ERR, "%s while getting tgt", error_message(retval));
krb5_cc_destroy(context, ccache);
exit(1);
}
if (retval = krb5_cc_store_cred(context, ccache, &creds)) {
syslog(LOG_ERR, "%s while saving credentials to ccache", error_message(retval));
krb5_cc_destroy(context, ccache);
exit(1);
}
if (k_hasafs())
k_setpag();
localcmd = malloc(sizeof(char) * (strlen(cmd) + strlen(cmddir) + 2));
if (localcmd == NULL) {
syslog(LOG_ERR, "Not enough memory (cmdpath malloc)");
exit(1);
}
sprintf(localcmd, "%s/%s", cmddir, cmd);
syslog(LOG_INFO, "Principal %s : system(%s)", princ, localcmd);
/* Set PATH to dircmd !!!! */
putenv(pathenv);
//system("/usr/bin/id -a; aklog");
if (system("aklog") == -1)
printf("Cannot execute aklog.\n");
result = system(localcmd);
syslog(LOG_INFO, "Principal %s : system(%s) returns with %d", princ, localcmd, result);
free(pathenv);
free(localcmd);
if (k_hasafs())
k_unlog();
krb5_cc_destroy(context, ccache);
return 0;
}
}