summaryrefslogtreecommitdiff
path: root/tools/fixown.c
blob: 578cf294eb06cdc0f931cc71d1c4134e8843379e (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
#include <iostream>
#include <iomanip>
#include <stdlib.h>     /* for exit(), atoi() */
#include <list>
#include <map>
#include <sstream>

#include <sys/types.h>          /* for stat() */
#include <sys/stat.h>           /* for stat() */
#include <unistd.h>             /* for stat() */
#include <pwd.h>                /* for getpwnam_r() */
#include <grp.h>                /* for getgrnam_r() */

#include "utils.h"              /* for basename() */

using namespace std;

class owngroup{
public:
  string owner;
  string group;
};

#define x(a,b,c) make_pair(a, owngroup({b,c}))

map<string,owngroup> allowed({
  x("fixown",   "+root",   ""),
  x("fixown2",  "+root",   ""),
  x("skrewt",   "+qmaild", ""),
  x("ward",   "+qmaild", ""),
  x("ltgrey",   "+qmaild", ""),
  x("greylist", "+qmaild", ""),
  x("wripper",  "",        "+daemon"),
});

pid_t mypid;
string progname;
string progid;

int main(int _argc, char** _argv){
  int argc(_argc);
  char** argv(_argv);
  {
    progname = *argv++; argc--;
    mypid = getpid();
    stringstream binder;
    binder << basename(progname) << "[" << mypid << "]";
    progid = binder.str();
  }

  list<string> todo;

  while (argc > 0) {
    string arg = argv[0]; argc--; argv++;
    todo.push_back(arg);
  }

  for (list<string>::const_iterator ptr = todo.begin();
        ptr != todo.end(); ptr++){
    string file = *ptr;
    if (allowed.find(file) == allowed.end()){
      cerr << progid << " not on the allowed list: " << file << endl;
      exit(1);
    }
    struct stat statbuf;
    int rslt = stat(file.c_str(), &statbuf);
    if (rslt < 0) {
      cerr << progid << " stat failed for "
       << file << " : ";
      perror(0);
      exit(1);
    }
    mode_t mode = statbuf.st_mode;
    string own = allowed[file].owner;
    string grp = allowed[file].group;
    if (0) cout << oct << mode
                << " " << own
                << " " << grp
                << dec << endl;

    if (own[0] == '+') {
      own = own.substr(1);
      mode |= S_ISUID;
    }
    if (grp[0] == '+') {
      grp = grp.substr(1);
      mode |= S_ISGID;
    }
    //xx cout << oct << mode << dec << endl << endl;

    uid_t own_num = statbuf.st_uid;
    gid_t grp_num = statbuf.st_gid;

    if (own.length()){
      struct passwd pw;
      struct passwd* ppw;
      long int size = sysconf(_SC_GETPW_R_SIZE_MAX);
      char scratch[size];
      getpwnam_r(own.c_str(), &pw,
                   scratch, size, &ppw);
      if (ppw == 0) {
        cerr << progid << " user not found: " << own << endl;
        exit(1);
      }
      own_num = pw.pw_uid;
    }
    //xxx cout << own << " --> " << own_num << endl;

    if (grp.length()){
      struct group gr;
      struct group* pgr;
      long int size = sysconf(_SC_GETPW_R_SIZE_MAX);
      char scratch[size];
      getgrnam_r(grp.c_str(), &gr,
                   scratch, size, &pgr);
      if (pgr == 0) {
        cerr << progid << " group not found: " << grp << endl;
        exit(1);
      }
      grp_num = gr.gr_gid;
    }
    //xxx cout << grp << " --> " << grp_num << endl;

    rslt = chown(file.c_str(), own_num, grp_num);
    if (rslt < 0) {
      cerr << progid << " chown failed for "
        << file << " : ";
      perror(0);
      exit(1);
    }
    chmod(file.c_str(), mode);
    if (rslt < 0) {
      cerr << progid << " chmod failed for "
        << file << " : ";
      perror(0);
      exit(1);
    }

  }
}