- added separators
[samba.git] / source3 / web / swat.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    html smb.conf editing - prototype only
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>\n\n");
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 a set of parameters for a service */
202 static void commit_parameters(int snum)
203 {
204         int i = 0;
205         struct parm_struct *parm;
206         pstring label;
207         char *v;
208
209         while ((parm = lp_next_parameter(snum, &i, 1))) {
210                 sprintf(label, "parm_%s", parm->label);
211                 if ((v = cgi_variable(label))) {
212                         lp_do_parameter(snum, parm->label, v); 
213                 }
214         }
215 }
216
217
218 /* load the smb.conf file into loadparm. */
219 static void load_config(void)
220 {
221         if (!lp_load(servicesf,False,True,False)) {
222                 printf("<b>Can't load %s - using defaults</b><p>\n", 
223                        servicesf);
224         }
225 }
226
227 /* spit out the html for a link with an image */
228 static void image_link(char *name,char *hlink, char *src, int width, int height)
229 {
230         printf("<A HREF=\"%s/%s\"><img width=%d height=%d src=\"%s%s\" alt=\"%s\"></A>\n", 
231                cgi_baseurl(),
232                hlink, width, height, 
233                cgi_rooturl(),
234                src, name);
235 }
236
237 /* display the main navigation controls at the top of each page along
238    with a title */
239 static void show_main_buttons(void)
240 {
241         printf("<H2 align=center>Samba Web Administration Tool</H2>\n");
242
243         image_link("Home", "", "images/home.gif", 50, 50);
244         image_link("Globals", "globals", "images/globals.gif", 50, 50);
245         image_link("Shares", "shares", "images/shares.gif", 50, 50);
246         image_link("Printers", "printers", "images/printers.gif", 50, 50);
247         image_link("Status", "status", "images/status.gif", 50, 50);
248         image_link("View Config", "viewconfig", "images/viewconfig.gif", 50, 50);
249
250         printf("<HR>\n");
251 }
252
253 /* display a welcome page  */
254 static void welcome_page(void)
255 {
256         include_html("help/welcome.html");
257 }
258
259
260 /* display the current smb.conf  */
261 static void viewconfig_page(void)
262 {
263         printf("<H2>Current Config</H2>\n");
264         printf("<pre>");
265         include_html(servicesf);
266         printf("</pre>");
267 }
268
269
270 /* display a globals editing page  */
271 static void globals_page(void)
272 {
273         int advanced = 0;
274
275         printf("<H2>Global Variables</H2>\n");
276
277         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
278                 advanced = 1;
279
280         if (cgi_variable("Commit")) {
281                 commit_parameters(GLOBALS_SNUM);
282                 save_reload();
283         }
284
285         printf("<FORM method=post>\n");
286
287         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
288         if (advanced == 0) {
289                 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
290         } else {
291                 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
292         }
293         printf("<p>\n");
294         
295         printf("<table>\n");
296         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
297         printf("</table>\n");
298
299         if (advanced) {
300                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
301         }
302
303         printf("</form>\n");
304 }
305
306 /* display a shares editing page  */
307 static void shares_page(void)
308 {
309         char *share = cgi_variable("share");
310         char *s;
311         int snum=-1;
312         int i;
313         int advanced = 0;
314
315         if (share)
316                 snum = lp_servicenumber(share);
317
318         printf("<H2>Share Parameters</H2>\n");
319
320         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
321                 advanced = 1;
322
323         if (cgi_variable("Commit") && snum >= 0) {
324                 commit_parameters(snum);
325                 save_reload();
326         }
327
328         if (cgi_variable("Delete") && snum >= 0) {
329                 lp_remove_service(snum);
330                 save_reload();
331                 share = NULL;
332                 snum = -1;
333         }
334
335         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
336                 lp_copy_service(GLOBALS_SNUM, share);
337                 save_reload();
338                 snum = lp_servicenumber(share);
339         }
340
341         printf("<FORM method=post>\n");
342
343         printf("<table>\n");
344         printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
345         printf("<td><select name=share>\n");
346         if (snum < 0)
347                 printf("<option value=\" \"> \n");
348         for (i=0;i<lp_numservices();i++) {
349                 s = lp_servicename(i);
350                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
351                         printf("<option %s value=\"%s\">%s\n", 
352                                (share && strcmp(share,s)==0)?"SELECTED":"",
353                                s, s);
354                 }
355         }
356         printf("</select></td></tr><p>");
357
358         printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
359         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
360         printf("</table>");
361
362
363         if (snum >= 0) {
364                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
365                 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
366                 if (advanced == 0) {
367                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
368                 } else {
369                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
370                 }
371                 printf("<p>\n");
372         }
373
374         if (snum >= 0) {
375                 printf("<table>\n");
376                 show_parameters(snum, 1, advanced, 0);
377                 printf("</table>\n");
378         }
379
380         if (advanced) {
381                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
382         }
383
384         printf("</FORM>\n");
385 }
386
387
388 /* display a printers editing page  */
389 static void printers_page(void)
390 {
391         char *share = cgi_variable("share");
392         char *s;
393         int snum=-1;
394         int i;
395         int advanced = 0;
396
397         if (share)
398                 snum = lp_servicenumber(share);
399
400         printf("<H2>Printer Parameters</H2>\n");
401
402         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
403                 advanced = 1;
404
405         if (cgi_variable("Commit") && snum >= 0) {
406                 commit_parameters(snum);
407                 save_reload();
408         }
409
410         if (cgi_variable("Delete") && snum >= 0) {
411                 lp_remove_service(snum);
412                 save_reload();
413                 share = NULL;
414                 snum = -1;
415         }
416
417         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
418                 lp_copy_service(GLOBALS_SNUM, share);
419                 snum = lp_servicenumber(share);
420                 lp_do_parameter(snum, "print ok", "Yes");
421                 save_reload();
422                 snum = lp_servicenumber(share);
423         }
424
425         printf("<FORM method=post>\n");
426
427         printf("<table>\n");
428         printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
429         printf("<td><select name=share>\n");
430         if (snum < 0 || !lp_print_ok(snum))
431                 printf("<option value=\" \"> \n");
432         for (i=0;i<lp_numservices();i++) {
433                 s = lp_servicename(i);
434                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
435                         printf("<option %s value=\"%s\">%s\n", 
436                                (share && strcmp(share,s)==0)?"SELECTED":"",
437                                s, s);
438                 }
439         }
440         printf("</select></td></tr><p>");
441
442         printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
443         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
444         printf("</table>");
445
446
447         if (snum >= 0) {
448                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
449                 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
450                 if (advanced == 0) {
451                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
452                 } else {
453                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
454                 }
455                 printf("<p>\n");
456         }
457
458         if (snum >= 0) {
459                 printf("<table>\n");
460                 show_parameters(snum, 1, advanced, 1);
461                 printf("</table>\n");
462         }
463
464         if (advanced) {
465                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
466         }
467
468         printf("</FORM>\n");
469 }
470
471
472 static void print_share_mode(share_mode_entry *e, char *fname)
473 {
474         printf("<tr><td>%d</td>",e->pid);
475         printf("<td>");
476         switch ((e->share_mode>>4)&0xF) {
477         case DENY_NONE: printf("DENY_NONE"); break;
478         case DENY_ALL:  printf("DENY_ALL   "); break;
479         case DENY_DOS:  printf("DENY_DOS   "); break;
480         case DENY_READ: printf("DENY_READ  "); break;
481         case DENY_WRITE:printf("DENY_WRITE "); break;
482         }
483         printf("</td>");
484
485         printf("<td>");
486         switch (e->share_mode&0xF) {
487         case 0: printf("RDONLY     "); break;
488         case 1: printf("WRONLY     "); break;
489         case 2: printf("RDWR       "); break;
490         }
491         printf("</td>");
492
493         printf("<td>");
494         if((e->op_type & 
495             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
496            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
497                 printf("EXCLUSIVE+BATCH ");
498         else if (e->op_type & EXCLUSIVE_OPLOCK)
499                 printf("EXCLUSIVE       ");
500         else if (e->op_type & BATCH_OPLOCK)
501                 printf("BATCH           ");
502         else
503                 printf("NONE            ");
504         printf("</td>");
505
506         printf("<td>%s</td><td>%s</td></tr>\n",
507                fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
508 }
509
510
511 /* show the current server status */
512 static void status_page(void)
513 {
514         struct connect_record crec;
515         pstring fname;
516         FILE *f;
517
518         printf("<H2>Server Status</H2>\n");
519
520         pstrcpy(fname,lp_lockdir());
521         standard_sub_basic(fname);
522         trim_string(fname,"","/");
523         strcat(fname,"/STATUS..LCK");
524
525         f = fopen(fname,"r");
526         if (!f) {
527                 printf("Couldn't open status file %s\n",fname);
528                 if (!lp_status(-1))
529                         printf("You need to have status=yes in your smb config file\n");
530                 return;
531         }
532
533
534         printf("\nSamba version %s\n<p>",VERSION);
535
536         printf("<h3>Active Connections</h3>\n");
537         printf("<table border=1>\n");
538         printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
539
540         while (!feof(f)) {
541                 if (fread(&crec,sizeof(crec),1,f) != 1)
542                         break;
543                 if (crec.magic == 0x280267 && process_exists(crec.pid)) {
544                         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s (%s)</td><td>%s</td></tr>\n",
545                                crec.name,uidtoname(crec.uid),
546                                gidtoname(crec.gid),crec.pid,
547                                crec.machine,crec.addr,
548                                asctime(LocalTime(&crec.start)));
549                 }
550         }
551
552         printf("</table><p>\n");
553
554         printf("<h3>Open Files</h3>\n");
555         printf("<table border=1>\n");
556         printf("<tr><th>PID</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n");
557
558         locking_init(1);
559         share_mode_forall(print_share_mode);
560         locking_end();
561         printf("</table>\n");
562
563         fclose(f);
564 }
565
566
567 int main(int argc, char *argv[])
568 {
569         extern char *optarg;
570         extern int optind;
571         extern FILE *dbf;
572         int opt;
573         char *page;
574         int auth_required = 1;
575
576         /* just in case it goes wild ... */
577         alarm(300);
578
579         dbf = fopen("/dev/null", "w");
580
581         if (!dbf) dbf = stderr;
582
583         while ((opt = getopt(argc, argv,"s:a")) != EOF) {
584                 switch (opt) {
585                 case 's':
586                         pstrcpy(servicesf,optarg);
587                         break;    
588                 case 'a':
589                         auth_required = 0;
590                         break;    
591                 }
592         }
593
594         cgi_setup(SWATDIR, auth_required);
595
596         print_header();
597         
598         charset_initialise();
599
600         /* if this binary is setuid then run completely as root */
601         setuid(0);
602
603         load_config();
604
605         cgi_load_variables(NULL);
606
607         show_main_buttons();
608
609         page = cgi_pathinfo();
610
611         if (strcmp(page, "globals")==0) {
612                 globals_page();
613         } else if (strcmp(page,"shares")==0) {
614                 shares_page();
615         } else if (strcmp(page,"printers")==0) {
616                 printers_page();
617         } else if (strcmp(page,"status")==0) {
618                 status_page();
619         } else if (strcmp(page,"viewconfig")==0) {
620                 viewconfig_page();
621         } else {
622                 welcome_page();
623         }
624         
625         print_footer();
626         return 0;
627 }
628
629