Skip to content

Commit

Permalink
Merge pull request #35 from stappersg/cgibin_example
Browse files Browse the repository at this point in the history
Add a CGI-bin example

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
  • Loading branch information
troglobit committed Jun 16, 2022
2 parents 4ee7a2e + e3c055f commit 9a5cf3f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
@@ -1,6 +1,8 @@
SUBDIRS = src www man tests
doc_DATA = README.md LICENSE ChangeLog.md
EXTRA_DIST = README.md LICENSE ChangeLog.md throttle.conf merecat.conf
EXTRA_DIST += www/onboard/form.html www/onboard/file/FYI.text
EXTRA_DIST += www/cgi-bin/onboard_ask.py
DISTCHECK_CONFIGURE_FLAGS = --with-systemd=$$dc_install_base/$(systemd)

if HAVE_CONFUSE
Expand Down
50 changes: 50 additions & 0 deletions www/cgi-bin/onboard_ask.py
@@ -0,0 +1,50 @@
#!/usr/bin/python3

import cgi
import cgitb
import datetime


cgitb.enable(display=0, logdir="/var/log/onboard")

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
if form.getvalue('username'):
username = form.getvalue('username')
else:
username = "username"

if form.getvalue('gecos'):
gecos = form.getvalue('gecos')
else:
gecos = "gecos"

if form.getvalue('sshkey'):
sshkey = form.getvalue('sshkey')
else:
sshkey = "no_sshkey entered"

mmss = datetime.datetime.now().strftime("%M%S")
f = open("/var/www/onboard/file/%s.text" % mmss, "w")
f.write("# k:v, k:v, separator, line(s)\n")
f.write("username: %s\n"% username)
f.write("gecos: %s\n"% gecos)
f.write("# SSH public key(s)\n")
f.write("%s\n"% sshkey)
f.close()

print("Content-type:text/html")
print() # separator
print("<html>")
print("<head>")
print("<title>So far</title>")
print("</head>")
print("<body>")
print("<p>Hello %s</p>" % username)
print('<p>There is now <a href="/onboard/file/%s.text">file/%s.text</a>.</p>' % (mmss,mmss))
print("<p>It has to be further processed, outside this <i>web form stuff</i></p>")
print('<p><a href="/onboard/">Back to <b>onboard begin</b></a></p>')
print("</body>")
print("</html>")
7 changes: 7 additions & 0 deletions www/onboard/file/FYI.text
@@ -0,0 +1,7 @@
For Your Information

This text file helps
* to tell that CGI-bin/onboard_ask.py writes files in this directory
* showing that created files are visible

(And this file ensures that git is aware of the onboard/file/ directory.)
28 changes: 28 additions & 0 deletions www/onboard/form.html
@@ -0,0 +1,28 @@
<html>
<head/>
<body>
<h3>Welcome</h3>
<p>
Please provide information for SSH access.
</p>

<form action = "/cgi-bin/onboard_ask.py" method = "post">
Your preferred user name: <br/>
(Example: <i>ws</i>)<br/>
<input type = "text" name = "username"/> <br/>
<br/>
Gecos, the fifth field in a <tt>/etc/passwd</tt> record: <br/>
(Example: <i>Winston Smith</i>)<br/>
(Example: <i>Winston Smith,room,phone,home phone,other</i>)<br/>
<input type = "text" name = "gecos"/> <br/>
<br/>
Public part of SSH key you gonna use: <br/>
<textarea name = "sshkey" cols = "72" rows = "6">
something like
cat ~/.ssh/id_algoritm.pub
</textarea> <br/>

<input type = "submit" value = "Submit" />
</form>
<body>
</html>

0 comments on commit 9a5cf3f

Please sign in to comment.