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
|
/*
* Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2013 Nexenta Systems. All rights reserved.
*/
/*
* Test client for kwarnd. This program is not shipped on the binary
* release. This code was taken and modified from gssdtest.c
*/
#include <stdio.h>
#include <strings.h>
#include <ctype.h>
#include <stdlib.h>
#include "kwarnd.h"
#include <rpc/rpc.h>
#define LOOP_COUNTER 100
#define OCTAL_MACRO "%03.3o."
#define MALLOC(n) malloc(n)
#define CALLOC(n, s) calloc((n), (s))
#define FREE(x, n) free(x)
static void instructs(void);
static void usage(void);
static int parse_input_line(char *, int *, char ***);
extern uid_t getuid(void);
static void _kwarnd_add_warning(int, char **);
static void _kwarnd_del_warning(int, char **);
static int do_kwarndtest(char *buf);
extern OM_UINT32 kwarn_add_warning();
extern OM_UINT32 kwarn_del_warning();
static int read_line(char *buf, int size)
{
int len;
/* read the next line. If cntl-d, return with zero char count */
printf(gettext("\n> "));
if (fgets(buf, size, stdin) == NULL)
return (0);
len = strlen(buf);
buf[--len] = '\0';
return (len);
}
int
main()
{
char buf[512];
int len, ret;
/* Print out usage and instructions to start off the session */
instructs();
usage();
/*
* Loop, repeatedly calling parse_input_line() to get the
* next line and parse it into argc and argv. Act on the
* arguements found on the line.
*/
do {
len = read_line(buf, 512);
if (len)
ret = do_kwarndtest(buf);
} while (len && !ret);
return (0);
}
static int
do_kwarndtest(char *buf)
{
int argc;
char **argv, **argv_array;
char *cmd;
argv = 0;
if (parse_input_line(buf, &argc, &argv) == 0) {
printf(gettext("\n"));
return (1);
}
if (argc == 0) {
usage();
FREE(argv, (argc+1)*sizeof (char *));
return (0);
}
/*
* remember argv_array address, which is memory calloc'd by
* parse_input_line, so it can be free'd at the end of the loop.
*/
argv_array = argv;
cmd = argv[0];
argc--;
argv++;
if (strcmp(cmd, "kwarn_add_warning") == 0 ||
strcmp(cmd, "add") == 0) {
_kwarnd_add_warning(argc, argv);
} else if (strcmp(cmd, "kwarn_del_warning") == 0 ||
strcmp(cmd, "delete") == 0) {
_kwarnd_del_warning(argc, argv);
} else if (strcmp(cmd, "exit") == 0) {
printf(gettext("\n"));
FREE(argv_array, (argc+2) * sizeof (char *));
return (1);
} else
usage();
/* free argv array */
FREE(argv_array, (argc+2) * sizeof (char *));
return (0);
}
static void
_kwarnd_add_warning(int argc, char **argv)
{
OM_UINT32 status;
time_t exptime;
time_t now;
/* set up the arguments specified in the input parameters */
if (argc == 0) {
usage();
return;
}
if (argc != 2) {
usage();
return;
}
time(&now);
exptime = atol(argv[1]);
exptime = now + exptime;
status = kwarn_add_warning(argv[0], exptime);
if (status == 0) {
printf(gettext("\nadd of credential\n\n"));
printf(gettext("warning message successful for \"%s\"\n\n"),
argv[0]);
} else {
printf(gettext("server ret err (octal) %o (%s)\n"),
status, gettext("add warning error"));
}
return;
}
static void
_kwarnd_del_warning(int argc, char **argv)
{
OM_UINT32 status;
if (argc != 1) {
usage();
return;
}
status = kwarn_del_warning(argv[0]);
if (status == 0) {
printf(gettext("delete of principal warning message"
"for %s successful"),
argv[0]);
} else {
printf(gettext("delete of principal %s unsuccessful\n\n"),
argv[0]);
}
}
static void
instructs(void)
{
fprintf(stderr,
gettext(
"\nThis program will test kwarnd. kwarnd must be running as root. Enter\n"
"the desired command and the principal to be added/deleted. If adding a\n"
"principal, also include the expiration time in seconds.\n"));
}
static void
usage(void)
{
fprintf(stderr,
gettext(
"\nusage:\t[kwarn_add_warning | add] (principal) (exptime)\n"
"\t[kwarn_del_warning | delete] (principal)\n"
"\texit\n\n"));
}
/* Copied from parse_argv(), then modified */
static int
parse_input_line(char *input_line, int *argc, char ***argv)
{
const char nil = '\0';
char *chptr;
int chr_cnt;
int arg_cnt = 0;
int ch_was_space = 1;
int ch_is_space;
chr_cnt = strlen(input_line);
/* Count the arguments in the input_line string */
*argc = 1;
for (chptr = &input_line[0]; *chptr != nil; chptr++) {
ch_is_space = isspace(*chptr);
if (ch_is_space && !ch_was_space) {
(*argc)++;
}
ch_was_space = ch_is_space;
}
if (ch_was_space) {
(*argc)--;
} /* minus trailing spaces */
/* Now that we know how many args calloc the argv array */
*argv = (char **)CALLOC((*argc)+1, sizeof (char *));
chptr = (char *)(&input_line[0]);
for (ch_was_space = 1; *chptr != nil; chptr++) {
ch_is_space = isspace(*chptr);
if (ch_is_space) {
*chptr = nil; /* replace each space with nil */
} else if (ch_was_space) { /* begining of word? */
(*argv)[arg_cnt++] = chptr; /* new argument ? */
}
ch_was_space = ch_is_space;
}
return (chr_cnt);
}
|