#include <ctype.h>
#include <time.h>
+#include <k5-platform.h>
+
#ifdef HAVE_GETCWD
#define getwd(x) getcwd(x,MAXPATHLEN)
#endif
namep = strrchr(shellprog,'/');
if (namep == NULL)
namep = shellprog;
- (void) strcpy(shellnam,"-");
- (void) strncat(shellnam, ++namep, sizeof(shellnam) - 1 - strlen(shellnam));
- shellnam[sizeof(shellnam) - 1] = '\0';
+ (void) snprintf(shellnam, sizeof(shellnam), "-%s", ++namep);
if (strcmp(namep, "sh") != 0)
shellnam[0] = '+';
if (debug) {
#include "pathnames.h"
#include <libpty.h>
+#include <k5-platform.h>
+
#ifdef NEED_SETENV
extern int setenv(char *, char *, int);
#endif
sin4 = &pasv_addr;
goto printaddr;
} else if (usedefault == 0) {
- strcpy(str, " PORT");
sin4 = &data_dest;
printaddr:
a = (u_char *) &sin4->sin_addr;
p = (u_char *) &sin4->sin_port;
#define UC(b) (((int) b) & 0xff)
- sprintf(&str[strlen(str)], " (%d,%d,%d,%d,%d,%d)", UC(a[0]),
- UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1]));
+ snprintf(str, sizeof(str), " PORT (%d,%d,%d,%d,%d,%d)",
+ UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]),
+ UC(p[1]));
#undef UC
} else
strcpy(str, " No data connection");
#include <errno.h>
#include <stdio.h>
#include "krb5.h"
+#include "k5-platform.h"
#include "com_err.h"
#include <netdb.h>
r = krb5_rd_req(telnet_context, &auth_context, &auth,
NULL, keytabid, NULL, &ticket);
if (r) {
- (void) strcpy(errbuf, "krb5_rd_req failed: ");
- errbuf[sizeof(errbuf) - 1] = '\0';
- (void) strncat(errbuf, error_message(r), sizeof(errbuf) - 1 - strlen(errbuf));
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "krb5_rd_req failed: %s",
+ error_message(r));
goto errout;
}
auth_context,
&authenticator);
if (r) {
- (void) strcpy(errbuf,
- "krb5_auth_con_getauthenticator failed: ");
- errbuf[sizeof(errbuf) - 1] = '\0';
- (void) strncat(errbuf, error_message(r), sizeof(errbuf) - 1 - strlen(errbuf));
- goto errout;
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "krb5_auth_con_getauthenticator failed: %s",
+ error_message(r));
+ goto errout;
}
if ((ap->way & AUTH_ENCRYPT_MASK) == AUTH_ENCRYPT_ON &&
!authenticator->checksum) {
r = krb5_auth_con_getkey(telnet_context, auth_context,
&key);
if (r) {
- (void) strcpy(errbuf, "krb5_auth_con_getkey failed: ");
- errbuf[sizeof(errbuf) - 1] = '\0';
- (void) strncat(errbuf, error_message(r), sizeof(errbuf) - 1 - strlen(errbuf));
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "krb5_auth_con_getkey failed: %s",
+ error_message(r));
goto errout;
}
r = krb5_verify_checksum(telnet_context,
* present at this time.
*/
if (r) {
- (void) strcpy(errbuf,
- "checksum verification failed: ");
- errbuf[sizeof(errbuf) - 1] = '\0';
- (void) strncat(errbuf, error_message(r), sizeof(errbuf) - 1 - strlen(errbuf));
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "checksum verification failed: %s",
+ error_message(r));
goto errout;
}
krb5_free_keyblock(telnet_context, key);
/* do ap_rep stuff here */
if ((r = krb5_mk_rep(telnet_context, auth_context,
&outbuf))) {
- (void) strcpy(errbuf, "Make reply failed: ");
- errbuf[sizeof(errbuf) - 1] = '\0';
- (void) strncat(errbuf, error_message(r), sizeof(errbuf) - 1 - strlen(errbuf));
+ (void) snprintf(errbuf, sizeof(errbuf),
+ "Make reply failed: %s",
+ error_message(r));
goto errout;
}
&inbuf, ticket))) {
char kerrbuf[128];
-
- (void) strcpy(kerrbuf, "Read forwarded creds failed: ");
- kerrbuf[sizeof(kerrbuf) - 1] = '\0';
- (void) strncat(kerrbuf, error_message(r),
- sizeof(kerrbuf) - 1 - strlen(kerrbuf));
+
+ (void) snprintf(kerrbuf, sizeof(kerrbuf),
+ "Read forwarded creds failed: %s",
+ error_message(r));
Data(ap, KRB_FORWARD_REJECT, kerrbuf, -1);
if (auth_debug_mode)
printf(
{
char eerrbuf[329];
- strcpy(eerrbuf, "telnetd: ");
- eerrbuf[sizeof(eerrbuf) - 1] = '\0';
- strncat(eerrbuf, errbuf, sizeof(eerrbuf) - 1 - strlen(eerrbuf));
+ snprintf(eerrbuf, sizeof(eerrbuf), "telnetd: %s", errbuf);
Data(ap, KRB_REJECT, eerrbuf, -1);
}
if (auth_debug_mode)
#include <arpa/telnet.h>
#include <stdio.h>
#include "gssapi_defs.h"
+#include "k5-platform.h"
#ifdef __STDC__
#include <stdlib.h>
#endif
if (server) {
str_data[3] = TELQUAL_REPLY;
gethostname(lhostname, sizeof(lhostname));
- strcpy(targ_printable, "SERVICE:rcmd@");
- strncat(targ_printable, lhostname, sizeof(targ_printable) - 1 - 13);
- targ_printable[sizeof(targ_printable) - 1] = '\0';
+ snprintf(targ_printable, sizeof(targ_printable),
+ "SERVICE:rcmd@%s", lhostname);
input_name_buffer.length = strlen(targ_printable);
input_name_buffer.value = targ_printable;
major_status = gss_import_name(&status,
char *address;
printf("[ Trying SPX ... ]\n");
- strcpy(targ_printable, "SERVICE:rcmd@");
- strncat(targ_printable, RemoteHostName, sizeof(targ_printable) - 1 - 13);
- targ_printable[sizeof(targ_printable) - 1] = '\0';
+ snprintf(targ_printable, sizeof(targ_printable), "SERVICE:rcmd@%s",
+ RemoteHostName);
input_name_buffer.length = strlen(targ_printable);
input_name_buffer.value = targ_printable;
gethostname(lhostname, sizeof(lhostname));
- strcpy(targ_printable, "SERVICE:rcmd@");
- strncat(targ_printable, lhostname, sizeof(targ_printable) - 1 - 13);
- targ_printable[sizeof(targ_printable) - 1] = '\0';
+ snprintf(targ_printable, sizeof(targ_printable),
+ "SERVICE:rcmd@%s", lhostname);
input_name_buffer.length = strlen(targ_printable);
input_name_buffer.value = targ_printable;
if (term == NULL || term[0] == 0) {
term = "-";
} else {
- strcpy(termbuf, "TERM=");
- strncat(termbuf, term, sizeof(termbuf) - 6);
- termbuf[sizeof(termbuf) - 1] = '\0';
+ snprintf(termbuf, sizeof(termbuf), "TERM=%s", term);
term = termbuf;
}
argv = addarg(argv, term);
FILE *fp;
char * linebuf;
char *newline;
- int gobble;
+ int gobble, result;
char ** buf_out;
struct stat st_temp;
int count = 0, chunk_count = 1;
if ((pwd = getpwnam(luser)) == NULL) {
return 0;
}
- if (strlen(pwd->pw_dir) + sizeof("/.k5login") > MAXPATHLEN) {
+ result = snprintf(pbuf, sizeof(pbuf), "%s/.k5login", pwd->pw_dir);
+ if (SNPRINTF_OVERFLOW(result, sizeof(pbuf))) {
fprintf (stderr, "home directory path for %s too long\n", luser);
exit (1);
}
- (void) strcpy(pbuf, pwd->pw_dir);
- (void) strcat(pbuf, "/.k5login");
if (stat(pbuf, &st_temp)) { /* not accessible */
return 0;
#endif /* have vasprintf and prototype? */
+/* Return true if the snprintf return value RESULT reflects a buffer
+ overflow for the buffer size SIZE.
+
+ We cast the result to unsigned int for two reasons. First, old
+ implementations of snprintf (such as the one in Solaris 9 and
+ prior) return -1 on a buffer overflow. Casting the result to -1
+ will convert that value to UINT_MAX, which should compare larger
+ than any reasonable buffer size. Second, comparing signed and
+ unsigned integers will generate warnings with some compilers, and
+ can have unpredictable results, particularly when the relative
+ widths of the types is not known (size_t may be the same width as
+ int or larger).
+*/
+#define SNPRINTF_OVERFLOW(result, size) \
+ ((unsigned int)(result) >= (size_t)(size))
+
#ifndef HAVE_MKSTEMP
extern int krb5int_mkstemp(char *);
#define mkstemp krb5int_mkstemp
krb5_keytab kt;
char ktname[MAXPATHLEN+sizeof("WRFILE:")+1];
krb5_error_code retval = 0;
+ int result;
- strcpy(ktname, "WRFILE:");
- if (strlen (name) >= MAXPATHLEN)
+ result = snprintf(ktname, sizeof(ktname), "WRFILE:%s", name);
+ if (SNPRINTF_OVERFLOW(result, sizeof(ktname)))
return ENAMETOOLONG;
- strncat (ktname, name, MAXPATHLEN);
retval = krb5_kt_resolve(context, ktname, &kt);
if (retval)
return retval;
char prompt2[BUFSIZ];
if (verify) {
- strcpy(prompt2, "Verifying, please re-enter ");
- strncat(prompt2, prompt, sizeof(prompt2)-(strlen(prompt2)+1));
- prompt2[sizeof(prompt2)-1] = '\0';
+ snprintf(prompt2, sizeof(prompt2), "Verifying, please re-enter %s",
+ prompt);
}
ok = des_rd_pwstr_2prompt(s, max, prompt, verify ? prompt2 : 0);
return ok;
int statrc;
if (!keyfile) {
- (void) strcpy(defkeyfile, DEFAULT_KEYFILE_STUB);
- (void) strncat(defkeyfile, realm->data,
- min(sizeof(defkeyfile)-sizeof(DEFAULT_KEYFILE_STUB)-1,
- realm->length));
- defkeyfile[sizeof(defkeyfile) - 1] = '\0';
+ (void) snprintf(defkeyfile, sizeof(defkeyfile), "%s%s",
+ DEFAULT_KEYFILE_STUB, realm->data);
keyfile = defkeyfile;
}
if (db_args != NULL) {
(void) strncpy(keyfile, db_args, sizeof(keyfile));
} else {
- (void) strcpy(keyfile, DEFAULT_KEYFILE_STUB);
- (void) strncat(keyfile, realm->data,
- min(sizeof(keyfile)-sizeof(DEFAULT_KEYFILE_STUB)-1,
- realm->length));
+ (void) snprintf(keyfile, sizeof(keyfile), "%s%s",
+ DEFAULT_KEYFILE_STUB, realm->data);
}
/* null terminate no matter what */
keyfile[sizeof(keyfile) - 1] = '\0';
if (kret)
return kret;
- (void) strcpy(scratch, TKT_ROOT);
- (void) strcat(scratch, "XXXXXX");
+ (void) snprintf(scratch, sizeof(scratch), "%sXXXXXX", TKT_ROOT);
ret = mkstemp(scratch);
if (ret == -1) {
k5_cc_mutex_unlock(context, &krb5int_cc_file_mutex);
* trt will happen if the name is passed back to resolve.
*/
{
- memset(name, 0, len);
-
- if (len < strlen(id->ops->prefix)+2)
- return(KRB5_KT_NAME_TOOLONG);
- strcpy(name, id->ops->prefix);
- name += strlen(id->ops->prefix);
- name[0] = ':';
- name++;
- len -= strlen(id->ops->prefix)+1;
+ int result;
- if (len < strlen(KTFILENAME(id))+1)
+ memset(name, 0, len);
+ result = snprintf(name, len, "%s:%s", id->ops->prefix, KTFILENAME(id));
+ if (SNPRINTF_OVERFLOW(result, len))
return(KRB5_KT_NAME_TOOLONG);
- strcpy(name, KTFILENAME(id));
- /* strcpy will NUL-terminate the destination */
-
return(0);
}
krb5_error_code KRB5_CALLCONV
krb5_mkt_get_name(krb5_context context, krb5_keytab id, char *name, unsigned int len)
{
- memset(name, 0, len);
-
- if (len < strlen(id->ops->prefix)+2)
- return(KRB5_KT_NAME_TOOLONG);
- strcpy(name, id->ops->prefix);
- name += strlen(id->ops->prefix);
- name[0] = ':';
- name++;
- len -= strlen(id->ops->prefix)+1;
+ int result;
- if (len < strlen(KTNAME(id))+1)
+ memset(name, 0, len);
+ result = snprintf(name, len, "%s:%s", id->ops->prefix, KTNAME(id));
+ if (SNPRINTF_OVERFLOW(result, len))
return(KRB5_KT_NAME_TOOLONG);
- strcpy(name, KTNAME(id));
- /* strcpy will NUL-terminate the destination */
-
return(0);
}
* trt will happen if the name is passed back to resolve.
*/
{
- memset(name, 0, len);
-
- if (len < strlen(id->ops->prefix)+2)
- return(KRB5_KT_NAME_TOOLONG);
- strcpy(name, id->ops->prefix);
- name += strlen(id->ops->prefix);
- name[0] = ':';
- name++;
- len -= strlen(id->ops->prefix)+1;
+ int result;
- if (len < strlen(KTFILENAME(id))+1)
+ memset(name, 0, len);
+ result = snprintf(name, len, "%s:%s", id->ops->prefix, KTFILENAME(id));
+ if (SNPRINTF_OVERFLOW(result, len))
return(KRB5_KT_NAME_TOOLONG);
- strcpy(name, KTFILENAME(id));
- /* strcpy will NUL-terminate the destination */
-
return(0);
}
if ((ret = krb5_unparse_name(context, client, &clientstr)))
return(ret);
- strcpy(promptstr, "Password for ");
- strncat(promptstr, clientstr, sizeof(promptstr)-strlen(promptstr)-1);
- promptstr[sizeof(promptstr)-1] = '\0';
-
+ snprintf(promptstr, sizeof(promptstr), "Password for %s", clientstr);
free(clientstr);
prompt.prompt = promptstr;