cleanup unused PLC_API_VERSION
[nodeconfig.git] / PlanetLabConf / keys.php
1 <?php
2 //
3 // .ssh/authorized_keys generator
4 // Node Manager should manage user keys.
5 // This script remains current for special cases like root, site_admin, and monitor
6 //
7 // Basic usage:
8 // keys.php?role=admin (all PlanetLab administrators)
9 // keys.php?root (PlanetLab root and users allowed root on the querying node)
10 // keys.php?site_admin (PIs and tech contacts at the querying node's site)
11 //
12 // Mark Huang <mlhuang@cs.princeton.edu>
13 // Aaron Klingaman <alk@cs.princeton.edu>
14 // Copyright (C) 2004 The Trustees of Princeton University
15 //
16 // $Id$
17 //
18
19 // Get admin API handle
20 require_once 'plc_api.php';
21 global $adm;
22
23 $persons = array();
24 $keys = array();
25
26 if (!empty($_REQUEST['role'])) {
27   // API cannot filter on role_ids nor roles 
28   // $persons = $adm->GetPersons(array('roles' => array($_REQUEST['role'])));
29   $all_persons = $adm->GetPersons();
30   foreach ($all_persons as $person) {
31     if (in_array($_REQUEST['role'], $person['roles'])) {
32       $persons[] = $person;
33     }
34   }
35 }
36 if (isset($_REQUEST[PLC_SLICE_PREFIX . '_monitor'])) {
37   $user = $adm->GetPersons(array('first_name' => 'Site', 'last_name' => 'Assistant'));
38   if (!empty($user)) {
39     $persons[] = $user[0];
40   }
41 }
42
43 if (isset($_REQUEST['site_admin']) && isset($_REQUEST['node_id'])) {
44   $nodes = $adm->GetNodes(array(intval($_REQUEST['node_id'])));
45   if (!empty($nodes)) {
46     $node = $nodes[0];
47   }
48   if (isset($node)) {
49     // Look up the site
50     $sites = $adm->GetSites(array($node['site_id']));
51     // Can't filter on roles so have to brute force through entire userlist of site.
52     if ($sites && $sites[0]['person_ids']) {
53       $all_persons = $adm->GetPersons($sites[0]['person_ids']);
54       if (!empty($all_persons))
55         foreach ($all_persons as $person)
56           if ((in_array('pi', $person['roles']) || in_array('tech', $person['roles'])) && $person['enabled']) 
57             $persons[] = $person; 
58     }
59   }
60 }
61
62 if (isset($_REQUEST['root']))
63   $keys[] = array('key' => file_get_contents(PLC_ROOT_SSH_KEY_PUB));
64
65
66 if (!empty($persons)) {
67   $key_ids = array();
68   foreach ($persons as $person) {
69       if ($person['key_ids']) {
70           $person_key_ids = $person['key_ids'];
71           foreach ($person_key_ids as $person_key_id) {
72               $key_ids[] = $person_key_id;
73           }
74       }
75   }
76   if (!empty($key_ids))
77     $keys = $adm->GetKeys($key_ids);
78 }
79
80 foreach ($keys as $key)
81   print $key['key']. "\n";
82
83 ?>