annotate build.rs @ 95:51c9d7e8261a

Return owned strings rather than borrowed strings. It's going to be irritating to have to work with strings borrowed from the PAM handle rather than just using your own. They're cheap enough to copy.
author Paul Fisher <paul@pfish.zone>
date Mon, 23 Jun 2025 14:03:44 -0400
parents 5ddbcada30f2
children efe2f5f8b5b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 use bindgen::MacroTypeVariation;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
2 use std::env;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
3 use std::path::PathBuf;
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
4
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
5 fn main() {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 if cfg!(feature = "link") {
90
f6186e41399b Miscellaneous fixes and cleanup:
Paul Fisher <paul@pfish.zone>
parents: 84
diff changeset
7 println!("cargo::rustc-link-lib=pam");
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 println!("cargo::rustc-check-cfg=cfg(pam_impl, values(\"linux-pam\",\"openpam\"))");
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9 let common_builder = bindgen::Builder::default()
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 .merge_extern_blocks(true)
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12 .blocklist_type("pam_handle")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13 .blocklist_type("pam_conv")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 .allowlist_var(".*")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
15 .allowlist_function("pam_start")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 .allowlist_function("pam_[gs]et_item")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
17 .allowlist_function("pam_get_user")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18 .allowlist_function("pam_get_authtok")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 .allowlist_function("pam_end")
90
f6186e41399b Miscellaneous fixes and cleanup:
Paul Fisher <paul@pfish.zone>
parents: 84
diff changeset
20 .allowlist_function("pam_strerror")
84
a638a45e5f1f do some real irritating i32/u32 juggling to make bindgen happy
Paul Fisher <paul@pfish.zone>
parents: 83
diff changeset
21 .default_macro_constant_type(MacroTypeVariation::Unsigned);
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22
81
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
23 let linux_builder = common_builder
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
24 .clone()
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
25 // This function is not available in OpenPAM.
92
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
26 // That means if somebody tries to run a binary compiled for
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
27 // Linux-PAM against a different impl, it will fail.
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
28 .allowlist_function("pam_syslog")
81
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
29 .header_contents(
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
30 "linux-pam.h",
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
31 r#"
92
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
32 #include <syslog.h> // for log levels
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
33 #include <security/_pam_types.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
34 #include <security/pam_appl.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
35 #include <security/pam_ext.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
36 #include <security/pam_modules.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
37 "#,
81
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
38 );
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
39 let openpam_builder = common_builder
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
40 .clone()
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
41 // This function is not available in Linux-PAM.
92
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
42 // That means if somebody tries to run a binary compiled for
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
43 // OpenPAM against a different impl, it will fail.
5ddbcada30f2 Add the ability to log against a PAM handle.
Paul Fisher <paul@pfish.zone>
parents: 90
diff changeset
44 .allowlist_function("openpam_log")
81
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
45 .header_contents(
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
46 "openpam.h",
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
47 r#"
83
9fc778c03bff Reorder pam_types to work on BSD.
Paul Fisher <paul@pfish.zone>
parents: 82
diff changeset
48 #include <security/pam_types.h>
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
49 #include <security/openpam.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 #include <security/pam_appl.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 #include <security/pam_constants.h>
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
52 "#,
81
a8f4718fed5d When dynamically linking against the wrong PAM, fail.
Paul Fisher <paul@pfish.zone>
parents: 80
diff changeset
53 );
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 let (pam_impl, bindings) = {
82
73c3f8e3b49d Don't immediately fail when running build.rs.
Paul Fisher <paul@pfish.zone>
parents: 81
diff changeset
56 if let Ok(bindings) = linux_builder.generate() {
80
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 ("linux-pam", bindings)
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 } else if let Ok(bindings) = openpam_builder.generate() {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 ("openpam", bindings)
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 } else {
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 panic!("unrecognized PAM implementation")
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 };
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 println!("cargo::rustc-cfg=pam_impl={pam_impl:?}");
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
65 let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
66 bindings
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67 .write_to_file(out_path.join("bindings.rs"))
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 .unwrap();
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 }
5aa1a010f1e8 Start using PAM headers; improve owned/borrowed distinction.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
70 }