File: krcmd.c

package info (click to toggle)
inetutils 2%3A2.0-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,864 kB
  • sloc: ansic: 111,062; sh: 9,867; yacc: 1,651; makefile: 624; perl: 72
file content (266 lines) | stat: -rw-r--r-- 7,901 bytes parent folder | download
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
/*
  Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
  2017, 2018, 2019, 2020, 2021 Free Software Foundation, Inc.

  This file is part of GNU Inetutils.

  GNU Inetutils is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or (at
  your option) any later version.

  GNU Inetutils is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see `http://www.gnu.org/licenses/'. */

/*
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <config.h>

#if defined KRB5 || defined SHISHI

# include <sys/types.h>
# ifdef ENCRYPTION
#  include <sys/socket.h>
# endif

# include <netinet/in.h>

# ifdef KRB5
#  ifdef HAVE_KRB5_H
#   include <krb5.h>
#  endif
#  include "kerberos5_def.h"

# elif defined(SHISHI) /* ! KRB5 */
#  include <shishi.h>
#  include "shishi_def.h"
#  ifdef HAVE_GETPWUID_R
#   include <stdlib.h>
#   include <unistd.h>
#   include <pwd.h>
#  endif /* HAVE_GETPWUID_R */
# endif /* SHISHI && !KRB5 */

# include <stdio.h>

# define SERVICE_NAME	"rcmd"

# if defined SHISHI
int kcmd (Shishi **, int *, char **, unsigned short, char *, char **,
	  char *, int *, char *, const char *, Shishi_key **,
	  struct sockaddr_storage *, struct sockaddr_storage *,
	  long, int);
# else /* KRB5 && !SHISHI */
int kcmd (krb5_context *, int *, char **, unsigned short, char *, char **,
	  char *, int *, char *, const char *, krb5_keyblock **,
	  struct sockaddr_in *, struct sockaddr_in *, long);
# endif /* !SHISHI */

/*
 * krcmd: simplified version of Athena's "kcmd"
 *	returns a socket attached to the destination, -1 or krb error on error
 *	if fd2p is non-NULL, another socket is filled in for it
 */

# if defined SHISHI
#  ifdef HAVE_GETPWUID_R
static int pwbuflen;
static char *pwbuf = NULL;	/* Reused after first allocation.  */
static struct passwd pwstor, *pwd;
#  endif /* HAVE_GETPWUID_R */

int
krcmd (Shishi ** h, char **ahost, unsigned short rport, char **remuser,
       char *cmd, int *fd2p, const char *realm, int af)
{
  int sock = -1, err = 0;
  long authopts = 0L;

#  ifdef HAVE_GETPWUID_R
  if (!pwbuf)
    {
      pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
      if (pwbuflen <= 0)
	pwbuflen = 1024;	/* Guessing only.  */

      pwbuf = malloc (pwbuflen);
    }

  if (pwbuf)
    (void) getpwuid_r (getuid (), &pwstor, pwbuf, pwbuflen, &pwd);
#  endif /* HAVE_GETPWUID_R */

  err = kcmd (h, &sock, ahost, rport,
#  ifdef HAVE_GETPWUID_R
	      pwd ? pwd->pw_name : *remuser,	/* locuser */
#  else /* !HAVE_GETPWUID_R */
	      NULL,		/* locuser not used */
#  endif
	      remuser, cmd, fd2p,
	      SERVICE_NAME, realm,
	      NULL,		/* key schedule not used */
	      NULL,		/* local addr not used */
	      NULL,		/* foreign addr not used */
	      authopts, af);

  if (err > SHISHI_OK)
    {
      fprintf (stderr, "krcmd: error %d, %s\n", err, shishi_strerror (err));
      return (-1);
    }
  if (err < 0)
    return (-1);
  return (sock);
}

# elif defined(KRB5) /* !SHISHI */
int
krcmd (krb5_context *ctx, char **ahost, unsigned short rport,
       char **remuser, char *cmd, int *fd2p, const char *realm)
{
  int sock = -1;
  krb5_error_code err = 0;
  long authopts = 0L;

  err = kcmd (ctx, &sock, ahost, rport,
	      NULL,	/* locuser not used */
	      remuser, cmd, fd2p,
	      SERVICE_NAME, realm,
	      (krb5_keyblock **) NULL,		/* key not used */
	      (struct sockaddr_in *) NULL,	/* local addr not used */
	      (struct sockaddr_in *) NULL,	/* foreign addr not used */
	      authopts);

  if (err > 0)
    {
      const char *text = krb5_get_error_message (*ctx, err);

      fprintf (stderr, "krcmd: %s\n", text);
      krb5_free_error_message (*ctx, text);
      return (-1);
    }
  if (err < 0)
    return (-1);
  return (sock);
}
# endif /* KRB5 && !SHISHI */

# ifdef ENCRYPTION

#  if defined SHISHI
int
krcmd_mutual (Shishi ** h, char **ahost, unsigned short rport, char **remuser,
	      char *cmd, int *fd2p, const char *realm, Shishi_key ** key, int af)
{
  int sock = -1, err = 0;
  struct sockaddr_storage laddr, faddr;
  long authopts = SHISHI_APOPTIONS_MUTUAL_REQUIRED;

#   ifdef HAVE_GETPWUID_R
  if (!pwbuf)
    {
      pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX);
      if (pwbuflen <= 0)
	pwbuflen = 1024;	/* Guessing only.  */

      pwbuf = malloc (pwbuflen);
    }

  if (pwbuf)
    (void) getpwuid_r (getuid (), &pwstor, pwbuf, pwbuflen, &pwd);
#   endif /* HAVE_GETPWUID_R */

  err = kcmd (h, &sock, ahost, rport,
#   ifdef HAVE_GETPWUID_R
	      pwd ? pwd->pw_name : *remuser,	/* locuser */
#   else /* !HAVE_GETPWUID_R */
	      NULL,		/* locuser not used */
#   endif
	      remuser, cmd, fd2p,
	      SERVICE_NAME, realm,
	      key,		/* filled in */
	      &laddr,		/* filled in */
	      &faddr,		/* filled in */
	      authopts, af);

  if (err > SHISHI_OK)
    {
      fprintf (stderr, "krcmd_mutual: error %d, %s\n",
	       err, shishi_strerror (err));
      return (-1);
    }

  if (err < 0)
    return (-1);
  return (sock);
}

#  elif defined(KRB5) /* !SHISHI */
int
krcmd_mutual (krb5_context *ctx, char **ahost, unsigned short rport,
	      char **remuser, char *cmd, int *fd2p, const char *realm,
	      krb5_keyblock **key)
{
  int sock;
  krb5_error_code err = 0;
  struct sockaddr_in laddr, faddr;
  long authopts = AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY;

  err = kcmd (ctx, &sock, ahost, rport,
	      NULL,		/* locuser not used */
	      remuser, cmd, fd2p,
	      SERVICE_NAME, realm,
	      key,		/* filled in */
	      &laddr,		/* filled in */
	      &faddr,		/* filled in */
	      authopts);

  if (err > 0)
    {
      const char *text = krb5_get_error_message (*ctx, err);

      fprintf (stderr, "krcmd_mutual: %s\n", text);
      krb5_free_error_message (*ctx, text);
      return (-1);
    }

  if (err < 0)
    return (-1);
  return (sock);
}
#  endif /* KRB5 && !SHISHI */
# endif /* ENCRYPTION  */
#endif /* KRB5 || SHISHI */