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
|
/*
GSS-PROXY
Copyright (C) 2011 Red Hat, Inc.
Copyright (C) 2011 Simo Sorce <simo.sorce@redhat.com>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "config.h"
#include <libintl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <termios.h>
#include <errno.h>
#include "popt.h"
#include "gp_common.h"
#include "gp_secrets.h"
#define _(STRING) gettext(STRING)
char *fgets_password(void)
{
struct termios old_flags, flags;
char buf[1024];
char *pwd;
int ret;
ret = tcgetattr(fileno(stdin), &old_flags);
flags = old_flags;
flags.c_lflag = (flags.c_lflag & ~ECHO) | ECHONL;
ret = tcsetattr(fileno(stdin), TCSANOW, &flags);
if (ret) return NULL;
fprintf(stdout, "Password: ");
pwd = fgets(buf, 1024, stdin);
/* nothing we can do if this fails anyway */
(void)tcsetattr(fileno(stdin), TCSANOW, &old_flags);
if (pwd) {
buf[strlen(buf) - 1] = '\0';
pwd = strdup(buf);
}
gp_safe_zero(buf, 1024);
return pwd;
}
int main(int argc, const char *argv[])
{
int opt;
poptContext pc;
const char *pc_arg;
const char *opt_type = "NTLMSSP";
uid_t realuid = getuid();
long opt_uid = realuid;
int opt_version = 0;
int opt_debug = 0;
char *domain = NULL;
char *username = NULL;
char *password = NULL;
const char *p;
int ret;
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "debug", 'd', POPT_ARG_NONE | POPT_ARGFLAG_SHOW_DEFAULT,
&opt_debug, 0, _("Enable debugging"), NULL },
{ "version", '\0', POPT_ARG_NONE,
&opt_version, 0, _("Print version number and exit"), NULL },
{ "type", 't', POPT_ARG_STRING | POPT_ARGFLAG_SHOW_DEFAULT,
&opt_type, 0, _("Type of credentials"), NULL },
{ "uid", 'u', POPT_ARG_LONG | POPT_ARGFLAG_SHOW_DEFAULT,
&opt_uid, 0, _("UserID owning of the credentials"), NULL },
POPT_TABLEEND
};
pc = poptGetContext(argv[0], argc, argv, long_options, 0);
poptSetOtherOptionHelp(pc, "[[domain:]username]");
while((opt = poptGetNextOpt(pc)) != -1) {
switch(opt) {
default:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
ret = 1;
goto done;
}
}
pc_arg = poptGetArg(pc);
if (opt_version != 0 && pc_arg != NULL) {
fprintf(stderr, "\nThe version option does not take an argument\n");
poptPrintUsage(pc, stderr, 0);
ret = 1;
goto done;
}
if (opt_version) {
puts(VERSION""DISTRO_VERSION""PRERELEASE_VERSION);
ret = 0;
goto done;
}
if (opt_debug) {
gp_debug_enable();
}
if (strcasecmp(opt_type, "NTLMSSP") != 0) {
fprintf(stderr, "\nUnsupported type %s\n", opt_type);
poptPrintUsage(pc, stderr, 0);
ret = 1;
goto done;
}
/* FIXME: if gssproxy is configure with run_as_user, we'll not use the
* right keyring here. We need to drop privileges like gssproxy does, which
* means reading the gssproxy.conf file. */
if (geteuid() != 0) {
if (opt_debug && opt_uid == 0) {
fprintf(stderr, "###### This is a test mode ######\n");
fprintf(stderr, "Please do not use real credentials\n");
realuid = 0;
} else {
fprintf(stderr, "This program requires root privileges\n");
return -EPERM;
}
}
/* only root can specify an arbitrary uid */
if (realuid != 0) {
opt_uid = realuid;
}
ret = gp_priv_init();
if (ret) {
fprintf(stderr, "Failed to initialize security infrastructure\n");
fprintf(stderr, "Error: (%d) %s\n", ret, gp_strerror(ret));
return -1;
}
if (pc_arg == NULL && realuid != 0) {
/* only root can read the credentials back */
fprintf(stderr, "\nPlease provide a [domain:]username option\n");
poptPrintUsage(pc, stderr, 0);
ret = 1;
goto done;
}
if (pc_arg == NULL) {
ret = gp_get_uid_creds(opt_uid, &domain, &username, &password);
if (ret) {
fprintf(stderr, "\nCredentials not found: (%d) %s\n",
ret, gp_strerror(ret));
ret = ENOENT;
goto done;
}
fprintf(stderr, "\nFound credentials for uid %ld\n", opt_uid);
fprintf(stderr, "Username: %s\n", username);
fprintf(stderr, "Domain: %s\n", domain);
ret = 0;
goto done;
}
p = strchr(pc_arg, ':');
if (p) {
domain = strndup(pc_arg, p - pc_arg);
if (!domain) {
ret = -ENOMEM;
goto done;
}
p += 1;
} else {
p = pc_arg;
}
username = strdup(p);
if (!username) {
ret = -ENOMEM;
goto done;
}
password = fgets_password();
if (!password) {
ret = -ENOMEM;
goto done;
}
ret = gp_save_uid_creds(opt_uid, domain, username, password);
if (ret) {
fprintf(stderr, "\nFailed to save credentials for uid %ld\n", opt_uid);
ret = 1;
goto done;
}
ret = 0;
done:
poptFreeContext(pc);
strzerofree(domain);
strzerofree(username);
strzerofree(password);
return ret;
}
|