added the ability to start/stop the server from SWAT.
[Samba/gbeck.git] / source / web / swat.c
blob422259a05a292967bd5be45a1d91a9375aa2a005
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 Samba Web Administration Tool
5 Copyright (C) Andrew Tridgell 1997-1998
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
26 #include "includes.h"
27 #include "smb.h"
29 #define GLOBALS_SNUM -1
31 static pstring servicesf = CONFIGFILE;
34 /* we need these because we link to locking*.o */
35 void become_root(BOOL save_dir) {}
36 void unbecome_root(BOOL restore_dir) {}
37 connection_struct Connections[MAX_CONNECTIONS];
38 files_struct Files[MAX_OPEN_FILES];
39 struct current_user current_user;
42 /* start the page with standard stuff */
43 static void print_header(void)
45 printf("Expires: 0\r\n");
46 printf("Content-type: text/html\r\n\r\n");
47 printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
48 printf("<HTML>\n<HEAD>\n<TITLE>Samba Web Administration Tool</TITLE>\n</HEAD>\n<BODY>\n\n");
52 /* finish off the page */
53 static void print_footer(void)
55 printf("\n</BODY>\n</HTML>\n");
58 /* include a lump of html in a page */
59 static void include_html(char *fname)
61 FILE *f = fopen(fname,"r");
62 char buf[1024];
63 int ret;
65 if (!f) {
66 printf("ERROR: Can't open %s\n", fname);
67 return;
70 while (!feof(f)) {
71 ret = fread(buf, 1, sizeof(buf), f);
72 if (ret <= 0) break;
73 fwrite(buf, 1, ret, stdout);
76 fclose(f);
80 /* display one editable parameter in a form */
81 static void show_parameter(int snum, struct parm_struct *parm)
83 int i;
84 void *ptr = parm->ptr;
86 if (parm->class == P_LOCAL && snum >= 0) {
87 ptr = lp_local_ptr(snum, ptr);
90 printf("<tr><td><A HREF=\"%shelp/parameters.html#%s\">?</A> %s</td><td>",
91 cgi_rooturl(), parm->label, parm->label);
93 switch (parm->type) {
94 case P_CHAR:
95 printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
96 parm->label, *(char *)ptr);
97 break;
99 case P_STRING:
100 case P_USTRING:
101 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
102 parm->label, *(char **)ptr);
103 break;
105 case P_GSTRING:
106 case P_UGSTRING:
107 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
108 parm->label, (char *)ptr);
109 break;
111 case P_BOOL:
112 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes&nbsp;&nbsp;", parm->label, (*(BOOL *)ptr)?"CHECKED":"");
113 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm->label, (*(BOOL *)ptr)?"":"CHECKED");
114 break;
116 case P_BOOLREV:
117 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes&nbsp;&nbsp;", parm->label, (*(BOOL *)ptr)?"":"CHECKED");
118 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm->label, (*(BOOL *)ptr)?"CHECKED":"");
119 break;
121 case P_INTEGER:
122 printf("<input type=text size=8 name=\"parm_%s\" value=%d>", parm->label, *(int *)ptr);
123 break;
125 case P_OCTAL:
126 printf("<input type=text size=8 name=\"parm_%s\" value=0%o>", parm->label, *(int *)ptr);
127 break;
129 case P_ENUM:
130 for (i=0;parm->enum_list[i].name;i++)
131 printf("<input type=radio name=\"parm_%s\" value=%s %s>%s&nbsp;&nbsp;",
132 parm->label, parm->enum_list[i].name,
133 (*(int *)ptr)==parm->enum_list[i].value?"CHECKED":"",
134 parm->enum_list[i].name);
135 break;
136 case P_SEP:
137 break;
139 printf("</td></tr>\n");
142 /* display a set of parameters for a service */
143 static void show_parameters(int snum, int allparameters, int advanced, int printers)
145 int i = 0;
146 struct parm_struct *parm;
147 char *heading = NULL;
148 char *last_heading = NULL;
150 while ((parm = lp_next_parameter(snum, &i, allparameters))) {
151 if (snum < 0 && parm->class == P_LOCAL && !(parm->flags & FLAG_GLOBAL))
152 continue;
153 if (parm->class == P_SEPARATOR) {
154 heading = parm->label;
155 continue;
157 if (parm->flags & FLAG_HIDE) continue;
158 if (!advanced) {
159 if (!printers && !(parm->flags & FLAG_BASIC)) continue;
160 if (printers && !(parm->flags & FLAG_PRINT)) continue;
162 if (heading && heading != last_heading) {
163 printf("<tr><td></td></tr><tr><td><b><u>%s</u></b></td></tr>\n", heading);
164 last_heading = heading;
166 show_parameter(snum, parm);
171 /* save and reoad the smb.conf config file */
172 static int save_reload(void)
174 FILE *f;
176 f = fopen(servicesf,"w");
177 if (!f) {
178 printf("failed to open %s for writing\n", servicesf);
179 return 0;
182 fprintf(f, "# Samba config file created using SWAT\n");
183 fprintf(f, "# Date: %s\n\n", timestring());
185 lp_dump(f);
187 fclose(f);
189 lp_killunused(NULL);
191 if (!lp_load(servicesf,False,False,False)) {
192 printf("Can't reload %s\n", servicesf);
193 return 0;
196 return 1;
201 /* commit one parameter */
202 static void commit_parameter(int snum, struct parm_struct *parm, char *v)
204 int i;
205 char *s;
207 if (snum < 0 && parm->class == P_LOCAL) {
208 /* this handles the case where we are changing a local
209 variable globally. We need to change the parameter in
210 all shares where it is currently set to the default */
211 for (i=0;i<lp_numservices();i++) {
212 s = lp_servicename(i);
213 if (s && (*s) && lp_is_default(i, parm)) {
214 lp_do_parameter(i, parm->label, v);
219 lp_do_parameter(snum, parm->label, v);
222 /* commit a set of parameters for a service */
223 static void commit_parameters(int snum)
225 int i = 0;
226 struct parm_struct *parm;
227 pstring label;
228 char *v;
230 while ((parm = lp_next_parameter(snum, &i, 1))) {
231 sprintf(label, "parm_%s", parm->label);
232 if ((v = cgi_variable(label))) {
233 if (parm->flags & FLAG_HIDE) continue;
234 commit_parameter(snum, parm, v);
240 /* load the smb.conf file into loadparm. */
241 static void load_config(void)
243 if (!lp_load(servicesf,False,True,False)) {
244 printf("<b>Can't load %s - using defaults</b><p>\n",
245 servicesf);
249 /* spit out the html for a link with an image */
250 static void image_link(char *name,char *hlink, char *src, int width, int height)
252 printf("<A HREF=\"%s/%s\"><img width=%d height=%d src=\"%s%s\" alt=\"%s\"></A>\n",
253 cgi_baseurl(),
254 hlink, width, height,
255 cgi_rooturl(),
256 src, name);
259 /* display the main navigation controls at the top of each page along
260 with a title */
261 static void show_main_buttons(void)
263 printf("<H2 align=center>Samba Web Administration Tool</H2>\n");
265 image_link("Home", "", "images/home.gif", 50, 50);
266 image_link("Globals", "globals", "images/globals.gif", 50, 50);
267 image_link("Shares", "shares", "images/shares.gif", 50, 50);
268 image_link("Printers", "printers", "images/printers.gif", 50, 50);
269 image_link("Status", "status", "images/status.gif", 50, 50);
270 image_link("View Config", "viewconfig", "images/viewconfig.gif", 50, 50);
272 printf("<HR>\n");
275 /* display a welcome page */
276 static void welcome_page(void)
278 include_html("help/welcome.html");
282 /* display the current smb.conf */
283 static void viewconfig_page(void)
285 printf("<H2>Current Config</H2>\n");
286 printf("<pre>");
287 include_html(servicesf);
288 printf("</pre>");
292 /* display a globals editing page */
293 static void globals_page(void)
295 int advanced = 0;
297 printf("<H2>Global Variables</H2>\n");
299 if (cgi_variable("Advanced") && !cgi_variable("Basic"))
300 advanced = 1;
302 if (cgi_variable("Commit")) {
303 commit_parameters(GLOBALS_SNUM);
304 save_reload();
307 printf("<FORM method=post>\n");
309 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
310 if (advanced == 0) {
311 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
312 } else {
313 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
315 printf("<p>\n");
317 printf("<table>\n");
318 show_parameters(GLOBALS_SNUM, 1, advanced, 0);
319 printf("</table>\n");
321 if (advanced) {
322 printf("<input type=hidden name=\"Advanced\" value=1>\n");
325 printf("</form>\n");
328 /* display a shares editing page */
329 static void shares_page(void)
331 char *share = cgi_variable("share");
332 char *s;
333 int snum=-1;
334 int i;
335 int advanced = 0;
337 if (share)
338 snum = lp_servicenumber(share);
340 printf("<H2>Share Parameters</H2>\n");
342 if (cgi_variable("Advanced") && !cgi_variable("Basic"))
343 advanced = 1;
345 if (cgi_variable("Commit") && snum >= 0) {
346 commit_parameters(snum);
347 save_reload();
350 if (cgi_variable("Delete") && snum >= 0) {
351 lp_remove_service(snum);
352 save_reload();
353 share = NULL;
354 snum = -1;
357 if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
358 lp_copy_service(GLOBALS_SNUM, share);
359 save_reload();
360 snum = lp_servicenumber(share);
363 printf("<FORM method=post>\n");
365 printf("<table>\n");
366 printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
367 printf("<td><select name=share>\n");
368 if (snum < 0)
369 printf("<option value=\" \"> \n");
370 for (i=0;i<lp_numservices();i++) {
371 s = lp_servicename(i);
372 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
373 printf("<option %s value=\"%s\">%s\n",
374 (share && strcmp(share,s)==0)?"SELECTED":"",
375 s, s);
378 printf("</select></td></tr><p>");
380 printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
381 printf("<td><input type=text size=30 name=newshare></td></tr>\n");
382 printf("</table>");
385 if (snum >= 0) {
386 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
387 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
388 if (advanced == 0) {
389 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
390 } else {
391 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
393 printf("<p>\n");
396 if (snum >= 0) {
397 printf("<table>\n");
398 show_parameters(snum, 1, advanced, 0);
399 printf("</table>\n");
402 if (advanced) {
403 printf("<input type=hidden name=\"Advanced\" value=1>\n");
406 printf("</FORM>\n");
410 /* display a printers editing page */
411 static void printers_page(void)
413 char *share = cgi_variable("share");
414 char *s;
415 int snum=-1;
416 int i;
417 int advanced = 0;
419 if (share)
420 snum = lp_servicenumber(share);
422 printf("<H2>Printer Parameters</H2>\n");
424 if (cgi_variable("Advanced") && !cgi_variable("Basic"))
425 advanced = 1;
427 if (cgi_variable("Commit") && snum >= 0) {
428 commit_parameters(snum);
429 save_reload();
432 if (cgi_variable("Delete") && snum >= 0) {
433 lp_remove_service(snum);
434 save_reload();
435 share = NULL;
436 snum = -1;
439 if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
440 lp_copy_service(GLOBALS_SNUM, share);
441 snum = lp_servicenumber(share);
442 lp_do_parameter(snum, "print ok", "Yes");
443 save_reload();
444 snum = lp_servicenumber(share);
447 printf("<FORM method=post>\n");
449 printf("<table>\n");
450 printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
451 printf("<td><select name=share>\n");
452 if (snum < 0 || !lp_print_ok(snum))
453 printf("<option value=\" \"> \n");
454 for (i=0;i<lp_numservices();i++) {
455 s = lp_servicename(i);
456 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
457 printf("<option %s value=\"%s\">%s\n",
458 (share && strcmp(share,s)==0)?"SELECTED":"",
459 s, s);
462 printf("</select></td></tr><p>");
464 printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
465 printf("<td><input type=text size=30 name=newshare></td></tr>\n");
466 printf("</table>");
469 if (snum >= 0) {
470 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
471 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
472 if (advanced == 0) {
473 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
474 } else {
475 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
477 printf("<p>\n");
480 if (snum >= 0) {
481 printf("<table>\n");
482 show_parameters(snum, 1, advanced, 1);
483 printf("</table>\n");
486 if (advanced) {
487 printf("<input type=hidden name=\"Advanced\" value=1>\n");
490 printf("</FORM>\n");
494 static void print_share_mode(share_mode_entry *e, char *fname)
496 printf("<tr><td>%d</td>",e->pid);
497 printf("<td>");
498 switch ((e->share_mode>>4)&0xF) {
499 case DENY_NONE: printf("DENY_NONE"); break;
500 case DENY_ALL: printf("DENY_ALL "); break;
501 case DENY_DOS: printf("DENY_DOS "); break;
502 case DENY_READ: printf("DENY_READ "); break;
503 case DENY_WRITE:printf("DENY_WRITE "); break;
505 printf("</td>");
507 printf("<td>");
508 switch (e->share_mode&0xF) {
509 case 0: printf("RDONLY "); break;
510 case 1: printf("WRONLY "); break;
511 case 2: printf("RDWR "); break;
513 printf("</td>");
515 printf("<td>");
516 if((e->op_type &
517 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) ==
518 (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
519 printf("EXCLUSIVE+BATCH ");
520 else if (e->op_type & EXCLUSIVE_OPLOCK)
521 printf("EXCLUSIVE ");
522 else if (e->op_type & BATCH_OPLOCK)
523 printf("BATCH ");
524 else
525 printf("NONE ");
526 printf("</td>");
528 printf("<td>%s</td><td>%s</td></tr>\n",
529 fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
533 /* show the current server status */
534 static void status_page(void)
536 struct connect_record crec;
537 pstring fname;
538 FILE *f;
540 if (cgi_variable("smbd_start")) {
541 start_smbd();
544 if (cgi_variable("smbd_stop")) {
545 stop_smbd();
548 if (cgi_variable("nmbd_start")) {
549 start_nmbd();
552 if (cgi_variable("nmbd_stop")) {
553 stop_nmbd();
556 printf("<H2>Server Status</H2>\n");
558 printf("<FORM method=post>\n");
560 pstrcpy(fname,lp_lockdir());
561 standard_sub_basic(fname);
562 trim_string(fname,"","/");
563 strcat(fname,"/STATUS..LCK");
565 f = fopen(fname,"r");
566 if (!f) {
567 printf("Couldn't open status file %s\n",fname);
568 if (!lp_status(-1))
569 printf("You need to have status=yes in your smb config file\n");
570 return;
574 printf("<table>\n");
576 printf("<tr><td>version:</td><td>%s</td></tr>",VERSION);
578 fflush(stdout);
579 if (smbd_running()) {
580 printf("<tr><td>smbd:</td><td>running</td><td><input type=submit name=\"smbd_stop\" value=\"Stop smbd\"></td></tr>\n");
581 } else {
582 printf("<tr><td>smbd:</td><td>not running</td><td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td></tr>\n");
585 fflush(stdout);
586 if (nmbd_running()) {
587 printf("<tr><td>nmbd:</td><td>running</td><td><input type=submit name=\"nmbd_stop\" value=\"Stop nmbd\"></td></tr>\n");
588 } else {
589 printf("<tr><td>nmbd:</td><td>not running</td><td><input type=submit name=\"nmbd_start\" value=\"Start nmbd\"></td></tr>\n");
592 printf("</table>\n");
593 fflush(stdout);
596 if (geteuid() != 0)
597 printf("<b>NOTE: You are not logged in as root and won't be able to start/stop the server</b><p>\n");
599 printf("<p><h3>Active Connections</h3>\n");
600 printf("<table border=1>\n");
601 printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
603 while (!feof(f)) {
604 if (fread(&crec,sizeof(crec),1,f) != 1)
605 break;
606 if (crec.magic == 0x280267 && process_exists(crec.pid)) {
607 printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s (%s)</td><td>%s</td></tr>\n",
608 crec.name,uidtoname(crec.uid),
609 gidtoname(crec.gid),crec.pid,
610 crec.machine,crec.addr,
611 asctime(LocalTime(&crec.start)));
615 printf("</table><p>\n");
617 printf("<h3>Open Files</h3>\n");
618 printf("<table border=1>\n");
619 printf("<tr><th>PID</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n");
621 locking_init(1);
622 share_mode_forall(print_share_mode);
623 locking_end();
624 printf("</table>\n");
626 fclose(f);
628 printf("</FORM>\n");
632 int main(int argc, char *argv[])
634 extern char *optarg;
635 extern int optind;
636 extern FILE *dbf;
637 int opt;
638 char *page;
639 int auth_required = 1;
641 /* just in case it goes wild ... */
642 alarm(300);
644 dbf = fopen("/dev/null", "w");
646 if (!dbf) dbf = stderr;
648 while ((opt = getopt(argc, argv,"s:a")) != EOF) {
649 switch (opt) {
650 case 's':
651 pstrcpy(servicesf,optarg);
652 break;
653 case 'a':
654 auth_required = 0;
655 break;
659 cgi_setup(SWATDIR, auth_required);
661 print_header();
663 charset_initialise();
665 /* if this binary is setuid then run completely as root */
666 setuid(0);
668 load_config();
670 cgi_load_variables(NULL);
672 show_main_buttons();
674 page = cgi_pathinfo();
676 if (strcmp(page, "globals")==0) {
677 globals_page();
678 } else if (strcmp(page,"shares")==0) {
679 shares_page();
680 } else if (strcmp(page,"printers")==0) {
681 printers_page();
682 } else if (strcmp(page,"status")==0) {
683 status_page();
684 } else if (strcmp(page,"viewconfig")==0) {
685 viewconfig_page();
686 } else {
687 welcome_page();
690 print_footer();
691 return 0;