2 Unix SMB/Netbios implementation.
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.
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");
66 printf("ERROR: Can't open %s\n", fname
);
71 ret
= fread(buf
, 1, sizeof(buf
), f
);
73 fwrite(buf
, 1, ret
, stdout
);
80 /* display one editable parameter in a form */
81 static void show_parameter(int snum
, struct parm_struct
*parm
)
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
);
95 printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
96 parm
->label
, *(char *)ptr
);
101 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
102 parm
->label
, *(char **)ptr
);
107 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
108 parm
->label
, (char *)ptr
);
112 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes ", parm
->label
, (*(BOOL
*)ptr
)?"CHECKED":"");
113 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm
->label
, (*(BOOL
*)ptr
)?"":"CHECKED");
117 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes ", parm
->label
, (*(BOOL
*)ptr
)?"":"CHECKED");
118 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm
->label
, (*(BOOL
*)ptr
)?"CHECKED":"");
122 printf("<input type=text size=8 name=\"parm_%s\" value=%d>", parm
->label
, *(int *)ptr
);
126 printf("<input type=text size=8 name=\"parm_%s\" value=0%o>", parm
->label
, *(int *)ptr
);
130 for (i
=0;parm
->enum_list
[i
].name
;i
++)
131 printf("<input type=radio name=\"parm_%s\" value=%s %s>%s ",
132 parm
->label
, parm
->enum_list
[i
].name
,
133 (*(int *)ptr
)==parm
->enum_list
[i
].value
?"CHECKED":"",
134 parm
->enum_list
[i
].name
);
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
)
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
))
153 if (parm
->class == P_SEPARATOR
) {
154 heading
= parm
->label
;
157 if (parm
->flags
& FLAG_HIDE
) continue;
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)
176 f
= fopen(servicesf
,"w");
178 printf("failed to open %s for writing\n", servicesf
);
182 fprintf(f
, "# Samba config file created using SWAT\n");
183 fprintf(f
, "# Date: %s\n\n", timestring());
191 if (!lp_load(servicesf
,False
,False
,False
)) {
192 printf("Can't reload %s\n", servicesf
);
201 /* commit one parameter */
202 static void commit_parameter(int snum
, struct parm_struct
*parm
, char *v
)
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
)
226 struct parm_struct
*parm
;
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",
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",
254 hlink
, width
, height
,
259 /* display the main navigation controls at the top of each page along
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);
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");
287 include_html(servicesf
);
292 /* display a globals editing page */
293 static void globals_page(void)
297 printf("<H2>Global Variables</H2>\n");
299 if (cgi_variable("Advanced") && !cgi_variable("Basic"))
302 if (cgi_variable("Commit")) {
303 commit_parameters(GLOBALS_SNUM
);
307 printf("<FORM method=post>\n");
309 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
311 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
313 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
318 show_parameters(GLOBALS_SNUM
, 1, advanced
, 0);
319 printf("</table>\n");
322 printf("<input type=hidden name=\"Advanced\" value=1>\n");
328 /* display a shares editing page */
329 static void shares_page(void)
331 char *share
= cgi_variable("share");
338 snum
= lp_servicenumber(share
);
340 printf("<H2>Share Parameters</H2>\n");
342 if (cgi_variable("Advanced") && !cgi_variable("Basic"))
345 if (cgi_variable("Commit") && snum
>= 0) {
346 commit_parameters(snum
);
350 if (cgi_variable("Delete") && snum
>= 0) {
351 lp_remove_service(snum
);
357 if (cgi_variable("createshare") && (share
=cgi_variable("newshare"))) {
358 lp_copy_service(GLOBALS_SNUM
, share
);
360 snum
= lp_servicenumber(share
);
363 printf("<FORM method=post>\n");
366 printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
367 printf("<td><select name=share>\n");
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":"",
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");
386 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
387 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
389 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
391 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
398 show_parameters(snum
, 1, advanced
, 0);
399 printf("</table>\n");
403 printf("<input type=hidden name=\"Advanced\" value=1>\n");
410 /* display a printers editing page */
411 static void printers_page(void)
413 char *share
= cgi_variable("share");
420 snum
= lp_servicenumber(share
);
422 printf("<H2>Printer Parameters</H2>\n");
424 if (cgi_variable("Advanced") && !cgi_variable("Basic"))
427 if (cgi_variable("Commit") && snum
>= 0) {
428 commit_parameters(snum
);
432 if (cgi_variable("Delete") && snum
>= 0) {
433 lp_remove_service(snum
);
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");
444 snum
= lp_servicenumber(share
);
447 printf("<FORM method=post>\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":"",
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");
470 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
471 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
473 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
475 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
482 show_parameters(snum
, 1, advanced
, 1);
483 printf("</table>\n");
487 printf("<input type=hidden name=\"Advanced\" value=1>\n");
494 static void print_share_mode(share_mode_entry
*e
, char *fname
)
496 printf("<tr><td>%d</td>",e
->pid
);
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;
508 switch (e
->share_mode
&0xF) {
509 case 0: printf("RDONLY "); break;
510 case 1: printf("WRONLY "); break;
511 case 2: printf("RDWR "); break;
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
)
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
;
540 if (cgi_variable("smbd_start")) {
544 if (cgi_variable("smbd_stop")) {
548 if (cgi_variable("nmbd_start")) {
552 if (cgi_variable("nmbd_stop")) {
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");
567 printf("Couldn't open status file %s\n",fname
);
569 printf("You need to have status=yes in your smb config file\n");
576 printf("<tr><td>version:</td><td>%s</td></tr>",VERSION
);
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");
582 printf("<tr><td>smbd:</td><td>not running</td><td><input type=submit name=\"smbd_start\" value=\"Start smbd\"></td></tr>\n");
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");
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");
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");
604 if (fread(&crec
,sizeof(crec
),1,f
) != 1)
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");
622 share_mode_forall(print_share_mode
);
624 printf("</table>\n");
632 int main(int argc
, char *argv
[])
639 int auth_required
= 1;
641 /* just in case it goes wild ... */
644 dbf
= fopen("/dev/null", "w");
646 if (!dbf
) dbf
= stderr
;
648 while ((opt
= getopt(argc
, argv
,"s:a")) != EOF
) {
651 pstrcpy(servicesf
,optarg
);
659 cgi_setup(SWATDIR
, auth_required
);
663 charset_initialise();
665 /* if this binary is setuid then run completely as root */
670 cgi_load_variables(NULL
);
674 page
= cgi_pathinfo();
676 if (strcmp(page
, "globals")==0) {
678 } else if (strcmp(page
,"shares")==0) {
680 } else if (strcmp(page
,"printers")==0) {
682 } else if (strcmp(page
,"status")==0) {
684 } else if (strcmp(page
,"viewconfig")==0) {