added a background image based on Pauls SAMBA logo but manipulated a
[samba.git] / source / web / swat.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba Web Administration Tool
5    Copyright (C) Andrew Tridgell 1997-1998
6    
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.
11    
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.
16    
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.
20 */
21
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
25
26 #include "includes.h"
27 #include "smb.h"
28
29 #define GLOBALS_SNUM -1
30
31 static pstring servicesf = CONFIGFILE;
32
33
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;
40
41
42 /* start the page with standard stuff */
43 static void print_header(void)
44 {
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 background=\"%simages/background.gif\">\n\n", cgi_rooturl());
49 }
50
51
52 /* finish off the page */
53 static void print_footer(void)
54 {
55         printf("\n</BODY>\n</HTML>\n");
56 }
57
58 /* include a lump of html in a page */
59 static void include_html(char *fname)
60 {
61         FILE *f = fopen(fname,"r");
62         char buf[1024];
63         int ret;
64
65         if (!f) {
66                 printf("ERROR: Can't open %s\n", fname);
67                 return;
68         }
69
70         while (!feof(f)) {
71                 ret = fread(buf, 1, sizeof(buf), f);
72                 if (ret <= 0) break;
73                 fwrite(buf, 1, ret, stdout);
74         }
75
76         fclose(f);
77 }
78
79
80 /* display one editable parameter in a form */
81 static void show_parameter(int snum, struct parm_struct *parm)
82 {
83         int i;
84         void *ptr = parm->ptr;
85
86         if (parm->class == P_LOCAL && snum >= 0) {
87                 ptr = lp_local_ptr(snum, ptr);
88         }
89
90         printf("<tr><td><A HREF=\"%shelp/parameters.html#%s\">?</A> %s</td><td>", 
91                cgi_rooturl(), parm->label, parm->label);
92
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;
98
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;
104
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;
110
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;
115
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;
120
121         case P_INTEGER:
122                 printf("<input type=text size=8 name=\"parm_%s\" value=%d>", parm->label, *(int *)ptr);
123                 break;
124
125         case P_OCTAL:
126                 printf("<input type=text size=8 name=\"parm_%s\" value=0%o>", parm->label, *(int *)ptr);
127                 break;
128
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;
138         }
139         printf("</td></tr>\n");
140 }
141
142 /* display a set of parameters for a service */
143 static void show_parameters(int snum, int allparameters, int advanced, int printers)
144 {
145         int i = 0;
146         struct parm_struct *parm;
147         char *heading = NULL;
148         char *last_heading = NULL;
149
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;
156                 }
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;
161                 }
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;
165                 }
166                 show_parameter(snum, parm);
167         }
168 }
169
170
171 /* save and reoad the smb.conf config file */
172 static int save_reload(void)
173 {
174         FILE *f;
175
176         f = fopen(servicesf,"w");
177         if (!f) {
178                 printf("failed to open %s for writing\n", servicesf);
179                 return 0;
180         }
181
182         fprintf(f, "# Samba config file created using SWAT\n");
183         fprintf(f, "# Date: %s\n\n", timestring());
184
185         lp_dump(f);
186
187         fclose(f);
188
189         lp_killunused(NULL);
190
191         if (!lp_load(servicesf,False,False,False)) {
192                 printf("Can't reload %s\n", servicesf);
193                 return 0;
194         }
195
196         return 1;
197 }
198
199
200
201 /* commit one parameter */
202 static void commit_parameter(int snum, struct parm_struct *parm, char *v)
203 {
204         int i;
205         char *s;
206
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);
215                         }
216                 }
217         }
218
219         lp_do_parameter(snum, parm->label, v);
220 }
221
222 /* commit a set of parameters for a service */
223 static void commit_parameters(int snum)
224 {
225         int i = 0;
226         struct parm_struct *parm;
227         pstring label;
228         char *v;
229
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); 
235                 }
236         }
237 }
238
239
240 /* load the smb.conf file into loadparm. */
241 static void load_config(void)
242 {
243         if (!lp_load(servicesf,False,True,False)) {
244                 printf("<b>Can't load %s - using defaults</b><p>\n", 
245                        servicesf);
246         }
247 }
248
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)
251 {
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);
257 }
258
259 /* display the main navigation controls at the top of each page along
260    with a title */
261 static void show_main_buttons(void)
262 {
263         printf("<H2 align=center>Samba Web Administration Tool</H2>\n");
264
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);
271
272         printf("<HR>\n");
273 }
274
275 /* display a welcome page  */
276 static void welcome_page(void)
277 {
278         include_html("help/welcome.html");
279 }
280
281
282 /* display the current smb.conf  */
283 static void viewconfig_page(void)
284 {
285         printf("<H2>Current Config</H2>\n");
286         printf("<pre>");
287         include_html(servicesf);
288         printf("</pre>");
289 }
290
291
292 /* display a globals editing page  */
293 static void globals_page(void)
294 {
295         int advanced = 0;
296
297         printf("<H2>Global Variables</H2>\n");
298
299         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
300                 advanced = 1;
301
302         if (cgi_variable("Commit")) {
303                 commit_parameters(GLOBALS_SNUM);
304                 save_reload();
305         }
306
307         printf("<FORM method=post>\n");
308
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");
314         }
315         printf("<p>\n");
316         
317         printf("<table>\n");
318         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
319         printf("</table>\n");
320
321         if (advanced) {
322                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
323         }
324
325         printf("</form>\n");
326 }
327
328 /* display a shares editing page  */
329 static void shares_page(void)
330 {
331         char *share = cgi_variable("share");
332         char *s;
333         int snum=-1;
334         int i;
335         int advanced = 0;
336
337         if (share)
338                 snum = lp_servicenumber(share);
339
340         printf("<H2>Share Parameters</H2>\n");
341
342         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
343                 advanced = 1;
344
345         if (cgi_variable("Commit") && snum >= 0) {
346                 commit_parameters(snum);
347                 save_reload();
348         }
349
350         if (cgi_variable("Delete") && snum >= 0) {
351                 lp_remove_service(snum);
352                 save_reload();
353                 share = NULL;
354                 snum = -1;
355         }
356
357         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
358                 lp_copy_service(GLOBALS_SNUM, share);
359                 save_reload();
360                 snum = lp_servicenumber(share);
361         }
362
363         printf("<FORM method=post>\n");
364
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);
376                 }
377         }
378         printf("</select></td></tr><p>");
379
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>");
383
384
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");
392                 }
393                 printf("<p>\n");
394         }
395
396         if (snum >= 0) {
397                 printf("<table>\n");
398                 show_parameters(snum, 1, advanced, 0);
399                 printf("</table>\n");
400         }
401
402         if (advanced) {
403                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
404         }
405
406         printf("</FORM>\n");
407 }
408
409
410 /* display a printers editing page  */
411 static void printers_page(void)
412 {
413         char *share = cgi_variable("share");
414         char *s;
415         int snum=-1;
416         int i;
417         int advanced = 0;
418
419         if (share)
420                 snum = lp_servicenumber(share);
421
422         printf("<H2>Printer Parameters</H2>\n");
423
424         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
425                 advanced = 1;
426
427         if (cgi_variable("Commit") && snum >= 0) {
428                 commit_parameters(snum);
429                 save_reload();
430         }
431
432         if (cgi_variable("Delete") && snum >= 0) {
433                 lp_remove_service(snum);
434                 save_reload();
435                 share = NULL;
436                 snum = -1;
437         }
438
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);
445         }
446
447         printf("<FORM method=post>\n");
448
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);
460                 }
461         }
462         printf("</select></td></tr><p>");
463
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>");
467
468
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");
476                 }
477                 printf("<p>\n");
478         }
479
480         if (snum >= 0) {
481                 printf("<table>\n");
482                 show_parameters(snum, 1, advanced, 1);
483                 printf("</table>\n");
484         }
485
486         if (advanced) {
487                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
488         }
489
490         printf("</FORM>\n");
491 }
492
493
494 static void print_share_mode(share_mode_entry *e, char *fname)
495 {
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;
504         }
505         printf("</td>");
506
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;
512         }
513         printf("</td>");
514
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>");
527
528         printf("<td>%s</td><td>%s</td></tr>\n",
529                fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
530 }
531
532
533 /* show the current server status */
534 static void status_page(void)
535 {
536         struct connect_record crec;
537         pstring fname;
538         FILE *f;
539
540         if (cgi_variable("smbd_start")) {
541                 start_smbd();
542         }
543
544         if (cgi_variable("smbd_stop")) {
545                 stop_smbd();
546         }
547
548         if (cgi_variable("nmbd_start")) {
549                 start_nmbd();
550         }
551
552         if (cgi_variable("nmbd_stop")) {
553                 stop_nmbd();
554         }
555
556         printf("<H2>Server Status</H2>\n");
557
558         printf("<FORM method=post>\n");
559
560         pstrcpy(fname,lp_lockdir());
561         standard_sub_basic(fname);
562         trim_string(fname,"","/");
563         strcat(fname,"/STATUS..LCK");
564
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;
571         }
572
573
574         printf("<table>\n");
575
576         printf("<tr><td>version:</td><td>%s</td></tr>",VERSION);
577
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");
583         }
584
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");
590         }
591
592         printf("</table>\n");
593         fflush(stdout);
594
595
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");
598         
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");
602
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)));
612                 }
613         }
614
615         printf("</table><p>\n");
616
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");
620
621         locking_init(1);
622         share_mode_forall(print_share_mode);
623         locking_end();
624         printf("</table>\n");
625
626         fclose(f);
627
628         printf("</FORM>\n");
629 }
630
631
632 int main(int argc, char *argv[])
633 {
634         extern char *optarg;
635         extern int optind;
636         extern FILE *dbf;
637         int opt;
638         char *page;
639         int auth_required = 1;
640
641         /* just in case it goes wild ... */
642         alarm(300);
643
644         dbf = fopen("/dev/null", "w");
645
646         if (!dbf) dbf = stderr;
647
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;    
656                 }
657         }
658
659         cgi_setup(SWATDIR, auth_required);
660
661         print_header();
662         
663         charset_initialise();
664
665         /* if this binary is setuid then run completely as root */
666         setuid(0);
667
668         load_config();
669
670         cgi_load_variables(NULL);
671
672         show_main_buttons();
673
674         page = cgi_pathinfo();
675
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();
688         }
689         
690         print_footer();
691         return 0;
692 }
693
694