luci.openfortivpn 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env ucode
  2. 'use strict';
  3. import { readfile, writefile, stat } from 'fs';
  4. const interfaceregex = /^[a-zA-Z0-9_]+$/;
  5. const user_cert_string = "/etc/openfortivpn/user-cert-%s.pem";
  6. const user_key_string = "/etc/openfortivpn/user-key-%s.pem";
  7. const ca_file_string = "/etc/openfortivpn/ca-%s.pem";
  8. // Utility to read a file
  9. function _readfile(path) {
  10. let _stat = stat(path);
  11. if (_stat && _stat.type == "file") {
  12. let content = readfile(path);
  13. return content ? trim(content) : 'File empty';
  14. }
  15. return 'File not found';
  16. }
  17. // Utility to write a file
  18. function _writefile(path, data) {
  19. if (!data) {
  20. return false;
  21. }
  22. return writefile(path, data) == length(data);
  23. }
  24. const methods = {
  25. list:{
  26. call: function() {
  27. return {
  28. getCertificates: {
  29. interface: "interface"
  30. },
  31. setCertificates: {
  32. interface: "interface",
  33. user_cert: "user_cert",
  34. user_key: "user_key",
  35. ca_file: "ca_file"
  36. }
  37. };
  38. }
  39. },
  40. getCertificates: {
  41. args: {
  42. interface: "interface",
  43. },
  44. call: function(req) {
  45. const _interface = req.args?.interface;
  46. if (!_interface || !match(_interface, interfaceregex)) {
  47. // printf("Invalid interface name");
  48. return;
  49. }
  50. const user_cert_pem = _readfile(sprintf(user_cert_string, _interface));
  51. const user_key_pem = _readfile(sprintf(user_key_string, _interface));
  52. const ca_file_pem = _readfile(sprintf(ca_file_string, _interface));
  53. if(user_cert_pem && user_key_pem && ca_file_pem){
  54. return {
  55. user_cert: user_cert_pem,
  56. user_key: user_key_pem,
  57. ca_file: ca_file_pem,
  58. };
  59. }
  60. }
  61. },
  62. setCertificates: {
  63. args: {
  64. interface: "interface",
  65. user_cert: "user_cert",
  66. user_key: "user_key",
  67. ca_file: "ca_file",
  68. },
  69. call: function(req) {
  70. let result = false;
  71. let interface = req.args?.interface;
  72. if (!interface || !match(interface, interfaceregex)) {
  73. // printf("Invalid interface name");
  74. return;
  75. }
  76. /* the interface is set up to call 1 write per certificate,
  77. with only one of the following arguments not null */
  78. if (req.args?.user_cert) {
  79. result = _writefile(sprintf(user_cert_string, interface), req.args?.user_cert);
  80. }
  81. if (req.args?.user_key) {
  82. result = _writefile(sprintf(user_key_string, interface), req.args?.user_key);
  83. }
  84. if (req.args?.ca_file) {
  85. result = _writefile(sprintf(ca_file_string, interface), req.args?.ca_file);
  86. }
  87. return {
  88. result: result,
  89. };
  90. }
  91. }
  92. };
  93. return { 'luci.openfortivpn': methods };