do not try to add /etc/planetlab/root_ssh_key.pub to authorized_keys in nodes - was...
[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
17 // Get admin API handle
18 require_once 'plc_api.php';
19 global $adm;
20
21 $persons = array();
22 $keys = array();
23
24 if (!empty($_REQUEST['role'])) {
25   // API cannot filter on role_ids nor roles 
26   // $persons = $adm->GetPersons(array('roles' => array($_REQUEST['role'])));
27   $all_persons = $adm->GetPersons();
28   foreach ($all_persons as $person) {
29     if (in_array($_REQUEST['role'], $person['roles'])) {
30       $persons[] = $person;
31     }
32   }
33 }
34
35 //
36 // NOTE: this user is not used by the monitor for the moment.
37 //
38 /* // {ple,pl}_monitor user is created on the nodes by the monitor-runlevelagent init script. */
39 /* if (isset($_REQUEST[PLC_SLICE_PREFIX . '_monitor'])) { */
40 /*   $user = $adm->GetPersons(array('first_name' => 'Site', 'last_name' => 'Assistant')); */
41 /*   if (!empty($user)) { */
42 /*     $persons[] = $user[0]; */
43 /*   } */
44 /* } */
45
46 if (isset($_REQUEST['site_admin']) && isset($_REQUEST['node_id'])) {
47   $nodes = $adm->GetNodes(array(intval($_REQUEST['node_id'])));
48   if (!empty($nodes)) {
49     $node = $nodes[0];
50   }
51   if (isset($node)) {
52     // Look up the site
53     $sites = $adm->GetSites(array($node['site_id']));
54     // Can't filter on roles so have to brute force through entire userlist of site.
55     if ($sites && $sites[0]['person_ids']) {
56       $all_persons = $adm->GetPersons($sites[0]['person_ids']);
57       if (!empty($all_persons))
58         foreach ($all_persons as $person)
59           if ((in_array('pi', $person['roles']) || in_array('tech', $person['roles'])) && $person['enabled']) 
60             $persons[] = $person; 
61     }
62   }
63 }
64
65 // Thierry on July 20 2015
66 // this has ceased to work ages ago with php5, as /etc/planetlab is not
67 // allowed to php - which is right
68 // exposing only admin's public keys is quite enough
69 // which will be a relief for /var/log/php.log where this message is all over the place
70 // if (isset($_REQUEST['root']))
71 //  $keys[] = array('key' => file_get_contents(PLC_ROOT_SSH_KEY_PUB));
72
73
74 if (!empty($persons)) {
75   $key_ids = array();
76   foreach ($persons as $person) {
77       if ($person['key_ids']) {
78           $person_key_ids = $person['key_ids'];
79           foreach ($person_key_ids as $person_key_id) {
80               $key_ids[] = $person_key_id;
81           }
82       }
83   }
84   if (!empty($key_ids))
85     $keys = $adm->GetKeys($key_ids);
86 }
87
88 foreach ($keys as $key)
89   print $key['key']. "\n";
90
91 ?>