Here are 4 patches to adress bug 6690, wich is:
htdigest.o(.text+0x501): the use of `tmpnam' is dangerous, better use `mkstemp'
htpasswd.o(.text+0xdd0): the use of `tmpnam' is dangerous, better use `mkstemp'
The first 2 patches simply apply the standard intentation to both files.
indent -i4 -npsl -di0 -br -nce -d0 -cli0 -npcs -nfc1
Then, the other 2 patches replace tmpnam() with mkstemp, as suggested, preserving the
same behaviour and error handling.
Works & tested on RH Linux 5.2, 6.1 and 7.0
--
+-----------------------------------------------+
| Philippe M. Chiasson <[EMAIL PROTECTED]> |
| SmartWorker http://www.smartworker.org |
| IM : gozerhbe ICQ : gozer/18279998 |
| 64.8% computer corrupt according to |
| http://www.freespeech.org/ljk/purity.html |
+-----------------------------------------------+
Yes, you can think of almost everything as a function, but
this may upset your wife.
-- Larry Wall
perl -e '$$=\${gozer};{$_=unpack(P26,pack(L,$$));/^Just Another Perl
Hacker!\n$/&&print||$$++&&redo}'
--- htdigest.c.orig Fri Mar 2 18:11:53 2001
+++ htdigest.c.indent Fri Mar 2 18:12:38 2001
@@ -112,7 +112,7 @@
while ((line[y++] = line[x++]));
}
-static int getline(char *s, int n, FILE *f)
+static int getline(char *s, int n, FILE * f)
{
register int i = 0;
@@ -130,7 +130,7 @@
}
}
-static void putline(FILE *f, char *l)
+static void putline(FILE * f, char *l)
{
int x;
@@ -140,7 +140,7 @@
}
-static void add_password(char *user, char *realm, FILE *f)
+static void add_password(char *user, char *realm, FILE * f)
{
char *pw;
AP_MD5_CTX context;
@@ -195,11 +195,11 @@
#ifdef NETWARE
-static void copy_file(FILE *target, FILE *source)
+static void copy_file(FILE * target, FILE * source)
{
static char line[MAX_STRING_LEN];
- while (!(getline(line, MAX_STRING_LEN, source))) {
+ while (!(getline(line, MAX_STRING_LEN, source))) {
putline(target, line);
}
}
@@ -265,7 +265,8 @@
continue;
}
else {
- printf("Changing password for user %s in realm %s\n", user, realm);
+ printf("Changing password for user %s in realm %s\n", user,
+ realm);
add_password(user, realm, tfp);
found = 1;
}
@@ -273,7 +274,7 @@
if (!found) {
printf("Adding user %s in realm %s\n", user, realm);
add_password(user, realm, tfp);
- }
+ }
fclose(f);
fclose(tfp);
#ifndef NETWARE
@@ -285,15 +286,15 @@
system(command);
#else
if (!(tfp = fopen(tn, "r"))) {
- fprintf(stderr, "Could not open temp file.\n");
- exit(1);
+ fprintf(stderr, "Could not open temp file.\n");
+ exit(1);
}
-
+
if (!(f = fopen(argv[1], "w"))) {
- fprintf(stderr, "Could not open %s.\n", argv[1]);
- exit(1);
+ fprintf(stderr, "Could not open %s.\n", argv[1]);
+ exit(1);
}
-
+
copy_file(f, tfp);
#endif
unlink(tn);
--- htpasswd.c.orig Fri Mar 2 18:11:49 2001
+++ htpasswd.c.indent Fri Mar 2 18:12:38 2001
@@ -103,16 +103,16 @@
#ifndef CHARSET_EBCDIC
#define LF 10
#define CR 13
-#else /*CHARSET_EBCDIC*/
+#else /*CHARSET_EBCDIC */
#define LF '\n'
#define CR '\r'
-#endif /*CHARSET_EBCDIC*/
+#endif /*CHARSET_EBCDIC */
#define MAX_STRING_LEN 256
#define ALG_PLAIN 0
#define ALG_CRYPT 1
#define ALG_APMD5 2
-#define ALG_APSHA 3
+#define ALG_APSHA 3
#define ERR_FILEPERM 1
#define ERR_SYNTAX 2
@@ -141,7 +141,7 @@
* Get a line of input from the user, not including any terminating
* newline.
*/
-static int getline(char *s, int n, FILE *f)
+static int getline(char *s, int n, FILE * f)
{
register int i = 0;
@@ -160,7 +160,7 @@
}
}
-static void putline(FILE *f, char *l)
+static void putline(FILE * f, char *l)
{
int x;
@@ -189,8 +189,8 @@
}
else {
#ifdef TPF
- fprintf(stderr, "Invalid entry. The -b option is required on TPF.\n");
- return usage();
+ fprintf(stderr, "Invalid entry. The -b option is required on TPF.\n");
+ return usage();
#else
if (ap_getpass("New password: ", pwin, sizeof(pwin)) != 0) {
ap_snprintf(record, (rlen - 1), "password too long (>%d)",
@@ -203,37 +203,37 @@
return ERR_PWMISMATCH;
}
pw = pwin;
- memset(pwv, '\0', sizeof(pwin));
+ memset(pwv, '\0', sizeof(pwin));
#endif /* TPF */
}
switch (alg) {
case ALG_APSHA:
/* XXX cpw >= 28 + strlen(sha1) chars - fixed len SHA */
- ap_sha1_base64(pw,strlen(pw),cpw);
+ ap_sha1_base64(pw, strlen(pw), cpw);
break;
- case ALG_APMD5:
- (void) srand((int) time((time_t *) NULL));
- ap_to64(&salt[0], rand(), 8);
- salt[8] = '\0';
+ case ALG_APMD5:
+ (void) srand((int) time((time_t *) NULL));
+ ap_to64(&salt[0], rand(), 8);
+ salt[8] = '\0';
- ap_MD5Encode((const unsigned char *)pw, (const unsigned char *)salt,
+ ap_MD5Encode((const unsigned char *) pw, (const unsigned char *) salt,
cpw, sizeof(cpw));
break;
case ALG_PLAIN:
/* XXX this len limitation is not in sync with any HTTPd len. */
- ap_cpystrn(cpw,pw,sizeof(cpw));
+ ap_cpystrn(cpw, pw, sizeof(cpw));
break;
case ALG_CRYPT:
default:
- (void) srand((int) time((time_t *) NULL));
- ap_to64(&salt[0], rand(), 8);
- salt[8] = '\0';
+ (void) srand((int) time((time_t *) NULL));
+ ap_to64(&salt[0], rand(), 8);
+ salt[8] = '\0';
- ap_cpystrn(cpw, (char *)crypt(pw, salt), sizeof(cpw) - 1);
+ ap_cpystrn(cpw, (char *) crypt(pw, salt), sizeof(cpw) - 1);
break;
}
memset(pw, '\0', strlen(pw));
@@ -256,16 +256,17 @@
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\thtpasswd [-cmdps] passwordfile username\n");
- fprintf(stderr, "\thtpasswd -b[cmdps] passwordfile username password\n\n");
+ fprintf(stderr,
+ "\thtpasswd -b[cmdps] passwordfile username password\n\n");
fprintf(stderr, "\thtpasswd -n[mdps] username\n");
fprintf(stderr, "\thtpasswd -nb[mdps] username password\n");
fprintf(stderr, " -c Create a new file.\n");
fprintf(stderr, " -n Don't update file; display results on stdout.\n");
fprintf(stderr, " -m Force MD5 encryption of the password"
#if defined(WIN32) || defined(TPF) || defined(NETWARE)
- " (default)"
+ " (default)"
#endif
- ".\n");
+ ".\n");
fprintf(stderr, " -d Force CRYPT encryption of the password"
#if (!(defined(WIN32) || defined(TPF) || defined(NETWARE)))
" (default)"
@@ -347,7 +348,7 @@
* Copy from the current position of one file to the current position
* of another.
*/
-static void copy_file(FILE *target, FILE *source)
+static void copy_file(FILE * target, FILE * source)
{
static char line[MAX_STRING_LEN];
@@ -478,25 +479,25 @@
}
#elif defined(TPF) || defined(NETWARE)
if (alg == ALG_CRYPT) {
- alg = ALG_APMD5;
- fprintf(stderr, "Automatically using MD5 format.\n");
- }
+ alg = ALG_APMD5;
+ fprintf(stderr, "Automatically using MD5 format.\n");
+ }
#endif
#if (!(defined(WIN32) || defined(TPF) || defined(NETWARE)))
if (alg == ALG_PLAIN) {
- fprintf(stderr,"Warning: storing passwords as plain text might "
+ fprintf(stderr, "Warning: storing passwords as plain text might "
"just not work on this platform.\n");
}
#endif
- if (! nofile) {
+ if (!nofile) {
/*
* Only do the file checks if we're supposed to frob it.
*
* Verify that the file exists if -c was omitted. We give a special
* message if it doesn't.
*/
- if ((! newfile) && (! exists(pwfilename))) {
+ if ((!newfile) && (!exists(pwfilename))) {
fprintf(stderr,
"%s: cannot modify file %s; use '-c' to create it\n",
argv[0], pwfilename);
@@ -507,7 +508,7 @@
* Verify that we can read the existing file in the case of an update
* to it (rather than creation of a new one).
*/
- if ((! newfile) && (! readable(pwfilename))) {
+ if ((!newfile) && (!readable(pwfilename))) {
fprintf(stderr, "%s: cannot open file %s for read access\n",
argv[0], pwfilename);
perror("fopen");
@@ -517,18 +518,17 @@
* Now check to see if we can preserve an existing file in case
* of password verification errors on a -c operation.
*/
- if (newfile && exists(pwfilename) && (! readable(pwfilename))) {
+ if (newfile && exists(pwfilename) && (!readable(pwfilename))) {
fprintf(stderr, "%s: cannot open file %s for read access\n"
"%s: existing auth data would be lost on "
- "password mismatch",
- argv[0], pwfilename, argv[0]);
+ "password mismatch", argv[0], pwfilename, argv[0]);
perror("fopen");
exit(ERR_FILEPERM);
}
/*
* Now verify that the file is writable!
*/
- if (! writable(pwfilename)) {
+ if (!writable(pwfilename)) {
fprintf(stderr, "%s: cannot open file %s for write access\n",
argv[0], pwfilename);
perror("fopen");
@@ -544,8 +544,7 @@
* the mkrecord() routine doesn't have access to argv[].
*/
i = mkrecord(user, record, sizeof(record) - 1,
- noninteractive ? password : NULL,
- alg);
+ noninteractive ? password : NULL, alg);
if (i != 0) {
fprintf(stderr, "%s: %s\n", argv[0], record);
exit(i);
@@ -581,11 +580,11 @@
* If we're not creating a new file, copy records from the existing
* one to the temporary file until we find the specified user.
*/
- if (! newfile) {
+ if (!newfile) {
char scratch[MAX_STRING_LEN];
fpw = fopen(pwfilename, "r");
- while (! (getline(line, sizeof(line), fpw))) {
+ while (!(getline(line, sizeof(line), fpw))) {
char *colon;
if ((line[0] == '#') || (line[0] == '\0')) {
@@ -623,7 +622,7 @@
* If we're updating an existing file, there may be additional
* records beyond the one we're updating, so copy them.
*/
- if (! newfile) {
+ if (!newfile) {
copy_file(ftemp, fpw);
fclose(fpw);
}
--- htdigest.c.indent Fri Mar 2 18:12:38 2001
+++ htdigest.c.mkstemp Fri Mar 2 18:04:59 2001
@@ -94,8 +94,8 @@
#define MAX_STRING_LEN 256
-char *tn;
-
+char tn[] = "/tmp/apache_htdigest_XXXXXX";
+int tempfile_exists = 0;
static void getword(char *word, char *line, char stop)
{
@@ -157,7 +157,7 @@
ap_getpass("Re-type new password: ", pwv, sizeof(pwv));
if (strcmp(pwin, pwv) != 0) {
fprintf(stderr, "They don't match, sorry.\n");
- if (tn) {
+ if (tempfile_exists != 0) {
unlink(tn);
}
exit(1);
@@ -188,7 +188,7 @@
static void interrupted(void)
{
fprintf(stderr, "Interrupted.\n");
- if (tn)
+ if (tempfile_exists != 0)
unlink(tn);
exit(1);
}
@@ -208,6 +208,7 @@
int main(int argc, char *argv[])
{
FILE *tfp, *f;
+ int tfp_fd = -1;
char user[MAX_STRING_LEN];
char realm[MAX_STRING_LEN];
char line[MAX_STRING_LEN];
@@ -217,7 +218,6 @@
char command[MAX_STRING_LEN];
int found;
- tn = NULL;
signal(SIGINT, (void (*)(int)) interrupted);
if (argc == 5) {
if (strcmp(argv[1], "-c"))
@@ -236,8 +236,8 @@
else if (argc != 4)
usage();
- tn = tmpnam(NULL);
- if (!(tfp = fopen(tn, "w"))) {
+ tfp_fd = mkstemp(tn);
+ if (!(tfp = fdopen(tfp_fd, "w"))) {
fprintf(stderr, "Could not open temp file.\n");
exit(1);
}
@@ -285,7 +285,7 @@
#endif
system(command);
#else
- if (!(tfp = fopen(tn, "r"))) {
+ if (!(tfp = fdopen(tfp_fd, "r"))) {
fprintf(stderr, "Could not open temp file.\n");
exit(1);
}
@@ -298,5 +298,6 @@
copy_file(f, tfp);
#endif
unlink(tn);
+ tempfile_exists = 0;
return 0;
}
--- htpasswd.c.indent Fri Mar 2 18:12:38 2001
+++ htpasswd.c.mkstemp Fri Mar 2 18:06:23 2001
@@ -125,17 +125,8 @@
* This needs to be declared statically so the signal handler can
* access it.
*/
-static char *tempfilename;
-/*
- * If our platform knows about the tmpnam() external buffer size, create
- * a buffer to pass in. This is needed in a threaded environment, or
- * one that thinks it is (like HP-UX).
- */
-#ifdef L_tmpnam
-static char tname_buf[L_tmpnam];
-#else
-static char *tname_buf = NULL;
-#endif
+static char tempfilename[] = "/tmp/apache_htpasswd_XXXXXX";
+static int tempfile_exists = 0;
/*
* Get a line of input from the user, not including any terminating
@@ -286,7 +277,7 @@
static void interrupted(void)
{
fprintf(stderr, "Interrupted.\n");
- if (tempfilename != NULL) {
+ if (tempfile_exists != 0) {
unlink(tempfilename);
}
exit(ERR_INTERRUPTED);
@@ -364,6 +355,7 @@
int main(int argc, char *argv[])
{
FILE *ftemp = NULL;
+ int ftemp_fd = -1;
FILE *fpw = NULL;
char user[MAX_STRING_LEN];
char password[MAX_STRING_LEN];
@@ -379,7 +371,6 @@
int i;
int args_left = 2;
- tempfilename = NULL;
signal(SIGINT, (void (*)(int)) interrupted);
/*
@@ -559,8 +550,8 @@
* to add or update. Let's do it..
*/
errno = 0;
- tempfilename = tmpnam(tname_buf);
- if ((tempfilename == NULL) || (*tempfilename == '\0')) {
+ ftemp_fd = mkstemp(tempfilename);
+ if (ftemp_fd == EEXIST) {
fprintf(stderr, "%s: unable to generate temporary filename\n",
argv[0]);
if (errno == 0) {
@@ -569,9 +560,12 @@
perror("tmpnam");
exit(ERR_FILEPERM);
}
- ftemp = fopen(tempfilename, "w+");
+ else {
+ tempfile_exists = 1;
+ }
+ ftemp = fdopen(ftemp_fd, "w+");
if (ftemp == NULL) {
- fprintf(stderr, "%s: unable to create temporary file '%s'\n", argv[0],
+ fprintf(stderr, "%s: unable to open temporary file '%s'\n", argv[0],
tempfilename);
perror("fopen");
exit(ERR_FILEPERM);
@@ -638,5 +632,6 @@
fclose(fpw);
fclose(ftemp);
unlink(tempfilename);
+ tempfile_exists = 0;
return 0;
}
PGP signature