Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
L
libplist
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pwn
libplist
Commits
c58482a7
Commit
c58482a7
authored
Aug 03, 2008
by
Jonathan Beck
Committed by
Matt Colyer
Aug 06, 2008
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prepare config for pairing
Signed-off-by:
Matt Colyer
<
matt@colyer.name
>
parent
b6ed5447
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
265 additions
and
9 deletions
+265
-9
userpref.c
src/userpref.c
+221
-9
userpref.h
src/userpref.h
+44
-0
No files found.
src/userpref.c
View file @
c58482a7
...
@@ -29,27 +29,239 @@
...
@@ -29,27 +29,239 @@
extern
int
debug
;
extern
int
debug
;
inline
void
create_config_dir
()
{
gchar
*
config_dir
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
NULL
);
g_mkdir_with_parents
(
config_dir
,
755
);
return
;
}
char
*
get_host_id
()
char
*
get_host_id
()
{
{
char
*
host_id
=
NULL
;
char
*
host_id
=
NULL
;
gchar
*
config_file
=
NULL
;
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
/* now parse file to get the HostID */
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
*
loc_host_id
=
g_key_file_get_value
(
key_file
,
"Global"
,
"HostID"
,
NULL
);
if
(
loc_host_id
)
host_id
=
strdup
((
char
*
)
loc_host_id
);
g_free
(
loc_host_id
);
}
g_key_file_free
(
key_file
);
if
(
debug
)
printf
(
"Using %s as HostID
\n
"
,
host_id
);
return
host_id
;
}
int
is_device_known
(
char
*
public_key
)
{
int
ret
=
0
;
/* first get config file */
/* first get config file */
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
/* now parse file to get
the HostID
*/
/* now parse file to get
knwon devices list
*/
GKeyFile
*
key_file
=
g_key_file_new
();
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
*
loc_host_id
=
g_key_file_get_value
(
key_file
,
"Global"
,
"HostID"
,
NULL
);
gchar
**
devices_list
=
g_key_file_get_string_list
(
key_file
,
"Global"
,
"DevicesList"
,
NULL
,
NULL
);
if
(
loc_host_id
)
if
(
devices_list
)
{
host_id
=
strdup
((
char
*
)
loc_host_id
);
gchar
**
pcur
=
devices_list
;
g_free
(
loc_host_id
);
while
(
*
pcur
&&
!
ret
)
{
/* open associated base64 encoded key */
gchar
*
keyfilepath
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
*
pcur
,
NULL
);
if
(
g_file_test
(
keyfilepath
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
GIOChannel
*
keyfile
=
g_io_channel_new_file
(
keyfilepath
,
"r"
,
NULL
);
gchar
*
stored_key
=
NULL
;
g_io_channel_read_to_end
(
keyfile
,
&
stored_key
,
NULL
,
NULL
);
/* now compare to input */
if
(
strcmp
(
public_key
,
stored_key
)
==
2
)
ret
=
1
;
g_free
(
stored_key
);
g_io_channel_shutdown
(
keyfile
,
FALSE
,
NULL
);
pcur
++
;
}
}
}
g_strfreev
(
devices_list
);
}
}
g_key_file_free
(
key_file
);
g_key_file_free
(
key_file
);
}
}
if
(
debug
)
printf
(
"Using %s as HostID
\n
"
,
host_id
);
return
ret
;
return
host_id
;
}
int
store_device_public_key
(
char
*
public_key
)
{
if
(
NULL
==
public_key
||
is_device_known
(
public_key
))
return
0
;
/* first get config file */
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
**
devices_list
=
g_key_file_get_string_list
(
key_file
,
"Global"
,
"DevicesList"
,
NULL
,
NULL
);
guint
length
=
0
;
if
(
devices_list
)
g_strv_length
(
devices_list
);
g_strfreev
(
devices_list
);
gchar
dev_file
[
20
];
g_sprintf
(
dev_file
,
"Device%i"
,
length
);
gchar
*
device_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
dev_file
,
NULL
);
GIOChannel
*
file
=
g_io_channel_new_file
(
device_file
,
"w"
,
NULL
);
g_io_channel_write_chars
(
file
,
public_key
,
length
,
NULL
,
NULL
);
g_io_channel_shutdown
(
file
,
FALSE
,
NULL
);
/* append device to list */
gchar
**
new_devices_list
=
(
gchar
**
)
g_malloc
(
sizeof
(
gchar
*
)
*
(
length
+
1
));
int
i
;
for
(
i
=
0
;
i
<
length
;
i
++
)
new_devices_list
[
i
]
=
devices_list
[
i
];
new_devices_list
[
length
]
=
dev_file
;
new_devices_list
[
length
+
1
]
=
NULL
;
g_key_file_set_string_list
(
key_file
,
"Global"
,
"DevicesList"
,
new_devices_list
,
length
+
1
);
g_free
(
new_devices_list
);
}
gsize
length
;
gchar
*
buf
=
g_key_file_to_data
(
key_file
,
&
length
,
NULL
);
GIOChannel
*
file
=
g_io_channel_new_file
(
config_file
,
"w"
,
NULL
);
g_io_channel_write_chars
(
file
,
buf
,
length
,
NULL
,
NULL
);
g_io_channel_shutdown
(
file
,
FALSE
,
NULL
);
g_key_file_free
(
key_file
);
}
return
1
;
}
char
*
get_root_private_key
()
{
char
*
private_key
=
NULL
;
/* first get config file */
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
/* now parse file to get knwon devices list */
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
*
loc_private_key
=
g_key_file_get_value
(
key_file
,
"Global"
,
"RootPrivateKey"
,
NULL
);
if
(
loc_private_key
)
private_key
=
strdup
((
char
*
)
loc_private_key
);
g_free
(
loc_private_key
);
}
g_key_file_free
(
key_file
);
}
return
private_key
;
}
}
char
*
get_host_private_key
()
{
char
*
private_key
=
NULL
;
/* first get config file */
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
/* now parse file to get knwon devices list */
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
*
loc_private_key
=
g_key_file_get_value
(
key_file
,
"Global"
,
"HostPrivateKey"
,
NULL
);
if
(
loc_private_key
)
private_key
=
strdup
((
char
*
)
loc_private_key
);
g_free
(
loc_private_key
);
}
g_key_file_free
(
key_file
);
}
return
private_key
;
}
char
*
get_root_certificate
()
{
char
*
cert
=
NULL
;
/* first get config file */
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
/* now parse file to get knwon devices list */
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
*
loc_cert
=
g_key_file_get_value
(
key_file
,
"Global"
,
"RootCertificate"
,
NULL
);
if
(
loc_cert
)
cert
=
strdup
((
char
*
)
loc_cert
);
g_free
(
loc_cert
);
}
g_key_file_free
(
key_file
);
}
return
cert
;
}
char
*
get_host_certificate
()
{
char
*
cert
=
NULL
;
/* first get config file */
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
if
(
g_file_test
(
config_file
,
(
G_FILE_TEST_EXISTS
|
G_FILE_TEST_IS_REGULAR
)))
{
/* now parse file to get knwon devices list */
GKeyFile
*
key_file
=
g_key_file_new
();
if
(
g_key_file_load_from_file
(
key_file
,
config_file
,
G_KEY_FILE_KEEP_COMMENTS
,
NULL
)
)
{
gchar
*
loc_cert
=
g_key_file_get_value
(
key_file
,
"Global"
,
"HostCertificate"
,
NULL
);
if
(
loc_cert
)
cert
=
strdup
((
char
*
)
loc_cert
);
g_free
(
loc_cert
);
}
g_key_file_free
(
key_file
);
}
return
cert
;
}
int
init_config_file
(
char
*
host_id
,
char
*
root_private_key
,
char
*
host_private_key
,
char
*
root_cert
,
char
*
host_cert
)
{
if
(
!
host_id
||
!
root_private_key
||
!
host_private_key
)
return
0
;
gchar
*
config_file
=
g_build_path
(
G_DIR_SEPARATOR_S
,
g_get_user_config_dir
(),
LIBIPHONE_CONF_DIR
,
LIBIPHONE_CONF_FILE
,
NULL
);
/* make sure config directory exists*/
create_config_dir
();
/* now parse file to get the HostID */
GKeyFile
*
key_file
=
g_key_file_new
();
/* store in config file */
g_key_file_set_value
(
key_file
,
"Global"
,
"HostID"
,
host_id
);
g_key_file_set_value
(
key_file
,
"Global"
,
"RootPrivateKey"
,
root_private_key
);
g_key_file_set_value
(
key_file
,
"Global"
,
"HostPrivateKey"
,
host_private_key
);
g_key_file_set_value
(
key_file
,
"Global"
,
"RootCertificate"
,
root_cert
);
g_key_file_set_value
(
key_file
,
"Global"
,
"HostCertificate"
,
host_cert
);
/* write config file on disk */
gsize
length
;
gchar
*
buf
=
g_key_file_to_data
(
key_file
,
&
length
,
NULL
);
GIOChannel
*
file
=
g_io_channel_new_file
(
config_file
,
"w"
,
NULL
);
g_io_channel_write_chars
(
file
,
buf
,
length
,
NULL
,
NULL
);
g_io_channel_shutdown
(
file
,
FALSE
,
NULL
);
g_key_file_free
(
key_file
);
return
1
;
}
src/userpref.h
View file @
c58482a7
...
@@ -29,4 +29,48 @@
...
@@ -29,4 +29,48 @@
*/
*/
char
*
get_host_id
();
char
*
get_host_id
();
/**
* \fn int is_device_known(char* public_key)
* determine if we already paired this device.
* \return 1 if device is already paired. Returns 0 otherwise.
*/
int
is_device_known
(
char
*
public_key
);
/**
* \fn int store_device_public_key(char* public_key)
* \return 1 if everything went well. Returns 0 otherwise.
*/
int
store_device_public_key
(
char
*
public_key
);
/**
* \fn char* get_root_private_key()
* \return RootPrivateKey if exists. Returns NULL otherwise.
*/
char
*
get_root_private_key
();
/**
* \fn char* get_host_private_key()
* \return HostPrivateKey if exists. Returns NULL otherwise.
*/
char
*
get_host_private_key
();
/**
* \fn char* get_root_certificate()
* \return RootCertificate if exists. Returns NULL otherwise.
*/
char
*
get_root_certificate
();
/**
* \fn char* get_host_certificate()
* \return HostCertificate if exists. Returns NULL otherwise.
*/
char
*
get_host_certificate
();
/**
* \fn int init_config_file(char* host_id, char* root_private_key, char* host_private_key, char* root_cert, char* host_cert)
* setup a brand new config file.
* \return 1 if everything went well. Returns 0 otherwise.
*/
int
init_config_file
(
char
*
host_id
,
char
*
root_private_key
,
char
*
host_private_key
,
char
*
root_cert
,
char
*
host_cert
);
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment