/* * libssh2_knownhost_writeline() * * Ask libssh2 to convert a known host to an output line for storage. * * Note that this function returns LIBSSH2_ERROR_BUFFER_TOO_SMALL if the given * output buffer is too small to hold the desired output. */ LIBSSH2_API int libssh2_knownhost_writeline(LIBSSH2_KNOWNHOSTS *hosts, struct libssh2_knownhost *known, char *buffer, size_t buflen, size_t *outlen, /* the amount of written data */ int type) { struct known_host *node; if(known->magic != KNOWNHOST_MAGIC) return LIBSSH2_ERROR_INVAL; node = known->node; return knownhost_writeline(hosts, node, buffer, buflen, outlen, type); }
/* * 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) { struct known_host *node; FILE *file; int rc = LIBSSH2_ERROR_NONE; char buffer[2048]; /* we only support this single file type for now, bail out on all other attempts */ if(type != LIBSSH2_KNOWNHOST_FILE_OPENSSH) return _libssh2_error(hosts->session, LIBSSH2_ERROR_METHOD_NOT_SUPPORTED, "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)) { size_t wrote = 0; size_t nwrote; rc = knownhost_writeline(hosts, node, buffer, sizeof(buffer), &wrote, type); if(rc) break; nwrote = fwrite(buffer, 1, wrote, file); if(nwrote != wrote) { /* failed to write the whole thing, bail out */ rc = _libssh2_error(hosts->session, LIBSSH2_ERROR_FILE, "Write failed"); break; } } fclose(file); return rc; }