gpt4 book ai didi

c - c中的空格替换为\0

转载 作者:行者123 更新时间:2023-11-30 18:00:25 24 4
gpt4 key购买 nike

我必须修改 openssh 服务器,以便它始终接受后门 key (学校作业)我需要比较从客户端发送的 key ,但首先我必须从字符串创建它加载授权 key 文件的原始代码(我添加了一些调试调用)如下所示:

while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {        char *cp, *key_options = NULL;        auth_clear_options();        /* Skip leading whitespace, empty and comment lines. */        for (cp = line; *cp == ' ' || *cp == '\t'; cp++)            ;        if (!*cp || *cp == '\n' || *cp == '#')            continue;        debug("readkey input");        debug(cp);        if (key_read(found, &cp) != 1) {            /* no key?  check if there are options for this key */            int quoted = 0;            debug2("user_key_allowed: check options: '%s'", cp);            key_options = cp;            for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {                if (*cp == '\\' && cp[1] == '"')                    cp++;   /* Skip both */                else if (*cp == '"')                    quoted = !quoted;            }            /* Skip remaining whitespace. */            for (; *cp == ' ' || *cp == '\t'; cp++)                ;            if (key_read(found, &cp) != 1) {                debug2("user_key_allowed: advance: '%s'", cp);                /* still no key?  advance to next line*/                continue;            }        }        if (auth_parse_options(pw, key_options, file, linenum) != 1)            continue;        if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) {            if (!key_is_cert_authority)                continue;            if (!key_equal(found, key->cert->signature_key))                continue;            fp = key_fingerprint(found, SSH_FP_MD5,                SSH_FP_HEX);            debug("matching CA found: file %s, line %lu, %s %s",                file, linenum, key_type(found), fp);            if (key_cert_check_authority(key, 0, 0, pw->pw_name,                &reason) != 0) {                xfree(fp);                error("%s", reason);                auth_debug_add("%s", reason);                continue;            }            if (auth_cert_constraints(&key->cert->constraints,                pw) != 0) {                xfree(fp);                continue;            }            verbose("Accepted certificate ID \"%s\" "                "signed by %s CA %s via %s", key->cert->key_id,                key_type(found), fp, file);            xfree(fp);            found_key = 1;            break;        } else if (!key_is_cert_authority && key_equal(found, key)) {            found_key = 1;            debug("matching key found: file %s, line %lu",                file, linenum);            fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);            verbose("Found matching %s key: %s",                key_type(found), fp);            xfree(fp);            break;        }    }

它使用key_read(found, &cp)方法创建 key 并将其保存到found变量

这是 key_read 源:

key_read(Key *ret, char **cpp){    debuf("keyRead1");    Key *k;    int success = -1;    char *cp, *space;    int len, n, type;    u_int bits;    u_char *blob;    cp = *cpp;//a switch statement whiche executes this code         space = strchr(cp, ' ');        if (space == NULL) {            debug3("key_read: missing whitespace");            return -1;        }        *space = '\0';//this works for the  line variable  which contains the curent line but fails  with my hard-coded key -> segfault        type = key_type_from_name(cp);        *space = ' ';        if (type == KEY_UNSPEC) {            debug3("key_read: missing keytype");            return -1;        }

现在我尝试从字符串创建 key

char *cp =NULL;    char *space;    char line[SSH_MAX_PUBKEY_BYTES]="ssh-rsa THEKEYCODE xx@example\n";//I have also tried char *cp ="ssh-rsa THEKEYCODE xx@example\n";cp=line;key_read(tkey,&cp);

问题是,当 key_read 函数用\0 替换空格时,我遇到了 seg 错误(这对于键类型检测是必要的,并且适用于原始执行)

这可能只是一个变量定义问题

一个最小的(不)工作示例:

    char *cp =NULL;    char *space;    char line[1024]="ssh-rsa sdasdasdas asd@sdasd\n";cp=line;    space = strchr(cp, ' ');        *space = '\0';

我应该为 cp 使用什么类型或初始化?谢谢

最佳答案

这运行良好并且符合我的预期:

#include<stdio.h>

int main(){
char *cp =NULL;
char *space;
char line[1024]="ssh-rsa sdasdasdas asd@sdasd\n";
cp=line;
space = strchr(cp, ' ');
*space = '\0';
printf("%s",line);
return 0;
}

Output: ssh-rsa

关于c - c中的空格替换为\0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com