fc33a15a0eb93f8c947e58c6b6d18b0f2fa8bc68
[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 -2
30 #define DEFAULTS_SNUM -1
31
32 static pstring servicesf = CONFIGFILE;
33
34
35 /* we need these because we link to locking*.o */
36  void become_root(BOOL save_dir) {}
37  void unbecome_root(BOOL restore_dir) {}
38 connection_struct Connections[MAX_CONNECTIONS];
39 files_struct Files[MAX_OPEN_FILES];
40 struct current_user current_user;
41
42
43 /* start the page with standard stuff */
44 static void print_header(void)
45 {
46         printf("Expires: 0\r\n");
47         printf("Content-type: text/html\r\n\r\n");
48         printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">\n");
49         printf("<HTML>\n<HEAD>\n<TITLE>Samba Web Administration Tool</TITLE>\n</HEAD>\n<BODY>\n\n");
50 }
51
52
53 /* finish off the page */
54 static void print_footer(void)
55 {
56         printf("\n</BODY>\n</HTML>\n");
57 }
58
59 /* include a lump of html in a page */
60 static void include_html(char *fname)
61 {
62         FILE *f = fopen(fname,"r");
63         char buf[1024];
64         int ret;
65
66         if (!f) {
67                 printf("ERROR: Can't open %s\n", fname);
68                 return;
69         }
70
71         while (!feof(f)) {
72                 ret = fread(buf, 1, sizeof(buf), f);
73                 if (ret <= 0) break;
74                 fwrite(buf, 1, ret, stdout);
75         }
76
77         fclose(f);
78 }
79
80
81 /* display one editable parameter in a form */
82 static void show_parameter(int snum, struct parm_struct *parm)
83 {
84         int i;
85         void *ptr = parm->ptr;
86
87         if (parm->class == P_LOCAL) {
88                 ptr = lp_local_ptr(snum, ptr);
89         }
90
91         printf("<tr><td><A HREF=\"%shelp/parameters.html#%s\">?</A> %s</td><td>", 
92                cgi_rooturl(), parm->label, parm->label);
93
94         switch (parm->type) {
95         case P_CHAR:
96                 printf("<input type=text size=2 name=\"parm_%s\" value=\"%c\">",
97                        parm->label, *(char *)ptr);
98                 break;
99
100         case P_STRING:
101         case P_USTRING:
102                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
103                        parm->label, *(char **)ptr);
104                 break;
105
106         case P_GSTRING:
107         case P_UGSTRING:
108                 printf("<input type=text size=40 name=\"parm_%s\" value=\"%s\">",
109                        parm->label, (char *)ptr);
110                 break;
111
112         case P_BOOL:
113                 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes&nbsp;&nbsp;", parm->label, (*(BOOL *)ptr)?"CHECKED":"");
114                 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm->label, (*(BOOL *)ptr)?"":"CHECKED");
115                 break;
116
117         case P_BOOLREV:
118                 printf("<input type=radio name=\"parm_%s\" value=Yes %s>yes&nbsp;&nbsp;", parm->label, (*(BOOL *)ptr)?"":"CHECKED");
119                 printf("<input type=radio name=\"parm_%s\" value=No %s>no", parm->label, (*(BOOL *)ptr)?"CHECKED":"");
120                 break;
121
122         case P_INTEGER:
123                 printf("<input type=text size=8 name=\"parm_%s\" value=%d>", parm->label, *(int *)ptr);
124                 break;
125
126         case P_OCTAL:
127                 printf("<input type=text size=8 name=\"parm_%s\" value=0%o>", parm->label, *(int *)ptr);
128                 break;
129
130         case P_ENUM:
131                 for (i=0;parm->enum_list[i].name;i++)
132                         printf("<input type=radio name=\"parm_%s\" value=%s %s>%s&nbsp;&nbsp;", 
133                                parm->label, parm->enum_list[i].name, 
134                                (*(int *)ptr)==parm->enum_list[i].value?"CHECKED":"", 
135                                parm->enum_list[i].name);
136                 break;
137                         
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
148         printf("<table>\n");
149
150         while ((parm = lp_next_parameter(snum, &i, allparameters))) {
151                 if (parm->flags & FLAG_HIDE) continue;
152                 if (!advanced) {
153                         if (!printers && !(parm->flags & FLAG_BASIC)) continue;
154                         if (printers && !(parm->flags & FLAG_PRINT)) continue;
155                 }
156                 show_parameter(snum, parm);
157         }
158         printf("</table>\n");
159 }
160
161
162 /* save and reoad the smb.conf config file */
163 static int save_reload(void)
164 {
165         FILE *f;
166
167         f = fopen(servicesf,"w");
168         if (!f) {
169                 printf("failed to open %s for writing\n", servicesf);
170                 return 0;
171         }
172
173         fprintf(f, "# Samba config file created using SWAT\n");
174
175         lp_dump(f);
176
177         fclose(f);
178
179         lp_killunused(NULL);
180
181         if (!lp_load(servicesf,False)) {
182                 printf("Can't reload %s\n", servicesf);
183                 return 0;
184         }
185
186         return 1;
187 }
188
189
190
191 /* commit a set of parameters for a service */
192 static void commit_parameters(int snum)
193 {
194         int i = 0;
195         struct parm_struct *parm;
196         pstring label;
197         char *v;
198
199         while ((parm = lp_next_parameter(snum, &i, 1))) {
200                 sprintf(label, "parm_%s", parm->label);
201                 if ((v = cgi_variable(label))) {
202                         lp_do_parameter(snum, parm->label, v); 
203                 }
204         }
205
206         save_reload();
207 }
208
209
210 /* load the smb.conf file into loadparm. */
211 static void load_config(void)
212 {
213         if (!lp_load(servicesf,False)) {
214                 printf("<b>Can't load %s - using defaults</b><p>\n", 
215                        servicesf);
216         }
217 }
218
219 /* spit out the html for a link with an image */
220 static void image_link(char *name,char *hlink, char *src, int width, int height)
221 {
222         printf("<A HREF=\"%s/%s\"><img width=%d height=%d src=\"%s%s\" alt=\"%s\"></A>\n", 
223                cgi_baseurl(),
224                hlink, width, height, 
225                cgi_rooturl(),
226                src, name);
227 }
228
229 /* display the main navigation controls at the top of each page along
230    with a title */
231 static void show_main_buttons(void)
232 {
233         printf("<H2 align=center>Samba Web Administration Tool</H2>\n");
234
235         image_link("Home", "", "images/home.gif", 50, 50);
236         image_link("Globals", "globals", "images/globals.gif", 50, 50);
237         image_link("Shares", "shares", "images/shares.gif", 50, 50);
238         image_link("Printers", "printers", "images/printers.gif", 50, 50);
239         image_link("Status", "status", "images/status.gif", 50, 50);
240
241         printf("<HR>\n");
242 }
243
244 /* display a welcome page  */
245 static void welcome_page(void)
246 {
247         include_html("help/welcome.html");
248 }
249
250
251 /* display a globals editing page  */
252 static void globals_page(void)
253 {
254         int advanced = 0;
255
256         printf("<H2>Global Variables</H2>\n");
257
258         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
259                 advanced = 1;
260
261         if (cgi_variable("Commit")) {
262                 commit_parameters(GLOBALS_SNUM);
263         }
264
265         printf("<FORM method=post>\n");
266
267         printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
268         if (advanced == 0) {
269                 printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
270         } else {
271                 printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
272         }
273         printf("<p>\n");
274         
275         show_parameters(GLOBALS_SNUM, 1, advanced, 0);
276
277         if (advanced) {
278                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
279         }
280
281         printf("</form>\n");
282 }
283
284 /* display a shares editing page  */
285 static void shares_page(void)
286 {
287         char *share = cgi_variable("share");
288         char *s;
289         int snum=-1;
290         int i;
291         int advanced = 0;
292
293         if (share)
294                 snum = lp_servicenumber(share);
295
296         printf("<H2>Share Parameters</H2>\n");
297
298         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
299                 advanced = 1;
300
301         if (cgi_variable("Commit") && snum >= 0) {
302                 commit_parameters(snum);
303         }
304
305         if (cgi_variable("Delete") && snum >= 0) {
306                 lp_remove_service(snum);
307                 save_reload();
308                 share = NULL;
309                 snum = -1;
310         }
311
312         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
313                 lp_copy_service(DEFAULTS_SNUM, share);
314                 save_reload();
315                 snum = lp_servicenumber(share);
316         }
317
318         printf("<FORM method=post>\n");
319
320         printf("<table>\n");
321         printf("<tr><td><input type=submit name=selectshare value=\"Choose Share\"></td>\n");
322         printf("<td><select name=share>\n");
323         if (snum < 0)
324                 printf("<option value=\" \"> \n");
325         for (i=0;i<lp_numservices();i++) {
326                 s = lp_servicename(i);
327                 if (s && (*s) && strcmp(s,"IPC$") && !lp_print_ok(i)) {
328                         printf("<option %s value=\"%s\">%s\n", 
329                                (share && strcmp(share,s)==0)?"SELECTED":"",
330                                s, s);
331                 }
332         }
333         printf("</select></td></tr><p>");
334
335         printf("<tr><td><input type=submit name=createshare value=\"Create Share\"></td>\n");
336         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
337         printf("</table>");
338
339
340         if (snum >= 0) {
341                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
342                 printf("<input type=submit name=\"Delete\" value=\"Delete Share\">\n");
343                 if (advanced == 0) {
344                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
345                 } else {
346                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
347                 }
348                 printf("<p>\n");
349         }
350
351         if (snum >= 0) {
352                 show_parameters(snum, 1, advanced, 0);
353         }
354
355         if (advanced) {
356                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
357         }
358
359         printf("</FORM>\n");
360 }
361
362
363 /* display a printers editing page  */
364 static void printers_page(void)
365 {
366         char *share = cgi_variable("share");
367         char *s;
368         int snum=-1;
369         int i;
370         int advanced = 0;
371
372         if (share)
373                 snum = lp_servicenumber(share);
374
375         printf("<H2>Printer Parameters</H2>\n");
376
377         if (cgi_variable("Advanced") && !cgi_variable("Basic"))
378                 advanced = 1;
379
380         if (cgi_variable("Commit") && snum >= 0) {
381                 commit_parameters(snum);
382         }
383
384         if (cgi_variable("Delete") && snum >= 0) {
385                 lp_remove_service(snum);
386                 save_reload();
387                 share = NULL;
388                 snum = -1;
389         }
390
391         if (cgi_variable("createshare") && (share=cgi_variable("newshare"))) {
392                 lp_copy_service(DEFAULTS_SNUM, share);
393                 snum = lp_servicenumber(share);
394                 lp_do_parameter(snum, "print ok", "Yes");
395                 save_reload();
396                 snum = lp_servicenumber(share);
397         }
398
399         printf("<FORM method=post>\n");
400
401         printf("<table>\n");
402         printf("<tr><td><input type=submit name=selectshare value=\"Choose Printer\"></td>\n");
403         printf("<td><select name=share>\n");
404         if (snum < 0 || !lp_print_ok(snum))
405                 printf("<option value=\" \"> \n");
406         for (i=0;i<lp_numservices();i++) {
407                 s = lp_servicename(i);
408                 if (s && (*s) && strcmp(s,"IPC$") && lp_print_ok(i)) {
409                         printf("<option %s value=\"%s\">%s\n", 
410                                (share && strcmp(share,s)==0)?"SELECTED":"",
411                                s, s);
412                 }
413         }
414         printf("</select></td></tr><p>");
415
416         printf("<tr><td><input type=submit name=createshare value=\"Create Printer\"></td>\n");
417         printf("<td><input type=text size=30 name=newshare></td></tr>\n");
418         printf("</table>");
419
420
421         if (snum >= 0) {
422                 printf("<input type=submit name=\"Commit\" value=\"Commit Changes\">\n");
423                 printf("<input type=submit name=\"Delete\" value=\"Delete Printer\">\n");
424                 if (advanced == 0) {
425                         printf("<input type=submit name=\"Advanced\" value=\"Advanced View\">\n");
426                 } else {
427                         printf("<input type=submit name=\"Basic\" value=\"Basic View\">\n");
428                 }
429                 printf("<p>\n");
430         }
431
432         if (snum >= 0) {
433                 show_parameters(snum, 1, advanced, 1);
434         }
435
436         if (advanced) {
437                 printf("<input type=hidden name=\"Advanced\" value=1>\n");
438         }
439
440         printf("</FORM>\n");
441 }
442
443
444 static void print_share_mode(share_mode_entry *e, char *fname)
445 {
446         printf("<tr><td>%d</td>",e->pid);
447         printf("<td>");
448         switch ((e->share_mode>>4)&0xF) {
449         case DENY_NONE: printf("DENY_NONE"); break;
450         case DENY_ALL:  printf("DENY_ALL   "); break;
451         case DENY_DOS:  printf("DENY_DOS   "); break;
452         case DENY_READ: printf("DENY_READ  "); break;
453         case DENY_WRITE:printf("DENY_WRITE "); break;
454         }
455         printf("</td>");
456
457         printf("<td>");
458         switch (e->share_mode&0xF) {
459         case 0: printf("RDONLY     "); break;
460         case 1: printf("WRONLY     "); break;
461         case 2: printf("RDWR       "); break;
462         }
463         printf("</td>");
464
465         printf("<td>");
466         if((e->op_type & 
467             (EXCLUSIVE_OPLOCK|BATCH_OPLOCK)) == 
468            (EXCLUSIVE_OPLOCK|BATCH_OPLOCK))
469                 printf("EXCLUSIVE+BATCH ");
470         else if (e->op_type & EXCLUSIVE_OPLOCK)
471                 printf("EXCLUSIVE       ");
472         else if (e->op_type & BATCH_OPLOCK)
473                 printf("BATCH           ");
474         else
475                 printf("NONE            ");
476         printf("</td>");
477
478         printf("<td>%s</td><td>%s</td></tr>\n",
479                fname,asctime(LocalTime((time_t *)&e->time.tv_sec)));
480 }
481
482
483 /* show the current server status */
484 static void status_page(void)
485 {
486         struct connect_record crec;
487         pstring fname;
488         FILE *f;
489
490         printf("<H2>Server Status</H2>\n");
491
492         pstrcpy(fname,lp_lockdir());
493         standard_sub_basic(fname);
494         trim_string(fname,"","/");
495         strcat(fname,"/STATUS..LCK");
496
497         f = fopen(fname,"r");
498         if (!f) {
499                 printf("Couldn't open status file %s\n",fname);
500                 if (!lp_status(-1))
501                         printf("You need to have status=yes in your smb config file\n");
502                 return;
503         }
504
505
506         printf("\nSamba version %s\n<p>",VERSION);
507
508         printf("<h3>Active Connections</h3>\n");
509         printf("<table border=1>\n");
510         printf("<tr><th>Share</th><th>User</th><th>Group</th><th>PID</th><th>Client</th><th>Date</th></tr>\n\n");
511
512         while (!feof(f)) {
513                 if (fread(&crec,sizeof(crec),1,f) != 1)
514                         break;
515                 if (crec.magic == 0x280267 && process_exists(crec.pid)) {
516                         printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%d</td><td>%s (%s)</td><td>%s</td></tr>\n",
517                                crec.name,uidtoname(crec.uid),
518                                gidtoname(crec.gid),crec.pid,
519                                crec.machine,crec.addr,
520                                asctime(LocalTime(&crec.start)));
521                 }
522         }
523
524         printf("</table><p>\n");
525
526         printf("<h3>Open Files</h3>\n");
527         printf("<table border=1>\n");
528         printf("<tr><th>PID</th><th>Sharing</th><th>R/W</th><th>Oplock</th><th>File</th><th>Date</th></tr>\n");
529
530         locking_init(1);
531         share_mode_forall(print_share_mode);
532         locking_end();
533         printf("</table>\n");
534
535         fclose(f);
536 }
537
538
539 int main(int argc, char *argv[])
540 {
541         extern char *optarg;
542         extern int optind;
543         extern FILE *dbf;
544         int opt;
545         char *page;
546         int auth_required = 1;
547
548         /* just in case it goes wild ... */
549         alarm(300);
550
551         dbf = fopen("/dev/null", "w");
552
553         if (!dbf) dbf = stderr;
554
555         while ((opt = getopt(argc, argv,"s:a")) != EOF) {
556                 switch (opt) {
557                 case 's':
558                         pstrcpy(servicesf,optarg);
559                         break;    
560                 case 'a':
561                         auth_required = 0;
562                         break;    
563                 }
564         }
565
566         cgi_setup(SWATDIR, auth_required);
567
568         print_header();
569         
570         charset_initialise();
571
572         /* if this binary is setuid then run completely as root */
573         setuid(0);
574
575         load_config();
576
577         cgi_load_variables(NULL);
578
579         show_main_buttons();
580
581         page = cgi_pathinfo();
582
583         if (strcmp(page, "globals")==0) {
584                 globals_page();
585         } else if (strcmp(page,"shares")==0) {
586                 shares_page();
587         } else if (strcmp(page,"printers")==0) {
588                 printers_page();
589         } else if (strcmp(page,"status")==0) {
590                 status_page();
591         } else {
592                 welcome_page();
593         }
594         
595         print_footer();
596         return 0;
597 }
598
599