修改Putty 0.6 代码支持SSH 密码保存功能

来源:互联网 发布:python获取日期的月份 编辑:程序博客网 时间:2024/05/14 08:08
      很早之前曾经修改过putty 的代码,使得telenet 的输出能够加入时间戳。 自己一向都是通过public key 的方式来完成SSH 登录,也就没有想过添加password 选项,但我知道我的很多同事每次在使用putty login 的时候,都不厌其烦的输入password ,他们还是有类似的需求。
   
====> config.c 

    1、setup_config_box  :
       putty 的界面的处理方式是很值得研究研究的。 首先要考虑在原有的界面上把新加的输入passowrd 的文本框放在什么地方,个人认为把新加入的文本框放在原有的输入port 端口后面的文本框后面是一个比较好的选择。
       修改: line :1155 行的 ctrl_columns(s, 2, 75, 25); 改为  ctrl_columns(s, 3, 50, 25,25);     后面的数字只是一个比例。
       添加: line : 1165 后加入:

        c = ctrl_editbox(s, HOST_PASSWORD, NO_SHORTCUT ,100,
        HELPCTX(session_hostname),
        configure_userpassword_handler, I(0), I(0));
        c->editbox.password = 1;
        c->generic.column = 2;
        hp->username = c;

       在文件开始处定义 HOST_PASSWORD 这个宏,如果不定义的话,上面使用这个宏的地方,用字符串自己代替。
       #define HOST_PASSWORD "Password"

    2、 这里加入一个输入 password 的输入文本框。 随便找个地方加入函数 configure_userpassword_handler ,这个函数是对passowrd 输入框的一些控制。

     static void configure_userpassword_handler(union control * ctrl, void *dlg, void * data, int event)
       {
    Config * cfg = (Config *) data ;
    if(event == EVENT_REFRESH) {
        HWND hWnd = dlg_get_control_handle(ctrl,dlg);
        if(cfg->protocol == PROT_SERIAL) { /* don't need password for serail port */           
            if(hWnd) EnableWindow(hWnd, 0);
        } else {
            if(hWnd) EnableWindow(hWnd, 1);
            dlg_editbox_set(ctrl, dlg, cfg->password);
        }
    } else if( event == EVENT_VALCHANGE) {
        if(cfg->protocol != PROT_SERIAL) {
            dlg_editbox_get(ctrl,dlg,cfg->password,lenof(cfg->password));
        }
    }   
     }

   3、修改  struct hostport 结构 为 :
    struct hostport {
        union control *host, *port , *username;
    };

   4、修改 config_protocolbuttons_handler : 在dlg_refresh(hp->port, dlg); 后面加入  dlg_refresh(hp->username,dlg);


===> wintrls.c

   加入函数:
   HWND dlg_get_control_handle(union control * ctrl, void *dlg)
   {
    struct dlgparam * dp = (struct dlgparam *) dlg;
    struct winctrl *c = dlg_findbyctrl(dp, ctrl);
    HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
    return hw;
   }
   这个函数的目的是可以得到文本框的window 句柄。

====> dialog.h

    加入函数的声明:    HWND dlg_get_control_handle(union control * ctrl, void *dlg) ;
 
====> putty.h

  修改 struct config_tag : 加入    char password[104];  (Putty 中允许的最长的密码是 100 个字符)

====> settings.c
   在函数 save_open_settings 中添加:     write_setting_b(sesskey,"Password",cfg->password);
   在函数 load_open_settings 中加入:     gpps(sesskey,"Password","", cfg->password,sizeof(cfg->password));   

====> ssh.c
    在函数 do_ssh1_login 中 3861  行后面的代码改为:
    如果保存的密码是空的话,这按照原来流程处理,否则直接拷贝保存的密码。 后面的 if(!ret) 流程不变。
 
            if(strlen(ssh->cfg.password) == 0 ) {
            ret = get_userpass_input(s->cur_prompt, NULL, 0);
            while (ret < 0) {
                ssh->send_ok = 1;
                crWaitUntil(!pktin);
                ret = get_userpass_input(s->cur_prompt, in, inlen);
                ssh->send_ok = 0;
            }
        } else {
            ret = 1;
            strcpy(s->cur_prompt->prompts[0]->result, ssh->cfg.password);   
        }


      在函数 do_ssh2_authconn 中 7926 行后面的代码改为:

               if(strlen(ssh->cfg.password) == 0 ) {
            ret = get_userpass_input(s->cur_prompt, NULL, 0);
            while (ret < 0) {
                ssh->send_ok = 1;
                crWaitUntilV(!pktin);
                ret = get_userpass_input(s->cur_prompt, in, inlen);
                ssh->send_ok = 0;
            }
        }
        else {
            ret = 1;
            strcpy(s->cur_prompt->prompts[0]->result, ssh->cfg.password);
        }

====> version.c
    在文件开始加入 #define RELEASE    0.60