Hello, I get a warning during linking about tempnam(3) being insecure. Heres a a patch to write the knownhosts to an already open file stream (which I create with tmpfile(3).
-- Ben Kibbey [XMPP: bjk AT thiessen DOT im] - [IRC: (bjk) FreeNode/OFTC]
>From 89a3f8aa853ef8587b1634d7c9f51c3be61f440c Mon Sep 17 00:00:00 2001 From: Ben Kibbey <[email protected]> Date: Sun, 21 Nov 2010 07:39:21 -0500 Subject: [PATCH] libssh2_knownhost_writefile_fp(). --- docs/libssh2_knownhost_writefile_fp.3 | 30 ++++++++++++++++++++++++++ include/libssh2.h | 10 ++++++++ src/knownhost.c | 38 ++++++++++++++++++++++---------- 3 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 docs/libssh2_knownhost_writefile_fp.3 diff --git a/docs/libssh2_knownhost_writefile_fp.3 b/docs/libssh2_knownhost_writefile_fp.3 new file mode 100644 index 0000000..b672ae0 --- /dev/null +++ b/docs/libssh2_knownhost_writefile_fp.3 @@ -0,0 +1,30 @@ +.\" +.\" Copyright (c) 2009 by Daniel Stenberg +.\" +.TH libssh2_knownhost_writefile_fp 3 "21 Nov 2010" "libssh2 1.2" "libssh2 manual" +.SH NAME +libssh2_knownhost_writefile_fp - write a collection of known hosts to a file stream +.SH SYNOPSIS +#include <libssh2.h> + +int libssh2_knownhost_writefile_fp(LIBSSH2_KNOWNHOSTS *hosts, + FILE *fp, int type); +.SH DESCRIPTION +Writes all the known hosts to the specified file stream using the specified +file format. + +\fIfp\fP an open file stream + +\fItype\fP specifies what file type it is, and +\fILIBSSH2_KNOWNHOST_FILE_OPENSSH\fP is the only currently supported +format. +.SH RETURN VALUE +Returns a regular libssh2 error code, where negative values are error codes +and 0 indicates success. +.SH AVAILABILITY +Added in libssh2 1.2 +.SH SEE ALSO +.BR libssh2_knownhost_readfile(3) +.BR libssh2_knownhost_add(3) +.BR fopen(3) + diff --git a/include/libssh2.h b/include/libssh2.h index e011d49..63ab650 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -974,6 +974,16 @@ libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, char *buffer, size_t buflen, size_t *outlen, /* the amount of written data */ int type); +/* + * libssh2_knownhost_writefile_fp + * + * Write hosts+key pairs to a given file stream. + * + * This implementation currently only knows one 'type' (openssh), all others + * are reserved for future use. + */ +LIBSSH2_API int +libssh2_knownhost_writefile_fp(LIBSSH2_KNOWNHOSTS *hosts, FILE *fp, int type); /* * libssh2_knownhost_writefile diff --git a/src/knownhost.c b/src/knownhost.c index 29f1a1d..9e89f2d 100644 --- a/src/knownhost.c +++ b/src/knownhost.c @@ -1021,16 +1021,15 @@ libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, } /* - * libssh2_knownhost_writefile() + * libssh2_knownhost_writefile_fp() * - * Write hosts+key pairs to the given file. + * Write hosts+key pairs to the given file stream. */ LIBSSH2_API int -libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, - const char *filename, int type) +libssh2_knownhost_writefile_fp(LIBSSH2_KNOWNHOSTS *hosts, + FILE *fp, int type) { struct known_host *node; - FILE *file; int rc = LIBSSH2_ERROR_NONE; char buffer[2048]; @@ -1042,11 +1041,6 @@ libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, "Unsupported type of known-host information " "store"); - file = fopen(filename, "w"); - if(!file) - return _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, - "Failed to open file"); - for(node = _libssh2_list_first(&hosts->head); node; node= _libssh2_list_next(&node->node) ) { @@ -1057,7 +1051,7 @@ libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, if(rc) break; - nwrote = fwrite(buffer, 1, wrote, file); + nwrote = fwrite(buffer, 1, wrote, fp); if(nwrote != wrote) { /* failed to write the whole thing, bail out */ rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, @@ -1065,11 +1059,31 @@ libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, break; } } - fclose(file); return rc; } +/* + * libssh2_knownhost_writefile() + * + * Write hosts+key pairs to the given file. + */ +LIBSSH2_API int +libssh2_knownhost_writefile(LIBSSH2_KNOWNHOSTS *hosts, + const char *filename, int type) +{ + FILE *file = fopen(filename, "w"); + int rc; + + if(!file) + return _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, + "Failed to open file"); + + rc = libssh2_knownhost_writefile_fp(hosts, file, type); + fclose(file); + return rc; +} + /* * libssh2_knownhost_get() -- 1.7.2.3
_______________________________________________ libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
