text
stringlengths
0
357
*/
char* dc_imex_has_backup(dc_context_t* context, const char* dir_name)
{
char* ret = NULL;
time_t ret_backup_time = 0;
DIR* dir_handle = NULL;
struct dirent* dir_entry = NULL;
int prefix_len = strlen(DC_BAK_PREFIX);
int suffix_len = strlen(DC_BAK_SUFFIX);
char* curr_pathNfilename = NULL;
dc_sqlite3_t* test_sql = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
return NULL;
}
if ((dir_handle=opendir(dir_name))==NULL) {
dc_log_info(context, 0, "Backup check: Cannot open directory \"%s\".", dir_name); /* this is not an error - eg. the directory may not exist or the user has not given us access to read data from the storage */
goto cleanup;
}
while ((dir_entry=readdir(dir_handle))!=NULL) {
const char* name = dir_entry->d_name; /* name without path; may also be `.` or `..` */
int name_len = strlen(name);
if (name_len > prefix_len && strncmp(name, DC_BAK_PREFIX, prefix_len)==0
&& name_len > suffix_len && strncmp(&name[name_len-suffix_len-1], "." DC_BAK_SUFFIX, suffix_len)==0)
{
free(curr_pathNfilename);
curr_pathNfilename = dc_mprintf("%s/%s", dir_name, name);
dc_sqlite3_unref(test_sql);
if ((test_sql=dc_sqlite3_new(context/*for logging only*/))!=NULL
&& dc_sqlite3_open(test_sql, curr_pathNfilename, DC_OPEN_READONLY))
{
time_t curr_backup_time = dc_sqlite3_get_config_int(test_sql, "backup_time", 0); /* reading the backup time also checks if the database is readable and the table `config` exists */
if (curr_backup_time > 0
&& curr_backup_time > ret_backup_time/*use the newest if there are multiple backup*/)
{
/* set return value to the tested database name */
free(ret);
ret = curr_pathNfilename;
ret_backup_time = curr_backup_time;
curr_pathNfilename = NULL;
}
}
}
}
cleanup:
if (dir_handle) { closedir(dir_handle); }
free(curr_pathNfilename);
dc_sqlite3_unref(test_sql);
return ret;
}
/**
* Check if the user is authorized by the given password in some way.
* This is to prompt for the password eg. before exporting keys/backup.
*
* @memberof dc_context_t
* @param context The context as created by dc_context_new().
* @param test_pw Password to check.
* @return 1=user is authorized, 0=user is not authorized.
*/
int dc_check_password(dc_context_t* context, const char* test_pw)
{
/* Check if the given password matches the configured mail_pw.
This is to prompt the user before starting eg. an export; this is mainly to avoid doing people bad thinkgs if they have short access to the device.
When we start supporting OAuth some day, we should think this over, maybe force the user to re-authenticate himself with the Android password. */
dc_loginparam_t* loginparam = dc_loginparam_new();
int success = 0;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
goto cleanup;
}
dc_loginparam_read(loginparam, context->sql, "configured_");
if ((loginparam->mail_pw==NULL || loginparam->mail_pw[0]==0) && (test_pw==NULL || test_pw[0]==0)) {
/* both empty or unset */
success = 1;
}
else if (loginparam->mail_pw==NULL || test_pw==NULL) {
/* one set, the other not */
success = 0;
}
else if (strcmp(loginparam->mail_pw, test_pw)==0) {
/* string-compared passwords are equal */
success = 1;
}
cleanup:
dc_loginparam_unref(loginparam);
return success;
}
/**
* @}