(read_str): return allocated string

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@12069 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
2003-04-16 19:49:29 +00:00
parent e3c31f9e53
commit 6bf3bbb91b

View File

@@ -98,7 +98,7 @@ syslog_and_die (const char *m, ...)
static void static void
fatal (int, const char*, const char *, ...) fatal (int, const char*, const char *, ...)
__attribute__ ((format (printf, 3, 4))); __attribute__ ((noreturn, format (printf, 3, 4)));
static void static void
fatal (int sock, const char *what, const char *m, ...) fatal (int sock, const char *what, const char *m, ...)
@@ -120,38 +120,41 @@ fatal (int sock, const char *what, const char *m, ...)
exit (1); exit (1);
} }
static void static char *
read_str (int s, char *str, size_t sz, char *expl) read_str (int s, size_t sz, char *expl)
{ {
while (sz > 0) { char *str = malloc(sz);
if (net_read (s, str, 1) != 1) char *p = str;
syslog_and_die ("read: %m"); if(str == NULL)
if (*str == '\0') fatal(s, NULL, "%s too long", expl);
return; while(p < str + sz) {
--sz; if(net_read(s, p, 1) != 1)
++str; syslog_and_die("read: %m");
if(*p == '\0')
return str;
p++;
} }
fatal (s, NULL, "%s too long", expl); fatal(s, NULL, "%s too long", expl);
} }
static int static int
recv_bsd_auth (int s, u_char *buf, recv_bsd_auth (int s, u_char *buf,
struct sockaddr_in *thisaddr, struct sockaddr_in *thisaddr,
struct sockaddr_in *thataddr, struct sockaddr_in *thataddr,
char *client_username, char **client_username,
char *server_username, char **server_username,
char *cmd) char **cmd)
{ {
struct passwd *pwd; struct passwd *pwd;
read_str (s, client_username, USERNAME_SZ, "local username"); *client_username = read_str (s, USERNAME_SZ, "local username");
read_str (s, server_username, USERNAME_SZ, "remote username"); *server_username = read_str (s, USERNAME_SZ, "remote username");
read_str (s, cmd, COMMAND_SZ, "command"); *cmd = read_str (s, ARG_MAX, "command");
pwd = getpwnam(server_username); pwd = getpwnam(*server_username);
if (pwd == NULL) if (pwd == NULL)
fatal(s, NULL, "Login incorrect."); fatal(s, NULL, "Login incorrect.");
if (iruserok(thataddr->sin_addr.s_addr, pwd->pw_uid == 0, if (iruserok(thataddr->sin_addr.s_addr, pwd->pw_uid == 0,
client_username, server_username)) *client_username, *server_username))
fatal(s, NULL, "Login incorrect."); fatal(s, NULL, "Login incorrect.");
return 0; return 0;
} }
@@ -161,9 +164,9 @@ static int
recv_krb4_auth (int s, u_char *buf, recv_krb4_auth (int s, u_char *buf,
struct sockaddr *thisaddr, struct sockaddr *thisaddr,
struct sockaddr *thataddr, struct sockaddr *thataddr,
char *client_username, char **client_username,
char *server_username, char **server_username,
char *cmd) char **cmd)
{ {
int status; int status;
int32_t options; int32_t options;
@@ -200,18 +203,18 @@ recv_krb4_auth (int s, u_char *buf,
if (strncmp (version, KCMD_OLD_VERSION, KRB_SENDAUTH_VLEN) != 0) if (strncmp (version, KCMD_OLD_VERSION, KRB_SENDAUTH_VLEN) != 0)
syslog_and_die ("bad version: %s", version); syslog_and_die ("bad version: %s", version);
read_str (s, server_username, USERNAME_SZ, "remote username"); *server_username = read_str (s, USERNAME_SZ, "remote username");
if (kuserok (&auth, server_username) != 0) if (kuserok (&auth, *server_username) != 0)
fatal (s, NULL, "Permission denied."); fatal (s, NULL, "Permission denied.");
read_str (s, cmd, COMMAND_SZ, "command"); *cmd = read_str (s, ARG_MAX, "command");
syslog(LOG_INFO|LOG_AUTH, syslog(LOG_INFO|LOG_AUTH,
"kerberos v4 shell from %s on %s as %s, cmd '%.80s'", "kerberos v4 shell from %s on %s as %s, cmd '%.80s'",
krb_unparse_name_long(auth.pname, auth.pinst, auth.prealm), krb_unparse_name_long(auth.pname, auth.pinst, auth.prealm),
inet_ntoa(((struct sockaddr_in *)thataddr)->sin_addr), inet_ntoa(((struct sockaddr_in *)thataddr)->sin_addr),
server_username, *server_username,
cmd); *cmd);
memcpy (iv, auth.session, sizeof(iv)); memcpy (iv, auth.session, sizeof(iv));
@@ -300,9 +303,9 @@ static int
recv_krb5_auth (int s, u_char *buf, recv_krb5_auth (int s, u_char *buf,
struct sockaddr *thisaddr, struct sockaddr *thisaddr,
struct sockaddr *thataddr, struct sockaddr *thataddr,
char *client_username, char **client_username,
char *server_username, char **server_username,
char *cmd) char **cmd)
{ {
u_int32_t len; u_int32_t len;
krb5_auth_context auth_context = NULL; krb5_auth_context auth_context = NULL;
@@ -344,9 +347,9 @@ recv_krb5_auth (int s, u_char *buf,
syslog_and_die ("krb5_recvauth: %s", syslog_and_die ("krb5_recvauth: %s",
krb5_get_err_text(context, status)); krb5_get_err_text(context, status));
read_str (s, server_username, USERNAME_SZ, "remote username"); *server_username = read_str (s, USERNAME_SZ, "remote username");
read_str (s, cmd, COMMAND_SZ, "command"); *cmd = read_str (s, ARG_MAX, "command");
read_str (s, client_username, COMMAND_SZ, "local username"); *client_username = read_str (s, ARG_MAX, "local username");
if(protocol_version == 2) { if(protocol_version == 2) {
status = krb5_auth_con_getremotesubkey(context, auth_context, status = krb5_auth_con_getremotesubkey(context, auth_context,
@@ -371,8 +374,8 @@ recv_krb5_auth (int s, u_char *buf,
cksum_data.length = asprintf ((char **)&cksum_data.data, cksum_data.length = asprintf ((char **)&cksum_data.data,
"%u:%s%s", "%u:%s%s",
ntohs(socket_get_port (thisaddr)), ntohs(socket_get_port (thisaddr)),
cmd, *cmd,
server_username); *server_username);
status = krb5_verify_authenticator_checksum(context, status = krb5_verify_authenticator_checksum(context,
auth_context, auth_context,
@@ -385,38 +388,38 @@ recv_krb5_auth (int s, u_char *buf,
free (cksum_data.data); free (cksum_data.data);
if (strncmp (client_username, "-u ", 3) == 0) { if (strncmp (*client_username, "-u ", 3) == 0) {
do_unique_tkfile = 1; do_unique_tkfile = 1;
memmove (client_username, client_username + 3, memmove (*client_username, *client_username + 3,
strlen(client_username) - 2); strlen(*client_username) - 2);
} }
if (strncmp (client_username, "-U ", 3) == 0) { if (strncmp (*client_username, "-U ", 3) == 0) {
char *end, *temp_tkfile; char *end, *temp_tkfile;
do_unique_tkfile = 1; do_unique_tkfile = 1;
if (strncmp (server_username + 3, "FILE:", 5) == 0) { if (strncmp (*client_username + 3, "FILE:", 5) == 0) {
temp_tkfile = tkfile; temp_tkfile = tkfile;
} else { } else {
strcpy (tkfile, "FILE:"); strcpy (tkfile, "FILE:");
temp_tkfile = tkfile + 5; temp_tkfile = tkfile + 5;
} }
end = strchr(client_username + 3,' '); end = strchr(*client_username + 3,' ');
strncpy(temp_tkfile, client_username + 3, end - client_username - 3); strncpy(temp_tkfile, *client_username + 3, end - *client_username - 3);
temp_tkfile[end - client_username - 3] = '\0'; temp_tkfile[end - *client_username - 3] = '\0';
memmove (client_username, end +1, strlen(end+1)+1); memmove (*client_username, end + 1, strlen(end+1)+1);
} }
kerberos_status = save_krb5_creds (s, auth_context, ticket->client); kerberos_status = save_krb5_creds (s, auth_context, ticket->client);
if(!krb5_kuserok (context, if(!krb5_kuserok (context,
ticket->client, ticket->client,
server_username)) *server_username))
fatal (s, NULL, "Permission denied."); fatal (s, NULL, "Permission denied.");
if (strncmp (cmd, "-x ", 3) == 0) { if (strncmp (*cmd, "-x ", 3) == 0) {
do_encrypt = 1; do_encrypt = 1;
memmove (cmd, cmd + 3, strlen(cmd) - 2); memmove (*cmd, *cmd + 3, strlen(*cmd) - 2);
} else { } else {
if(do_encrypt) if(do_encrypt)
fatal (s, NULL, "Encryption is required."); fatal (s, NULL, "Encryption is required.");
@@ -439,8 +442,8 @@ recv_krb5_auth (int s, u_char *buf,
"kerberos v5 shell from %s on %s as %s, cmd '%.80s'", "kerberos v5 shell from %s on %s as %s, cmd '%.80s'",
name, name,
addr_str, addr_str,
server_username, *server_username,
cmd); *cmd);
free (name); free (name);
} }
} }
@@ -650,8 +653,7 @@ doit (void)
socklen_t thisaddr_len, thataddr_len; socklen_t thisaddr_len, thataddr_len;
int port; int port;
int errsock = -1; int errsock = -1;
char client_user[COMMAND_SZ], server_user[USERNAME_SZ]; char *client_user, *server_user, *cmd;
char cmd[COMMAND_SZ];
struct passwd *pwd; struct passwd *pwd;
int s = STDIN_FILENO; int s = STDIN_FILENO;
char **env; char **env;
@@ -725,18 +727,18 @@ doit (void)
#ifdef KRB4 #ifdef KRB4
if ((do_kerberos & DO_KRB4) && if ((do_kerberos & DO_KRB4) &&
recv_krb4_auth (s, buf, thisaddr, thataddr, recv_krb4_auth (s, buf, thisaddr, thataddr,
client_user, &client_user,
server_user, &server_user,
cmd) == 0) &cmd) == 0)
auth_method = AUTH_KRB4; auth_method = AUTH_KRB4;
else else
#endif /* KRB4 */ #endif /* KRB4 */
#ifdef KRB5 #ifdef KRB5
if((do_kerberos & DO_KRB5) && if((do_kerberos & DO_KRB5) &&
recv_krb5_auth (s, buf, thisaddr, thataddr, recv_krb5_auth (s, buf, thisaddr, thataddr,
client_user, &client_user,
server_user, &server_user,
cmd) == 0) &cmd) == 0)
auth_method = AUTH_KRB5; auth_method = AUTH_KRB5;
else else
#endif /* KRB5 */ #endif /* KRB5 */
@@ -746,9 +748,9 @@ doit (void)
if(recv_bsd_auth (s, buf, if(recv_bsd_auth (s, buf,
(struct sockaddr_in *)thisaddr, (struct sockaddr_in *)thisaddr,
(struct sockaddr_in *)thataddr, (struct sockaddr_in *)thataddr,
client_user, &client_user,
server_user, &server_user,
cmd) == 0) { &cmd) == 0) {
auth_method = AUTH_BROKEN; auth_method = AUTH_BROKEN;
if(do_vacuous) { if(do_vacuous) {
printf("Remote host requires Kerberos authentication\n"); printf("Remote host requires Kerberos authentication\n");