aboutsummaryrefslogtreecommitdiff
path: root/src/verify.c
blob: 15a2d8b817091be90d7f718391d208a2c928c4f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2008, 2009 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include "sql.h"

/* Return the length of the string without trailing whitespace */
size_t
trim_length (const char *str)
{
  size_t len;

  for (len = strlen (str); len > 0 && isspace (str[len-1]); len--)
    ;
  return len;
}

/* Chop off any trailing whitespace. Return the length of the resulting string.
 */
size_t
trim (char *str)
{
  size_t len = trim_length (str);
  str[len] = 0;
  return len;
}

int
fill_project_name (struct file_triplet *trp)
{
  char *project;
  const char *directory;
  char *p;

  if (trp->project)
    return 1;
  if (directive_get_value (trp, "directory", &directory))
    {
      logmsg (LOG_ERR, _("%s: missing `directory' directive"),
	      trp->file[file_directive].name);
      return 1;
    }
  p = strchr (directory, '/');
  if (p)
    {
      size_t len = p - directory;
      if (len == 0)
	{
	  logmsg (LOG_ERR, _("%s: empty `directory' directive"),
		  trp->file[file_directive].name);
	  return 1;
	}
      project = xmalloc (len + 1);
      memcpy (project, directory, len);
      project[len] = 0;
    }
  else
    project = xstrdup (directory);
  trp->project = project;
  return 0;
}

/* Verify if USER has upload rights on the directory (project) requested
   by TRP */
int
check_access_rights (struct file_triplet *trp, const struct spool *spool,
		     const char *user)
{
  struct access_method *method = spool->access_method[verify_method];
  int rc;
  char *command;
  const char *result;
  void *md;
  struct group *grp;
  
  if (fill_project_name (trp))
    return 1;
  grp = getgrnam (trp->project);
  if (grp)
	  trp->gid = grp->gr_gid;
  else
	  logmsg (LOG_NOTICE, _("no such group: %s"), trp->project);
  
  if (debug_level)
    logmsg (LOG_DEBUG, _("verifying access rights for user %s to project %s"),
	    user, trp->project);

  md = method_open (method);
  if (!md)
    return 1;

  command = triplet_expand_method_query (method, md, trp);

  rc = method_run (method, md, command);
  free (command);
  method_close (method, md);
  if (rc)
    {
      logmsg (LOG_ERR, _("cannot verify access rights for %s on %s"),
	      user, trp->project);
      return 1;
    }

  result = method_result (method, md, 0, 0);
  if (!result || strcmp (result, user))
    {
      UPDATE_STATS (STAT_ACCESS_VIOLATIONS);
      logmsg (LOG_ERR, _("%s: %s has no rights on %s"),
	      trp->name, user, trp->project);
      notify (spool->notification, trp, ev_bad_ownership);
      return 1;
    }

  return 0;
}

int
verify_directive_file (struct file_triplet *trp, const struct spool *spool)
{
  struct passwd *pw;
  char *command;
  struct access_method *method = spool->access_method[gpg_key_method];
  const char *pubkey;
  int rc;
  void *md;

  if (!trp->file[file_directive].name)
    return 1;

  md = method_open (method);
  if (!md)
    return 1;
  pw = getpwuid (TRIPLET_UID (trp));
  if (!pw)
    {
      logmsg (LOG_ERR, _("%s: getpwuid failed: %s"),
	      trp->name, strerror (errno));
      method_close (method, md);
      return 1;
    }
  trp->gid = pw->pw_gid;
  trp->user = xstrdup (pw->pw_name);

  command = triplet_expand_method_query (method, md, trp);

  rc = method_run (method, md, command);
  free (command);
  if (rc)
    {
      logmsg (LOG_ERR, _("cannot get PGP key for %s"), trp->user);
      method_close (method, md);
      return 1;
    }

  pubkey = method_result (method, md, 0, 0);
  if (!pubkey || pubkey[0] == 0)
    {
      logmsg (LOG_ERR, _("no GPG key for %s"), trp->user);
      method_close (method, md);
      return 1;
    }

  if (debug_level > 3)
    logmsg (LOG_DEBUG, _("public key: %s"), pubkey);

  if (verify_directive_signature (trp, spool, pubkey))
    {
      /*FIXME: Update stats */
      logmsg (LOG_ERR, _("invalid signature for %s"), trp->name);
      method_close (method, md);
      return 1;
    }
  else if (debug_level)
    logmsg (LOG_DEBUG, _("%s: directive file signature OK"), trp->name);

  method_close (method, md);

  if (debug_level > 1)
    {
      int i;
      for (i = 0; trp->directive[i]; i++)
	logmsg (LOG_DEBUG, "directive[%d] = %s", i, trp->directive[i]);
    }

  if (verify_directive_format (trp))
    return 1;

  if (check_access_rights (trp, spool, trp->user))
    return 1;

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.