Exemplo n.º 1
0
 public function update($id)
 {
     $current_user = User::current();
     if ($current_user->id != $id && !$current_user->isAdmin()) {
         http_response_code(403);
         echo "Not allowed";
         return;
     }
     if (!Request::isPost()) {
         http_response_code(400);
         return;
     }
     $data = json_decode(file_get_contents('php://input'));
     $update_data = array();
     $valid_keys = array();
     if ($current_user->isAdmin()) {
         $valid_keys[] = "admin";
     }
     $user = $this->users->getById($id);
     $vars = get_object_vars($user);
     foreach ($vars as $k => $v) {
         if (in_array($k, $valid_keys) && isset($data->{$k})) {
             if ($data->{$k} != $v) {
                 $update_data[$k] = $data->{$k};
             }
         }
     }
     if (count($update_data) > 0) {
         $this->users->update($user->id, $update_data);
         Audit::log($current_user, 'update user ' . $user, $update_data);
     }
 }
Exemplo n.º 2
0
 public function createMany($data)
 {
     $current_user = User::current();
     if (!$current_user->isAdmin()) {
         http_response_code(403);
         echo 'Not allowed';
         return;
     }
     $results = array();
     foreach ($data as $data) {
         $result = array('user' => $data->user, 'host' => $data->host);
         if ($this->validate_key($data, $result)) {
             $user = User::instance()->get($data->user);
             $result['user_id'] = $user->id;
             $key = $this->keys->getByUserHost($user, $data->host);
             if ($key != NULL) {
                 $result['status'] = 409;
                 $result['message'] = 'Host already exists for that user';
                 $result['key_id'] = $key->id;
             } else {
                 $key = $this->keys->create($user, $data->host, $data->hash);
                 Audit::log($current_user, 'create key ' . $key->id . ' for ' . $user, $key);
                 $result['key_id'] = $key->id;
                 $result['status'] = 200;
                 $result['message'] = 'Ok';
             }
         }
         $results[] = $result;
     }
     echo json_encode($results, JSON_PRETTY_PRINT);
 }
Exemplo n.º 3
0
function update_keys($users, $keys, $export_path, $dry_run = false)
{
    $num_changed = 0;
    /* Check for removed users */
    $db_logins = $users->getAllLogins();
    $fs_logins = find_existing_logins($export_path);
    $missing_logins = array_diff($fs_logins, $db_logins);
    foreach ($missing_logins as $login) {
        $fs_hosts = find_existing_hosts($export_path, $login);
        foreach ($fs_hosts as $host) {
            $filename = $login . '@' . $host . '.pub';
            $abs_filename = key_abs_path($filename);
            $log = ' REMOVED ' . $filename;
            dbg($log);
            $num_changed = $num_changed + 1;
            if (!$dry_run) {
                if (file_exists($abs_filename)) {
                    Audit::log('exportkeys', $log);
                    unlink($abs_filename);
                } else {
                    $log .= ' (File doesn\'t exist!)';
                    Audit::log('exportkeys', $log);
                }
            }
        }
    }
    foreach ($users->getAll() as $user) {
        /* Check for removed keys */
        $db_hosts = $keys->getHostsByUser($user);
        $fs_hosts = find_existing_hosts($export_path, $user->login);
        $missing_hosts = array_diff($fs_hosts, $db_hosts);
        foreach ($missing_hosts as $host) {
            $filename = $user->login . '@' . $host . '.pub';
            $abs_filename = key_abs_path($filename);
            $log = ' REMOVED ' . $filename;
            dbg($log);
            $num_changed = $num_changed + 1;
            if (!$dry_run) {
                if (file_exists($abs_filename)) {
                    Audit::log('exportkeys', $log);
                    unlink($abs_filename);
                } else {
                    $log .= ' (File doesn\'t exist!)';
                    Audit::log('exportkeys', $log);
                }
            }
        }
        /* Check for new and modified keys */
        $user_keys = $keys->getAllByUser($user);
        foreach ($user_keys as $key) {
            $filename = get_key_filename($user, $key);
            $abs_filename = key_abs_path($filename);
            if (!file_exists($abs_filename)) {
                $reason = 'NEW';
            } else {
                if (key_has_changed($abs_filename, $key)) {
                    $reason = 'CHANGED';
                } else {
                    continue;
                }
            }
            $log = ' ' . $reason . ' ' . $filename;
            dbg($log);
            $num_changed = $num_changed + 1;
            if (!$dry_run) {
                $f = fopen($abs_filename, "w");
                if ($f) {
                    Audit::log('exportkeys', $log);
                    fwrite($f, $key->hash);
                    fclose($f);
                } else {
                    $log .= " (Failed to open!)";
                    Audit::log('exportkeys', $log);
                }
            }
        }
    }
    return $num_changed;
}
Exemplo n.º 4
0
         dbg("Missing login");
         usage();
     }
     $user = $users->getByLogin($var);
     if ($user == NULL) {
         dbg('Login not found');
         exit(1);
     }
     $keys = $keys->getAllByuser($user);
     foreach ($keys as $key) {
         show_key($key);
     }
 } else {
     if ($action == "delete_all_users_keys") {
         $users->deleteAll();
         Audit::log('console', 'delete all users');
     } else {
         if ($action == "audit") {
             $since = 0;
             if ($var != NULL) {
                 preg_match_all('/([0-9]+[DHM])/i', strtolower($var), $matches);
                 foreach ($matches[0] as $m) {
                     $var = substr($m, -1);
                     $val = substr($m, 0, strlen($m) - 1);
                     switch ($var) {
                         case 'd':
                             $val *= 24;
                         case 'h':
                             $val *= 60;
                         case 'm':
                             $val *= 60;