62 // init NSAPI functions |
62 // init NSAPI functions |
63 func_init(); |
63 func_init(); |
64 add_functions(webserver_funcs); |
64 add_functions(webserver_funcs); |
65 |
65 |
66 // load init.conf |
66 // load init.conf |
67 load_init_conf("config/init.conf"); |
67 if(load_init_conf("config/init.conf")) { |
|
68 return -1; |
|
69 } |
68 |
70 |
69 // load server.conf |
71 // load server.conf |
70 init_configuration_manager(); |
72 init_configuration_manager(); |
71 if(cfgmgr_load_config() != 0) { |
73 if(cfgmgr_load_config() != 0) { |
72 fprintf(stderr, "Cannot load configuration\n"); |
74 fprintf(stderr, "Cannot load configuration\n"); |
96 vars->Vuserpw = malloc(sizeof(struct passwd)); |
98 vars->Vuserpw = malloc(sizeof(struct passwd)); |
97 // open user database |
99 // open user database |
98 setpwent(); |
100 setpwent(); |
99 if(!util_getpwnam(cfg->user.ptr, vars->Vuserpw, pwbuf, DEF_PWBUF)) { |
101 if(!util_getpwnam(cfg->user.ptr, vars->Vuserpw, pwbuf, DEF_PWBUF)) { |
100 log_ereport( |
102 log_ereport( |
101 LOG_LEVEL_ERROR, |
103 LOG_MISCONFIG, |
102 "user %s does not exist!", |
104 "user %s does not exist!", |
103 cfg->user.ptr); |
105 cfg->user.ptr); |
104 free(vars->Vuserpw); |
106 free(vars->Vuserpw); |
105 vars->Vuserpw = NULL; |
107 vars->Vuserpw = NULL; |
106 } |
108 } |
112 if(vars->Vuserpw && geteuid() == 0) { |
114 if(vars->Vuserpw && geteuid() == 0) { |
113 // a webserver user is set and we are root |
115 // a webserver user is set and we are root |
114 |
116 |
115 if(setgid(vars->Vuserpw->pw_gid) != 0) { |
117 if(setgid(vars->Vuserpw->pw_gid) != 0) { |
116 log_ereport( |
118 log_ereport( |
117 LOG_LEVEL_ERROR, |
119 LOG_FAILURE, |
118 "setgid(%d) failed", |
120 "setgid(%d) failed", |
119 vars->Vuserpw->pw_gid); |
121 vars->Vuserpw->pw_gid); |
120 } else { |
122 } else { |
121 // setgid was successful |
123 // setgid was successful |
122 // we need to call initgroups to have all group permissions |
124 // we need to call initgroups to have all group permissions |
123 if(initgroups(vars->Vuserpw->pw_name, vars->Vuserpw->pw_gid)!=0) { |
125 if(initgroups(vars->Vuserpw->pw_name, vars->Vuserpw->pw_gid)!=0) { |
124 log_ereport(LOG_LEVEL_ERROR, "initgroups failed"); |
126 log_ereport(LOG_FAILURE, "initgroups failed"); |
125 } |
127 } |
126 } |
128 } |
127 |
129 |
128 // change the uid |
130 // change the uid |
129 if(setuid(vars->Vuserpw->pw_uid)) { |
131 if(setuid(vars->Vuserpw->pw_uid)) { |
130 log_ereport( |
132 log_ereport( |
131 LOG_LEVEL_ERROR, |
133 LOG_FAILURE, |
132 "setuid(%d) failed", |
134 "setuid(%d) failed", |
133 vars->Vuserpw->pw_uid); |
135 vars->Vuserpw->pw_uid); |
134 } |
136 } |
135 } else if(vars->Vuserpw) { |
137 } else if(vars->Vuserpw) { |
136 log_ereport( |
138 log_ereport( |
137 LOG_LEVEL_INFO, |
139 LOG_WARN, |
138 "server must be started as root to change uid"); |
140 "server must be started as root to change uid"); |
139 } |
141 } |
140 |
|
141 |
142 |
142 return 0; |
143 return 0; |
143 } |
144 } |
144 |
145 |
145 int webserver_run() { |
146 int webserver_run() { |