summaryrefslogtreecommitdiffstats
path: root/src/lib/rpc/unit-test/server.c
blob: 32f5de349fb8fe21c6bf4025b484e0d874028340 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
 *
 * $Id$
 * $Source$
 */

#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <gssrpc/rpc.h>
#include <gssrpc/pmap_clnt.h>
#include <arpa/inet.h>  /* inet_ntoa */
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_generic.h>
#include <gssrpc/auth_gssapi.h>
#include <sys/param.h>	/* MAXHOSTNAMELEN */
#include "rpc_test.h"

extern void rpc_test_prog_1();

extern int svc_debug_gssapi, misc_debug_gssapi;

void rpc_test_badauth(OM_uint32 major, OM_uint32 minor,
		 struct sockaddr_in *addr, void *data);
void log_miscerr(struct svc_req *rqst, struct rpc_msg *msg, char
		 *error, char *data);
void log_badauth_display_status(OM_uint32 major, OM_uint32 minor);
void log_badauth_display_status_1(OM_uint32 code, int type, int rec);
static void rpc_test_badverf(gss_name_t client, gss_name_t server,
			     struct svc_req *rqst, struct rpc_msg *msg,
			     caddr_t data);

#ifndef SERVICE_NAME
#define SERVICE_NAME "server"
#endif

void usage()
{
     fprintf(stderr, "Usage: server {-t|-u} [svc-debug] [misc-debug]\n");
     exit(1);
}

void handlesig(void)
{
    exit(0);
}

main(int argc, char **argv)
{
     int c, prot;
     auth_gssapi_name names[2];
     register SVCXPRT *transp;
     extern int optind;
#ifdef POSIX_SIGNALS
     struct sigaction sa;
#endif     

     names[0].name = SERVICE_NAME;
     names[0].type = (gss_OID) gss_nt_service_name;
     names[1].name = 0;
     names[1].type = 0;

     prot = 0;
     while ((c = getopt(argc, argv, "tu")) != -1) {
	  switch (c) {
	  case 't':
	       prot = IPPROTO_TCP;
	       break;
	  case 'u':
	       prot = IPPROTO_UDP;
	       break;
	  case '?':
	       usage();
	       break;
	  }
     }
     if (prot == 0)
	  usage();

     argv += optind;
     argc -= optind;

     switch (argc) {
     case 2:
	  misc_debug_gssapi = atoi(argv[1]);
     case 1:
	  svc_debug_gssapi = atoi(argv[0]);
     case 0:
	  break;
     default:
	  usage();
	  exit(1);
     }

     (void) pmap_unset(RPC_TEST_PROG, RPC_TEST_VERS_1);

     if (prot == IPPROTO_TCP)
	  transp = svctcp_create(RPC_ANYSOCK, 0, 0);
     else
	  transp = svcudp_create(RPC_ANYSOCK);
     if (transp == NULL) {
	  fprintf(stderr, "cannot create tcp service.");
	  exit(1);
     }
     if (!svc_register(transp, RPC_TEST_PROG, RPC_TEST_VERS_1,
		       rpc_test_prog_1, prot)) { 
	  fprintf(stderr,
		  "unable to register (RPC_TEST_PROG, RPC_TEST_VERS_1, %s).",
		  prot == IPPROTO_TCP ? "tcp" : "udp");
	  exit(1);
     }
     
     if (_svcauth_gssapi_set_names(names, 0) == FALSE) {
	  fprintf(stderr, "unable to set gssapi names\n");
	  exit(1);
     }

     _svcauth_gssapi_set_log_badauth_func(rpc_test_badauth, NULL);
     _svcauth_gssapi_set_log_badverf_func(rpc_test_badverf, NULL);
     _svcauth_gssapi_set_log_miscerr_func(log_miscerr, NULL);

#ifdef POSIX_SIGNALS
     (void) sigemptyset(&sa.sa_mask);
     sa.sa_flags = 0;
     sa.sa_handler = handlesig;
     (void) sigaction(SIGHUP, &sa, NULL);
     (void) sigaction(SIGINT, &sa, NULL);
     (void) sigaction(SIGTERM, &sa, NULL);
#else
     signal(SIGHUP, handlesig);
     signal(SIGINT, handlesig);
     signal(SIGTERM, handlesig);
#endif
     printf("running\n");
     
     svc_run();
     fprintf(stderr, "svc_run returned");
     exit(1);
     /* NOTREACHED */
}

char **rpc_test_echo_1(char **arg, struct svc_req *h)
{
     static char *res = NULL;

     if (res)
	  free(res);
     res = (char *) malloc(strlen(*arg) + strlen("Echo: ") + 1);
     sprintf(res, "Echo: %s", *arg);
     return &res;
}

static void rpc_test_badverf(gss_name_t client, gss_name_t server,
			     struct svc_req *rqst, struct rpc_msg *msg,
			     caddr_t data)
{
     OM_uint32 minor_stat;
     gss_OID type;
     gss_buffer_desc client_name, server_name;

     (void) gss_display_name(&minor_stat, client, &client_name, &type);
     (void) gss_display_name(&minor_stat, server, &server_name, &type);

     printf("rpc_test server: bad verifier from %s at %s:%d for %s\n",
	    client_name.value,
	    inet_ntoa(rqst->rq_xprt->xp_raddr.sin_addr), 
	    ntohs(rqst->rq_xprt->xp_raddr.sin_port),
	    server_name.value);

     (void) gss_release_buffer(&minor_stat, &client_name);
     (void) gss_release_buffer(&minor_stat, &server_name);
}

/*
 * Function: log_badauth
 *
 * Purpose: Callback from GSS-API Sun RPC for authentication
 * failures/errors.
 *
 * Arguments:
 * 	major 		(r) GSS-API major status
 * 	minor		(r) GSS-API minor status
 * 	addr		(r) originating address
 * 	data		(r) arbitrary data (NULL), not used
 *
 * Effects:
 *
 * Logs the GSS-API error to stdout.
 */
void rpc_test_badauth(OM_uint32 major, OM_uint32 minor,
		 struct sockaddr_in *addr, void *data)
{
     char *a;
     
     /* Authentication attempt failed: <IP address>, <GSS-API error */
     /* strings> */

     a = inet_ntoa(addr->sin_addr);

     printf("rpc_test server: Authentication attempt failed: %s", a);
     log_badauth_display_status(major, minor);
     printf("\n");
}

void log_miscerr(struct svc_req *rqst, struct rpc_msg *msg,
		 char *error, char *data)
{
     char *a;
     
     a = inet_ntoa(rqst->rq_xprt->xp_raddr.sin_addr);
     printf("Miscellaneous RPC error: %s, %s\n", a, error);
}

void log_badauth_display_status(OM_uint32 major, OM_uint32 minor)
{
     log_badauth_display_status_1(major, GSS_C_GSS_CODE, 0);
     log_badauth_display_status_1(minor, GSS_C_MECH_CODE, 0);
}

void log_badauth_display_status_1(OM_uint32 code, int type, int rec)
{
     OM_uint32 gssstat, minor_stat;
     gss_buffer_desc msg;
     int msg_ctx;

     msg_ctx = 0;
     while (1) {
	  gssstat = gss_display_status(&minor_stat, code,
				       type, GSS_C_NULL_OID,
				       &msg_ctx, &msg);
	  if (gssstat != GSS_S_COMPLETE) {
 	       if (!rec) {
		    log_badauth_display_status_1(gssstat,GSS_C_GSS_CODE,1); 
		    log_badauth_display_status_1(minor_stat,
						 GSS_C_MECH_CODE, 1);
	       } else
		    printf("GSS-API authentication error %s: "
			   "recursive failure!\n", msg);
	       return;
	  }
	  
	  printf(", %s", (char *)msg.value); 
	  (void) gss_release_buffer(&minor_stat, &msg);
	  
	  if (!msg_ctx)
	       break;
     }
}


#if 0

/* this hack is no longer necessary, since the library supports it
   internally */

/* This is a hack to change the default keytab name */

#include <krb5/krb5.h>
extern char *krb5_defkeyname;

krb5_error_code
krb5_kt_default_name(char *name, int namesize)
{
   char *ktname;

   if ((ktname = getenv("KRB5KTNAME")) == NULL)
      ktname = krb5_defkeyname;

   if (namesize < strlen(ktname)+1)
      return(KRB5_CONFIG_NOTENUFSPACE);

   strcpy(name, ktname);

   return(0);
}

#endif