1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* password.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2001 by Judd Montgomery
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "password.h"
#include "prefs.h"
#include "i18n.h"
struct dialog_data {
GtkWidget *entry;
int button_hit;
char text[PASSWD_LEN+2];
};
#define DIALOG_SAID_1 454
#define DIALOG_SAID_2 455
#ifdef ENABLE_PRIVATE
unsigned char short_salt[]=
{
0x09, 0x02, 0x13, 0x45, 0x07, 0x04, 0x13, 0x44,
0x0C, 0x08, 0x13, 0x5A, 0x32, 0x15, 0x13, 0x5D,
0xD2, 0x17, 0xEA, 0xD3, 0xB5, 0xDF, 0x55, 0x63,
0x22, 0xE9, 0xA1, 0x4A, 0x99, 0x4B, 0x0F, 0x88
};
unsigned char long_salt[]=
{
0xB1, 0x56, 0x35, 0x1A, 0x9C, 0x98, 0x80, 0x84,
0x37, 0xA7, 0x3D, 0x61, 0x7F, 0x2E, 0xE8, 0x76,
0x2A, 0xF2, 0xA5, 0x84, 0x07, 0xC7, 0xEC, 0x27,
0x6F, 0x7D, 0x04, 0xCD, 0x52, 0x1E, 0xCD, 0x5B,
0xB3, 0x29, 0x76, 0x66, 0xD9, 0x5E, 0x4B, 0xCA,
0x63, 0x72, 0x6F, 0xD2, 0xFD, 0x25, 0xE6, 0x7B,
0xC5, 0x66, 0xB3, 0xD3, 0x45, 0x9A, 0xAF, 0xDA,
0x29, 0x86, 0x22, 0x6E, 0xB8, 0x03, 0x62, 0xBC
};
/* len is the length of the bin str, hex_str must be at least twice as long */
void bin_to_hex_str(unsigned char *bin, char *hex_str, int len)
{
int i;
hex_str[0]='\0';
for (i=0; i<len; i++) {
sprintf(&(hex_str[i*2]), "%02x", bin[i]);
}
}
/*
* encoded is a pre-allocated buffer at least 34 bytes long
*/
void palm_encode(unsigned char *ascii, unsigned char *encoded)
{
unsigned char index, shift;
unsigned short temp;
int si, ai, ei; /* salt i, ascii i, encoded i */
int end;
int len;
int m_array[4]={2,16,24,8
};
int mi;
int m;
/* It seems that Palm OS lower cases the password first */
/* Yes, I have found this documented on Palms site */
len = strlen(encoded);
for (ai=0; ai < PASSWD_LEN; ai++) {
ascii[ai] = tolower(ascii[ai]);
}
encoded[0]='\0';
end=0;
if (strlen(ascii)<5) {
si = (ascii[0] + strlen(ascii)) % PASSWD_LEN;
for (ai=ei=0; ei<32; ai++, ei++, si++) {
if (ascii[ai]=='\0') {
end=1;
}
if (end) {
encoded[ei]=short_salt[si%PASSWD_LEN];
} else {
encoded[ei]=ascii[ai] ^ short_salt[si%PASSWD_LEN];
}
}
return;
}
strncpy(encoded, ascii, PASSWD_LEN);
encoded[PASSWD_LEN-1]='\0';
len = strlen(encoded);
for (ai=len; ai < PASSWD_LEN; ai++) {
encoded[ai] = encoded[ai-len] + len;
}
for (mi=0; mi<4; mi++) {
m=m_array[mi];
index = (encoded[m] + encoded[m+1]) & 0x3F;
shift = (encoded[m+2] + encoded[m+3]) & 0x7;
for (ei=0; ei<PASSWD_LEN; ei++) {
temp = long_salt[index%64];
temp <<= 8;
temp |= long_salt[index%64];
temp >>= shift;
encoded[m%32] ^= temp & 0xFF;
m++;
index++;
}
}
}
/*
* hide passed HIDE_PRIVATES will set the hide flag.
* hide passed SHOW_PRIVATES will unset the hide flag and also need a
* correct password.
* hide passed MASK_PRIVATES will unset the hide flag and also need a
* correct password if current state is not SHOW_PRIVATES.
* hide passed GET_PRIVATES will return the current hide flag.
* hide flag is always returned, it is boolean.
*/
int show_privates(int hide, char *password)
{
static int hidden=HIDE_PRIVATES;
unsigned char encoded[34];
unsigned char hex_str[68];
unsigned long ivalue;
const char *pref_password;
if (hide==GET_PRIVATES) {
return hidden;
}
if (hide==HIDE_PRIVATES) {
hidden=HIDE_PRIVATES;
return hidden;
}
if ((hide==MASK_PRIVATES) && (hidden==SHOW_PRIVATES)) {
hidden=MASK_PRIVATES;
return hidden;
}
if ( (hide==SHOW_PRIVATES) ||
((hide==MASK_PRIVATES) && (hidden!=SHOW_PRIVATES)) ) {
/* Need to have the proper password */
if (!password) {
return HIDE_PRIVATES;
}
palm_encode(password, encoded);
bin_to_hex_str(encoded, hex_str, 32);
get_pref(PREF_PASSWORD, &ivalue, &pref_password);
if (!strcmp(hex_str, pref_password)) {
hidden=SHOW_PRIVATES;
}
}
return hidden;
}
#endif
/*
* PASSWORD GUI
*/
/*
* Start of Dialog window code
*/
static void cb_dialog_button(GtkWidget *widget,
gpointer data)
{
struct dialog_data *Pdata;
GtkWidget *w;
int i;
for (w=widget, i=10; w && (i>0); w=w->parent, i--) {
if (GTK_IS_WINDOW(w)) {
Pdata = gtk_object_get_data(GTK_OBJECT(w), "dialog_data");
if (Pdata) {
Pdata->button_hit = GPOINTER_TO_INT(data);
}
gtk_widget_destroy(GTK_WIDGET(w));
}
}
}
static gboolean cb_destroy_dialog(GtkWidget *widget)
{
struct dialog_data *Pdata;
char *entry;
Pdata = gtk_object_get_data(GTK_OBJECT(widget), "dialog_data");
if (!Pdata) {
return TRUE;
}
entry = gtk_entry_get_text(GTK_ENTRY(Pdata->entry));
if (entry) {
strncpy(Pdata->text, entry, PASSWD_LEN);
Pdata->text[PASSWD_LEN]='\0';
}
gtk_main_quit();
return TRUE;
}
/*
* returns 1 if OK was pressed, 2 if cancel was hit
*/
int dialog_password(char *ascii_password)
{
GtkWidget *button, *label;
GtkWidget *hbox1, *vbox1;
GtkWidget *dialog;
GtkWidget *entry;
struct dialog_data *Pdata;
int ret;
if (!ascii_password) {
return -1;
}
ascii_password[0]='\0';
ret = 2;
dialog = gtk_widget_new(GTK_TYPE_WINDOW,
"type", GTK_WINDOW_TOPLEVEL,
"title", _("Palm Password"),
NULL);
gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
gtk_signal_connect(GTK_OBJECT(dialog), "destroy",
GTK_SIGNAL_FUNC(cb_destroy_dialog), dialog);
gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
vbox1 = gtk_vbox_new(FALSE, 2);
gtk_container_set_border_width(GTK_CONTAINER(vbox1), 5);
gtk_container_add(GTK_CONTAINER(dialog), vbox1);
hbox1 = gtk_hbox_new(TRUE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
/* Label */
label = gtk_label_new(_("Enter PalmOS Password"));
gtk_box_pack_start(GTK_BOX(hbox1), label, FALSE, FALSE, 2);
entry = gtk_entry_new_with_max_length(32);
gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_1));
gtk_box_pack_start(GTK_BOX(hbox1), entry, TRUE, TRUE, 1);
/* Button Box */
hbox1 = gtk_hbox_new(TRUE, 2);
gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, FALSE, 2);
/* Buttons */
button = gtk_button_new_with_label(_("OK"));
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_1));
gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 1);
button = gtk_button_new_with_label(_("Cancel"));
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(cb_dialog_button),
GINT_TO_POINTER(DIALOG_SAID_2));
gtk_box_pack_start(GTK_BOX(hbox1), button, TRUE, TRUE, 1);
Pdata = malloc(sizeof(struct dialog_data));
if (Pdata) {
/* Set the default button pressed to CANCEL */
Pdata->button_hit = DIALOG_SAID_2;
Pdata->entry=entry;
Pdata->text[0]='\0';
}
gtk_object_set_data(GTK_OBJECT(dialog), "dialog_data", Pdata);
gtk_widget_grab_focus(GTK_WIDGET(entry));
gtk_widget_show_all(dialog);
gtk_main();
if (Pdata->button_hit==DIALOG_SAID_1) {
ret = 1;
}
if (Pdata->button_hit==DIALOG_SAID_2) {
ret = 2;
}
strncpy(ascii_password, Pdata->text, PASSWD_LEN);
free(Pdata);
return ret;
}
|