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