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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
package org.openslx.taskmanager.tasks;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.io.FileUtils;
import org.openslx.satserver.util.Archive;
import org.openslx.satserver.util.Constants;
import org.openslx.satserver.util.Exec;
import org.openslx.satserver.util.Exec.ExecCallback;
import org.openslx.satserver.util.Template;
import org.openslx.satserver.util.Util;
import org.openslx.taskmanager.api.AbstractTask;
import com.google.gson.annotations.Expose;
public class CreateLdapConfig extends AbstractTask
{
public static final String DEFAULT_CA_BUNDLE = "/etc/ssl/certs/ca-certificates.crt";
@Expose
private int moduleid = 0;
@Expose
private String filename = null;
@Expose
private String server = null;
@Expose
private String searchbase = null;
@Expose
private String binddn = null;
@Expose
private String bindpw = null;
@Expose
private String proxyip = null;
@Expose
private int proxyport = 0;
@Expose
private int adport = 0;
@Expose
private String home = null;
@Expose
private String fingerprint;
@Expose
private String certificate;
@Expose
private boolean plainldap = false;
private Output status = new Output();
@Override
protected boolean initTask()
{
// TODO: Check path is allowed
this.setStatusObject( this.status );
if ( filename == null || server == null || searchbase == null || proxyip == null || proxyport == 0 || moduleid == 0 ) {
status.error = "Missing argument to task";
return false;
}
if ( this.home == null )
this.home = "";
if ( this.binddn == null )
this.binddn = "";
if ( this.bindpw == null )
this.bindpw = "";
if ( this.certificate == null )
this.certificate = "";
if ( this.fingerprint == null )
this.fingerprint = "";
return true;
}
@Override
protected boolean execute()
{
TarArchiveOutputStream outArchive = null;
File keyFile = new File( "/opt/ldadp/configs/" + this.moduleid + ".key.pem" );
File certFile = new File( "/opt/ldadp/configs/" + this.moduleid + ".crt.pem" );
File caFile = new File( "/opt/ldadp/configs/" + this.moduleid + ".ca-bundle.pem" );
String uri = "ldaps://" + this.proxyip + ":" + this.proxyport + "/";
String cacertPath = "/etc/ldap-proxy.pem";
String caPath = "";
final String subject = "/C=DE/ST=Nowhere/L=Springfield/O=Dis/CN=" + this.proxyip;
try {
// If cert already exists, check if the subject (most importantly the CN) matches the desired one
if ( certFile.exists() ) {
final AtomicBoolean subjectStillGood = new AtomicBoolean( false );
Exec.sync( 4, new ExecCallback() {
@Override
public void processStdOut( String line )
{
if ( line.trim().endsWith( subject ) ) {
subjectStillGood.set( true );
}
}
@Override
public void processStdErr( String line )
{
}
}, "openssl", "x509", "-noout", "-in", certFile.getAbsolutePath(), "-subject" );
if ( !subjectStillGood.get() ) {
certFile.delete();
keyFile.delete();
}
}
// Generate keys if not existent
if ( !keyFile.exists() || !certFile.exists() ) {
int ret = Exec.sync( 20, "openssl", "req",
"-x509", "-new", "-newkey", "rsa:4096", "-keyout", keyFile.getAbsolutePath(), "-out", certFile.getAbsolutePath(),
"-days", "5000", "-nodes", "-subj", subject );
if ( ret == -1 ) {
status.error = "openssl process didn't finish in time.";
} else if ( ret == -2 ) {
status.error = "Internal error generating certificate.";
} else if ( ret != 0 ) {
status.error = "openssl exited with code " + ret;
}
if ( ret != 0 )
return false;
}
// Handle ca-bundle; write to file if custom one is passed
if ( this.certificate.equals( "default" ) ) {
caPath = DEFAULT_CA_BUNDLE;
this.fingerprint = "";
} else if ( !this.certificate.isEmpty() ) {
// Write out
try {
FileUtils.writeStringToFile( caFile, this.certificate );
} catch ( Exception e ) {
status.error = "Could not write trusted certificate(s) to file " + caFile.getAbsolutePath();
return false;
}
caPath = caFile.getAbsolutePath();
this.fingerprint = "";
}
// ldadp config
String ldadpConf = String.format(
"[%s]\n"
+ "binddn=%s\n"
+ "bindpw=%s\n"
+ "base=%s\n"
+ "home=%s\n"
+ "port=%s\n"
+ "fingerprint=%s\n"
+ "cabundle=%s\n"
+ "plainldap=%s\n"
+ "[local]\n"
+ "port=%s\n"
+ "cert=%s\n"
+ "privkey=%s\n"
+ "\n",
this.server,
this.binddn,
this.bindpw,
this.searchbase,
this.home,
this.adport,
this.fingerprint,
caPath,
Boolean.toString( this.plainldap ),
this.proxyport,
certFile,
keyFile );
// Generic ldap config
final Template ldapConf = new Template( "./data/ad/ldap.conf.template" );
ldapConf.replace( "%URI%", uri );
ldapConf.replace( "%SEARCHBASE%", this.searchbase );
ldapConf.replace( "%CACERT%", cacertPath );
// sssd config
final Template sssdConf = new Template( "./data/ad/sssd.conf.template" );
sssdConf.replace( "%URI%", uri );
sssdConf.replace( "%SEARCHBASE%", this.searchbase );
sssdConf.replace( "%CACERT%", cacertPath );
String fileName = "/opt/ldadp/configs/" + this.moduleid + ".cfg";
try {
Files.deleteIfExists( Paths.get( this.filename ) );
} catch ( IOException e1 ) {
}
try {
FileUtils.writeStringToFile( new File( fileName ), ldadpConf, StandardCharsets.UTF_8 );
if ( 0 != Exec.sync( 10,
"/usr/bin/sudo",
"-n",
"-u", "root",
Constants.BASEDIR + "/scripts/ldadp-setperms",
Integer.toString( this.moduleid ) ) )
status.error = "Warning: Could not chown/chmod ldadp config!";
} catch ( IOException e ) {
status.error = e.toString();
return false;
}
try {
outArchive = Archive.createTarArchive( this.filename );
} catch ( IOException e ) {
status.error = "Could not create archive at " + this.filename;
return false;
}
// The cert we just created
if ( !Archive.tarAddFile( outArchive, cacertPath, certFile, 0644 ) ) {
status.error = "Could not add ldap-proxy.pem to module";
return false;
}
// nsswitch.conf with ldap enabled
if ( !Archive.tarAddFile( outArchive, "/etc/nsswitch.conf", new File( "./data/ad/nsswitch.conf" ), 0644 ) ) {
status.error = "Could not add nsswitch.conf to module";
return false;
}
// All the pam.d common-XXXX files
for ( String file : new String[] { "common-auth", "common-account", "common-session", "common-session-noninteractive",
"common-password" } ) {
if ( !Archive.tarAddFile( outArchive, "/etc/pam.d/" + file, new File( "./data/ad/" + file ), 0644 ) ) {
status.error = "Could not add " + file + " to module";
return false;
}
}
// Home if present
if ( !Archive.tarAddFile( outArchive, "/opt/openslx/scripts/pam_script_mount_persistent", new File( "./data/ad/mountscript" ),
0644 ) ) {
status.error = "Could not add mount script to module";
return false;
}
boolean ret = Archive.tarCreateFileFromString( outArchive, "/etc/ldap.conf", ldapConf.toString(), 0644 )
&& Archive.tarCreateFileFromString( outArchive, "/etc/sssd/sssd.conf", sssdConf.toString(), 0600 )
&& Archive.tarCreateSymlink( outArchive, "/etc/ldap.conf", "/etc/ldap/ldap.conf" )
&& Archive.tarCreateSymlink( outArchive, "/etc/ldap.conf", "/etc/openldap/ldap.conf" )
&& Archive.tarCreateSymlink( outArchive, "../sssd.service", "/etc/systemd/system/basic.target.wants/sssd.service" );
if ( !ret ) {
status.error = "Could not add ldap configs to module";
}
return ret;
} catch ( IOException e ) {
status.error = e.toString();
return false;
} finally {
Util.multiClose( outArchive );
}
}
/**
* Output - contains additional status data of this task
*/
@SuppressWarnings( "unused" )
private static class Output
{
protected String error = null;
}
}
|