-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathman.c
More file actions
160 lines (151 loc) · 3.4 KB
/
man.c
File metadata and controls
160 lines (151 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <fcgiapp.h>
#include <fcgios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
int showMan(FCGX_Request *req, char *manpage, char *section)
{
FILE *fp;
char fline[512];
char comm[strlen(manpage) + 3 + 4];
memset(comm, 0, strlen(manpage) + 3 + 4);
memcpy(comm, "man ", 4);
if (section != NULL)
{
strncat(comm, section, strlen(section));
strncat(comm, " ", 1);
}
strncat(comm, manpage, strlen(manpage));
fp = popen(comm, "r");
if (fp == NULL)
{
return 0;
}
while (fgets(fline, 511, fp) != NULL)
{
fline[511] = '\0';
FCGX_FPrintF(req->out, "%s", fline);
memset(fline, 0, 512);
}
FCGX_FPrintF(req->out, "\r\n");
pclose(fp);
return 1;
}
int showApropos(FCGX_Request *req, char *section)
{
FILE *fp;
char fline[512];
char name[64];
char comm[17];
char *c;
int idx;
memset(fline, 0, 512 * sizeof(char));
memset(name, 0, 512 * sizeof(char));
memset(comm, 0, 512 * sizeof(char));
memcpy(comm, "apropos -s ", 12);
strncat(comm, section, strlen(section));
strncat(comm, " .", 2);
fp = popen(comm, "r");
if (fp == NULL)
{
return 0;
}
FCGX_FPrintF(req->out, "<pre>");
while (fgets(fline, 511, fp) != NULL)
{
fline[511] = '\0';
c = strchr(fline, '(');
idx = (int)(c - fline);
strncpy(name, fline, idx);
name[idx - 1] = '\0';
c = strchr(fline, ')');
FCGX_FPrintF(req->out, "<a href=\"https://hamkee.net/man?%s&%s\" style=\"text-decoration:none;color:black\">%s</a>", name, section, fline);
memset(fline, 0, 512);
memset(name, 0, 64);
}
FCGX_FPrintF(req->out, "</pre>\r\n");
pclose(fp);
return 1;
}
int main(void)
{
char *param;
char *c;
char sec[3];
int idx;
int sockfd;
FCGX_Request req;
struct passwd *p;
sockfd = FCGX_OpenSocket("/var/www/fcgi-man.sock", 512);
if (sockfd == -1)
{
perror("FCGX_OpenSocket");
return 0;
}
p = getpwnam("nginx");
if (p == NULL)
{
write(STDERR_FILENO, "getpwmam\n", 9);
return 0;
}
if (chmod("/var/www/fcgi-man.sock", 664) != 0)
{
perror("chmod");
return 0;
}
if (chown("/var/www/fcgi-man.sock", p->pw_uid, p->pw_gid) != 0)
{
perror("chown");
return 0;
}
if (FCGX_Init() != 0)
{
write(STDERR_FILENO, "FCGX_Init\n", 10);
return 0;
}
memset(&req, 0, sizeof(FCGX_Request));
FCGX_InitRequest(&req, sockfd, 0);
while (FCGX_Accept_r(&req) >= 0)
{
memset(sec, 0, 3);
param = FCGX_GetParam("QUERY_STRING", req.envp);
if (strlen(param) == 0)
{
FCGX_FPrintF(req.out, "Content-Type: text/html\r\n\r\n");
FCGX_FPrintF(req.out, "<!DOCTYPE html><html lang=\"en\"><body>\r\n");
showApropos(&req, "2");
showApropos(&req, "3");
showApropos(&req, "3p");
FCGX_FPrintF(req.out, "</body></html>r\n");
}
else
{
FCGX_FPrintF(req.out, "Content-Type: text/plain\r\n\r\n");
c = strchr(param, '&');
if (c == NULL)
showMan(&req, param, NULL);
else
{
idx = (int)(c - param) + 1;
for (int i = 0; i < 3; i++)
{
sec[i] = param[idx + i];
}
param[idx - 1] = '\0';
showMan(&req, param, sec);
}
}
FCGX_Finish_r(&req);
}
FCGX_Free(&req, sockfd);
OS_LibShutdown();
return 0;
}