| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1989, 1993 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include <config.h> |
| 49 |
|
| 50 |
#if defined KRB5 || defined SHISHI |
| 51 |
|
| 52 |
# include <sys/types.h> |
| 53 |
# ifdef ENCRYPTION |
| 54 |
# include <sys/socket.h> |
| 55 |
# endif |
| 56 |
|
| 57 |
# include <netinet/in.h> |
| 58 |
|
| 59 |
# ifdef KRB5 |
| 60 |
# ifdef HAVE_KRB5_H |
| 61 |
# include <krb5.h> |
| 62 |
# endif |
| 63 |
# include "kerberos5_def.h" |
| 64 |
|
| 65 |
# elif defined(SHISHI) /* ! KRB5 */ |
| 66 |
# include <shishi.h> |
| 67 |
# include "shishi_def.h" |
| 68 |
# ifdef HAVE_GETPWUID_R |
| 69 |
# include <stdlib.h> |
| 70 |
# include <unistd.h> |
| 71 |
# include <pwd.h> |
| 72 |
# endif |
| 73 |
/* HAVE_GETPWUID_R */ |
| 74 |
# endif/* SHISHI && !KRB5 */ |
| 75 |
|
| 76 |
# include <stdio.h> |
| 77 |
|
| 78 |
# define SERVICE_NAME "rcmd" |
| 79 |
|
| 80 |
# if defined SHISHI |
| 81 |
int kcmd (Shishi **, int *, char **, unsigned short, char *, char **, |
| 82 |
char *, int *, char *, const char *, Shishi_key **, |
| 83 |
struct sockaddr_storage *, struct sockaddr_storage *, long, int); |
| 84 |
# else/* KRB5 && !SHISHI */ |
| 85 |
int kcmd (krb5_context *, int *, char **, unsigned short, char *, char **, |
| 86 |
char *, int *, char *, const char *, krb5_keyblock **, |
| 87 |
struct sockaddr_in *, struct sockaddr_in *, long); |
| 88 |
# endif/* !SHISHI */ |
| 89 |
|
| 90 |
/* |
| 91 |
* krcmd: simplified version of Athena's "kcmd" |
| 92 |
* returns a socket attached to the destination, -1 or krb error on error |
| 93 |
* if fd2p is non-NULL, another socket is filled in for it |
| 94 |
*/ |
| 95 |
|
| 96 |
# if defined SHISHI |
| 97 |
# ifdef HAVE_GETPWUID_R |
| 98 |
static int pwbuflen; |
| 99 |
static char *pwbuf = NULL; /* Reused after first allocation. */ |
| 100 |
static struct passwd pwstor, *pwd; |
| 101 |
# endif |
| 102 |
/* HAVE_GETPWUID_R */ |
| 103 |
|
| 104 |
int |
| 105 |
krcmd (Shishi **h, char **ahost, unsigned short rport, char **remuser, |
| 106 |
char *cmd, int *fd2p, const char *realm, int af) |
| 107 |
{ |
| 108 |
int sock = -1, err = 0; |
| 109 |
long authopts = 0L; |
| 110 |
|
| 111 |
# ifdef HAVE_GETPWUID_R |
| 112 |
if (!pwbuf) |
| 113 |
{ |
| 114 |
pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX); |
| 115 |
if (pwbuflen <= 0) |
| 116 |
pwbuflen = 1024; /* Guessing only. */ |
| 117 |
|
| 118 |
pwbuf = malloc (pwbuflen); |
| 119 |
} |
| 120 |
|
| 121 |
if (pwbuf) |
| 122 |
(void) getpwuid_r (getuid (), &pwstor, pwbuf, pwbuflen, &pwd); |
| 123 |
# endif |
| 124 |
/* HAVE_GETPWUID_R */ |
| 125 |
|
| 126 |
err = kcmd (h, &sock, ahost, rport, |
| 127 |
# ifdef HAVE_GETPWUID_R |
| 128 |
pwd ? pwd->pw_name : *remuser, /* locuser */ |
| 129 |
# else |
| 130 |
/* !HAVE_GETPWUID_R */ |
| 131 |
NULL, /* locuser not used */ |
| 132 |
# endif |
| 133 |
remuser, cmd, fd2p, SERVICE_NAME, realm, NULL, /* key schedule not used */ |
| 134 |
NULL, /* local addr not used */ |
| 135 |
NULL, /* foreign addr not used */ |
| 136 |
authopts, af); |
| 137 |
|
| 138 |
if (err > SHISHI_OK) |
| 139 |
{ |
| 140 |
fprintf (stderr, "krcmd: error %d, %s\n", err, shishi_strerror (err)); |
| 141 |
return (-1); |
| 142 |
} |
| 143 |
if (err < 0) |
| 144 |
return (-1); |
| 145 |
return (sock); |
| 146 |
} |
| 147 |
|
| 148 |
# elif defined(KRB5) /* !SHISHI */ |
| 149 |
int |
| 150 |
krcmd (krb5_context *ctx, char **ahost, unsigned short rport, |
| 151 |
char **remuser, char *cmd, int *fd2p, const char *realm) |
| 152 |
{ |
| 153 |
int sock = -1; |
| 154 |
krb5_error_code err = 0; |
| 155 |
long authopts = 0L; |
| 156 |
|
| 157 |
err = kcmd (ctx, &sock, ahost, rport, NULL, /* locuser not used */ |
| 158 |
remuser, cmd, fd2p, SERVICE_NAME, realm, (krb5_keyblock **) NULL, /* key not used */ |
| 159 |
(struct sockaddr_in *) NULL, /* local addr not used */ |
| 160 |
(struct sockaddr_in *) NULL, /* foreign addr not used */ |
| 161 |
authopts); |
| 162 |
|
| 163 |
if (err > 0) |
| 164 |
{ |
| 165 |
const char *text = krb5_get_error_message (*ctx, err); |
| 166 |
|
| 167 |
fprintf (stderr, "krcmd: %s\n", text); |
| 168 |
krb5_free_error_message (*ctx, text); |
| 169 |
return (-1); |
| 170 |
} |
| 171 |
if (err < 0) |
| 172 |
return (-1); |
| 173 |
return (sock); |
| 174 |
} |
| 175 |
# endif/* KRB5 && !SHISHI */ |
| 176 |
|
| 177 |
# ifdef ENCRYPTION |
| 178 |
|
| 179 |
# if defined SHISHI |
| 180 |
int |
| 181 |
krcmd_mutual (Shishi **h, char **ahost, unsigned short rport, char **remuser, |
| 182 |
char *cmd, int *fd2p, const char *realm, Shishi_key **key, |
| 183 |
int af) |
| 184 |
{ |
| 185 |
int sock = -1, err = 0; |
| 186 |
struct sockaddr_storage laddr, faddr; |
| 187 |
long authopts = SHISHI_APOPTIONS_MUTUAL_REQUIRED; |
| 188 |
|
| 189 |
# ifdef HAVE_GETPWUID_R |
| 190 |
if (!pwbuf) |
| 191 |
{ |
| 192 |
pwbuflen = sysconf (_SC_GETPW_R_SIZE_MAX); |
| 193 |
if (pwbuflen <= 0) |
| 194 |
pwbuflen = 1024; /* Guessing only. */ |
| 195 |
|
| 196 |
pwbuf = malloc (pwbuflen); |
| 197 |
} |
| 198 |
|
| 199 |
if (pwbuf) |
| 200 |
(void) getpwuid_r (getuid (), &pwstor, pwbuf, pwbuflen, &pwd); |
| 201 |
# endif |
| 202 |
/* HAVE_GETPWUID_R */ |
| 203 |
|
| 204 |
err = kcmd (h, &sock, ahost, rport, |
| 205 |
# ifdef HAVE_GETPWUID_R |
| 206 |
pwd ? pwd->pw_name : *remuser, /* locuser */ |
| 207 |
# else |
| 208 |
/* !HAVE_GETPWUID_R */ |
| 209 |
NULL, /* locuser not used */ |
| 210 |
# endif |
| 211 |
remuser, cmd, fd2p, SERVICE_NAME, realm, key, /* filled in */ |
| 212 |
&laddr, /* filled in */ |
| 213 |
&faddr, /* filled in */ |
| 214 |
authopts, af); |
| 215 |
|
| 216 |
if (err > SHISHI_OK) |
| 217 |
{ |
| 218 |
fprintf (stderr, "krcmd_mutual: error %d, %s\n", |
| 219 |
err, shishi_strerror (err)); |
| 220 |
return (-1); |
| 221 |
} |
| 222 |
|
| 223 |
if (err < 0) |
| 224 |
return (-1); |
| 225 |
return (sock); |
| 226 |
} |
| 227 |
|
| 228 |
# elif defined(KRB5) /* !SHISHI */ |
| 229 |
int |
| 230 |
krcmd_mutual (krb5_context *ctx, char **ahost, unsigned short rport, |
| 231 |
char **remuser, char *cmd, int *fd2p, const char *realm, |
| 232 |
krb5_keyblock **key) |
| 233 |
{ |
| 234 |
int sock; |
| 235 |
krb5_error_code err = 0; |
| 236 |
struct sockaddr_in laddr, faddr; |
| 237 |
long authopts = AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY; |
| 238 |
|
| 239 |
err = kcmd (ctx, &sock, ahost, rport, NULL, /* locuser not used */ |
| 240 |
remuser, cmd, fd2p, SERVICE_NAME, realm, key, /* filled in */ |
| 241 |
&laddr, /* filled in */ |
| 242 |
&faddr, /* filled in */ |
| 243 |
authopts); |
| 244 |
|
| 245 |
if (err > 0) |
| 246 |
{ |
| 247 |
const char *text = krb5_get_error_message (*ctx, err); |
| 248 |
|
| 249 |
fprintf (stderr, "krcmd_mutual: %s\n", text); |
| 250 |
krb5_free_error_message (*ctx, text); |
| 251 |
return (-1); |
| 252 |
} |
| 253 |
|
| 254 |
if (err < 0) |
| 255 |
return (-1); |
| 256 |
return (sock); |
| 257 |
} |
| 258 |
# endif |
| 259 |
/* KRB5 && !SHISHI */ |
| 260 |
# endif/* ENCRYPTION */ |
| 261 |
#endif /* KRB5 || SHISHI */ |