add all defined keys for the user
[nodeconfig.git] / PlanetLabConf / keys.php
1 <?php
2 //
3 // Deprecated. Node Manager should manage keys.
4 //
5 // .ssh/authorized_keys generator
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 // backwards compatibility with the old 4.2 API
27 global $__PLC_API_VERSION;
28 if ( ! method_exists ($adm,"GetInterfaces"))
29   $__PLC_API_VERSION = 4.2;
30 else
31   $__PLC_API_VERSION = 4.3;
32
33 if (!empty($_REQUEST['role'])) {
34   // XXX Implement API query filters
35   // $persons = $adm->GetPersons(array('roles' => array($_REQUEST['role'])));
36   $all_persons = $adm->GetPersons();
37   foreach ($all_persons as $person) {
38     if (in_array($_REQUEST['role'], $person['roles'])) {
39       $persons[] = $person;
40     }
41   }
42 }
43 if (isset($_REQUEST[PLC_SLICE_PREFIX . '_monitor'])) {
44   $user = $adm->GetPersons(array('first_name' => 'Site', 'last_name' => 'Assistant'));
45   if (!empty($user)) {
46     $persons[] = $user[0];
47   }
48 }
49
50 if (isset($_REQUEST['site_admin']) && isset($_REQUEST['node_id'])) {
51   $nodes = $adm->GetNodes(array(intval($_REQUEST['node_id'])));
52   if (!empty($nodes)) {
53     $node = $nodes[0];
54   }
55   if (isset($node)) {
56     // Look up the site
57     $sites = $adm->GetSites(array($node['site_id']));
58     // Can't filter on roles so have to bruit force through entire userlist of site.
59     if ($sites && $sites[0]['person_ids']) {
60       $all_persons = $adm->GetPersons($sites[0]['person_ids']);
61       if (!empty($all_persons))
62         foreach ($all_persons as $person)
63           if ((in_array('pi', $person['roles']) || in_array('tech', $person['roles'])) && $person['enabled']) 
64             $persons[] = $person; 
65     }
66   }
67 }
68
69 if (isset($_REQUEST['root']))
70   $keys[] = array('key' => file_get_contents(PLC_ROOT_SSH_KEY_PUB));
71
72
73 if (!empty($persons)) {
74   $key_ids = array();
75   foreach ($persons as $person) {
76       if ($person['key_ids']) {
77           $person_key_ids = $person['key_ids'];
78           foreach ($person_key_ids as $person_key_id) {
79               $key_ids[] = $person_key_id;
80           }
81       }
82   }
83   if (!empty($key_ids))
84     $keys = $adm->GetKeys($key_ids);
85 }
86
87 foreach ($keys as $key)
88   print $key['key']. "\n";
89
90 ?>