~ubuntu-branches/ubuntu/quantal/apt-cacher-ng/quantal-backports

« back to all changes in this revision

Viewing changes to source/maintenance.cc

  • Committer: Bazaar Package Importer
  • Author(s): Eduard Bloch
  • Date: 2011-08-01 23:40:11 UTC
  • mfrom: (1.1.29 upstream) (29.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110801234011-2ouft5pay71773vs
Tags: 0.6.4-1
* New upstream version
  + fixes potential pipeline freeze (closes: #628995)
  + supports "soft" blacklisting of bad hosts when important files are
    missing (see keyfile in documentation, closes: #616091)

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#include "expiration.h"
8
8
#include "pkgimport.h"
9
9
#include "showinfo.h"
 
10
#include "mirror.h"
10
11
#include "aclogger.h"
11
12
#include "filereader.h"
12
13
#include "acfg.h"
82
83
                        "Content-Type: " << (mimetype?mimetype:"text/html") << "\r\n\r\n";
83
84
        SendRawData(s.data(), s.length(), MSG_MORE);
84
85
}
85
 
/*
86
 
void maintenance::SendDecoration(bool bBegin, const char *szDecoFile)
87
 
{
88
86
 
89
 
        acbuf m_deco;
90
 
        char *mark(NULL);
91
 
        
92
 
        if(szDecoFile && m_deco.initFromFile((acfg::confdir+sPathSep+szDecoFile).c_str()))
93
 
                mark=strchr(m_deco.c_str(), '~');
94
 
        
95
 
        // deco file specified and loaded?
96
 
        if(mark)
97
 
        {
98
 
                if(bBegin)
99
 
                        m_deco.erase(mark-m_deco.rptr());
100
 
                else
101
 
                        m_deco.drop(mark-m_deco.rptr()+1);
102
 
                string sDeco(m_deco.rptr(), m_deco.size());
103
 
                SetStyle(sDeco);
104
 
                SendMsg(sDeco);
105
 
        }       
106
 
        else
107
 
                SendMsg(bBegin?"<html><body>":"</html></body>");
108
 
        
109
 
        if(!bBegin)
110
 
                EndTransfer();
111
 
}
112
 
*/
113
 
class authbounce : public tWUIPage
 
87
class tAuthRequest : public tWUIPage
114
88
{
115
89
public:
116
 
        // some NOOPs
117
 
        authbounce(int fd) : tWUIPage(fd)
118
 
        {
119
 
        }
120
 
        ~authbounce()
121
 
        {
122
 
        }
123
 
        void Action(const MYSTD::string & src)
 
90
        tAuthRequest(int fd) : tWUIPage(fd)
124
91
        {
125
92
        }
126
93
        void Run(const string &)
128
95
                const char authmsg[] = "HTTP/1.1 401 Not Authorized\r\nWWW-Authenticate: "
129
96
        "Basic realm=\"Apt-Cacher NG administration area\"\r\n"
130
97
        "Connection: Close\r\n"
131
 
        "Content-Type: text/html\r\nContent-Length:81\r\n\r\n"
 
98
        "Content-Type: text/plain\r\nContent-Length:81\r\n\r\n"
132
99
        "Not Authorized. Please contact Apt-Cacher NG administrator for further questions.";
133
100
                SendRawData(authmsg, sizeof(authmsg)-1, 0);
134
101
        }
135
 
        
136
102
};
137
103
 
138
 
void DispatchAndRunMaintTask(const MYSTD::string &cmd, int conFD, const char * szAuthLine)
139
 
{       
140
 
        tWUIPage *pWorker(NULL);
141
 
        if(!szAuthLine)
142
 
                szAuthLine="";
143
 
 
144
 
        // not effective, why? signal(SIGPIPE, SIG_IGN);
145
 
        
146
 
        MYTRY
147
 
        {
148
 
                //std::cout << "vgl: " << authLine << " und " << acfg::adminauth << "\n";
149
 
                
150
 
                // admin actions are passed with GET parameters, appended after ?
151
 
                if(cmd.find('?')!=stmiss)
152
 
                {
153
 
                        if( ! acfg::adminauth.empty() && acfg::adminauth!=szAuthLine )
154
 
                                pWorker = new authbounce(conFD);
155
 
                        else if(cmd.find("doExpire=")!=stmiss || 
156
 
                                        cmd.find("justShow=")!=stmiss ||
157
 
                                        cmd.find("justRemove=")!=stmiss)
158
 
                                pWorker = new expiration(conFD);
159
 
                        else if(cmd.find("doImport=")!=stmiss)
160
 
                                pWorker=new pkgimport(conFD);
161
 
                        else if(cmd.find("doCount=")!=stmiss)
162
 
                                pWorker = new tStaticFileSend(conFD, "report.html", "text/html", "200");
163
 
                }
164
 
                else if (cmd==acfg::reportpage)
165
 
                        pWorker = new tStaticFileSend(conFD, "report.html", "text/html", "200");
166
 
                else if (cmd == "/style.css")
167
 
                        pWorker = new tStaticFileSend(conFD, "style.css", "text/css", "200");
168
 
                else
169
 
                        pWorker = new tStaticFileSend(conFD, "userinfo.html", "text/html", "404");
170
 
 
171
 
                if(pWorker)
172
 
                        pWorker->Run(cmd);
173
 
        }
174
 
        MYCATCH(...)
175
 
        { /* whatever */ };
176
 
        if(pWorker)
177
 
                delete pWorker;
178
 
}
179
 
 
180
 
void tWUIPage::SetStyle(string &s)
 
104
class authbounce : public tWUIPage
181
105
{
182
 
        tStrPos pos=0;
183
 
 
184
 
        acbuf buf;
185
 
        if( ! buf.initFromFile( (acfg::confdir+SZPATHSEP "style.css").c_str()) )
186
 
                return;
187
 
        
188
 
        while(stmiss != (pos = s.find("$STYLE", pos)))
189
 
        {
190
 
                //cerr << "replacing " << s.substr(pos, 6) << " with " << buf.c_str()<<endl;
191
 
                s.replace(pos, 6, buf.c_str());
192
 
                pos+=buf.size();
193
 
        }
194
 
}
 
106
public:
 
107
        authbounce(int fd) : tWUIPage(fd)
 
108
        {
 
109
        }
 
110
        void Run(const string &)
 
111
        {
 
112
                const char authmsg[] = "HTTP/1.1 200 Not Authorized\r\n"
 
113
        "Connection: Close\r\n"
 
114
        "Content-Type: text/plain\r\nContent-Length: 102\r\n\r\n"
 
115
        "Not Authorized. To start this action, an administrator password must be set and "
 
116
        "you must be logged in.";
 
117
                SendRawData(authmsg, sizeof(authmsg)-1, 0);
 
118
        }
 
119
};
195
120
 
196
121
string & tWUIPage::GetHostname()
197
122
{
203
128
 
204
129
                if (0==getsockname(m_reportFD, (struct sockaddr *)&ss, &slen) && 0
205
130
                                ==getnameinfo((struct sockaddr*) &ss, sizeof(ss), hbuf,
206
 
                                                sizeof(hbuf), 
 
131
                                                sizeof(hbuf),
207
132
                                                NULL, 0, NI_NUMERICHOST))
208
133
                {
209
134
                        const char *p=hbuf;
212
137
                                p+=7; // no more colons there, looks like v4 IP in v6 space -> crop it
213
138
                        else if(strchr(p, (int) ':'))
214
139
                                bAddBrs=true; // full v6 address for sure, add brackets
215
 
                        
 
140
 
216
141
                        if(bAddBrs)
217
142
                                m_sHostname="[";
218
143
                        m_sHostname+=p;
223
148
                        m_sHostname="IP-of-this-cache-server";
224
149
        }
225
150
        return m_sHostname;
226
 
 
227
151
}
228
152
 
229
153
tWUIPage::tFmtSendObj::tFmtSendObj(tWUIPage *p) : m_parent(p)
240
164
        m_buf=NULL;
241
165
        m_nCapacity=0;
242
166
}
 
167
 
 
168
 
 
169
void DispatchAndRunMaintTask(MYSTD::string &cmd, int conFD, const char * szAuthLine)
 
170
{       
 
171
        tWUIPage *pWorker(NULL);
 
172
        if(!szAuthLine)
 
173
                szAuthLine="";
 
174
 
 
175
#ifdef DEBUG
 
176
                if(cmd.find("tickTack")!=stmiss)
 
177
                {
 
178
                        tBgTester(conFD).Run(cmd);
 
179
                        return;
 
180
                }
 
181
#endif
 
182
 
 
183
        // not effective, why? signal(SIGPIPE, SIG_IGN);
 
184
        
 
185
        MYTRY
 
186
        {
 
187
                //std::cout << "vgl: " << authLine << " und " << acfg::adminauth << "\n";
 
188
                // admin actions are passed with GET parameters, appended after ?
 
189
                tStrPos qpos=cmd.find('?');
 
190
                reeval_cmd:
 
191
                if(qpos!=stmiss)
 
192
                {
 
193
                        if( ! acfg::adminauth.empty() && acfg::adminauth!=szAuthLine )
 
194
                                pWorker = new tAuthRequest(conFD);
 
195
                        else if(cmd.find("doExpire=")!=stmiss || 
 
196
                                        cmd.find("justShow=")!=stmiss ||
 
197
                                        cmd.find("justRemove=")!=stmiss)
 
198
                                pWorker = new expiration(conFD);
 
199
                        else if(cmd.find("doImport=")!=stmiss)
 
200
                                pWorker=new pkgimport(conFD);
 
201
                        else if(cmd.find("doMirror=")!=stmiss)
 
202
                                if(acfg::adminauth.empty())
 
203
                                        pWorker=new authbounce(conFD);
 
204
                                else
 
205
                                        pWorker=new pkgmirror(conFD);
 
206
                        else if(cmd.find("doDelete=")!=stmiss ||cmd.find("doDeleteYes=")!=stmiss)
 
207
                                if(acfg::adminauth.empty())
 
208
                                        pWorker=new authbounce(conFD);
 
209
                                else
 
210
                                        pWorker=new tDeleter(conFD);
 
211
                        else if(cmd.find("doCount=")!=stmiss)
 
212
                                pWorker = new tStaticFileSend(conFD, "report.html", "text/html", "200");
 
213
                        else // ok... just show the default page
 
214
                        {
 
215
                                cmd=cmd.substr(0,qpos);
 
216
                                qpos=stmiss;
 
217
                                goto reeval_cmd;
 
218
                        }
 
219
                }
 
220
                else if (cmd==acfg::reportpage)
 
221
                        pWorker = new tStaticFileSend(conFD, "report.html", "text/html", "200");
 
222
                else if (cmd == "/style.css")
 
223
                        pWorker = new tStaticFileSend(conFD, "style.css", "text/css", "200");
 
224
                else
 
225
                        pWorker = new tStaticFileSend(conFD, "userinfo.html", "text/html", "404");
 
226
 
 
227
                if(pWorker)
 
228
                        pWorker->Run(cmd);
 
229
        }
 
230
        MYCATCH(...)
 
231
        { /* whatever */ };
 
232
        delete pWorker;
 
233
}