PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/source3/web/statuspage.c

https://repo.or.cz/Samba/vfs_proxy.git
C | 454 lines | 344 code | 72 blank | 38 comment | 89 complexity | 665a583b87f81599266b9d84a6c3b545 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. /*
  2. Unix SMB/CIFS implementation.
  3. web status page
  4. Copyright (C) Andrew Tridgell 1997-1998
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "includes.h"
  17. #include "web/swat_proto.h"
  18. #define _(x) lang_msg_rotate(talloc_tos(),x)
  19. #define PIDMAP struct PidMap
  20. /* how long to wait for start/stops to take effect */
  21. #define SLEEP_TIME 3
  22. PIDMAP {
  23. PIDMAP *next, *prev;
  24. struct server_id pid;
  25. char *machine;
  26. };
  27. static PIDMAP *pidmap;
  28. static int PID_or_Machine; /* 0 = show PID, else show Machine name */
  29. static struct server_id smbd_pid;
  30. /* from 2nd call on, remove old list */
  31. static void initPid2Machine (void)
  32. {
  33. /* show machine name rather PID on table "Open Files"? */
  34. if (PID_or_Machine) {
  35. PIDMAP *p, *next;
  36. for (p = pidmap; p != NULL; p = next) {
  37. next = p->next;
  38. DLIST_REMOVE(pidmap, p);
  39. SAFE_FREE(p->machine);
  40. SAFE_FREE(p);
  41. }
  42. pidmap = NULL;
  43. }
  44. }
  45. /* add new PID <-> Machine name mapping */
  46. static void addPid2Machine (struct server_id pid, const char *machine)
  47. {
  48. /* show machine name rather PID on table "Open Files"? */
  49. if (PID_or_Machine) {
  50. PIDMAP *newmap;
  51. if ((newmap = SMB_MALLOC_P(PIDMAP)) == NULL) {
  52. /* XXX need error message for this?
  53. if malloc fails, PID is always shown */
  54. return;
  55. }
  56. newmap->pid = pid;
  57. newmap->machine = SMB_STRDUP(machine);
  58. DLIST_ADD(pidmap, newmap);
  59. }
  60. }
  61. /* lookup PID <-> Machine name mapping */
  62. static char *mapPid2Machine (struct server_id pid)
  63. {
  64. static char pidbuf [64];
  65. PIDMAP *map;
  66. /* show machine name rather PID on table "Open Files"? */
  67. if (PID_or_Machine) {
  68. for (map = pidmap; map != NULL; map = map->next) {
  69. if (procid_equal(&pid, &map->pid)) {
  70. if (map->machine == NULL) /* no machine name */
  71. break; /* show PID */
  72. return map->machine;
  73. }
  74. }
  75. }
  76. /* PID not in list or machine name NULL? return pid as string */
  77. snprintf (pidbuf, sizeof (pidbuf) - 1, "%s",
  78. procid_str_static(&pid));
  79. return pidbuf;
  80. }
  81. static const char *tstring(TALLOC_CTX *ctx, time_t t)
  82. {
  83. char *buf;
  84. buf = talloc_strdup(ctx, time_to_asc(t));
  85. if (!buf) {
  86. return "";
  87. }
  88. buf = talloc_all_string_sub(ctx,
  89. buf,
  90. " ",
  91. "&nbsp;");
  92. if (!buf) {
  93. return "";
  94. }
  95. return buf;
  96. }
  97. static void print_share_mode(const struct share_mode_entry *e,
  98. const char *sharepath,
  99. const char *fname,
  100. void *dummy)
  101. {
  102. char *utf8_fname;
  103. int deny_mode;
  104. size_t converted_size;
  105. if (!is_valid_share_mode_entry(e)) {
  106. return;
  107. }
  108. deny_mode = map_share_mode_to_deny_mode(e->share_access,
  109. e->private_options);
  110. printf("<tr><td>%s</td>",_(mapPid2Machine(e->pid)));
  111. printf("<td>%u</td>",(unsigned int)e->uid);
  112. printf("<td>");
  113. switch ((deny_mode>>4)&0xF) {
  114. case DENY_NONE: printf("DENY_NONE"); break;
  115. case DENY_ALL: printf("DENY_ALL "); break;
  116. case DENY_DOS: printf("DENY_DOS "); break;
  117. case DENY_FCB: printf("DENY_FCB "); break;
  118. case DENY_READ: printf("DENY_READ "); break;
  119. case DENY_WRITE:printf("DENY_WRITE "); break;
  120. }
  121. printf("</td>");
  122. printf("<td>");
  123. if (e->access_mask & (FILE_READ_DATA|FILE_WRITE_DATA)) {
  124. printf("%s", _("RDWR "));
  125. } else if (e->access_mask & FILE_WRITE_DATA) {
  126. printf("%s", _("WRONLY "));
  127. } else {
  128. printf("%s", _("RDONLY "));
  129. }
  130. printf("</td>");
  131. printf("<td>");
  132. if((e->op_type &
  133. (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
  134. (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
  135. printf("EXCLUSIVE+BATCH ");
  136. else if (e->op_type & EXCLUSIVE_OPLOCK)
  137. printf("EXCLUSIVE ");
  138. else if (e->op_type & BATCH_OPLOCK)
  139. printf("BATCH ");
  140. else if (e->op_type & LEVEL_II_OPLOCK)
  141. printf("LEVEL_II ");
  142. else
  143. printf("NONE ");
  144. printf("</td>");
  145. push_utf8_allocate(&utf8_fname, fname, &converted_size);
  146. printf("<td>%s</td><td>%s</td></tr>\n",
  147. utf8_fname,tstring(talloc_tos(),e->time.tv_sec));
  148. SAFE_FREE(utf8_fname);
  149. }
  150. /* kill off any connections chosen by the user */
  151. static int traverse_fn1(struct db_record *rec,
  152. const struct connections_key *key,
  153. const struct connections_data *crec,
  154. void *private_data)
  155. {
  156. if (crec->cnum == -1 && process_exists(crec->pid)) {
  157. char buf[30];
  158. slprintf(buf,sizeof(buf)-1,"kill_%s", procid_str_static(&crec->pid));
  159. if (cgi_variable(buf)) {
  160. kill_pid(crec->pid);
  161. sleep(SLEEP_TIME);
  162. }
  163. }
  164. return 0;
  165. }
  166. /* traversal fn for showing machine connections */
  167. static int traverse_fn2(struct db_record *rec,
  168. const struct connections_key *key,
  169. const struct connections_data *crec,
  170. void *private_data)
  171. {
  172. if (crec->cnum == -1 || !process_exists(crec->pid) ||
  173. procid_equal(&crec->pid, &smbd_pid))
  174. return 0;
  175. addPid2Machine (crec->pid, crec->machine);
  176. printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td>\n",
  177. procid_str_static(&crec->pid),
  178. crec->machine, crec->addr,
  179. tstring(talloc_tos(),crec->start));
  180. if (geteuid() == 0) {
  181. printf("<td><input type=submit value=\"X\" name=\"kill_%s\"></td>\n",
  182. procid_str_static(&crec->pid));
  183. }
  184. printf("</tr>\n");
  185. return 0;
  186. }
  187. /* traversal fn for showing share connections */
  188. static int traverse_fn3(struct db_record *rec,
  189. const struct connections_key *key,
  190. const struct connections_data *crec,
  191. void *private_data)
  192. {
  193. if (crec->cnum == -1 || !process_exists(crec->pid))
  194. return 0;
  195. printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
  196. crec->servicename, uidtoname(crec->uid),
  197. gidtoname(crec->gid),procid_str_static(&crec->pid),
  198. crec->machine,
  199. tstring(talloc_tos(),crec->start));
  200. return 0;
  201. }
  202. /* show the current server status */
  203. void status_page(void)
  204. {
  205. const char *v;
  206. int autorefresh=0;
  207. int refresh_interval=30;
  208. int nr_running=0;
  209. bool waitup = False;
  210. TALLOC_CTX *ctx = talloc_stackframe();
  211. smbd_pid = pid_to_procid(pidfile_pid("smbd"));
  212. if (cgi_variable("smbd_restart") || cgi_variable("all_restart")) {
  213. stop_smbd();
  214. start_smbd();
  215. waitup=True;
  216. }
  217. if (cgi_variable("smbd_start") || cgi_variable("all_start")) {
  218. start_smbd();
  219. waitup=True;
  220. }
  221. if (cgi_variable("smbd_stop") || cgi_variable("all_stop")) {
  222. stop_smbd();
  223. waitup=True;
  224. }
  225. if (cgi_variable("nmbd_restart") || cgi_variable("all_restart")) {
  226. stop_nmbd();
  227. start_nmbd();
  228. waitup=True;
  229. }
  230. if (cgi_variable("nmbd_start") || cgi_variable("all_start")) {
  231. start_nmbd();
  232. waitup=True;
  233. }
  234. if (cgi_variable("nmbd_stop")|| cgi_variable("all_stop")) {
  235. stop_nmbd();
  236. waitup=True;
  237. }
  238. #ifdef WITH_WINBIND
  239. if (cgi_variable("winbindd_restart") || cgi_variable("all_restart")) {
  240. stop_winbindd();
  241. start_winbindd();
  242. waitup=True;
  243. }
  244. if (cgi_variable("winbindd_start") || cgi_variable("all_start")) {
  245. start_winbindd();
  246. waitup=True;
  247. }
  248. if (cgi_variable("winbindd_stop") || cgi_variable("all_stop")) {
  249. stop_winbindd();
  250. waitup=True;
  251. }
  252. #endif
  253. /* wait for daemons to start/stop */
  254. if (waitup)
  255. sleep(SLEEP_TIME);
  256. if (cgi_variable("autorefresh")) {
  257. autorefresh = 1;
  258. } else if (cgi_variable("norefresh")) {
  259. autorefresh = 0;
  260. } else if (cgi_variable("refresh")) {
  261. autorefresh = 1;
  262. }
  263. if ((v=cgi_variable("refresh_interval"))) {
  264. refresh_interval = atoi(v);
  265. }
  266. if (cgi_variable("show_client_in_col_1")) {
  267. PID_or_Machine = 1;
  268. }
  269. if (cgi_variable("show_pid_in_col_1")) {
  270. PID_or_Machine = 0;
  271. }
  272. connections_forall(traverse_fn1, NULL);
  273. initPid2Machine ();
  274. printf("<H2>%s</H2>\n", _("Server Status"));
  275. printf("<FORM method=post>\n");
  276. if (!autorefresh) {
  277. printf("<input type=submit value=\"%s\" name=\"autorefresh\">\n", _("Auto Refresh"));
  278. printf("<br>%s", _("Refresh Interval: "));
  279. printf("<input type=text size=2 name=\"refresh_interval\" value=\"%d\">\n",
  280. refresh_interval);
  281. } else {
  282. printf("<input type=submit value=\"%s\" name=\"norefresh\">\n", _("Stop Refreshing"));
  283. printf("<br>%s%d\n", _("Refresh Interval: "), refresh_interval);
  284. printf("<input type=hidden name=\"refresh\" value=\"1\">\n");
  285. }
  286. printf("<p>\n");
  287. printf("<table>\n");
  288. printf("<tr><td>%s</td><td>%s</td></tr>", _("version:"), SAMBA_VERSION_STRING);
  289. fflush(stdout);
  290. printf("<tr><td>%s</td><td>%s</td>\n", _("smbd:"), smbd_running()?_("running"):_("not running"));
  291. if (geteuid() == 0) {
  292. if (smbd_running()) {
  293. nr_running++;
  294. printf("<td><input type=submit name=\"smbd_stop\" value=\"%s\"></td>\n", _("Stop smbd"));
  295. } else {
  296. printf("<td><input type=submit name=\"smbd_start\" value=\"%s\"></td>\n", _("Start smbd"));
  297. }
  298. printf("<td><input type=submit name=\"smbd_restart\" value=\"%s\"></td>\n", _("Restart smbd"));
  299. }
  300. printf("</tr>\n");
  301. fflush(stdout);
  302. printf("<tr><td>%s</td><td>%s</td>\n", _("nmbd:"), nmbd_running()?_("running"):_("not running"));
  303. if (geteuid() == 0) {
  304. if (nmbd_running()) {
  305. nr_running++;
  306. printf("<td><input type=submit name=\"nmbd_stop\" value=\"%s\"></td>\n", _("Stop nmbd"));
  307. } else {
  308. printf("<td><input type=submit name=\"nmbd_start\" value=\"%s\"></td>\n", _("Start nmbd"));
  309. }
  310. printf("<td><input type=submit name=\"nmbd_restart\" value=\"%s\"></td>\n", _("Restart nmbd"));
  311. }
  312. printf("</tr>\n");
  313. #ifdef WITH_WINBIND
  314. fflush(stdout);
  315. printf("<tr><td>%s</td><td>%s</td>\n", _("winbindd:"), winbindd_running()?_("running"):_("not running"));
  316. if (geteuid() == 0) {
  317. if (winbindd_running()) {
  318. nr_running++;
  319. printf("<td><input type=submit name=\"winbindd_stop\" value=\"%s\"></td>\n", _("Stop winbindd"));
  320. } else {
  321. printf("<td><input type=submit name=\"winbindd_start\" value=\"%s\"></td>\n", _("Start winbindd"));
  322. }
  323. printf("<td><input type=submit name=\"winbindd_restart\" value=\"%s\"></td>\n", _("Restart winbindd"));
  324. }
  325. printf("</tr>\n");
  326. #endif
  327. if (geteuid() == 0) {
  328. printf("<tr><td></td><td></td>\n");
  329. if (nr_running >= 1) {
  330. /* stop, restart all */
  331. printf("<td><input type=submit name=\"all_stop\" value=\"%s\"></td>\n", _("Stop All"));
  332. printf("<td><input type=submit name=\"all_restart\" value=\"%s\"></td>\n", _("Restart All"));
  333. }
  334. else if (nr_running == 0) {
  335. /* start all */
  336. printf("<td><input type=submit name=\"all_start\" value=\"%s\"></td>\n", _("Start All"));
  337. }
  338. printf("</tr>\n");
  339. }
  340. printf("</table>\n");
  341. fflush(stdout);
  342. printf("<p><h3>%s</h3>\n", _("Active Connections"));
  343. printf("<table border=1>\n");
  344. printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th>\n", _("PID"), _("Client"), _("IP address"), _("Date"));
  345. if (geteuid() == 0) {
  346. printf("<th>%s</th>\n", _("Kill"));
  347. }
  348. printf("</tr>\n");
  349. connections_forall(traverse_fn2, NULL);
  350. printf("</table><p>\n");
  351. printf("<p><h3>%s</h3>\n", _("Active Shares"));
  352. printf("<table border=1>\n");
  353. printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n\n",
  354. _("Share"), _("User"), _("Group"), _("PID"), _("Client"), _("Date"));
  355. connections_forall(traverse_fn3, NULL);
  356. printf("</table><p>\n");
  357. printf("<h3>%s</h3>\n", _("Open Files"));
  358. printf("<table border=1>\n");
  359. printf("<tr><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th></tr>\n", _("PID"), _("Sharing"), _("R/W"), _("Oplock"), _("File"), _("Date"));
  360. locking_init_readonly();
  361. share_mode_forall(print_share_mode, NULL);
  362. locking_end();
  363. printf("</table>\n");
  364. printf("<br><input type=submit name=\"show_client_in_col_1\" value=\"%s\">\n", _("Show Client in col 1"));
  365. printf("<input type=submit name=\"show_pid_in_col_1\" value=\"%s\">\n", _("Show PID in col 1"));
  366. printf("</FORM>\n");
  367. if (autorefresh) {
  368. /* this little JavaScript allows for automatic refresh
  369. of the page. There are other methods but this seems
  370. to be the best alternative */
  371. printf("<script language=\"JavaScript\">\n");
  372. printf("<!--\nsetTimeout('window.location.replace(\"%s/status?refresh_interval=%d&refresh=1\")', %d)\n",
  373. cgi_baseurl(),
  374. refresh_interval,
  375. refresh_interval*1000);
  376. printf("//-->\n</script>\n");
  377. }
  378. TALLOC_FREE(ctx);
  379. }